PLCnext API Documentation  21.0.0.35466
Enum.hxx
1 //
3 // Copyright PHOENIX CONTACT Electronics GmbH
4 //
6 #pragma once
7 #include "Arp/System/Core/Arp.h"
8 #include "Arp/System/Core/TypeName.hxx"
9 #include "Arp/System/Core/EnumStrings.hxx"
10 #include "Arp/System/Core/Exception.hpp"
11 #include <iostream>
12 #include <utility>
13 
14 namespace Arp
15 {
16 
19 
22 template<class T = void>
23 class Enum
24 {
25 public: // typedefs
27  typedef T Value;
28 
29 public: // construction
32  Enum(Value value = Value(0));
33 
34 public: // static operations
39  static bool TryParse(const char* input, Enum& result);
40 
45  static bool TryParse(const String& input, Enum& result);
46 
51  static Enum Parse(const char* input);
52 
57  static Enum Parse(const String& input);
58 
59 public: // operations
62  T ToValue(void)const;
63 
66  String ToString(void)const;
67 
68 public: // operators
72  Enum& operator=(const Enum& rhs) = default;
73 
77  Enum& operator=(Value rhs);
78 
82  Enum& operator&=(Enum rhs);
83 
87  Enum& operator|=(Enum rhs);
88 
89 public: // conversion operators
91  operator T(void)const;
92 
93 private: // fields
94  Value value;
95 };
96 
98 // global function related to class Enum<T>
99 // define global make_ function as usual for C++
104 template<class T>
106 {
107  return Enum<T>(value);
108 }
109 
111 // inline methods of class Enum<T>
112 template<class T>
113 inline Enum<T>::Enum(Value valueArg)
114  : value(valueArg)
115 {
116 }
117 
118 template<class T>
119 inline Enum<T>& Enum<T>::operator=(Value arg)
120 {
121  this->value = arg;
122  return *this;
123 }
124 
125 template<class T>
126 inline Enum<T>::operator T()const
127 {
128  return this->ToValue();
129 }
130 
131 template<class T>
132 inline Enum<T>& Enum<T>::operator&=(Enum rhs)
133 {
134  using U = typename std::underlying_type<T>::type;
135  this->value = static_cast<T>(static_cast<U>(this->value) & static_cast<U>(rhs.value));
136  return *this;
137 }
138 
139 template<class T>
141 {
142  using U = typename std::underlying_type<T>::type;
143  this->value = static_cast<T>(static_cast<U>(this->value) | static_cast<U>(rhs.value));
144  return *this;
145 }
146 
147 template<class T>
148 inline T Enum<T>::ToValue()const
149 {
150  return value;
151 }
152 
153 template<class T>
155 {
156  return EnumStrings<T>::GetEntry(this->value);
157 }
158 
159 template<class T>
160 inline bool Enum<T>::TryParse(const String& input, Enum& result)
161 {
162  T resultValue = (T)0;
163  if (EnumStrings<T>::TryGetEntryValue(input, resultValue))
164  {
165  result = Enum(resultValue);
166  return true;
167  }
168  // else not found
169  return false;
170 }
171 
172 template<class T>
173 inline Enum<T> Enum<T>::Parse(const String& input)
174 {
175  Enum<T> result;
176  if (!TryParse(input, result))
177  {
178  throw Exception("Unknown string of Enum '{0}' occurs: {1}", TypeName<Enum>().Value, input);
179  }
180  return result;
181 }
182 
183 template<class T>
184 inline bool Enum<T>::TryParse(const char* input, Enum& result)
185 {
186  return Enum::TryParse(String(input), result);
187 }
188 
189 template<class T>
190 inline Enum<T> Enum<T>::Parse(const char* input)
191 {
192  Enum result;
193  if (!TryParse(input, result))
194  {
195  throw Exception("Unknown string of Enum '{0}' occurs: {1}", TypeName<Enum>().Value, input);
196  }
197  return result;
198 }
199 
201 // global operators of class Enum<T>
202 
207 template<class T>
208 inline std::ostream& operator<<(std::ostream& os, Enum<T> value)
209 {
210  return os << Enum<T>(value).ToString();
211 }
212 
217 template<class T>
218 inline bool operator==(Enum<T> lhs, Enum<T> rhs)
219 {
220  return lhs.ToValue() == rhs.ToValue();
221 }
222 
227 template<class T>
228 inline bool operator<(Enum<T> lhs, Enum<T> rhs)
229 {
230  return lhs.ToValue() < rhs.ToValue();
231 }
232 
237 template<class T>
239 {
240  Enum<T> result = lhs;
241  result &= rhs;
242  return result;
243 }
244 
249 template<class T>
251 {
252  Enum<T> result = lhs;
253  result |= rhs;
254  return result;
255 }
256 
258 
259 } // end of namespace Arp
Enum & operator=(const Enum &rhs)=default
The default assignment operator.
Enum< T > operator|(Enum< T > lhs, Enum< T > rhs)
Bitwise Or operator for class Enum.
Definition: Enum.hxx:250
Enum(Value value=Value(0))
Constructs an instace of Enum with the given value.
Definition: Enum.hxx:113
static bool TryParse(const char *input, Enum &result)
Tries to parse the given input string.
Definition: Enum.hxx:184
This (meta programming) class provides the C++ typename of the as template argument passed type...
Definition: TypeName.hxx:55
Enum< T > make_enum(T value)
Global make function to adapt any enum class by class Enum.
Definition: Enum.hxx:105
T ToValue(void) const
Converts this instance to the value of its adapted type
Definition: Enum.hxx:148
static Enum Parse(const char *input)
Parses the given input string.
Definition: Enum.hxx:190
T Value
The adapted enum class type.
Definition: Enum.hxx:27
Adapter class for enums to make them loggable and parsable from e.g. XML files.
Definition: Enum.hxx:23
Enum & operator &=(Enum rhs)
The assignment AND operator.
Arp::BasicString< char8 > String
The Arp String class.
Definition: TypeSystem.h:27
static const char * GetEntry(T key)
Returns the string of the specified enum entry.
Definition: EnumStrings.hxx:70
The class implements an adapter for enums to define the string literals of the enum entries...
Definition: EnumStrings.hxx:39
Root namespace for the PLCnext API
Enum< T > operator &(Enum< T > lhs, Enum< T > rhs)
Bitwise And operator for class Enum.
Definition: Enum.hxx:238
Enum & operator|=(Enum rhs)
The assignment OR operator.
Definition: Enum.hxx:140
bool operator==(const BasicString< CharType, Alloc > &left, const BasicString< CharType, Alloc > &right)
Compares the left string to the right string on equality.
Definition: BasicString.hxx:1752
String ToString(void) const
Converts this instance to its string representation.
Definition: Enum.hxx:154
This is the base class of all Arp exception classes.
Definition: Exception.hpp:15