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
141// template <class E>
142// const E EnumDict<E>::defaultValue = 0;
143
144// Useful macros
145#define DRAIN_ENUM_ENTRY(nspace, key) {#key, nspace::key}
146
147#define DRAIN_ENUM_OSTREAM(enumtype) inline std::ostream & operator<<(std::ostream &ostr, const enumtype & e){return ostr << drain::EnumDict<enumtype>::dict.getKey(e);}
148
149/* Perhaps useful!
150template <class E>
151class EnumKey {
152
153public:
154
155 inline
156 EnumKey(const E & value) : key(EnumDict<E>::dict.getKey(value)){
157 };
158
159 inline
160 operator const std::string & () const {
161 return key;
162 }
163
164protected:
165
166 const std::string & key;
167};
168*/
169
170
172
181template <class F> // F =SingleFlagger<E>
182class EnumFlagger : public F {
183
184public:
185
186 typedef F fbase_t;
187 typedef typename F::value_t value_t;
188 typedef typename F::storage_t storage_t;
189 typedef typename F::dict_t dict_t;
190 typedef FlagResolver::ivalue_t ivalue_t;
191
193 inline
194 EnumFlagger(): fbase_t(EnumDict<value_t>::dict){
195 }
196
198 inline
199 EnumFlagger(const storage_t & v): fbase_t(EnumDict<value_t>::dict) {
200 this->value = v;
201 }
202
203 /*
204 inline
205 EnumFlagger(value_t v): fbase_t(v) {
206 }
207 */
208
210 template <typename ... T>
211 inline
212 EnumFlagger(const T & ... arg): fbase_t(EnumDict<value_t>::dict, arg...) { // designed for MultiFlagger
213 }
214
215 /* // keep
216 virtual void reset() override {
217 this->value = EnumDict<value_t>::defaultValue; // ALERT! enums need neutral value.
218 };
219 */
220
222
233
237 static
238 ivalue_t getValueNEW(const std::string & key){
239 return (ivalue_t)EnumDict<value_t>::dict.getValue(key);
240 };
241
242 // Raise?
243 /*
244 static inline
245 std::string getKeysNEW2(const storage_t & value, char separator = ','){
246 // currentStr = FlagResolver::getKeys(dict, this->value, this->separator);
247 return FlagResolver::getKeys(EnumDict<value_t>::dict, value, separator);
248 }
249 */
250
262 inline
264 this->set(flagger.value);
265 return *this;
266 }
267
268 template <class T>
269 inline
270 EnumFlagger<F> & operator=(const T & v){
271 this->set(v);
272 return *this;
273 }
274
275};
276
277
278} // drain::
279
280
281#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 EnumFlags.h:182
EnumFlagger(const storage_t &v)
Constructor with initial value.
Definition EnumFlags.h:199
EnumFlagger()
Default constructor.
Definition EnumFlags.h:194
EnumFlagger< F > & operator=(const EnumFlagger< F > &flagger)
Definition EnumFlags.h:263
static ivalue_t getValueNEW(const std::string &key)
Returns the static dictionary created for this value_t .
Definition EnumFlags.h:238
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 E getValue(const std::string &key, bool lenient=true)
Convenience for object.set(...) like commands.
Definition EnumFlags.h:124