Loading...
Searching...
No Matches
EnumUtils.h
1/*
2
3MIT License
4
5Copyright (c) 2017 FMI Open Development / Markus Peura, first.last@fmi.fi
6
7Permission is hereby granted, free of charge, to any person obtaining a copy
8of this software and associated documentation files (the "Software"), to deal
9in the Software without restriction, including without limitation the rights
10to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11copies of the Software, and to permit persons to whom the Software is
12furnished to do so, subject to the following conditions:
13
14The above copyright notice and this permission notice shall be included in all
15copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23SOFTWARE.
24
25*/
26/*
27Part of Rack development has been done in the BALTRAD projects part-financed
28by the European Union (European Regional Development Fund and European
29Neighbourhood Partnership Instrument, Baltic Sea Region Programme 2007-2013)
30*/
31
32#ifndef DRAIN_ENUM_UTILS
33#define DRAIN_ENUM_UTILS
34
35#include "Dictionary.h"
36#include "Flags.h"
37
38namespace drain {
39
40
41
43
51template <class E, class OWNER=E>
52struct EnumDict {
53
54 //typedef FlagResolver::dict_t dict_t;
56
57 static
58 const dict_t dict;
59
60 static
61 const dict_t & getDict(){
62 return dict;
63 }
64
65
66
68 static inline
69 const std::string & str(const E & value){
70 return dict.getKey(value);
71 }
72
73
75
78 static
79 bool setValue(const std::string & key, E & value){ // NOTE: could be more general, without explicit template
80 if (drain::EnumDict<E>::dict.hasKey(key)){
82 return true; // assigned
83 }
84 else {
85 return false; // not found
86 }
87 }
88
89
91
96 static inline
97 E getValue(const E & value, bool lenient=true){
98 return value;
99 }
100
102
106 static inline
107 E getValue(const std::string &key, bool lenient=true){
108 return dict.getValue(key, lenient);
109 }
110
112
116 static inline
117 E getValue(const char *key, bool lenient=true){
118 return dict.getValue(key, lenient);
119 }
120
121 /*
122 static inline
123 E getValue(char * const key, bool lenient=true){
124 return dict.getValue(key, lenient);
125 }
126 */
127
129
140
144 static inline // TODO: remove/handle misleading lenient here
145 const std::string & getKey(const std::string & s, bool lenient=true){
146 return s;
147 }
148
149 static inline // TODO: remove/handle misleading lenient here
150 const char * getKey(const char * s, bool lenient=true){
151 return s;
152 }
153
154 static inline // TODO: remove/handle misleading lenient here
155 const std::string & getKey(const E & value, bool lenient=true){
156 return dict.getKey(value, lenient);
157 }
158
159 /*
160 static inline
161 const std::string & getKey(const char *value, bool lenient=true){
162 //return dict.getKey(std::string(value), lenient);
163 return dict.getKey(std::string(value), lenient);
164 }
165 */
166
168
178};
179
184template <typename E, bool STRICT=true>
185class EnumWrapper : public std::string { // StringWrapper<E> {
186
187public:
188
189 inline
190 EnumWrapper(const std::string & s=""){ // : std::string(s){
191 set(s);
192 };
193
195 inline
196 EnumWrapper(const E & x){
197 set(x);
198 };
199
200 template <typename T>
201 void set(const T & value){
202 // handle empty with default enum value?
203 assign(drain::EnumDict<E>::dict.getKey(value, !STRICT));
204 }
205
206 /*
207 virtual inline
208 void setSpecial(const E & x) override {
209 assign(drain::EnumDict<E>::dict.getKey(x, !STRICT));
210 };
211 */
212
213};
214
215class MultiEnumWrapper : public std::string { // StringWrapper<E> {
216
217public:
218
219 inline
220 MultiEnumWrapper(const std::string & s="") : std::string(s){
221 };
222
223 inline
224 MultiEnumWrapper(const char *s) : std::string(s){
225 };
226
228 // inline
229 // MultiEnumWrapper(const MultiEnumWrapper & wrapper) : std::string(wrapper){
230 // };
231
233 template <typename T>
234 inline
235 MultiEnumWrapper(const T & arg){
236 set(arg);
237 };
238
239
240 void set(const std::string &arg){
241 assign(arg);
242 }
243
244 void set(const char * arg){
245 assign(arg);
246 }
247
248 void set(const MultiEnumWrapper &arg){
249 assign(arg);
250 }
251
252 template <typename T>
253 void set(const T & value){
254 assign(drain::EnumDict<T>::dict.getKey(value, false));
255 }
256
257};
258
259
260
261/* Perhaps useful!
262template <class E>
263class EnumKey {
264
265public:
266
267 inline
268 EnumKey(const E & value) : key(EnumDict<E>::dict.getKey(value)){
269 };
270
271 inline
272 operator const std::string & () const {
273 return key;
274 }
275
276protected:
277
278 const std::string & key;
279};
280*/
281
282
284
293template <class F> // F =SingleFlagger<E>
294class EnumFlagger : public F {
295
296public:
297
298 typedef F fbase_t;
299 typedef typename F::value_t value_t;
300 typedef typename F::storage_t storage_t;
301 typedef typename F::dict_t dict_t;
302 typedef FlagResolver::ivalue_t ivalue_t;
303
305 inline
306 EnumFlagger(): fbase_t(EnumDict<value_t>::dict){
307 }
308
310 inline
311 EnumFlagger(const storage_t & v): fbase_t(EnumDict<value_t>::dict) {
312 this->value = v;
313 }
314
315 /*
316 inline
317 EnumFlagger(value_t v): fbase_t(v) {
318 }
319 */
320
322 template <typename ... T>
323 inline
324 EnumFlagger(const T & ... arg): fbase_t(EnumDict<value_t>::dict, arg...) { // designed for MultiFlagger
325 }
326
327 /* // keep
328 virtual void reset() override {
329 this->value = EnumDict<value_t>::defaultValue; // ALERT! enums need neutral value.
330 };
331 */
332
334
345
349 static
350 ivalue_t getValueNEW(const std::string & key){
351 return (ivalue_t)EnumDict<value_t>::dict.getValue(key);
352 };
353
354 // Raise?
355 /*
356 static inline
357 std::string getKeysNEW2(const storage_t & value, char separator = ','){
358 // currentStr = FlagResolver::getKeys(dict, this->value, this->separator);
359 return FlagResolver::getKeys(EnumDict<value_t>::dict, value, separator);
360 }
361 */
362
374 inline
376 this->set(flagger.value);
377 return *this;
378 }
379
380 template <class T>
381 inline
382 EnumFlagger<F> & operator=(const T & v){
383 this->set(v);
384 return *this;
385 }
386
387};
388
389
390} // drain::
391
392#define DRAIN_ENUM_DICT(enumtype) template <> const drain::EnumDict<enumtype>::dict_t drain::EnumDict<enumtype>::dict
393#define DRAIN_ENUM_DICT2(enumtype,owner) template <> const drain::EnumDict<enumtype,owner>::dict_t drain::EnumDict<enumtype,owner>::dict
394
395#define DRAIN_ENUM_ENTRY(nspace, key) {#key, nspace::key}
396
397#define DRAIN_ENUM_OSTREAM(enumtype) inline std::ostream & operator<<(std::ostream &ostr, const enumtype & e){return ostr << drain::EnumDict<enumtype>::dict.getKey(e);}
398
399
400#endif
Two-way mapping between strings and objects of template class T.
Definition Dictionary.h:63
const K & getKey(const V &value, bool lenient=true) const
Identity mapping useful for type deduction of template arguments in functions.
Definition Dictionary.h:172
const V & getValue(const K &key, bool lenient=true) const
Given a key, return the first value associated with it.
Definition Dictionary.h:149
Flagger accepting values of enum type E.
Definition EnumUtils.h:294
EnumFlagger(const storage_t &v)
Constructor with initial value.
Definition EnumUtils.h:311
EnumFlagger()
Default constructor.
Definition EnumUtils.h:306
EnumFlagger< F > & operator=(const EnumFlagger< F > &flagger)
Definition EnumUtils.h:375
static ivalue_t getValueNEW(const std::string &key)
Returns the static dictionary created for this value_t .
Definition EnumUtils.h:350
Definition EnumUtils.h:185
EnumWrapper(const E &x)
All the other constructors, including default constructor.
Definition EnumUtils.h:196
Definition EnumUtils.h:215
MultiEnumWrapper(const T &arg)
Default constructor.
Definition EnumUtils.h:235
Definition DataSelector.cpp:1277
A container for a static dictionary of enumeration values.
Definition EnumUtils.h:52
static E getValue(const char *key, bool lenient=true)
Convenience for object.set(...) like commands.
Definition EnumUtils.h:117
static const std::string & str(const E &value)
Convenience.
Definition EnumUtils.h:69
static E getValue(const E &value, bool lenient=true)
Convenience for object.set(...) like commands.
Definition EnumUtils.h:97
static bool setValue(const std::string &key, E &value)
Assign string values to an enumeration type.
Definition EnumUtils.h:79
static const std::string & getKey(const std::string &s, bool lenient=true)
Convenience for object.set(...) like commands.
Definition EnumUtils.h:145
static E getValue(const std::string &key, bool lenient=true)
Convenience for object.set(...) like commands.
Definition EnumUtils.h:107