41#include <sys/syslog.h>
44#include <drain/Castable.h>
45#include <drain/SmartMapTools.h>
46#include <drain/Sprinter.h>
47#include <drain/StringTools.h>
66class 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>
232 void importEntries(
const std::string & entries,
char assignmentChar=
'=',
char separatorChar=0);
242 template <
bool STRICT=true>
243 void importEntries(
const std::list<std::string> & entries,
char assignmentChar=
'='){
245 MapTools::setValues<smap_t,STRICT>(*
this,
getKeyList(), entries, assignmentChar);
254 template <
class S,
bool STRICT=true>
258 MapTools::setValues<smap_t,S,STRICT>(*
this, m);
273 template <
class T2,
bool STRICT=true>
275 SmartMapTools::setCastableValues<smap_t,T2,STRICT>(*
this, m);
287 importMap<T2,false>(m);
294 importCastableMap<T2,false>(m);
303 void setValues(
const std::string & entries,
char assignmentChar=
'=',
char separatorChar=0){
304 importEntries<true>(entries, assignmentChar, separatorChar);
308 void setValues(
const char * entries,
char assignmentChar=
'=',
char separatorChar=0){
309 importEntries<true>(entries, assignmentChar, separatorChar);
314 void setValuesSEQ(
const S & sequence);
318 void updateValues(
const std::string & entries,
char assignmentChar=
'=',
char separatorChar=0){
319 importEntries<false>(entries, assignmentChar, separatorChar);
324 void getKeys(std::ostream &ostr)
const {
327 for (keylist_t::const_iterator it = l.begin(); it != l.end(); ++it ){
348 for (keylist_t::const_iterator it = l.begin(); it != l.end(); ++it ){
351 if (this->find(*it) != this->end())
352 ostr << (*
this)[*it];
354 ostr <<
"*SMARTMAP::FAIL* " << __FUNCTION__;
378 std::ostream &
toStream(std::ostream & ostr,
char equal=
'=',
char startChar=
'{',
char endChar=
'}',
char separatorChar=
',')
const {
383 layout.pairChars.separator = equal;
391 std::string toStr(
char equal=
'=',
char start=0,
char end=0,
char separator=0)
const {
392 std::stringstream sstr;
404 void dump(std::ostream & ostr = std::cout)
const;
438template <
bool STRICT>
443 Logger mout(__FILE__, __FUNCTION__);
446 if (entries.empty()){
451 separatorChar = separatorChar ? separatorChar : separator;
453 std::list<std::string> l;
455 MapTools::setValues<smap_t,STRICT>(*
this, this->getKeyList(), l, assignmentChar);
554 Logger log(__FILE__, __FUNCTION__);
556 const std::list<std::string> & keys = getKeyList();
557 std::list<std::string>::const_iterator kit = keys.begin();
559 for (
typename S::const_iterator it = sequence.begin(); it != sequence.end(); ++it){
561 if (kit != keys.end()){
567 log.error() <<
"too many ("<< sequence.size() <<
") params for map of size ("<< this->size() <<
"), run out of keys with entry=" << *it << log.endl;
730 for (const_iterator it = this->begin(); it != this->end(); ++it){
731 ostr << it->first <<
':' <<
' ';
732 it->second.info(ostr);
739std::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:66
map_t::iterator iterator
Needed?
Definition SmartMap.h:80
void importEntries(const std::list< std::string > &entries, char assignmentChar='=')
Definition SmartMap.h:243
char arraySeparator
Default separator character for array elements (std::vector's)
Definition SmartMap.h:88
std::string getKeys() const
Convenience function for std::string output.
Definition SmartMap.h:337
std::list< std::string > keyList
Assigns values from std::string of type "value,value2,...valueN".
Definition SmartMap.h:430
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
char separator
Default character used for splitting input and output. See setValues.
Definition SmartMap.h:85
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:274
std::string getValues() const
Convenience function for std::string output.
Definition SmartMap.h:362
void exportMap(std::map< std::string, T2 > &m) const
Copies the contents to another map.
Definition SmartMap.h:212
virtual T & operator[](const std::string &key)
Returns an element. Creates one, conditionally.
Definition SmartMap.h:167
void getValues(std::ostream &ostr) const
Dumps the values.
Definition SmartMap.h:345
void updateFromMap(const std::map< std::string, T2 > &m)
Assign values from a map. Updates existing entries only.
Definition SmartMap.h:286
const map_t & getMap() const
Definition SmartMap.h:206
void updateFromCastableMap(const drain::SmartMap< T2 > &m)
Convenience.
Definition SmartMap.h:293
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:439
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 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:318
void dump(std::ostream &ostr=std::cout) const
Write map as a JSON code.
Definition SmartMap.h:727
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
std::ostream & toStream(std::ostream &ostr, char equal='=', char startChar='{', char endChar='}', char separatorChar=',') const
Note: parameters discarded.
Definition SmartMap.h:378
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:303
void importMap(const std::map< std::string, S > &m)
Assign values from a map, overriding existing entries.
Definition SmartMap.h:255
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:325
static const SprinterLayout jsonLayout
Resembles JSON structure: {"a":1,"b":22,"c":3}.
Definition Sprinter.h:225
Definition DataSelector.cpp:1277
Definition Sprinter.h:137