CommonLibSSE (powerof3)
Version.h
Go to the documentation of this file.
1 #pragma once
2 
3 namespace REL
4 {
5  class Version
6  {
7  public:
8  using value_type = std::uint16_t;
9  using reference = value_type&;
10  using const_reference = const value_type&;
11 
12  constexpr Version() noexcept = default;
13 
14  explicit constexpr Version(std::array<value_type, 4> a_version) noexcept :
15  _impl(a_version)
16  {}
17 
18  constexpr Version(value_type a_v1, value_type a_v2 = 0, value_type a_v3 = 0, value_type a_v4 = 0) noexcept :
19  _impl{ a_v1, a_v2, a_v3, a_v4 }
20  {}
21 
22  [[nodiscard]] constexpr reference operator[](std::size_t a_idx) noexcept { return _impl[a_idx]; }
23  [[nodiscard]] constexpr const_reference operator[](std::size_t a_idx) const noexcept { return _impl[a_idx]; }
24 
25  [[nodiscard]] constexpr decltype(auto) begin() const noexcept { return _impl.begin(); }
26  [[nodiscard]] constexpr decltype(auto) cbegin() const noexcept { return _impl.cbegin(); }
27  [[nodiscard]] constexpr decltype(auto) end() const noexcept { return _impl.end(); }
28  [[nodiscard]] constexpr decltype(auto) cend() const noexcept { return _impl.cend(); }
29 
30  [[nodiscard]] std::strong_ordering constexpr compare(const Version& a_rhs) const noexcept
31  {
32  for (std::size_t i = 0; i < _impl.size(); ++i) {
33  if ((*this)[i] != a_rhs[i]) {
34  return (*this)[i] < a_rhs[i] ? std::strong_ordering::less : std::strong_ordering::greater;
35  }
36  }
37  return std::strong_ordering::equal;
38  }
39 
40  [[nodiscard]] constexpr std::uint32_t pack() const noexcept
41  {
42  return static_cast<std::uint32_t>(
43  (_impl[0] & 0x0FF) << 24u |
44  (_impl[1] & 0x0FF) << 16u |
45  (_impl[2] & 0xFFF) << 4u |
46  (_impl[3] & 0x00F) << 0u);
47  }
48 
49  [[nodiscard]] std::string string() const
50  {
51  std::string result;
52  for (auto&& ver : _impl) {
53  result += std::to_string(ver);
54  result += '-';
55  }
56  result.pop_back();
57  return result;
58  }
59 
60  [[nodiscard]] std::wstring wstring() const
61  {
62  std::wstring result;
63  for (auto&& ver : _impl) {
64  result += std::to_wstring(ver);
65  result += L'-';
66  }
67  result.pop_back();
68  return result;
69  }
70 
71  [[nodiscard]] static constexpr Version unpack(std::uint32_t a_packedVersion) noexcept
72  {
73  return REL::Version{
74  static_cast<value_type>((a_packedVersion >> 24) & 0x0FF),
75  static_cast<value_type>((a_packedVersion >> 16) & 0x0FF),
76  static_cast<value_type>((a_packedVersion >> 4) & 0xFFF),
77  static_cast<value_type>(a_packedVersion & 0x0F)
78  };
79  }
80 
81  private:
82  std::array<value_type, 4> _impl{ 0, 0, 0, 0 };
83  };
84 
85  [[nodiscard]] constexpr bool operator==(const Version& a_lhs, const Version& a_rhs) noexcept { return a_lhs.compare(a_rhs) == 0; }
86  [[nodiscard]] constexpr std::strong_ordering operator<=>(const Version& a_lhs, const Version& a_rhs) noexcept { return a_lhs.compare(a_rhs); }
87 
88  [[nodiscard]] std::optional<Version> GetFileVersion(stl::zwstring a_filename);
89 }
90 
91 #ifdef __cpp_lib_format
92 template <class CharT>
93 struct std::formatter<REL::Version, CharT> : formatter<std::string, CharT>
94 {
95  template <class FormatContext>
96  constexpr auto format(const REL::Version a_version, FormatContext& a_ctx) const
97  {
98  return formatter<std::string, CharT>::format(a_version.string(), a_ctx);
99  }
100 };
101 #endif
102 
103 #ifdef FMT_VERSION
104 template <class CharT>
105 struct fmt::formatter<REL::Version, CharT> : formatter<std::string, CharT>
106 {
107  template <class FormatContext>
108  auto format(const REL::Version a_version, FormatContext& a_ctx) const
109  {
110  return formatter<std::string, CharT>::format(a_version.string(), a_ctx);
111  }
112 };
113 #endif
Definition: Version.h:6
constexpr decltype(auto) end() const noexcept
Definition: Version.h:27
constexpr Version(value_type a_v1, value_type a_v2=0, value_type a_v3=0, value_type a_v4=0) noexcept
Definition: Version.h:18
constexpr reference operator[](std::size_t a_idx) noexcept
Definition: Version.h:22
constexpr decltype(auto) cbegin() const noexcept
Definition: Version.h:26
std::string string() const
Definition: Version.h:49
constexpr std::uint32_t pack() const noexcept
Definition: Version.h:40
constexpr const_reference operator[](std::size_t a_idx) const noexcept
Definition: Version.h:23
static constexpr Version unpack(std::uint32_t a_packedVersion) noexcept
Definition: Version.h:71
std::uint16_t value_type
Definition: Version.h:8
constexpr decltype(auto) cend() const noexcept
Definition: Version.h:28
value_type & reference
Definition: Version.h:9
const value_type & const_reference
Definition: Version.h:10
constexpr Version() noexcept=default
constexpr std::strong_ordering compare(const Version &a_rhs) const noexcept
Definition: Version.h:30
constexpr decltype(auto) begin() const noexcept
Definition: Version.h:25
std::wstring wstring() const
Definition: Version.h:60
Definition: ID.h:6
constexpr std::strong_ordering operator<=>(const Version &a_lhs, const Version &a_rhs) noexcept
Definition: Version.h:86
constexpr bool operator==(const Version &a_lhs, const Version &a_rhs) noexcept
Definition: Version.h:85
std::optional< Version > GetFileVersion(stl::zwstring a_filename)
string(const CharT(&)[N]) -> string< CharT, N - 1 >
basic_zstring< wchar_t > zwstring
Definition: PCH.h:82
Definition: EffectArchetypes.h:65
std::string to_string(RE::EffectArchetype a_archetype)
Definition: EffectArchetypes.h:66