PLCnext API Documentation 25.0.2.69
Enum.hxx
1
2//
3// Copyright Phoenix Contact GmbH & Co. KG
4//
6#pragma once
8#include <iosfwd>
9#include <type_traits>
10
11namespace Arp { namespace Base { namespace Core
12{
13
16
19template<class T>
20class Enum
21{
22public: // usings
23 using Value = T;
24 using U = typename std::underlying_type<T>::type;
25
26public: // construction
27 Enum(Value initialValue = Zero);
28
29public: // static/const fields
30 static constexpr T Zero = static_cast<T>(0);
31
32public: // static operations
33 static bool TryParse(const String& input, Enum& result);
34 static bool TryParse(const String& input, T& result);
35 static bool TryParse(const char* input, Enum& result);
36 static bool TryParse(const char* input, T& result);
37 static Enum Parse(const String& input);
38 static Enum Parse(const char* input);
39
40public: // conversion operators
41 operator T(void)const;
42
43public: // (logical) assign operators
44 Enum& operator=(Value arg);
45 Enum& operator&=(Enum rhs);
46 Enum& operator|=(Enum rhs);
47
48public: // getter
49 T GetValue(void)const;
50
51public: // operations
52 bool HasFlag(Value flag);
53 bool HasAnyFlag(Value flags);
54 bool HasAllFlags(Value flags);
55 U ToUnderlyingType(void)const;
56 String ToString(bool throwIfInvalid = true)const;
57
58private: // fields
59 Value value;
60
61public: // global (friend) operators (shall be defined implicit inline due to EXPORT on Windows)
62
64 // stream operators of class Enum<T>
65
70 friend std::ostream& operator<<(std::ostream& os, Enum arg)
71 {
72 return os << arg.value;
73 }
74
79 friend std::istream& operator>>(std::istream& is, Enum& arg)
80 {
81 T enumValue = Enum<T>::Zero;
82 is >> enumValue;
83 if (!is.fail())
84 {
85 arg = enumValue;
86 }
87 return is;
88 }
89
91 // global bit operators of class Enum<T>
92
97 friend Enum operator|(Enum lhs, Enum rhs)
98 {
99 Enum result = lhs;
100 result |= rhs;
101 return result;
102 }
103
108 friend Enum operator&(Enum lhs, Enum rhs)
109 {
110 Enum result = lhs;
111 result &= rhs;
112 return result;
113 }
114
116 // global compare operators of class Enum<T>
121 friend bool operator==(Enum lhs, Enum rhs)
122 {
123 return lhs.value == rhs.value;
124 }
125
130 friend bool operator<(Enum lhs, Enum rhs)
131 {
132 return lhs.value < rhs.value;
133 }
134
139 friend bool operator>(Enum lhs, Enum rhs)
140 {
141 return lhs.value > rhs.value;
142 }
143
148 friend bool operator<=(Enum lhs, Enum rhs)
149 {
150 return lhs.value <= rhs.value;
151 }
152
157 friend bool operator>=(Enum lhs, Enum rhs)
158 {
159 return lhs.value >= rhs.value;
160 }
161
163 // global compare operators of class Enum<T> and T
164
169 friend bool operator==(Enum lhs, T rhs)
170 {
171 return lhs.value == rhs;
172 }
173
178 friend bool operator<(Enum lhs, T rhs)
179 {
180 return lhs.value < rhs;
181 }
182
187 friend bool operator>(Enum lhs, T rhs)
188 {
189 return lhs.value > rhs;
190 }
191
196 friend bool operator<=(Enum lhs, T rhs)
197 {
198 return lhs.value <= rhs;
199 }
200
205 friend bool operator>=(Enum lhs, T rhs)
206 {
207 return lhs.value >= rhs;
208 }
209
211 // compare operators of T and class Enum<T>
212
217 friend bool operator==(T lhs, Enum rhs)
218 {
219 return lhs == rhs.value;
220 }
221
226 friend bool operator<(T lhs, Enum rhs)
227 {
228 return lhs < rhs.value;
229 }
230
235 friend bool operator>(T lhs, Enum rhs)
236 {
237 return lhs > rhs.value;
238 }
239
244 friend bool operator<=(T lhs, Enum rhs)
245 {
246 return lhs <= rhs.value;
247 }
248
253 friend bool operator>=(T lhs, Enum rhs)
254 {
255 return lhs >= rhs.value;
256 }
257};
258
259template<class T>
260Enum<T> make_enum(T value);
261
263
264}}} // end of namespace Arp::Base::Core
265
266namespace Arp {
271}
273template<class T> struct fmt::formatter<Arp::Base::Core::Enum<T>> : public fmt::ostream_formatter {};
274
275#include "Arp/Base/Core/Detail/Enum.ipp"
Adapter class for enums to make them loggable and parsable from e.g. XML files.
Definition: Enum.hxx:21
friend std::ostream & operator<<(std::ostream &os, Enum arg)
Makes the Enum class loggable and streamable.
Definition: Enum.hxx:70
friend bool operator>=(Enum lhs, Enum rhs)
Greater or equal operator for class Enum.
Definition: Enum.hxx:157
friend Enum operator&(Enum lhs, Enum rhs)
Bitwise And operator for class Enum.
Definition: Enum.hxx:108
friend bool operator<(Enum lhs, Enum rhs)
Less operator for class Enum.
Definition: Enum.hxx:130
friend bool operator>(Enum lhs, Enum rhs)
Greater operator for class Enum.
Definition: Enum.hxx:139
friend bool operator>(T lhs, Enum rhs)
Greater operator for class Enum.
Definition: Enum.hxx:235
friend bool operator==(Enum lhs, T rhs)
Equality operator of class Enum.
Definition: Enum.hxx:169
friend bool operator<(Enum lhs, T rhs)
Less operator for class Enum.
Definition: Enum.hxx:178
friend bool operator<=(T lhs, Enum rhs)
Less or equal operator for class Enum.
Definition: Enum.hxx:244
friend bool operator==(Enum lhs, Enum rhs)
Equality operator of class Enum.
Definition: Enum.hxx:121
friend bool operator<(T lhs, Enum rhs)
Less operator for class Enum.
Definition: Enum.hxx:226
friend bool operator<=(Enum lhs, Enum rhs)
Less or equal operator for class Enum.
Definition: Enum.hxx:148
friend bool operator>(Enum lhs, T rhs)
Greater operator for class Enum.
Definition: Enum.hxx:187
friend bool operator>=(Enum lhs, T rhs)
Greater or equal operator for class Enum.
Definition: Enum.hxx:205
static constexpr T Zero
Zero initialized Enum value.
Definition: Enum.hxx:30
T Value
The adapted enum class type.
Definition: Enum.hxx:23
friend bool operator==(T lhs, Enum rhs)
Equality operator of class Enum.
Definition: Enum.hxx:217
typename std::underlying_type< T >::type U
The underlying integral type of the adapted enum class type.
Definition: Enum.hxx:24
friend bool operator>=(T lhs, Enum rhs)
Greater or equal operator for class Enum.
Definition: Enum.hxx:253
friend std::istream & operator>>(std::istream &is, Enum &arg)
Makes the Enum class parsable.
Definition: Enum.hxx:79
friend bool operator<=(Enum lhs, T rhs)
Less or equal operator for class Enum.
Definition: Enum.hxx:196
friend Enum operator|(Enum lhs, Enum rhs)
Bitwise Or operator for class Enum.
Definition: Enum.hxx:97
This class represents the Arp String. The implementation is based on std::string.
Definition: String.hpp:39
Enum< T > make_enum(T value)
Global make function to adapt any enum class by class Enum.
Definition: Enum.ipp:222
Enum & operator=(Value arg)
The assignment operator for a value of the adapted type.
Definition: Enum.ipp:126
T GetValue(void) const
Gets the adapted enum value.
Definition: Enum.ipp:155
bool HasAnyFlag(Value flags)
Determines if this enum value has any of the the supplied flags set.
Definition: Enum.ipp:173
Enum & operator&=(Enum rhs)
The assignment AND operator.
Definition: Enum.ipp:136
Enum & operator|=(Enum rhs)
The assignment OR operator.
Definition: Enum.ipp:146
static bool TryParse(const String &input, Enum &result)
Tries to parse the given input string.
Definition: Enum.ipp:45
String ToString(bool throwIfInvalid=true) const
Converts this instance to its string representation.
Definition: Enum.ipp:199
U ToUnderlyingType(void) const
Converts this instance to the underlying integral type of its adapted enum type
Definition: Enum.ipp:190
Enum(Value initialValue=Zero)
Constructs an instance of Enum with the given value.
Definition: Enum.ipp:25
static Enum Parse(const String &input)
Parses the given input string.
Definition: Enum.ipp:90
bool HasAllFlags(Value flags)
Determines if this enum value has all of the supplied flag set.
Definition: Enum.ipp:182
bool HasFlag(Value flag)
Determines if this enum value has the supplied flag set.
Definition: Enum.ipp:164
Root namespace for the PLCnext API