PLCnext API Documentation 25.0.2.69
EnumDictionary.hxx
1
2//
3// Copyright Phoenix Contact GmbH & Co. KG
4//
6#pragma once
7
8#ifndef ARP_USE_ARP_SYSTEM_CORE
9
10#include "Arp/Base/Core/EnumDictionary.hxx"
11
12#else
13
14#include "Arp/System/Core/Arp.h"
15#include "Arp/System/Core/EnumDictionaryBase.hxx"
16#include "Arp/System/Core/TypeName.hxx"
17
18namespace Arp
19{
20
27template<class T>
28class EnumDictionary : public EnumDictionaryBase<T>
29{
30protected: // usings
31 using Base = EnumDictionaryBase<T>;
32 using U = typename Base::U;
33 using InitializerList = typename Base::InitializerList;
34
35public: // construction/destruction
38 EnumDictionary(const InitializerList& fields);
42 EnumDictionary(const char* invalidValueString, const InitializerList& fields);
43
44public: // operations used by stream operators of enums
50 std::ostream& WriteEnumString(std::ostream& os, T value)const;
51
56 std::istream& ReadEnumValue(std::istream& is, T& value)const;
57
58public: // string conversion operations
63 const char* GetEnumString(T value)const;
64
69 bool TryGetEnumString(T value, const char*& pResult)const;
70
75 T GetEnumValue(const char* fieldName)const;
76
81 bool TryGetEnumValue(const char* fieldName, T& value)const;
82};
83
85// inline methods of class EnumDictionary<T>
86template<class T>
87EnumDictionary<T>::EnumDictionary(const InitializerList& fields)
88 : Base("", fields)
89{
90}
91
92template<class T>
93EnumDictionary<T>::EnumDictionary(const char* invalidValueString, const InitializerList& fields)
94 : Base(invalidValueString, fields)
95{
96}
97
98template<class T>
99std::ostream& EnumDictionary<T>::WriteEnumString(std::ostream& os, T value)const
100{
101 const char* pString = nullptr;
102 if (this->TryGetEnumString(value, pString))
103 {
104 os << pString;
105 }
106 else
107 {
108 if (this->invalidValueString.IsEmpty())
109 {
110 os << String::Format("{:d}", static_cast<U>(value));
111 }
112 else
113 {
114 os << this->invalidValueString;
115 }
116 os.setstate(std::ios::failbit); // inform caller that entry was not found
117 }
118 return os;
119}
120
121template<class T>
122inline std::istream& EnumDictionary<T>::ReadEnumValue(std::istream& is, T& value)const
123{
124 String token;
125 is >> token;
126 if (!TryGetEnumValue(token, value))
127 {
128 is.setstate(std::ios::failbit);
129 }
130 return is;
131}
132
133template<class T>
134inline const char* EnumDictionary<T>::GetEnumString(T value)const
135{
136 const char* pResult = nullptr;
137 if(!this->TryGetEnumString(value, pResult))
138 {
139 throw Exception("Could not find enum string of enum '{}' for value '{}'.", TypeName<T>(), static_cast<U>(value));
140 }
141 return pResult;
142}
143
144template<class T>
145inline bool EnumDictionary<T>::TryGetEnumString(T value, const char*& pResult)const
146{
147 return this->TryGetEnumStringInternal(value, pResult); // from base class
148}
149
150template<class T>
151inline T EnumDictionary<T>::GetEnumValue(const char* fieldName)const
152{
153 T result;
154 if (!this->TryGetEnumValue(fieldName, result))
155 {
156 throw Exception("Could not find enum value of enum '{}' for string '{}'.", TypeName<T>(), fieldName);
157 }
158 return result;
159}
160
161template<class T>
162inline bool EnumDictionary<T>::TryGetEnumValue(const char* fieldName, T& value)const
163{
164 return this->TryGetEnumValueInternal(fieldName, value); // from base class
165}
166
167} // end of namespace Arp
168
169#endif // ndef ARP_USE_ARP_SYSTEM_CORE
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
const char * GetEnumString(T value) const
Returns the string of the specified enum entry.
Definition: EnumDictionary.ipp:82
bool TryGetEnumValue(const char *fieldName, T &value) const
Tries to get the enum value of the specified string.
Definition: EnumDictionary.ipp:122
std::istream & ReadEnumValue(std::istream &is, T &value) const
Tries to return the enum entry of the specified string.
Definition: EnumDictionary.ipp:66
T GetEnumValue(const char *fieldName) const
Returns the value of the specified enum string.
Definition: EnumDictionary.ipp:107
EnumDictionary(const InitializerList &fields)
Constructor passing the enum fields as initializer list.
Definition: EnumDictionary.ipp:20
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.ipp:97
std::ostream & WriteEnumString(std::ostream &os, T value) const
Writes the string of the specified enum value to the given stream.
Definition: EnumDictionary.ipp:40
Root namespace for the PLCnext API