32#ifndef DRAIN_FLAG_BASE
33#define DRAIN_FLAG_BASE
45#include <drain/StringTools.h>
46#include <drain/Type.h>
48#include <drain/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;
218 for (
const key_t & key: keys){
223 else if (key ==
"0"){
232 if (dit != dict.end()){
234 v = (v |
static_cast<FlagResolver::ivalue_t
>(dit->second) );
238 FlagResolver::ivalue_t x = 0;
239 std::stringstream sstr(key);
248 mout.
error(
"key '", key,
"' not found in: ", dict);
269 std::stringstream sstr;
288 separator = dict.separator;
292 for (
const auto & entry: dict){
293 const ivalue_t v = (entry.second);
296 if ((v > 0) && ((v & value) == v)){
314template <
typename T,
typename V>
316 for (
const auto & entry: dict){
317 if ((entry.second > 0) && ((entry.second & value) == entry.second)){
319 container.push_back(
static_cast<V
>(entry.second));
362template <
typename E=std::
size_t,
typename T=E>
374 typedef typename dict_t::key_t key_t;
388 FlaggerBase(
char separator=
','): value(ownValue), separator(
','), ownValue((storage_t)0){
397 FlaggerBase(storage_t & v,
char separator=
','): value(v), separator(
','){
423 this->value = storage_t(0);
433 bool isSet(
const storage_t & x)
const = 0;
437 bool isSet(
const key_t & key)
const {
442 const storage_t & getValue()
const {
449 operator const storage_t & ()
const {
454 operator storage_t & () {
459 operator bool()
const {
460 return static_cast<FlagResolver::ivalue_t
>(this->value) != 0;
466 const key_t &
str()
const = 0;
469 operator const key_t & ()
const {
478 void debug(std::ostream & ostr)
const;
484 void set(const key_t & key){
487 assign(key); // potential problem: assign assumes string arg
492 void set(const value_t & value){
497 void set(const FlaggerBase<E> & flagger){
498 this->value = flagger.value;
508 void assign(
const std::string & s) = 0;
512 storage_t ownValue = 0;
517template <
typename E,
typename T>
520 ostr << drain::TypeName<E>::str() <<
": value=" << getValue() <<
", ";
522 ostr <<
" dict: " << getDict();
525template <
typename E,
typename T>
528 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:280
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:315
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:268
Definition FlagBase.h:363
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:397
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