FlagsOld.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_FLAGS_OLD_H_
33 #define DRAIN_FLAGS_OLD_H_
34 
35 #include "Flags.h"
36 
37 namespace drain {
38 
39 
40 
41 
42 
43 
45 class Flagger :public FlagResolver { // for typedefs
46 
47 public:
48 
49  ivalue_t ownValue;
50  ivalue_t & value;
51 
52  const dict_t & dictionary; // todo open (keep own secret, later)
53 
54  /*
55  virtual inline
56  const dict_t & getDict() const {
57  return dictionary;
58  }
59  */
60 
61  char separator; // needed?
62 
63  inline
64  Flagger(const dict_t & dict, char separator = 0) :
65  ownValue(0),
66  value(ownValue),
67  dictionary(dict),
68  separator(separator?separator:dict.separator){
69  };
70 
71  inline
72  Flagger(ivalue_t & value, const dict_t & dict, char separator = 0) :
73  ownValue(0),
74  value(value),
75  dictionary(dict),
76  separator(separator?separator:dict.separator){
77  };
78 
79  inline
80  Flagger(const Flagger & flags) : //FlagResolver(flags.dictionary, flags.separator), value(flags.value){
81  ownValue(flags.value), // OK init
82  value(flags.value), // <-- FIX: this will cause error (non-const referencing const). Should be
83  dictionary(flags.dictionary),
84  separator(flags.separator){
85 
86  drain::Logger mout(__FILE__, __FUNCTION__);
87  mout.error("Flagger inits value &= flags.value", flags);
88  };
89 
90 
91  inline virtual
92  ~Flagger(){};
93 
94 
96  // Variadic-argument member set function.
97  // https://en.cppreference.com/w/cpp/language/parameter_pack
98  // TODO: should explicit zero reset the value?
99  template <typename T, typename ... TT>
100  inline
101  Flagger & set(const T & arg, const TT &... args) {
102  deepSet(arg);
103  set(args...);
104  return *this;
105  }
106 
107 
108 protected:
109 
110  inline
111  Flagger & set(){
112  return *this;
113  }
114 
115  // TODO: should explicit zero reset the value?
116  inline
117  Flagger & deepSet(ivalue_t x){
118  this->value = (this->value | x);
119  return *this;
120  }
121 
122 
124 
127  inline
128  Flagger & deepSet(const key_t & key){
129  deepSet(getValue(key, separator));
130  return *this;
131  };
132 
133 
134 
135 public:
136 
137 
139  inline
140  Flagger & unset(ivalue_t x){
141  value = (value & ~x);
142  return *this;
143  };
144 
146  inline
147  Flagger & unset(const key_t & key){
148  //unset(dictionary.getValue(key, separator));
149  unset(getValue(key, separator));
150  return *this;
151  };
152 
153 
155  inline
157  value = 0;
158  return *this;
159  }
160 
162  inline
163  bool isSet(ivalue_t x) const {
164  return (value & x) != 0;
165  };
166 
167  inline
168  bool isSet(const key_t & key) const {
169  return isSet(dictionary.getValue(key));
170  };
171 
172 
173  // For some reason this cannot be templated (see below)
174  inline
175  Flagger & operator=(const Flagger &x){
176  assign(x);
177  return *this;
178  }
179 
181 
184  template <class T>
185  inline
186  Flagger & operator=(const T &x){
187  assign(x);
188  return *this;
189  }
190 
192 
195  inline
196  void assign(ivalue_t x){
197  value = x;
198  }
199 
201 
204  inline
205  void assign(const Flagger & flags){
206  value = flags.value;
207  }
208 
210 
213  void assign(const std::string & params);
214 
215 
216  inline
217  operator ivalue_t() const {
218  return value;
219  }
220 
221  inline
222  operator bool() const {
223  return (value > 0);
224  }
225 
226  inline
227  operator std::string() const {
228  std::stringstream sstr;
229  keysToStream(sstr);
230  //toStream(sstr);
231  return sstr.str();
232  }
233 
234 
235 
237 
241  inline
242  ivalue_t getValue(const std::string & key, char separator=0) const {
243  // important: if sep undefined, use Flags.sep.
244  // Or/consider Flag dict sep => '|'
245  return FlagResolver::getValue(dictionary, key, separator ? separator : this->separator);
246  }
247 
249  inline
250  std::string getKeys(ivalue_t value, char separator=0) const {
251  return FlagResolver::getKeys(dictionary, value, separator ? separator : this->separator);
252  }
253 
255  inline
256  std::string getKeys(char separator=0) const {
257  return FlagResolver::getKeys(dictionary, value, separator ? separator : this->separator);
258  }
259 
260  // TODO consider rename
261  const dict_t::keylist_t & keys() const;
262 
263 protected:
264 
265  mutable dict_t::keylist_t keyList;
266 
267  //mutable dict_t::valuelist_t valueList;
268 
269 public:
270 
271 
273 
278  inline
279  std::ostream & keysToStream(std::ostream & ostr=std::cout, char separator=0) const {
280  return FlagResolver::keysToStream(dictionary, value, ostr, separator ? separator : this->separator);
281  }
282 
283  //std::ostream & toStream(std::ostream & ostr=std::cout, char separator=',') const; // PathMatcher '|' !
284  //std::ostream & toStream(std::ostream & ostr=std::cout, char separator=0) const; // PathMatcher '|' !
285 
286 
287  template <class T>
288  void exportStatus(std::map<std::string, T> & statusMap) const {
289  for (dict_t::const_iterator it = dictionary.begin(); it != dictionary.end(); ++it){
290  if ((it->second > 0) && ((it->second & value) == it->second)){ // fully covered in value
291  statusMap[it->first] = 1;
292  }
293  else {
294  statusMap[it->first] = 0;
295  }
296  }
297  }
298 
299 
300  static
301  const drain::SprinterLayout flagDictLayout;
302 
303 
304 };
305 
306 
307 inline
308 std::ostream & operator<<(std::ostream & ostr, const drain::Flagger & flags) {
309  return flags.keysToStream(ostr);
310 }
311 
312 
314 class Flags : public Flagger {
315 
316 protected:
317 
318  dict_t ownDictionary;
319 
320 public:
321 
322  virtual inline
323  const dict_t & getDict() const{
324  return ownDictionary;
325  }
326 
327 
328  virtual inline
329  dict_t & getDict(){
330  return ownDictionary;
331  }
332 
333 
334  Flags(char separator = ',') : Flagger(ownDictionary, separator){
335  }
336 
337  //Flags(dict_t & d, char separator = ',') : Flagger(ownValue, d, separator){ }
338 
339  Flags(const Flags & flags) : Flagger(ownValue = flags.ownValue, ownDictionary, flags.separator){
340  // Copy dict?
341  }
342 
343  /*
344  * std::initializer_list<entry_t> d
345  */
347  Flags(std::initializer_list<dict_t::entry_t> d, char separator = ',') : Flagger(ownValue, ownDictionary, separator) { //, ownDictionary(d){
348  for (const dict_t::entry_t & entry: d){
349  addEntry(entry.first, entry.second);
350  }
351  }
352 
353 
354 
356  template <class T>
357  inline
358  Flags & operator=(const T &x){
359  assign(x);
360  return *this;
361  }
362 
364 
367  inline
368  ivalue_t addEntry(const dict_t::key_t & key, ivalue_t i=0){
369  return Flagger::addEntry(ownDictionary, key, i);
370  }
371 
372  /*
373  inline
374  ivalue_t add(const dict_t::entry_t & entry){
375  return Flagger::add(ownDictionary, entry.first, entry.second);
376  }
377  */
378 
379 protected:
380 
381  /*
382  bool usesOwnDict() const { //??
383  return (&dictionary == &dictionary);
384  }
385  */
386 };
387 
388 
389 
390 
391 
392 
393 } // drain::
394 
395 
396 #endif
const V & getValue(const K &key) const
Given a key, return the first value associated with it.
Definition: Dictionary.h:149
Referencing a dictionary of binary values: {"A",1: "B":2, "C": 4, "D": 8, ...} resolves two-way mappi...
Definition: Flags.h:56
static std::ostream & keysToStream(const drain::Dictionary< key_t, T > &dict, ivalue_t value, std::ostream &ostr, char separator=',')
Write keys in a stream, in numeric order.
Definition: Flags.h:274
static ivalue_t getValue(const drain::Dictionary< key_t, T > &dict, const std::string &key, char separator=',')
Computes bitwise OR function on the numeric or alphabetic value(s) presented by a string.
Definition: Flags.h:198
static ivalue_t addEntry(drain::Dictionary< key_t, T > &dict, const key_t &key, ivalue_t i=0)
Add a new entry in the dictionary.
static std::string getKeys(const drain::Dictionary< key_t, T > &dict, ivalue_t, char separator=',')
Given an integer, retrieves dictionary keys corresponding to each index of set bits.
Definition: Flags.h:262
A bit vector with external Dictionary mapping from strings to bits and vice versa.
Definition: FlagsOld.h:45
std::ostream & keysToStream(std::ostream &ostr=std::cout, char separator=0) const
Display current value as key values of which the value is composed with OR function.
Definition: FlagsOld.h:279
std::string getKeys(ivalue_t value, char separator=0) const
Given an integer, retrieves dictionary keys corresponding to each index of set bits.
Definition: FlagsOld.h:250
Flagger & unset(const key_t &key)
Unset desired flags. Does not set any flag.
Definition: FlagsOld.h:147
Flagger & set(const T &arg, const TT &... args)
Set desired flags. Does not reset any flag (zero value has no effect).
Definition: FlagsOld.h:101
bool isSet(ivalue_t x) const
Checks if a given bit, or any of given bits, is set.
Definition: FlagsOld.h:163
void assign(ivalue_t x)
Sets value, ie. set or unsets all the flags.
Definition: FlagsOld.h:196
Flagger & unset(ivalue_t x)
Unset desired flags. Does not set any flag.
Definition: FlagsOld.h:140
Flagger & deepSet(const key_t &key)
Set desired flags. Does not reset any flag.
Definition: FlagsOld.h:128
Flagger & operator=(const T &x)
Sets value, ie. set or unsets all the flags.
Definition: FlagsOld.h:186
void assign(const Flagger &flags)
Copies flags as an integer value. Same dictionary not checked.
Definition: FlagsOld.h:205
std::string getKeys(char separator=0) const
Given an integer, retrieves dictionary keys corresponding to each index of set bits.
Definition: FlagsOld.h:256
ivalue_t getValue(const std::string &key, char separator=0) const
Computes bitwise OR function on the numeric or alphabetic value(s) presented by a string.
Definition: FlagsOld.h:242
Flagger & reset()
Reset all the flags.
Definition: FlagsOld.h:156
Self-contained Flagger – with an own dictionary and value.
Definition: FlagsOld.h:314
Flags(std::initializer_list< dict_t::entry_t > d, char separator=',')
Initialize with {{"first", 123}, {"second", 456}}.
Definition: FlagsOld.h:347
ivalue_t addEntry(const dict_t::key_t &key, ivalue_t i=0)
Add a new entry in the dictionary.
Definition: FlagsOld.h:368
Flags & operator=(const T &x)
Sets value, ie. set or unsets all the flags.
Definition: FlagsOld.h:358
LogSourc e is the means for a function or any program segment to "connect" to a Log.
Definition: Log.h:308
Logger & error(const TT &... args)
Echoes.
Definition: Log.h:412
Definition: DataSelector.cpp:1277
Definition: Sprinter.h:137