Public Types | Public Member Functions | Public Attributes | Protected Attributes | List of all members
SmartMap< T > Class Template Reference

A base class for smart maps providing methods for importing and exporting values, among others. More...

#include <SmartMap.h>

Inheritance diagram for SmartMap< T >:
Inheritance graph
[legend]
Collaboration diagram for SmartMap< T >:
Collaboration graph
[legend]

Public Types

typedef SmartMap< T > smap_t
 
typedef std::map< std::string, T > map_t
 
typedef map_t::key_type key_t
 
typedef map_t::mapped_type value_t
 
typedef map_t::value_type entry_t
 
typedef std::list< std::string > keylist_t
 
typedef map_t::iterator iterator
 Needed?
 
typedef map_t::const_iterator const_iterator
 

Public Member Functions

 SmartMap (char separator='\0', char arraySeparator=':')
 
 SmartMap (const SmartMap &smap)
 
virtual void clear ()
 
bool hasKey (const std::string &key) const
 
std::string get (const std::string &key, const std::string &defaultValue) const
 Retrieves a value, or default value if value is unset. More...
 
std::string get (const std::string &key, const char *defaultValue) const
 
template<class T2 >
T2 get (const std::string &key, const T2 &defaultValue) const
 Retrieves a value, if set, else returns the given default value. More...
 
virtual T & operator[] (const std::string &key)
 Returns an element. Creates one, conditionally. More...
 
virtual const T & operator[] (const std::string &key) const
 Unlike with std::map, operator[] const is defined, returning reference to a static empty instance.
 
virtual const keylist_t & getKeyList () const
 Derived versions may produce an ordered set of keys.
 
const map_t & getMap () const
 
template<class T2 >
void exportMap (std::map< std::string, T2 > &m) const
 Copies the contents to another map.
 
template<bool STRICT = true>
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 operator[]. More...
 
template<bool STRICT = true>
void importEntries (const std::list< std::string > &entries, char assignmentSymbol='=')
 
template<class S , bool STRICT = true>
void importMap (const std::map< std::string, S > &m)
 Assign values from a map, overriding existing entries. More...
 
template<class T2 , bool STRICT = true>
void importCastableMap (const drain::SmartMap< T2 > &m)
 Assign values from a map, possibly extending the map. More...
 
template<class T2 >
void updateFromMap (const std::map< std::string, T2 > &m)
 Assign values from a map. Updates existing entries only. More...
 
template<class T2 >
void updateFromCastableMap (const drain::SmartMap< T2 > &m)
 Convenience.
 
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.
 
void setValues (const char *entries, char assignmentSymbol='=', char separatorSymbol=0)
 
template<class S >
void setValuesSEQ (const S &sequence)
 
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 silently.
 
void getKeys (std::ostream &ostr) const
 
std::string getKeys () const
 Convenience function for std::string output.
 
void getValues (std::ostream &ostr) const
 Dumps the values.
 
std::string getValues () const
 Convenience function for std::string output.
 
std::ostream & toStream (std::ostream &ostr, char equal='=', char startChar='{', char endChar='}', char separatorChar=',') const
 Note: parameters discarded.
 
std::string toStr (char equal='=', char start=0, char end=0, char separator=0) const
 
void dump (std::ostream &ostr=std::cout) const
 Write map as a JSON code. More...
 

Public Attributes

char separator
 Default character used for splitting input and output. See setValues.
 
char arraySeparator
 Default separator character for array elements (std::vector's)
 

Protected Attributes

std::list< std::string > keyList
 Assigns values from std::string of type "value,value2,...valueN". More...
 

Detailed Description

template<class T>
class drain::SmartMap< T >

A base class for smart maps providing methods for importing and exporting values, among others.

Unlike with std::map<>, operator[] is defined as const, too, returning reference to a static empty instance.

SmartMap maintains an ordered list of keys, accessible with getKeys(). The items in the key list are appended in the order they have been created. This is order is generally different from the (alphabetical) order appearing in std::map iteration.

Comma-separated values can be assigned simultaneously with setValues() and updateValues()

Constructor & Destructor Documentation

◆ SmartMap()

SmartMap ( char  separator = '\0',
char  arraySeparator = ':' 
)
inline
Parameters
separator- default character used for splitting input and output. See setValues
arraySeparator- default character for inner elements that are array (std::vector's)

Member Function Documentation

◆ dump()

void dump ( std::ostream &  ostr = std::cout) const

Write map as a JSON code.

Write map as a JSON code (array or scalar) Debugging

◆ get() [1/2]

std::string get ( const std::string &  key,
const std::string &  defaultValue 
) const
inline

Retrieves a value, or default value if value is unset.

Parameters
key- name of the variable
defaultValue- the value to be returned if the variable is unset
Returns
- the value, if set; else the default value.

◆ get() [2/2]

T2 get ( const std::string &  key,
const T2 &  defaultValue 
) const
inline

Retrieves a value, if set, else returns the given default value.

Parameters
key- name of the variable
defaultValue- the value to be returned if the variable is unset
Returns
- the value, if set; else the default value.

◆ getMap()

const map_t& getMap ( ) const
inline

? but <T> ?

◆ importCastableMap()

void importCastableMap ( const drain::SmartMap< T2 > &  m)
inline

Assign values from a map, possibly extending the map.

Parameters
m- source of keys and values
updateOnly- bypass unexisting keys silently, otherwise throw exception.
criticality- define sensibility if new keys cannot be added (LOG_NOTE, LOG_ERROR)

If a key is not known, and the map is of fixed type like ReferenceMap, throws exception.

◆ importEntries() [1/2]

void importEntries ( const std::list< std::string > &  entries,
char  assignmentSymbol = '=' 
)
inline
Parameters
entries- a sequence containing key=value pairs separated by separator TODO: sequence

◆ importEntries() [2/2]

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 operator[].

Parameters
updateOnly- if true, only existing elements are updated, otherwise skipped; if false, tries to add elements.
entries- string containing key=value pairs separated by separator

◆ importMap()

void importMap ( const std::map< std::string, S > &  m)
inline

Assign values from a map, overriding existing entries.

m - source of keys and values

If a key is not known, and the map is of fixed type like ReferenceMap, throws exception.

◆ operator[]()

virtual T& operator[] ( const std::string &  key)
inlinevirtual

Returns an element. Creates one, conditionally.

  • OPEN: creates a new elment
  • CLOSED: returns a dummy element.
  • STRICTLY_CLOSED: throws exception

Reimplemented in ReferenceMap.

◆ updateFromMap()

void updateFromMap ( const std::map< std::string, T2 > &  m)
inline

Assign values from a map. Updates existing entries only.

m - source of keys and values
Convenience

Member Data Documentation

◆ keyList

std::list<std::string> keyList
mutableprotected

Assigns values from std::string of type "value,value2,...valueN".

Allows setting values in python style function calls:

  1. in the definition order of entries: "value,value2,...valueN" become assined as key=value, key2=value2, ... and so on.
  2. specified mode, allowing specific assignments "key=value,key2=value2,key3=value3"

Mixed types like "value,key4=value2,value3" accepted but not encouraged. In the mixed mode, the unspecified keys become assigned in \i their order of appearance. Hence, in the above example value3 will be assigned to key2 , not key3.

Parameters
criticality- if true, skip non-existing entries silently Defines the 1) valid keys and 2) their order.

The documentation for this class was generated from the following file: