Loading...
Searching...
No Matches
Enum.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
33#define DRAIN_ENUM
34
35#include "Dictionary.h"
36
37namespace drain {
38
39
40
42
50template <class E, class OWNER=E>
51struct Enum {
52
53 //typedef FlagResolver::dict_t dict_t;
55
56 static
57 const dict_t dict;
58
59 static
60 const dict_t & getDict(){
61 return dict;
62 }
63
64
65
67 static inline
68 const std::string & str(const E & value){
69 return dict.getKey(value);
70 }
71
72
74
77 static
78 bool setValue(const std::string & key, E & value){ // NOTE: could be more general, without explicit template
79 if (drain::Enum<E>::dict.hasKey(key)){
80 value = drain::Enum<E>::dict.getValue(key);
81 return true; // assigned
82 }
83 else {
84 return false; // not found
85 }
86 }
87
88
90
95 static inline
96 E getValue(const E & value, bool lenient=true){
97 return value;
98 }
99
101
105 static inline
106 E getValue(const std::string &key, bool lenient=true){
107 return dict.getValue(key, lenient);
108 }
109
111
115 static inline
116 E getValue(const char *key, bool lenient=true){
117 return dict.getValue(key, lenient);
118 }
119
120 /*
121 static inline
122 E getValue(char * const key, bool lenient=true){
123 return dict.getValue(key, lenient);
124 }
125 */
126
128
139
143 static inline // TODO: remove/handle misleading lenient here
144 const std::string & getKey(const std::string & s, bool lenient=true){
145 return s;
146 }
147
148 static inline // TODO: remove/handle misleading lenient here
149 const char * getKey(const char * s, bool lenient=true){
150 return s;
151 }
152
153 static inline // TODO: remove/handle misleading lenient here
154 const std::string & getKey(const E & value, bool lenient=true){
155 return dict.getKey(value, lenient);
156 }
157
158 /*
159 static inline
160 const std::string & getKey(const char *value, bool lenient=true){
161 //return dict.getKey(std::string(value), lenient);
162 return dict.getKey(std::string(value), lenient);
163 }
164 */
165
167
177 // Was: separate EnumConverter
178
179 static
180 void convert(const E & value, std::string &s){
181 // std::cerr << "Using fwd EnumConverter" << std::endl;
182 s.assign(dict.getKey(value));
183 };
184
185 static
186 void convert(const std::string &s, E & value){
187 // std::cerr << "Using inv EnumConverter" << std::endl;
188 value = dict.getValue(s);
189 };
190
191};
192
193
194/*
195template <class EnumType>
196class EnumConverter {
197public:
198
199 static
200 void convert(const EnumType & value, std::string &s){
201 // std::cerr << "Using fwd EnumConverter" << std::endl;
202 s.assign(drain::Enum<EnumType>::dict.getKey(value));
203 };
204
205 static
206 void convert(const std::string &s, EnumType & value){
207 // std::cerr << "Using inv EnumConverter" << std::endl;
208 value = drain::Enum<EnumType>::dict.getValue(s);
209 };
210
211};
212*/
213
214
222template <typename E, bool STRICT=true>
223class EnumWrapper : public std::string { // StringWrapper<E> {
224
225public:
226
227 inline
228 EnumWrapper(const std::string & s=""){ // : std::string(s){
229 set(s);
230 };
231
232 // what about: const char *
233
235 inline
236 EnumWrapper(const E & x){
237 set(x);
238 };
239
240 template <typename T>
241 void set(const T & value){
242 // handle empty with default enum value?
243 assign(drain::Enum<E>::dict.getKey(value, !STRICT));
244 }
245
246 /*
247 virtual inline
248 void setSpecial(const E & x) override {
249 assign(drain::Enum<E>::dict.getKey(x, !STRICT));
250 };
251 */
252
253};
254
260class MultiEnumWrapper : public std::string { // StringWrapper<E> {
261
262public:
263
264 inline
265 MultiEnumWrapper(const std::string & s="") : std::string(s){
266 };
267
268 inline
269 MultiEnumWrapper(const char *s) : std::string(s){
270 };
271
273 // inline
274 // MultiEnumWrapper(const MultiEnumWrapper & wrapper) : std::string(wrapper){
275 // };
276
278 template <typename T>
279 inline
280 MultiEnumWrapper(const T & arg){
281 set(arg);
282 };
283
284
285 void set(const std::string &arg){
286 assign(arg);
287 }
288
289 void set(const char * arg){
290 assign(arg);
291 }
292
293 void set(const MultiEnumWrapper &arg){
294 assign(arg);
295 }
296
297 template <typename T>
298 void set(const T & value){
299 assign(drain::Enum<T>::dict.getKey(value, false));
300 }
301
302};
303
304
305
306/* Perhaps useful!
307template <class E>
308class EnumKey {
309
310public:
311
312 inline
313 EnumKey(const E & value) : key(Enum<E>::dict.getKey(value)){
314 };
315
316 inline
317 operator const std::string & () const {
318 return key;
319 }
320
321protected:
322
323 const std::string & key;
324};
325*/
326
327
333#define DRAIN_ENUM_CONV(enumtype) template <> class Converter<enumtype> : public Enum<enumtype> {}
334
335
336} // drain::
337
338#define DRAIN_ENUM_DICT(enumtype) template <> const drain::Enum<enumtype>::dict_t drain::Enum<enumtype>::dict
339#define DRAIN_ENUM_DICT2(enumtype,owner) template <> const drain::Enum<enumtype,owner>::dict_t drain::Enum<enumtype,owner>::dict
340
341#define DRAIN_ENUM_ENTRY(nspace, key) {#key, nspace::key}
342
343#define DRAIN_ENUM_OSTREAM(enumtype) inline std::ostream & operator<<(std::ostream &ostr, const enumtype & e){return ostr << drain::Enum<enumtype>::dict.getKey(e);}
344
345
346
347#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
Definition Enum.h:223
EnumWrapper(const E &x)
All the other constructors, including default constructor.
Definition Enum.h:236
Definition Enum.h:260
MultiEnumWrapper(const T &arg)
Default constructor.
Definition Enum.h:280
Definition DataSelector.cpp:1277
A container for a static dictionary of enumeration values.
Definition Enum.h:51
static E getValue(const char *key, bool lenient=true)
Convenience for object.set(...) like commands.
Definition Enum.h:116
static const std::string & str(const E &value)
Convenience.
Definition Enum.h:68
static E getValue(const E &value, bool lenient=true)
Convenience for object.set(...) like commands.
Definition Enum.h:96
static bool setValue(const std::string &key, E &value)
Assign string values to an enumeration type.
Definition Enum.h:78
static void convert(const E &value, std::string &s)
Experimental. Convenience for object.set(...) like commands.
Definition Enum.h:180
static const std::string & getKey(const std::string &s, bool lenient=true)
Convenience for object.set(...) like commands.
Definition Enum.h:144
static E getValue(const std::string &key, bool lenient=true)
Convenience for object.set(...) like commands.
Definition Enum.h:106