41#include <drain/SmartMapTools.h>
42#include <drain/Sprinter.h>
43#include <drain/StringTools.h>
62class SmartMap :
public std::map<std::string, T> {
68 typedef std::map<std::string, T> map_t;
69 typedef typename map_t::key_type key_t;
70 typedef typename map_t::mapped_type value_t;
71 typedef typename map_t::value_type entry_t;
73 typedef std::list<std::string> keylist_t;
77 typedef typename map_t::const_iterator const_iterator;
112 bool hasKey(
const std::string &key)
const {
113 return (this->find(key) != this->end());
123 std::string
get(
const std::string &key,
const std::string & defaultValue)
const {
124 const_iterator it = this->find(key);
125 if (it == this->end())
135 std::string
get(
const std::string & key,
const char *defaultValue)
const {
136 return get(key, std::string(defaultValue));
147 T2
get(
const std::string &key,
const T2 &defaultValue)
const {
148 const_iterator it = this->find(key);
149 if (it == this->end())
152 return static_cast<T2
>(it->second);
166 if (it != this->end()) {
172 T & element = map_t::operator[](key);
183 const_iterator it = this->find(key);
184 if (it != this->end()) {
188 static const T empty;
209 for (const_iterator it = this->begin(); it != this->end(); ++it)
210 m[it->first] = it->second;
226 template <
bool STRICT=true>
228 void importEntries(
const std::string & entries,
char assignmentChar=
'=',
char separatorChar=0);
238 template <
bool STRICT=true>
239 void importEntries(
const std::list<std::string> & entries,
char assignmentChar=
'='){
241 MapTools::setValues<smap_t,STRICT>(*
this,
getKeyList(), entries, assignmentChar);
250 template <
class S,
bool STRICT=true>
254 MapTools::setValues<smap_t,S,STRICT>(*
this, m);
269 template <
class T2,
bool STRICT=true>
271 SmartMapTools::setCastableValues<smap_t,T2,STRICT>(*
this, m);
283 importMap<T2,false>(m);
290 importCastableMap<T2,false>(m);
299 void setValues(
const std::string & entries,
char assignmentChar=
'=',
char separatorChar=0){
300 importEntries<true>(entries, assignmentChar, separatorChar);
304 void setValues(
const char * entries,
char assignmentChar=
'=',
char separatorChar=0){
305 importEntries<true>(entries, assignmentChar, separatorChar);
310 void setValuesSEQ(
const S & sequence);
314 void updateValues(
const std::string & entries,
char assignmentChar=
'=',
char separatorChar=0){
315 importEntries<false>(entries, assignmentChar, separatorChar);
320 void getKeys(std::ostream &ostr)
const {
323 for (keylist_t::const_iterator it = l.begin(); it != l.end(); ++it ){
344 for (keylist_t::const_iterator it = l.begin(); it != l.end(); ++it ){
347 if (this->find(*it) != this->end())
348 ostr << (*
this)[*it];
350 ostr <<
"*SMARTMAP::FAIL* " << __FUNCTION__;
374 std::ostream &
toStream(std::ostream & ostr,
char equal=
'=',
char startChar=
'{',
char endChar=
'}',
char separatorChar=
',')
const {
379 layout.pairChars.separator = equal;
387 std::string toStr(
char equal=
'=',
char start=0,
char end=0,
char separator=0)
const {
388 std::stringstream sstr;
400 void dump(std::ostream & ostr = std::cout)
const;
434template <
bool STRICT>
439 Logger mout(__FILE__, __FUNCTION__);
442 if (entries.empty()){
447 separatorChar = separatorChar ? separatorChar : separator;
449 std::list<std::string> l;
451 MapTools::setValues<smap_t,STRICT>(*
this, this->getKeyList(), l, assignmentChar);
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);
735std::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:313
A base class for smart maps providing methods for importing and exporting values, among others.
Definition SmartMap.h:62
map_t::iterator iterator
Needed?
Definition SmartMap.h:76
void importEntries(const std::list< std::string > &entries, char assignmentChar='=')
Definition SmartMap.h:239
char arraySeparator
Default separator character for array elements (std::vector's)
Definition SmartMap.h:84
std::string getKeys() const
Convenience function for std::string output.
Definition SmartMap.h:333
std::list< std::string > keyList
Assigns values from std::string of type "value,value2,...valueN".
Definition SmartMap.h:426
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:181
char separator
Default character used for splitting input and output. See setValues.
Definition SmartMap.h:81
virtual const keylist_t & getKeyList() const
Derived versions may produce an ordered set of keys.
Definition SmartMap.h:196
void importCastableMap(const drain::SmartMap< T2 > &m)
Assign values from a map, possibly extending the map.
Definition SmartMap.h:270
std::string getValues() const
Convenience function for std::string output.
Definition SmartMap.h:358
void exportMap(std::map< std::string, T2 > &m) const
Copies the contents to another map.
Definition SmartMap.h:208
virtual T & operator[](const std::string &key)
Returns an element. Creates one, conditionally.
Definition SmartMap.h:163
void getValues(std::ostream &ostr) const
Dumps the values.
Definition SmartMap.h:341
void updateFromMap(const std::map< std::string, T2 > &m)
Assign values from a map. Updates existing entries only.
Definition SmartMap.h:282
const map_t & getMap() const
Definition SmartMap.h:202
void updateFromCastableMap(const drain::SmartMap< T2 > &m)
Convenience.
Definition SmartMap.h:289
void importEntries(const std::string &entries, char assignmentChar='=', char separatorChar=0)
Assigns a value to given key; if the entry does not exist, tries to create it with directly with oper...
Definition SmartMap.h:435
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:123
void updateValues(const std::string &entries, char assignmentChar='=', char separatorChar=0)
Sets applicable values ie. modifies existing entries only. In ordered maps, skips extra entries silen...
Definition SmartMap.h:314
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:147
SmartMap(char separator='\0', char arraySeparator=':')
Definition SmartMap.h:91
std::ostream & toStream(std::ostream &ostr, char equal='=', char startChar='{', char endChar='}', char separatorChar=',') const
Note: parameters discarded.
Definition SmartMap.h:374
void setValues(const std::string &entries, char assignmentChar='=', char separatorChar=0)
Sets values. If strictness==STRICTLY_CLOSED, throws exception if tries to assign a non-existing entry...
Definition SmartMap.h:299
void importMap(const std::map< std::string, S > &m)
Assign values from a map, overriding existing entries.
Definition SmartMap.h:251
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:324
static const SprinterLayout jsonLayout
Resembles JSON structure: {"a":1,"b":22,"c":3}.
Definition Sprinter.h:224
Definition DataSelector.cpp:1277
Definition Sprinter.h:136