PLCnext API Documentation 25.0.2.69
EnumDictionaryBase.hxx
1
2//
3// Copyright Phoenix Contact GmbH & Co. KG
4//
6#pragma once
8#include <map>
9
10namespace Arp { namespace Base { namespace Core
11{
12
16template<class T>
18{
19 static_assert(std::is_enum_v<T>);
20 static_assert(std::is_integral_v<std::underlying_type_t<T>>);
21
22protected: // usings
23 using Fields = std::map<T, const char*>;
24 using InitializerList = std::initializer_list<typename Fields::value_type>;
25 using U = typename std::underlying_type<T>::type;
26
27protected: // construction/destruction
28 explicit EnumDictionaryBase(const InitializerList& fields);
29 EnumDictionaryBase(const char* invalidValueString, const InitializerList& fields);
35
36protected: // string conversion operations
37 bool TryGetEnumStringInternal(T value, const char*& pResult)const;
38 bool TryGetEnumValueInternal(const char* fieldName, T& value)const;
39
40protected: // fields
41 Fields fields;
42 String invalidValueString;
43
44protected: // static/const fields
45 static const T zeroValue = static_cast<T>(0);
46};
47
49// inline methods of class EnumDictionaryBase<T>
50
53template<class T>
55 : fields(fields)
56{
57}
58
62template<class T>
63inline EnumDictionaryBase<T>::EnumDictionaryBase(const char* invalidValueString, const InitializerList& fields)
64 : fields(fields)
65 , invalidValueString(invalidValueString)
66{
67}
68
70template<class T>
72
74template<class T>
76
78template<class T>
80
82template<class T>
84
86template<class T>
88
93template<class T>
94inline bool EnumDictionaryBase<T>::TryGetEnumStringInternal(T value, const char*& pResult)const
95{
96 auto it = this->fields.find(value);
97 if (it == this->fields.end())
98 {
99 pResult = nullptr;
100 return false;
101 }
102 // else
103 pResult = it->second;
104 return true;
105}
106
111template<class T>
112inline bool EnumDictionaryBase<T>::TryGetEnumValueInternal(const char* fieldName, T& value)const
113{
114 auto it = std::find_if(this->fields.begin(), this->fields.end(), [fieldName](const auto & each)
115 {
116 return ::strcmp(each.second, fieldName) == 0;
117 });
118 if (it == this->fields.end())
119 {
120 value = T{};
121 return false;
122 }
123 // else
124 value = it->first;
125 return true;
126}
127
128}}} // end of namespace Arp::Base::Core
129
130namespace Arp {
133}
This class implements an adapter for enums to define the string literals of the enum entries.
Definition: EnumDictionaryBase.hxx:18
bool TryGetEnumStringInternal(T value, const char *&pResult) const
Returns the string of the specified enum value or nullptr if the value could not be found.
Definition: EnumDictionaryBase.hxx:94
EnumDictionaryBase & operator=(EnumDictionaryBase &&arg) noexcept
Move-assignment operator.
EnumDictionaryBase & operator=(const EnumDictionaryBase &arg)
Copy-assignment operator.
EnumDictionaryBase(const EnumDictionaryBase &arg)
Copy constructor.
std::map< T, const char * > Fields
The container type containing the value/name pairs of all enum fields.
Definition: EnumDictionaryBase.hxx:23
bool TryGetEnumValueInternal(const char *fieldName, T &value) const
Tries to get the enum value of the specified string.
Definition: EnumDictionaryBase.hxx:112
std::initializer_list< typename Fields::value_type > InitializerList
The initializer list type of the Fields container.
Definition: EnumDictionaryBase.hxx:24
typename std::underlying_type< T >::type U
The underlying integral type of the enum.
Definition: EnumDictionaryBase.hxx:25
EnumDictionaryBase(EnumDictionaryBase &&arg) noexcept
Move constructor.
EnumDictionaryBase(const InitializerList &fields)
Constructor passing the enum fields as initializer list.
Definition: EnumDictionaryBase.hxx:54
This class represents the Arp String. The implementation is based on std::string.
Definition: String.hpp:39
Root namespace for the PLCnext API