EnumFlags.h
1 /*
2 
3 MIT License
4 
5 Copyright (c) 2017 FMI Open Development / Markus Peura, first.last@fmi.fi
6 
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13 
14 The above copyright notice and this permission notice shall be included in all
15 copies or substantial portions of the Software.
16 
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 SOFTWARE.
24 
25 */
26 /*
27 Part of Rack development has been done in the BALTRAD projects part-financed
28 by the European Union (European Regional Development Fund and European
29 Neighbourhood 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 
55 namespace drain {
56 
57 
58 
60 
65 template <class E, class OWNER=E>
66 struct EnumDict {
67 
68  //typedef FlagResolver::dict_t dict_t;
70 
71  /*
72  static
73  const E defaultValue;
74  */
75 
76  static
77  const dict_t dict;
78 
79  static
80  const dict_t & getDict(){
81  return dict;
82  }
83 
84 
85 
87  static inline
88  const std::string & str(const E & value){
89  return dict.getKey(value);
90  }
91 
92 
94 
98  //template <typename E2>
99  static
100  bool setValue(const std::string & key, E & value){ // NOTE: could be more general, without explicit template
101  if (drain::EnumDict<E>::dict.hasKey(key)){
102  value = drain::EnumDict<E>::dict.getValue(key);
103  return true; // assigned
104  }
105  else {
106  return false; // not found
107  }
108  }
109 
110 
112 
117  static inline
118  E getValue(const E & value, bool lenient=true){
119  return value;
120  }
121 
123 
127  static inline
128  E getValue(const std::string &key, bool lenient=true){
129  return dict.getValue(key, lenient);
130  }
131 
133 
137  static inline
138  E getValue(const char *key, bool lenient=true){
139  return dict.getValue(key, lenient);
140  }
141 
142 };
143 
145 // template <class E>
146 // const E EnumDict<E>::defaultValue = 0;
147 
148 
149 /*
150 #undef DRAIN_ENUM_NAMESPACE
151 #define DRAIN_ENUM_ENTRY_PRE(key) {#key, DRAIN_ENUM_NAMESPACE::key}
152 */
153 #define DRAIN_ENUM_ENTRY(nspace, key) {#key, nspace::key}
154 
155 #define DRAIN_ENUM_OSTREAM(enumtype) inline std::ostream & operator<<(std::ostream &ostr, const enumtype & e){return ostr << drain::EnumDict<enumtype>::dict.getKey(e);}
156 
157 /* Perhaps useful!
158 template <class E>
159 class EnumKey {
160 
161 public:
162 
163  inline
164  EnumKey(const E & value) : key(EnumDict<E>::dict.getKey(value)){
165  };
166 
167  inline
168  operator const std::string & () const {
169  return key;
170  }
171 
172 protected:
173 
174  const std::string & key;
175 };
176 */
177 
178 
180 
189 template <class F> // F =SingleFlagger<E>
190 class EnumFlagger : public F {
191 
192 public:
193 
194  typedef F fbase_t;
195  typedef typename F::value_t value_t;
196  typedef typename F::storage_t storage_t;
197  typedef typename F::dict_t dict_t;
198  typedef FlagResolver::ivalue_t ivalue_t;
199 
201  inline
202  EnumFlagger(): fbase_t(EnumDict<value_t>::dict){
203  }
204 
206  inline
207  EnumFlagger(const storage_t & v): fbase_t(EnumDict<value_t>::dict) {
208  this->value = v;
209  }
210 
211  /*
212  inline
213  EnumFlagger(value_t v): fbase_t(v) {
214  }
215  */
216 
218  template <typename ... T>
219  inline
220  EnumFlagger(const T & ... arg): fbase_t(EnumDict<value_t>::dict, arg...) { // designed for MultiFlagger
221  }
222 
223  /* // keep
224  virtual void reset() override {
225  this->value = EnumDict<value_t>::defaultValue; // ALERT! enums need neutral value.
226  };
227  */
228 
230 
241 
245  static
246  ivalue_t getValueNEW(const std::string & key){
247  return (ivalue_t)EnumDict<value_t>::dict.getValue(key);
248  };
249 
250  // Raise?
251  /*
252  static inline
253  std::string getKeysNEW2(const storage_t & value, char separator = ','){
254  // currentStr = FlagResolver::getKeys(dict, this->value, this->separator);
255  return FlagResolver::getKeys(EnumDict<value_t>::dict, value, separator);
256  }
257  */
258 
270  inline
272  this->set(flagger.value);
273  return *this;
274  }
275 
276  template <class T>
277  inline
278  EnumFlagger<F> & operator=(const T & v){
279  this->set(v);
280  return *this;
281  }
282 
283 };
284 
285 
286 } // drain::
287 
288 
289 #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:190
EnumFlagger(const storage_t &v)
Constructor with initial value.
Definition: EnumFlags.h:207
EnumFlagger< F > & operator=(const EnumFlagger< F > &flagger)
Definition: EnumFlags.h:271
EnumFlagger()
Default constructor.
Definition: EnumFlags.h:202
static ivalue_t getValueNEW(const std::string &key)
Returns the static dictionary created for this value_t .
Definition: EnumFlags.h:246
Definition: DataSelector.cpp:1277
Wrapper for unique (static) dictionary of enum values.
Definition: EnumFlags.h:66
static E getValue(const char *key, bool lenient=true)
Convenience for object.set(...) like commands.
Definition: EnumFlags.h:138
static E getValue(const E &value, bool lenient=true)
Convenience for object.set(...) like commands.
Definition: EnumFlags.h:118
static bool setValue(const std::string &key, E &value)
Convenience function for leniently setting string values to separate enum lists.
Definition: EnumFlags.h:100
static const std::string & str(const E &value)
Convenience.
Definition: EnumFlags.h:88
static E getValue(const std::string &key, bool lenient=true)
Convenience for object.set(...) like commands.
Definition: EnumFlags.h:128