CommonLibSSE (powerof3)
Enum.h
Go to the documentation of this file.
1 #pragma once
2 
3 namespace REX
4 {
5  template <
6  class E,
7  class U = std::underlying_type_t<E>>
8  class Enum
9  {
10  public:
11  using enum_type = E;
12  using underlying_type = U;
13 
14  static_assert(std::is_enum_v<E>, "Enum<E, ...> must be an enum");
15  static_assert(std::is_integral_v<U>, "Enum<..., U> must be an integral");
16 
17  constexpr Enum() noexcept = default;
18  constexpr Enum(const Enum&) noexcept = default;
19  constexpr Enum(Enum&&) noexcept = default;
20 
21  template <class U2> // NOLINTNEXTLINE(google-explicit-constructor)
22  constexpr Enum(Enum<E, U2> a_rhs) noexcept :
23  _impl(static_cast<U>(a_rhs.get()))
24  {}
25 
26  constexpr Enum(E a_value) noexcept :
27  _impl(static_cast<U>(a_value))
28  {}
29 
30  ~Enum() noexcept = default;
31 
32  constexpr Enum& operator=(const Enum&) noexcept = default;
33  constexpr Enum& operator=(Enum&&) noexcept = default;
34 
35  template <class U2>
36  constexpr Enum& operator=(Enum<E, U2> a_rhs) noexcept
37  {
38  _impl = static_cast<U>(a_rhs.get());
39  }
40 
41  constexpr Enum& operator=(E a_value) noexcept
42  {
43  _impl = static_cast<U>(a_value);
44  return *this;
45  }
46 
47  public:
48  [[nodiscard]] explicit constexpr operator bool() const noexcept { return _impl != static_cast<U>(0); }
49 
50  [[nodiscard]] constexpr E operator*() const noexcept { return get(); }
51  [[nodiscard]] constexpr E get() const noexcept { return static_cast<E>(_impl); }
52  [[nodiscard]] constexpr U underlying() const noexcept { return _impl; }
53 
54  public:
55  friend constexpr bool operator==(Enum a_lhs, Enum a_rhs) noexcept { return a_lhs.underlying() == a_rhs.underlying(); }
56  friend constexpr bool operator==(Enum a_lhs, E a_rhs) noexcept { return a_lhs.underlying() == static_cast<U>(a_rhs); }
57  friend constexpr bool operator==(E a_lhs, Enum a_rhs) noexcept { return static_cast<U>(a_lhs) == a_rhs.underlying(); }
58 
59  private:
60  U _impl{ 0 };
61  };
62 
63  template <class... Args>
64  Enum(Args...) -> Enum<
65  std::common_type_t<Args...>,
66  std::underlying_type_t<
67  std::common_type_t<Args...>>>;
68 }
Definition: Enum.h:9
~Enum() noexcept=default
constexpr Enum() noexcept=default
constexpr friend bool operator==(Enum a_lhs, E a_rhs) noexcept
Definition: Enum.h:56
constexpr friend bool operator==(E a_lhs, Enum a_rhs) noexcept
Definition: Enum.h:57
constexpr E get() const noexcept
Definition: Enum.h:51
constexpr friend bool operator==(Enum a_lhs, Enum a_rhs) noexcept
Definition: Enum.h:55
constexpr Enum & operator=(E a_value) noexcept
Definition: Enum.h:41
constexpr E operator*() const noexcept
Definition: Enum.h:50
constexpr U underlying() const noexcept
Definition: Enum.h:52
U underlying_type
Definition: Enum.h:12
constexpr Enum(E a_value) noexcept
Definition: Enum.h:26
E enum_type
Definition: Enum.h:11
Definition: BSDirectInputManager.h:8
Enum(Args...) -> Enum< std::common_type_t< Args... >, std::underlying_type_t< std::common_type_t< Args... >>>