PLCnext API Documentation 23.3.0.32
EnumDictionary.hxx
1
2//
3// Copyright PHOENIX CONTACT Electronics GmbH
4//
6#pragma once
8#include "Arp/System/Core/EnumDictionaryBase.hxx"
9#include "Arp/System/Core/TypeName.hxx"
10
11namespace Arp
12{
13
20template<class T>
22{
23protected: // usings
25 using U = typename Base::U;
26 using InitializerList = typename Base::InitializerList;
27
28public: // construction/destruction
31 EnumDictionary(const InitializerList& fields);
35 EnumDictionary(const char* invalidValueString, const InitializerList& fields);
36
37public: // operations used by stream operators of enums
43 std::ostream& WriteEnumString(std::ostream& os, T value)const;
44
49 std::istream& ReadEnumValue(std::istream& is, T& value)const;
50
51public: // string conversion operations
56 const char* GetEnumString(T value)const;
57
62 bool TryGetEnumString(T value, const char*& pResult)const;
63
68 T GetEnumValue(const char* fieldName)const;
69
74 bool TryGetEnumValue(const char* fieldName, T& value)const;
75};
76
78// inline methods of class EnumDictionary<T>
79template<class T>
80EnumDictionary<T>::EnumDictionary(const InitializerList& fields)
81 : Base("", fields)
82{
83}
84
85template<class T>
86EnumDictionary<T>::EnumDictionary(const char* invalidValueString, const InitializerList& fields)
87 : Base(invalidValueString, fields)
88{
89}
90
91template<class T>
92std::ostream& EnumDictionary<T>::WriteEnumString(std::ostream& os, T value)const
93{
94 const char* pString = nullptr;
95 if (this->TryGetEnumString(value, pString))
96 {
97 os << pString;
98 }
99 else
100 {
101 if (this->invalidValueString.IsEmpty())
102 {
103 os << static_cast<U>(value);
104 }
105 else
106 {
107 os << this->invalidValueString;
108 }
109 os.setstate(std::ios::failbit); // inform caller that entry was not found
110 }
111 return os;
112}
113
114template<class T>
115inline std::istream& EnumDictionary<T>::ReadEnumValue(std::istream& is, T& value)const
116{
117 String token;
118 is >> token;
119 if (!TryGetEnumValue(token, value))
120 {
121 is.setstate(std::ios::failbit);
122 }
123 return is;
124}
125
126template<class T>
127inline const char* EnumDictionary<T>::GetEnumString(T value)const
128{
129 const char* pResult = nullptr;
130 if(!this->TryGetEnumString(value, pResult))
131 {
132 throw Exception("Could not find enum string of enum '{}' for value '{}'.", TypeName<T>().Value, static_cast<U>(value));
133 }
134 return pResult;
135}
136
137template<class T>
138inline bool EnumDictionary<T>::TryGetEnumString(T value, const char*& pResult)const
139{
140 return this->TryGetEnumStringInternal(value, pResult); // from base class
141}
142
143template<class T>
144inline T EnumDictionary<T>::GetEnumValue(const char* fieldName)const
145{
146 T result;
147 if (!this->TryGetEnumValue(fieldName, result))
148 {
149 throw Exception("Could not find enum value of enum '{}' for string '{}'.", TypeName<T>().Value, fieldName);
150 }
151 return result;
152}
153
154template<class T>
155inline bool EnumDictionary<T>::TryGetEnumValue(const char* fieldName, T& value)const
156{
157 return this->TryGetEnumValueInternal(fieldName, value); // from base class
158}
159
160} // end of namespace Arp
161
The class implements an adapter for enums to define the string literals of the enum entries.
Definition: EnumDictionaryBase.hxx:21
The class implements an adapter for enums to define the string literals of the enum entries.
Definition: EnumDictionary.hxx:22
std::ostream & WriteEnumString(std::ostream &os, T value) const
Writes the string of the specified enum value to the given stream.
Definition: EnumDictionary.hxx:92
bool TryGetEnumString(T value, const char *&pResult) const
Returns the string of the specified enum value or nullptr if the value could not be found.
Definition: EnumDictionary.hxx:138
T GetEnumValue(const char *fieldName) const
Returns the value of the specified enum string.
Definition: EnumDictionary.hxx:144
std::istream & ReadEnumValue(std::istream &is, T &value) const
Tries to return the enum entry of the specified string.
Definition: EnumDictionary.hxx:115
bool TryGetEnumValue(const char *fieldName, T &value) const
Tries to get the enum value of the specified string.
Definition: EnumDictionary.hxx:155
EnumDictionary(const InitializerList &fields)
Constructor passing the enum fields as initializer list.
Definition: EnumDictionary.hxx:80
const char * GetEnumString(T value) const
Returns the string of the specified enum entry.
Definition: EnumDictionary.hxx:127
This is the base class of all Arp exception classes.
Definition: Exception.hpp:16
This (meta programming) class provides the C++ typename of the as template argument passed type.
Definition: TypeName.hxx:67
Root namespace for the PLCnext API
ARP_CXX_SYMBOL_EXPORT bool TryGetEnumValue(const char *name, T &value)
This helper function tries to return the enum entry of the specified string.