44 #include <drain/Log.h>
45 #include <drain/String.h>
46 #include <drain/Type.h>
48 #include "Dictionary.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>
126 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 );
173 template <
typename T>
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 );
197 template <
typename T>
202 FlagResolver::ivalue_t v = 0;
204 std::list<key_t> keys;
207 separator = dict.separator;
212 for (
const key_t & key: keys){
217 else if (key ==
"0"){
226 if (dit != dict.end()){
228 v = (v |
static_cast<FlagResolver::ivalue_t
>(dit->second) );
232 FlagResolver::ivalue_t x = 0;
233 std::stringstream sstr(key);
242 mout.
error(
"key '", key,
"' not found in: ", dict);
261 template <
typename T>
263 std::stringstream sstr;
273 template <
typename T>
282 separator = dict.separator;
286 for (
const auto & entry: dict){
287 if ((entry.second > 0) && ((entry.second & value) == entry.second)){
305 template <
typename T,
typename V>
307 for (
const auto & entry: dict){
308 if ((entry.second > 0) && ((entry.second & value) == entry.second)){
310 container.push_back(
static_cast<V
>(entry.second));
353 template <
typename E=std::
size_t>
365 typedef typename dict_t::key_t key_t;
368 typedef typename dict_t::value_t dict_value_t;
377 FlaggerBase(): value(ownValue), separator(
','), ownValue((dict_value_t)0){
399 const dict_t & getDict()
const = 0;
402 this->value = dict_value_t(0);
407 void set(
const key_t & key){
412 if (this->getDict().hasKey(key)){
413 this->value = (dict_value_t)this->getDict().
getValue(key);
424 void set(
const dict_value_t & value){
429 void set(
const FlaggerBase<E> & flagger){
430 this->value = flagger.value;
435 void assign(
const std::string & s) = 0;
440 return (value & x) != 0;
444 bool isSet(
const key_t & key)
const {
445 return isSet(getDict().getValue(key));
449 const dict_value_t & getValue()
const {
464 operator const dict_value_t & ()
const {
469 operator dict_value_t & () {
475 const key_t &
str()
const = 0;
478 operator const key_t & ()
const {
483 dict_value_t & value;
487 void debug(std::ostream & ostr)
const;
492 dict_value_t ownValue = 0;
497 template <
typename E>
500 ostr << drain::TypeName<E>::str() <<
": value=" << getValue() <<
", ";
502 ostr <<
" dict: " << getDict();
511 template <
typename E>
514 return ostr << flagger.
str();
517 template <
typename E>
526 typedef typename dict_t::key_t key_t;
527 typedef typename dict_t::value_t dvalue_t;
542 void assign(
const std::string & s){
555 const key_t &
str()
const {
558 return this->getDict().
getKey(this->value);
565 template <
typename E>
568 ostr << flagger.
str();
573 template <
typename E>
583 typedef typename dict_t::key_t
key_t;
598 template <
typename ... T>
616 template <
typename ... T>
618 void set(
const T &... args) {
639 const dict_t & dict = this->getDict();
645 template <
typename T,
typename ... TT>
647 void add(
const T & arg,
const TT &... args) {
658 const dict_t & dict = this->getDict();
700 void addOne(
const key_t & key){
727 std::string currentStr;
735 template <
typename E>
768 typedef typename fbase_t::value_t value_t;
769 typedef FlagResolver::ivalue_t ivalue_t;
781 template <
typename ... T>
812 std::string getKeysNEW2(
const value_t & value,
char separator =
','){
829 this->set(flagger.value);
841 operator value_t()
const {
842 return static_cast<value_t
>(this->value);
846 operator bool()
const {
847 return static_cast<ivalue_t
>(this->value) != 0;
868 typedef typename fbase_t::value_t value_t;
869 typedef typename fbase_t::dict_t dict_t;
872 const dict_t & getDict()
const {
877 Flagger2(
const dict_t & dict) : dict(dict){
912 typedef typename fbase_t::value_t value_t;
913 typedef typename fbase_t::dict_t dict_t;
917 const dict_t & getDict()
const {
943 Flags2(std::initializer_list<typename dict_t::entry_t> list) : dict(list){
952 Flags2<F> & operator=(
const T & v){
const V & getValue(const K &key) const
Given a key, return the first value associated with it.
Definition: Dictionary.h:149
const K & getKey(const V &value) const
Given a value, return the first key associated with it.
Definition: Dictionary.h:161
Flagger accepting values of enum type E.
Definition: Flags.h:763
virtual const FlaggerBase< value_t >::dict_t & getDict() const
Returns the static dictionary created for this value_t .
Definition: Flags.h:794
EnumFlagger< F > & operator=(const EnumFlagger< F > &flagger)
Definition: Flags.h:828
static ivalue_t getValueNEW(const std::string &key)
Return the integer value corresponding to a key.
Definition: Flags.h:807
Referencing a dictionary of binary values: {"A",1: "B":2, "C": 4, "D": 8, ...} resolves two-way mappi...
Definition: Flags.h:56
drain::Dictionary< key_t, ivalue_t > dict_t
"Recommended" dictionary type. All the methods are templates, however.
Definition: Flags.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: 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 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: Flags.h:306
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: Flags.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: Flags.h:262
Flagger with referenced/external dictionary accepting values of (integer) type T.
Definition: Flags.h:863
FlaggerBase(dict_value_t &v)
Own value will be unused (and invisible).
Definition: Flags.h:385
virtual const key_t & str() const =0
String corresponding the current value. Returns empty, if not found.
bool isSet(dict_value_t x) const
Checks if a given bit, or any of given bits, is set.
Definition: Flags.h:439
Flagger with own dictionary, and accepting values of (integer) type T.
Definition: Flags.h:907
Flags2(std::initializer_list< typename dict_t::entry_t > list)
Constructor for relatively short dictionaries.
Definition: Flags.h:943
Flags2()
Definition: Flags.h:933
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
Logger & debug(const TT &... args)
Public, yet typically used "internally", when TIMING=true.
Definition: Log.h:676
Logger & debug2(const TT &... args)
Debug information.
Definition: Log.h:686
void add(const T &arg, const TT &... args)
Add bit values.
Definition: Flags.h:647
virtual void assign(const std::string &s)
Set bits, accepting keys only.
Definition: Flags.h:638
virtual const key_t & str() const
For exporting values.
Definition: Flags.h:657
void add()
This should be called after assigning a string to & str() .
Definition: Flags.h:690
void set(const T &... args)
Set bits, accepting numeric values or keys.
Definition: Flags.h:618
dict_t::key_t key_t
Fundamental type of the bitvector - an integral type.
Definition: Flags.h:583
FlaggerBase< E >::dict_value_t dict_value_t
Fundamental type – in this case an enum.
Definition: Flags.h:579
virtual std::string & str()
Given only a numeric/enum value,.
Definition: Flags.h:673
dict_t::value_t dvalue_t
Fundamental type of the bitvector - an integral type.
Definition: Flags.h:585
virtual const key_t & str() const
String corresponding the current value. Returns empty, if not found.
Definition: Flags.h:555
Definition: DataSelector.cpp:1277