32 #ifndef DRAIN_FLAG_BASE
33 #define DRAIN_FLAG_BASE
44 #include <drain/Log.h>
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>
127 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 );
174 template <
typename T>
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 );
198 template <
typename T>
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);
265 template <
typename T>
267 std::stringstream sstr;
277 template <
typename T>
286 separator = dict.separator;
290 for (
const auto & entry: dict){
291 const ivalue_t v = (entry.second);
294 if ((v > 0) && ((v & value) == v)){
312 template <
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));
360 template <
typename E=std::
size_t,
typename T=E>
372 typedef typename dict_t::key_t key_t;
385 FlaggerBase(
char separator=
','): value(ownValue), separator(
','), ownValue((storage_t)0){
394 FlaggerBase(storage_t & v,
char separator=
','): value(v), separator(
','){
418 this->value = storage_t(0);
423 void set(
const key_t & key){
452 void set(
const value_t & value){
458 this->value = flagger.value;
467 bool isSet(
const storage_t & x)
const = 0;
477 bool isSet(
const key_t & key)
const {
482 const storage_t & getValue()
const {
497 operator const storage_t & ()
const {
502 operator storage_t & () {
507 operator bool()
const {
508 return static_cast<FlagResolver::ivalue_t
>(this->value) != 0;
514 const key_t &
str()
const = 0;
517 operator const key_t & ()
const {
526 void debug(std::ostream & ostr)
const;
532 void assign(
const std::string & s) = 0;
536 storage_t ownValue = 0;
541 template <
typename E,
typename T>
544 ostr << drain::TypeName<E>::str() <<
": value=" << getValue() <<
", ";
546 ostr <<
" dict: " << getDict();
549 template <
typename E,
typename T>
552 return ostr << flagger.
str();
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 void set(const value_t &value)
Set a single flag.
Definition: FlagBase.h:452
virtual const key_t & str() const =0
String corresponding the current value. Returns empty, if not found.
virtual void set(const key_t &key)
Sets one or several flags.
Definition: FlagBase.h:423
FlaggerBase(storage_t &v, char separator=',')
Value-referencing constructor.
Definition: FlagBase.h:394
virtual const dict_t & getDict() const =0
Returns the static dictionary created for this value_t .
LogSourc e is the means for a function or any program segment to "connect" to a Log.
Definition: Log.h:310
Logger & error(const TT &... args)
Echoes.
Definition: Log.h:414
Logger & debug(const TT &... args)
Public, yet typically used "internally", when TIMING=true.
Definition: Log.h:678
Logger & debug2(const TT &... args)
Debug information.
Definition: Log.h:688
Definition: DataSelector.cpp:1277