Plasma Engine  2.0
Loading...
Searching...
No Matches
Enum.h
1#pragma once
2
4
35template <typename Derived>
36struct plEnum : public Derived
37{
38public:
40 using StorageType = typename Derived::StorageType;
41
43 PL_ALWAYS_INLINE plEnum()
44 : m_Value((StorageType)Derived::Default)
45 {
46 } // [tested]
47
49 PL_ALWAYS_INLINE plEnum(const SelfType& rh)
50 : m_Value(rh.m_Value)
51 {
52 }
53
55 PL_ALWAYS_INLINE plEnum(typename Derived::Enum init)
56 : m_Value((StorageType)init)
57 {
58 } // [tested]
59
61 PL_ALWAYS_INLINE void operator=(const SelfType& rh) // [tested]
62 {
63 m_Value = rh.m_Value;
64 }
65
67 PL_ALWAYS_INLINE void operator=(const typename Derived::Enum value) // [tested]
68 {
69 m_Value = (StorageType)value;
70 }
71
73 PL_ALWAYS_INLINE bool operator==(const SelfType& rhs) const { return m_Value == rhs.m_Value; }
74 PL_ALWAYS_INLINE bool operator!=(const SelfType& rhs) const { return m_Value != rhs.m_Value; }
75 PL_ALWAYS_INLINE bool operator>(const SelfType& rhs) const { return m_Value > rhs.m_Value; }
76 PL_ALWAYS_INLINE bool operator<(const SelfType& rhs) const { return m_Value < rhs.m_Value; }
77 PL_ALWAYS_INLINE bool operator>=(const SelfType& rhs) const { return m_Value >= rhs.m_Value; }
78 PL_ALWAYS_INLINE bool operator<=(const SelfType& rhs) const { return m_Value <= rhs.m_Value; }
79
80 PL_ALWAYS_INLINE bool operator==(typename Derived::Enum value) const { return m_Value == value; }
81 PL_ALWAYS_INLINE bool operator!=(typename Derived::Enum value) const { return m_Value != value; }
82 PL_ALWAYS_INLINE bool operator>(typename Derived::Enum value) const { return m_Value > value; }
83 PL_ALWAYS_INLINE bool operator<(typename Derived::Enum value) const { return m_Value < value; }
84 PL_ALWAYS_INLINE bool operator>=(typename Derived::Enum value) const { return m_Value >= value; }
85 PL_ALWAYS_INLINE bool operator<=(typename Derived::Enum value) const { return m_Value <= value; }
86
88 PL_ALWAYS_INLINE SelfType operator|(const SelfType& rhs) const { return static_cast<typename Derived::Enum>(m_Value | rhs.m_Value); } // [tested]
89 PL_ALWAYS_INLINE SelfType operator&(const SelfType& rhs) const { return static_cast<typename Derived::Enum>(m_Value & rhs.m_Value); } // [tested]
90
92 PL_ALWAYS_INLINE operator typename Derived::Enum() const // [tested]
93 {
94 return static_cast<typename Derived::Enum>(m_Value);
95 }
96
98 PL_ALWAYS_INLINE StorageType GetValue() const // [tested]
99 {
100 return m_Value;
101 }
102
104 PL_ALWAYS_INLINE void SetValue(StorageType value) // [tested]
105 {
106 m_Value = value;
107 }
108
109private:
110 StorageType m_Value;
111};
112
113
114#define PL_ENUM_VALUE_TO_STRING(name) \
115 case name: \
116 return PL_PP_STRINGIFY(name);
117
134#define PL_ENUM_TO_STRING(...) \
135 const char* ToString(plUInt32 value) \
136 { \
137 switch (value) \
138 { \
139 PL_EXPAND_ARGS(PL_ENUM_VALUE_TO_STRING, ##__VA_ARGS__) \
140 default: \
141 return nullptr; \
142 } \
143 }
A custom enum implementation that allows to define the underlying storage type to control its memory ...
Definition Enum.h:37
PL_ALWAYS_INLINE StorageType GetValue() const
Returns the enum value as an integer.
Definition Enum.h:98
PL_ALWAYS_INLINE plEnum()
Default constructor.
Definition Enum.h:43
PL_ALWAYS_INLINE void SetValue(StorageType value)
Sets the enum value through an integer.
Definition Enum.h:104
PL_ALWAYS_INLINE void operator=(const SelfType &rh)
Assignment operator.
Definition Enum.h:61
PL_ALWAYS_INLINE bool operator==(const SelfType &rhs) const
Comparison operators.
Definition Enum.h:73
PL_ALWAYS_INLINE plEnum(const SelfType &rh)
Copy constructor.
Definition Enum.h:49
PL_ALWAYS_INLINE void operator=(const typename Derived::Enum value)
Assignment operator.
Definition Enum.h:67
PL_ALWAYS_INLINE plEnum(typename Derived::Enum init)
Construct from a C++ enum, and implicit conversion from enum type.
Definition Enum.h:55
PL_ALWAYS_INLINE SelfType operator|(const SelfType &rhs) const
brief Bitwise operators
Definition Enum.h:88