34 #include <drain/Log.h>
41 #include <sys/syslog.h>
73 typedef std::map<std::string, T> map_t;
74 typedef typename map_t::key_type key_t;
75 typedef typename map_t::mapped_type value_t;
76 typedef typename map_t::value_type entry_t;
78 typedef std::list<std::string> keylist_t;
82 typedef typename map_t::const_iterator const_iterator;
117 bool hasKey(
const std::string &key)
const {
118 return (this->find(key) != this->end());
128 std::string
get(
const std::string &key,
const std::string & defaultValue)
const {
129 const_iterator it = this->find(key);
130 if (it == this->end())
140 std::string
get(
const std::string & key,
const char *defaultValue)
const {
141 return get(key, std::string(defaultValue));
152 T2
get(
const std::string &key, T2 defaultValue)
const {
153 const_iterator it = this->find(key);
154 if (it == this->end())
157 return static_cast<T2
>(it->second);
171 if (it != this->end()) {
177 T & element = map_t::operator[](key);
188 const_iterator it = this->find(key);
189 if (it != this->end()) {
193 static const T empty;
214 for (const_iterator it = this->begin(); it != this->end(); ++it)
215 m[it->first] = it->second;
231 template <
bool STRICT=true>
232 void importEntries(
const std::string & entries,
char assignmentSymbol=
'=',
char separatorSymbol=0);
240 template <
bool STRICT=true>
241 void importEntries(
const std::list<std::string> & entries,
char assignmentSymbol=
'='){
243 SmartMapTools::setValues<smap_t,STRICT>(*
this,
getKeyList(), entries, assignmentSymbol);
252 template <
class S,
bool STRICT=true>
256 SmartMapTools::setValues<smap_t,S,STRICT>(*
this, m);
271 template <
class T2,
bool STRICT=true>
274 SmartMapTools::setCastableValues<smap_t,T2,STRICT>(*
this, m);
296 importMap<T2,false>(m);
303 importCastableMap<T2,false>(m);
312 void setValues(
const std::string & entries,
char assignmentSymbol=
'=',
char separatorSymbol=0){
313 importEntries<true>(entries, assignmentSymbol, separatorSymbol);
317 void setValues(
const char * entries,
char assignmentSymbol=
'=',
char separatorSymbol=0){
318 importEntries<true>(entries, assignmentSymbol, separatorSymbol);
323 void setValuesSEQ(
const S & sequence);
327 void updateValues(
const std::string & entries,
char assignmentSymbol=
'=',
char separatorSymbol=0){
328 importEntries<false>(entries, assignmentSymbol, separatorSymbol);
333 void getKeys(std::ostream &ostr)
const {
336 for (keylist_t::const_iterator it = l.begin(); it != l.end(); ++it ){
357 for (keylist_t::const_iterator it = l.begin(); it != l.end(); ++it ){
360 if (this->find(*it) != this->end())
361 ostr << (*this)[*it];
363 ostr <<
"*SMARTMAP::FAIL* " << __FUNCTION__;
387 std::ostream &
toStream(std::ostream & ostr,
char equal=
'=',
char startChar=
'{',
char endChar=
'}',
char separatorChar=
',')
const {
392 layout.pairChars.separator = equal;
400 std::string toStr(
char equal=
'=',
char start=0,
char end=0,
char separator=0)
const {
401 std::stringstream sstr;
413 void dump(std::ostream & ostr = std::cout)
const;
444 template <
bool STRICT>
447 Logger mout(__FILE__, __FUNCTION__);
450 if (entries.empty()){
454 separatorSymbol = separatorSymbol ? separatorSymbol : separator;
457 std::list<std::string> p;
464 p.push_back(entries);
466 importEntries<STRICT>(p, assignmentSymbol);
551 Logger log(__FILE__, __FUNCTION__);
553 const std::list<std::string> & keys = getKeyList();
554 std::list<std::string>::const_iterator kit = keys.begin();
556 for (
typename S::const_iterator it = sequence.begin(); it != sequence.end(); ++it){
558 if (kit != keys.end()){
564 log.error() <<
"too many ("<< sequence.size() <<
") params for map of size ("<< this->size() <<
"), run out of keys with entry=" << *it << log.endl;
727 for (const_iterator it = this->begin(); it != this->end(); ++it){
728 ostr << it->first <<
':' <<
' ';
729 it->second.info(ostr);
736 std::ostream &operator<<(std::ostream &ostr,
const SmartMapFoo<T> & m){
LogSourc e is the means for a function or any program segment to "connect" to a Log.
Definition: Log.h:308
A base class for smart maps providing methods for importing and exporting values, among others.
Definition: SmartMap2.h:67
map_t::iterator iterator
Needed?
Definition: SmartMap2.h:81
virtual T & operator[](const std::string &key)
Returns an element. Creates one, conditionally.
Definition: SmartMap2.h:168
void importEntries(const std::string &entries, char assignmentSymbol='=', char separatorSymbol=0)
Assigns a value to given key; if the entry does not exist, tries to create it with directly with oper...
Definition: SmartMap2.h:445
char arraySeparator
Default separator character for array elements (std::vector's)
Definition: SmartMap2.h:89
virtual const T & operator[](const std::string &key) const
Unlike with std::map, operator[] const is defined, returning reference to a static empty instance.
Definition: SmartMap2.h:186
std::string getKeys() const
Convenience function for std::string output.
Definition: SmartMap2.h:346
std::list< std::string > keyList
Assigns values from std::string of type "value,value2,...valueN".
Definition: SmartMap2.h:439
SmartMapFoo(char separator='\0', char arraySeparator=':')
Definition: SmartMap2.h:96
void updateFromCastableMap(const drain::SmartMapFoo< T2 > &m)
Convenience.
Definition: SmartMap2.h:302
void importEntries(const std::list< std::string > &entries, char assignmentSymbol='=')
Definition: SmartMap2.h:241
std::ostream & toStream(std::ostream &ostr, char equal='=', char startChar='{', char endChar='}', char separatorChar=',') const
Note: parameters discarded.
Definition: SmartMap2.h:387
char separator
Default character used for splitting input and output. See setValues.
Definition: SmartMap2.h:86
void setValues(const std::string &entries, char assignmentSymbol='=', char separatorSymbol=0)
Sets values. If strictness==STRICTLY_CLOSED, throws exception if tries to assign a non-existing entry...
Definition: SmartMap2.h:312
virtual const keylist_t & getKeyList() const
Derived versions may produce an ordered set of keys.
Definition: SmartMap2.h:201
void importCastableMap(const drain::SmartMap< T2 > &m)
Assign values from a map, possibly extending the map.
Definition: SmartMap2.h:272
std::string getValues() const
Convenience function for std::string output.
Definition: SmartMap2.h:371
void exportMap(std::map< std::string, T2 > &m) const
Copies the contents to another map.
Definition: SmartMap2.h:213
void getValues(std::ostream &ostr) const
Dumps the values.
Definition: SmartMap2.h:354
void updateFromMap(const std::map< std::string, T2 > &m)
Assign values from a map. Updates existing entries only.
Definition: SmartMap2.h:295
void updateValues(const std::string &entries, char assignmentSymbol='=', char separatorSymbol=0)
Sets applicable values ie. modifies existing entries only. In ordered maps, skips extra entries silen...
Definition: SmartMap2.h:327
std::string get(const std::string &key, const std::string &defaultValue) const
Retrieves a value, or default value if value is unset.
Definition: SmartMap2.h:128
void dump(std::ostream &ostr=std::cout) const
Write map as a JSON code.
Definition: SmartMap2.h:724
T2 get(const std::string &key, T2 defaultValue) const
Retrieves a value, if set, else returns the given default value.
Definition: SmartMap2.h:152
const map_t & getMap() const
Definition: SmartMap2.h:207
void importMap(const std::map< std::string, S > &m)
Assign values from a map, overriding existing entries.
Definition: SmartMap2.h:253
A base class for smart maps providing methods for importing and exporting values, among others.
Definition: SmartMap.h:66
static std::ostream & sequenceToStream(std::ostream &ostr, const T &x, const SprinterLayout &layout)
Convenience: if sequence type (array, list, set, map) not given, assume array.
Definition: Sprinter.h:321
static const SprinterLayout jsonLayout
Resembles JSON structure: {"a":1,"b":22,"c":3}.
Definition: Sprinter.h:221
Definition: DataSelector.cpp:1277
Definition: Sprinter.h:137
Definition: Sprinter.h:80