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 // tmp hack for DRAIN_ENUM
186 static inline
187 void to(const E & value, std::string &s){
188 convert(value, s);
189 }
190
191
192 static
193 void convert(const std::string &s, E & value){
194 // std::cerr << "Using inv EnumConverter" << std::endl;
195 value = dict.getValue(s);
196 };
197
198 // tmp hack for DRAIN_ENUM
199 static inline
200 void to(const std::string & s, E & value){
201 convert(s, value);
202 }
203
204};
205
206
207/*
208template <class EnumType>
209class EnumConverter {
210public:
211
212 static
213 void convert(const EnumType & value, std::string &s){
214 // std::cerr << "Using fwd EnumConverter" << std::endl;
215 s.assign(drain::Enum<EnumType>::dict.getKey(value));
216 };
217
218 static
219 void convert(const std::string &s, EnumType & value){
220 // std::cerr << "Using inv EnumConverter" << std::endl;
221 value = drain::Enum<EnumType>::dict.getValue(s);
222 };
223
224};
225*/
226
227
235template <typename E, bool STRICT=true>
236class EnumWrapper : public std::string { // StringWrapper<E> {
237
238public:
239
240 inline
241 EnumWrapper(const std::string & s=""){ // : std::string(s){
242 set(s);
243 };
244
245 // what about: const char *
246
248 inline
249 EnumWrapper(const E & x){
250 set(x);
251 };
252
253 template <typename T>
254 void set(const T & value){
255 // handle empty with default enum value?
256 assign(drain::Enum<E>::dict.getKey(value, !STRICT));
257 }
258
259 /*
260 virtual inline
261 void setSpecial(const E & x) override {
262 assign(drain::Enum<E>::dict.getKey(x, !STRICT));
263 };
264 */
265
266};
267
273class MultiEnumWrapper : public std::string { // StringWrapper<E> {
274
275public:
276
277 inline
278 MultiEnumWrapper(const std::string & s="") : std::string(s){
279 };
280
281 inline
282 MultiEnumWrapper(const char *s) : std::string(s){
283 };
284
286 // inline
287 // MultiEnumWrapper(const MultiEnumWrapper & wrapper) : std::string(wrapper){
288 // };
289
291 template <typename T>
292 inline
293 MultiEnumWrapper(const T & arg){
294 set(arg);
295 };
296
297
298 void set(const std::string &arg){
299 assign(arg);
300 }
301
302 void set(const char * arg){
303 assign(arg);
304 }
305
306 void set(const MultiEnumWrapper &arg){
307 assign(arg);
308 }
309
310 template <typename T>
311 void set(const T & value){
312 assign(drain::Enum<T>::dict.getKey(value, false));
313 }
314
315};
316
317
318
319/* Perhaps useful!
320template <class E>
321class EnumKey {
322
323public:
324
325 inline
326 EnumKey(const E & value) : key(Enum<E>::dict.getKey(value)){
327 };
328
329 inline
330 operator const std::string & () const {
331 return key;
332 }
333
334protected:
335
336 const std::string & key;
337};
338*/
339
340
346#define DRAIN_ENUM_CONV(enumtype) template <> class Converter<enumtype> : public Enum<enumtype> {}
347
348
349} // drain::
350
351#define DRAIN_ENUM_DICT(enumtype) template <> const drain::Enum<enumtype>::dict_t drain::Enum<enumtype>::dict
352#define DRAIN_ENUM_DICT2(enumtype,owner) template <> const drain::Enum<enumtype,owner>::dict_t drain::Enum<enumtype,owner>::dict
353
354#define DRAIN_ENUM_ENTRY(nspace, key) {#key, nspace::key}
355
356#define DRAIN_ENUM_OSTREAM(enumtype) inline std::ostream & operator<<(std::ostream &ostr, const enumtype & e){return ostr << drain::Enum<enumtype>::dict.getKey(e);}
357
358
359
360#endif
Two-way mapping between strings and objects of template class T.
Definition Dictionary.h:61
const K & getKey(const V &value, bool lenient=true) const
Identity mapping useful for type deduction of template arguments in functions.
Definition Dictionary.h:170
const V & getValue(const K &key, bool lenient=true) const
Given a key, return the first value associated with it.
Definition Dictionary.h:147
Definition Enum.h:236
EnumWrapper(const E &x)
All the other constructors, including default constructor.
Definition Enum.h:249
Definition Enum.h:273
MultiEnumWrapper(const T &arg)
Default constructor.
Definition Enum.h:293
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