32#ifndef DRAIN_FLAG_BASE
33#define DRAIN_FLAG_BASE
45#include <drain/StringTools.h>
46#include <drain/Dictionary.h>
47#include <drain/TypeName.h>
61 typedef std::string key_t;
64 typedef unsigned long int ivalue_t;
101 template <
typename T,
typename V>
109 template <
typename T>
118 template <
typename T>
132 ivalue_t currentMask = 0;
133 for (
const auto & entry: dict){
134 currentMask = currentMask | entry.second;
137 mout.
debug2(
"sum:" , currentMask );
140 while ((i¤tMask) > 0){
141 mout.debug3(
"checking bit:" , i ,
" vs.\t" , currentMask );
153 mout.
debug(
"released: " , i );
178 if (dict.hasKey(key)){
179 mout.info(key ,
" already in dict: " , dict );
190 mout.warn(key ,
" could not get a valid (non-zero) bit flag for dict: " , dict );
202 FlagResolver::ivalue_t v = 0;
204 std::list<key_t> keys;
207 separator = dict.separator;
217 for (
const key_t & key: keys){
222 else if (key ==
"0"){
231 if (dit != dict.end()){
233 v = (v |
static_cast<FlagResolver::ivalue_t
>(dit->second) );
237 FlagResolver::ivalue_t x = 0;
238 std::stringstream sstr(key);
247 mout.
error(
"key '", key,
"' not found in: ", dict);
268 std::stringstream sstr;
287 separator = dict.separator;
291 for (
const auto & entry: dict){
292 const ivalue_t v = (entry.second);
295 if ((v > 0) && ((v & value) == v)){
313template <
typename T,
typename V>
315 for (
const auto & entry: dict){
316 if ((entry.second > 0) && ((entry.second & value) == entry.second)){
318 container.push_back(
static_cast<V
>(entry.second));
361template <
typename E=std::
size_t,
typename T=E>
373 typedef typename dict_t::key_t key_t;
387 FlaggerBase(
char separator=
','): value(ownValue), separator(
','), ownValue((storage_t)0){
396 FlaggerBase(storage_t & v,
char separator=
','): value(v), separator(
','){
422 this->value = storage_t(0);
432 bool isSet(
const storage_t & x)
const = 0;
436 bool isSet(
const key_t & key)
const {
441 const storage_t & getValue()
const {
448 operator const storage_t & ()
const {
453 operator storage_t & () {
458 operator bool()
const {
459 return static_cast<FlagResolver::ivalue_t
>(this->value) != 0;
465 const key_t &
str()
const = 0;
468 operator const key_t & ()
const {
477 void debug(std::ostream & ostr)
const;
483 void set(const key_t & key){
486 assign(key); // potential problem: assign assumes string arg
491 void set(const value_t & value){
496 void set(const FlaggerBase<E> & flagger){
497 this->value = flagger.value;
507 void assign(
const std::string & s) = 0;
511 storage_t ownValue = 0;
516template <
typename E,
typename T>
519 ostr << drain::TypeName<E>::str() <<
": value=" << getValue() <<
", ";
521 ostr <<
" dict: " << getDict();
524template <
typename E,
typename T>
527 return ostr << flagger.
str();
Two-way mapping between strings and objects of template class T.
Definition Dictionary.h:63
const V & getValue(const K &key, bool lenient=true) 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 FlagBase.h:56
static ivalue_t getIntValue(const drain::Dictionary< key_t, T > &dict, const std::string &keys, char separator=',')
Computes bitwise OR function on the numeric or alphabetic value(s) presented by a string.
Definition FlagBase.h:198
drain::Dictionary< key_t, ivalue_t > dict_t
"Recommended" dictionary type. All the methods are templates, however.
Definition FlagBase.h:67
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 FlagBase.h:279
static void valuesToList(ivalue_t value, const drain::Dictionary< key_t, T > &dict, std::list< V > &container)
Given a bit vector (integer value), extracts separate flag values to a list.
Definition FlagBase.h:314
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 ivalue_t getFreeBit(const drain::Dictionary< key_t, T > &dict)
Return an interger (bit vector) with a new, previously unused value.
Definition FlagBase.h:127
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 FlagBase.h:267
Definition FlagBase.h:362
virtual bool isSet(const storage_t &x) const =0
Checks if a given bit, or any of given bits, is set.
virtual const key_t & str() const =0
String corresponding the current value. Returns empty, if not found.
virtual const dict_t & getDict() const =0
Returns the static dictionary created for this value_t .
FlaggerBase(storage_t &v, char separator=',')
Value-referencing constructor.
Definition FlagBase.h:396
LogSourc e is the means for a function or any program segment to "connect" to a Log.
Definition Log.h:313
Logger & debug(const TT &... args)
Debug information.
Definition Log.h:667
Logger & error(const TT &... args)
Echoes.
Definition Log.h:417
Logger & debug2(const TT &... args)
Debug information.
Definition Log.h:677
Definition DataSelector.cpp:1277