Loading...
Searching...
No Matches
EnumFlags.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_FLAGS
33#define DRAIN_ENUM_FLAGS
34
35/*
36#include <iostream>
37#include <list>
38#include <iterator>
39#include <iostream>
40#include <list>
41#include <sstream>
42#include <stdexcept>
43//#include <typeinfo>
44#include <stdlib.h>
45
46#include <drain/Log.h>
47#include <drain/String.h>
48#include <drain/Type.h>
49
50*/
51
52// #include "Dictionary.h"
53#include "Flags.h"
54
55namespace drain {
56
57
58
60
68template <class E, class OWNER=E>
69struct EnumDict {
70
71 //typedef FlagResolver::dict_t dict_t;
73
74 static
75 const dict_t dict;
76
77 static
78 const dict_t & getDict(){
79 return dict;
80 }
81
82
83
85 static inline
86 const std::string & str(const E & value){
87 return dict.getKey(value);
88 }
89
90
92
95 static
96 bool setValue(const std::string & key, E & value){ // NOTE: could be more general, without explicit template
97 if (drain::EnumDict<E>::dict.hasKey(key)){
99 return true; // assigned
100 }
101 else {
102 return false; // not found
103 }
104 }
105
106
108
113 static inline
114 E getValue(const E & value, bool lenient=true){
115 return value;
116 }
117
119
123 static inline
124 E getValue(const std::string &key, bool lenient=true){
125 return dict.getValue(key, lenient);
126 }
127
129
133 static inline
134 E getValue(const char *key, bool lenient=true){
135 return dict.getValue(key, lenient);
136 }
137
138 /*
139 static inline
140 E getValue(char * const key, bool lenient=true){
141 return dict.getValue(key, lenient);
142 }
143 */
144
146
157
161 static inline
162 const std::string & getKey(const std::string & s, bool lenient=true){
163 return s;
164 }
165
166 static inline
167 const char * getKey(const char * s, bool lenient=true){
168 return s;
169 }
170
171 static inline
172 const std::string & getKey(const E & value, bool lenient=true){
173 return dict.getKey(value, lenient);
174 }
175
176 /*
177 static inline
178 const std::string & getKey(const char *value, bool lenient=true){
179 //return dict.getKey(std::string(value), lenient);
180 return dict.getKey(std::string(value), lenient);
181 }
182 */
183
184
185
187
197};
198
200// template <class E>
201// const E EnumDict<E>::defaultValue = 0;
202
203// Useful macros
204
205// For declaration and definition.
206/*
207#define DRAIN_ENUM_DICT(enumtype) template <> const EnumDict<enumtype>::dict_t EnumDict<enumtype>::dict
208
209#define DRAIN_ENUM_ENTRY(nspace, key) {#key, nspace::key}
210
211#define DRAIN_ENUM_OSTREAM(enumtype) inline std::ostream & operator<<(std::ostream &ostr, const enumtype & e){return ostr << drain::EnumDict<enumtype>::dict.getKey(e);}
212*/
213
214/* Perhaps useful!
215template <class E>
216class EnumKey {
217
218public:
219
220 inline
221 EnumKey(const E & value) : key(EnumDict<E>::dict.getKey(value)){
222 };
223
224 inline
225 operator const std::string & () const {
226 return key;
227 }
228
229protected:
230
231 const std::string & key;
232};
233*/
234
235
237
246template <class F> // F =SingleFlagger<E>
247class EnumFlagger : public F {
248
249public:
250
251 typedef F fbase_t;
252 typedef typename F::value_t value_t;
253 typedef typename F::storage_t storage_t;
254 typedef typename F::dict_t dict_t;
255 typedef FlagResolver::ivalue_t ivalue_t;
256
258 inline
259 EnumFlagger(): fbase_t(EnumDict<value_t>::dict){
260 }
261
263 inline
264 EnumFlagger(const storage_t & v): fbase_t(EnumDict<value_t>::dict) {
265 this->value = v;
266 }
267
268 /*
269 inline
270 EnumFlagger(value_t v): fbase_t(v) {
271 }
272 */
273
275 template <typename ... T>
276 inline
277 EnumFlagger(const T & ... arg): fbase_t(EnumDict<value_t>::dict, arg...) { // designed for MultiFlagger
278 }
279
280 /* // keep
281 virtual void reset() override {
282 this->value = EnumDict<value_t>::defaultValue; // ALERT! enums need neutral value.
283 };
284 */
285
287
298
302 static
303 ivalue_t getValueNEW(const std::string & key){
304 return (ivalue_t)EnumDict<value_t>::dict.getValue(key);
305 };
306
307 // Raise?
308 /*
309 static inline
310 std::string getKeysNEW2(const storage_t & value, char separator = ','){
311 // currentStr = FlagResolver::getKeys(dict, this->value, this->separator);
312 return FlagResolver::getKeys(EnumDict<value_t>::dict, value, separator);
313 }
314 */
315
327 inline
329 this->set(flagger.value);
330 return *this;
331 }
332
333 template <class T>
334 inline
335 EnumFlagger<F> & operator=(const T & v){
336 this->set(v);
337 return *this;
338 }
339
340};
341
342
343} // drain::
344
345#define DRAIN_ENUM_DICT(enumtype) template <> const drain::EnumDict<enumtype>::dict_t drain::EnumDict<enumtype>::dict
346
347#define DRAIN_ENUM_ENTRY(nspace, key) {#key, nspace::key}
348
349#define DRAIN_ENUM_OSTREAM(enumtype) inline std::ostream & operator<<(std::ostream &ostr, const enumtype & e){return ostr << drain::EnumDict<enumtype>::dict.getKey(e);}
350
351
352#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
Default default value...
Definition EnumFlags.h:247
EnumFlagger(const storage_t &v)
Constructor with initial value.
Definition EnumFlags.h:264
EnumFlagger()
Default constructor.
Definition EnumFlags.h:259
EnumFlagger< F > & operator=(const EnumFlagger< F > &flagger)
Definition EnumFlags.h:328
static ivalue_t getValueNEW(const std::string &key)
Returns the static dictionary created for this value_t .
Definition EnumFlags.h:303
Definition DataSelector.cpp:1277
A container for a static dictionary of enumeration values.
Definition EnumFlags.h:69
static E getValue(const char *key, bool lenient=true)
Convenience for object.set(...) like commands.
Definition EnumFlags.h:134
static const std::string & str(const E &value)
Convenience.
Definition EnumFlags.h:86
static E getValue(const E &value, bool lenient=true)
Convenience for object.set(...) like commands.
Definition EnumFlags.h:114
static bool setValue(const std::string &key, E &value)
Assign string values to an enumeration type.
Definition EnumFlags.h:96
static const std::string & getKey(const std::string &s, bool lenient=true)
Convenience for object.set(...) like commands.
Definition EnumFlags.h:162
static E getValue(const std::string &key, bool lenient=true)
Convenience for object.set(...) like commands.
Definition EnumFlags.h:124