34 #include <drain/Log.h>
41 #include <sys/syslog.h>
44 #include <drain/Castable.h>
45 #include <drain/SmartMapTools.h>
46 #include <drain/Sprinter.h>
47 #include <drain/String.h>
66 class SmartMap :
public std::map<std::string, T> {
72 typedef std::map<std::string, T> map_t;
73 typedef typename map_t::key_type key_t;
74 typedef typename map_t::mapped_type value_t;
75 typedef typename map_t::value_type entry_t;
77 typedef std::list<std::string> keylist_t;
81 typedef typename map_t::const_iterator const_iterator;
116 bool hasKey(
const std::string &key)
const {
117 return (this->find(key) != this->end());
127 std::string
get(
const std::string &key,
const std::string & defaultValue)
const {
128 const_iterator it = this->find(key);
129 if (it == this->end())
139 std::string
get(
const std::string & key,
const char *defaultValue)
const {
140 return get(key, std::string(defaultValue));
151 T2
get(
const std::string &key,
const T2 &defaultValue)
const {
152 const_iterator it = this->find(key);
153 if (it == this->end())
156 return static_cast<T2
>(it->second);
170 if (it != this->end()) {
176 T & element = map_t::operator[](key);
187 const_iterator it = this->find(key);
188 if (it != this->end()) {
192 static const T empty;
213 for (const_iterator it = this->begin(); it != this->end(); ++it)
214 m[it->first] = it->second;
230 template <
bool STRICT=true>
231 void importEntries(
const std::string & entries,
char assignmentSymbol=
'=',
char separatorSymbol=0);
239 template <
bool STRICT=true>
240 void importEntries(
const std::list<std::string> & entries,
char assignmentSymbol=
'='){
242 SmartMapTools::setValues<smap_t,STRICT>(*
this,
getKeyList(), entries, assignmentSymbol);
251 template <
class S,
bool STRICT=true>
255 SmartMapTools::setValues<smap_t,S,STRICT>(*
this, m);
270 template <
class T2,
bool STRICT=true>
273 SmartMapTools::setCastableValues<smap_t,T2,STRICT>(*
this, m);
295 importMap<T2,false>(m);
302 importCastableMap<T2,false>(m);
311 void setValues(
const std::string & entries,
char assignmentSymbol=
'=',
char separatorSymbol=0){
312 importEntries<true>(entries, assignmentSymbol, separatorSymbol);
316 void setValues(
const char * entries,
char assignmentSymbol=
'=',
char separatorSymbol=0){
317 importEntries<true>(entries, assignmentSymbol, separatorSymbol);
322 void setValuesSEQ(
const S & sequence);
326 void updateValues(
const std::string & entries,
char assignmentSymbol=
'=',
char separatorSymbol=0){
327 importEntries<false>(entries, assignmentSymbol, separatorSymbol);
332 void getKeys(std::ostream &ostr)
const {
335 for (keylist_t::const_iterator it = l.begin(); it != l.end(); ++it ){
356 for (keylist_t::const_iterator it = l.begin(); it != l.end(); ++it ){
359 if (this->find(*it) != this->end())
360 ostr << (*this)[*it];
362 ostr <<
"*SMARTMAP::FAIL* " << __FUNCTION__;
386 std::ostream &
toStream(std::ostream & ostr,
char equal=
'=',
char startChar=
'{',
char endChar=
'}',
char separatorChar=
',')
const {
391 layout.pairChars.separator = equal;
399 std::string toStr(
char equal=
'=',
char start=0,
char end=0,
char separator=0)
const {
400 std::stringstream sstr;
412 void dump(std::ostream & ostr = std::cout)
const;
443 template <
bool STRICT>
446 Logger mout(__FILE__, __FUNCTION__);
449 if (entries.empty()){
453 separatorSymbol = separatorSymbol ? separatorSymbol : separator;
456 std::list<std::string> p;
463 p.push_back(entries);
465 importEntries<STRICT>(p, assignmentSymbol);
550 Logger log(__FILE__, __FUNCTION__);
552 const std::list<std::string> & keys = getKeyList();
553 std::list<std::string>::const_iterator kit = keys.begin();
555 for (
typename S::const_iterator it = sequence.begin(); it != sequence.end(); ++it){
557 if (kit != keys.end()){
563 log.error() <<
"too many ("<< sequence.size() <<
") params for map of size ("<< this->size() <<
"), run out of keys with entry=" << *it << log.endl;
726 for (const_iterator it = this->begin(); it != this->end(); ++it){
727 ostr << it->first <<
':' <<
' ';
728 it->second.info(ostr);
735 std::ostream &operator<<(std::ostream &ostr,
const SmartMap<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: SmartMap.h:66
map_t::iterator iterator
Needed?
Definition: SmartMap.h:80
virtual T & operator[](const std::string &key)
Returns an element. Creates one, conditionally.
Definition: SmartMap.h:167
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: SmartMap.h:444
char arraySeparator
Default separator character for array elements (std::vector's)
Definition: SmartMap.h:88
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: SmartMap.h:185
std::string getKeys() const
Convenience function for std::string output.
Definition: SmartMap.h:345
std::list< std::string > keyList
Assigns values from std::string of type "value,value2,...valueN".
Definition: SmartMap.h:438
void importEntries(const std::list< std::string > &entries, char assignmentSymbol='=')
Definition: SmartMap.h:240
std::ostream & toStream(std::ostream &ostr, char equal='=', char startChar='{', char endChar='}', char separatorChar=',') const
Note: parameters discarded.
Definition: SmartMap.h:386
char separator
Default character used for splitting input and output. See setValues.
Definition: SmartMap.h:85
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: SmartMap.h:311
virtual const keylist_t & getKeyList() const
Derived versions may produce an ordered set of keys.
Definition: SmartMap.h:200
void importCastableMap(const drain::SmartMap< T2 > &m)
Assign values from a map, possibly extending the map.
Definition: SmartMap.h:271
std::string getValues() const
Convenience function for std::string output.
Definition: SmartMap.h:370
void exportMap(std::map< std::string, T2 > &m) const
Copies the contents to another map.
Definition: SmartMap.h:212
void getValues(std::ostream &ostr) const
Dumps the values.
Definition: SmartMap.h:353
void updateFromMap(const std::map< std::string, T2 > &m)
Assign values from a map. Updates existing entries only.
Definition: SmartMap.h:294
void updateFromCastableMap(const drain::SmartMap< T2 > &m)
Convenience.
Definition: SmartMap.h:301
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: SmartMap.h:326
std::string get(const std::string &key, const std::string &defaultValue) const
Retrieves a value, or default value if value is unset.
Definition: SmartMap.h:127
void dump(std::ostream &ostr=std::cout) const
Write map as a JSON code.
Definition: SmartMap.h:723
T2 get(const std::string &key, const T2 &defaultValue) const
Retrieves a value, if set, else returns the given default value.
Definition: SmartMap.h:151
SmartMap(char separator='\0', char arraySeparator=':')
Definition: SmartMap.h:95
const map_t & getMap() const
Definition: SmartMap.h:206
void importMap(const std::map< std::string, S > &m)
Assign values from a map, overriding existing entries.
Definition: SmartMap.h:252
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