32#ifndef DRAIN_FLAG_BASE
33#define DRAIN_FLAG_BASE
45#include <drain/String.h>
46#include <drain/Type.h>
48#include "Dictionary.h"
62 typedef std::string key_t;
65 typedef unsigned long int ivalue_t;
102 template <
typename T,
typename V>
110 template <
typename T>
119 template <
typename T>
133 ivalue_t currentMask = 0;
134 for (
const auto & entry: dict){
135 currentMask = currentMask | entry.second;
138 mout.
debug2(
"sum:" , currentMask );
141 while ((i¤tMask) > 0){
142 mout.debug3(
"checking bit:" , i ,
" vs.\t" , currentMask );
154 mout.
debug(
"released: " , i );
179 if (dict.hasKey(key)){
180 mout.info(key ,
" already in dict: " , dict );
191 mout.warn(key ,
" could not get a valid (non-zero) bit flag for dict: " , dict );
203 FlagResolver::ivalue_t v = 0;
205 std::list<key_t> keys;
208 separator = dict.separator;
216 for (
const key_t & key: keys){
221 else if (key ==
"0"){
230 if (dit != dict.end()){
232 v = (v |
static_cast<FlagResolver::ivalue_t
>(dit->second) );
236 FlagResolver::ivalue_t x = 0;
237 std::stringstream sstr(key);
246 mout.
error(
"key '", key,
"' not found in: ", dict);
267 std::stringstream sstr;
286 separator = dict.separator;
290 for (
const auto & entry: dict){
291 const ivalue_t v = (entry.second);
294 if ((v > 0) && ((v & value) == v)){
312template <
typename T,
typename V>
314 for (
const auto & entry: dict){
315 if ((entry.second > 0) && ((entry.second & value) == entry.second)){
317 container.push_back(
static_cast<V
>(entry.second));
360template <
typename E=std::
size_t,
typename T=E>
372 typedef typename dict_t::key_t key_t;
386 FlaggerBase(
char separator=
','): value(ownValue), separator(
','), ownValue((storage_t)0){
395 FlaggerBase(storage_t & v,
char separator=
','): value(v), separator(
','){
421 this->value = storage_t(0);
431 bool isSet(
const storage_t & x)
const = 0;
435 bool isSet(
const key_t & key)
const {
440 const storage_t & getValue()
const {
447 operator const storage_t & ()
const {
452 operator storage_t & () {
457 operator bool()
const {
458 return static_cast<FlagResolver::ivalue_t
>(this->value) != 0;
464 const key_t &
str()
const = 0;
467 operator const key_t & ()
const {
476 void debug(std::ostream & ostr)
const;
482 void set(const key_t & key){
485 assign(key); // potential problem: assign assumes string arg
490 void set(const value_t & value){
495 void set(const FlaggerBase<E> & flagger){
496 this->value = flagger.value;
506 void assign(
const std::string & s) = 0;
510 storage_t ownValue = 0;
515template <
typename E,
typename T>
518 ostr << drain::TypeName<E>::str() <<
": value=" << getValue() <<
", ";
520 ostr <<
" dict: " << getDict();
523template <
typename E,
typename T>
526 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:57
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:199
drain::Dictionary< key_t, ivalue_t > dict_t
"Recommended" dictionary type. All the methods are templates, however.
Definition FlagBase.h:68
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:278
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:313
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:128
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:266
Definition FlagBase.h:361
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:395
LogSourc e is the means for a function or any program segment to "connect" to a Log.
Definition Log.h:312
Logger & debug(const TT &... args)
Debug information.
Definition Log.h:666
Logger & error(const TT &... args)
Echoes.
Definition Log.h:416
Logger & debug2(const TT &... args)
Debug information.
Definition Log.h:676
Definition DataSelector.cpp:1277