Config Class Reference

#include <config.h>

Inheritance diagram for Config:

Inheritance graph
[legend]
List of all members.

Detailed Description

Generic configuration storage class.

Config class is capable of storing key/value pairs of arbitary type in a structured hierarchy. Values can be stored at either top-level, or at sub-dirs. Accessing keys can be done either using full path of the key, e.g. prepending "/" character before the key location, or using combination of setPath() and read() calls.

Internally the data is stored as strings, and converted to requested types on request. Thus, it is possible to write apples and read back oranges, as long as:

Config class is also capable of storing the stored values in physical file between sessions, using save() and load() methods. The file format is not 100% compatible with standard configuration files, but attempts to be as close as possible. The actual format of the file stored on disk:

 [Boo]
 Boolean=1
 [Floating/Yeah]
 Pi=3.141
 [Test]
 HelloWorld=Good Morning
 []
 Integer Value=10
 Doh=0

Note:
Using multi-word names for keys is allowed.

Definition at line 65 of file config.h.


Public Member Functions

 Config ()
 Default constructor.
 Config (const std::string &filename)
 Constructs and loads from file.
virtual ~Config ()
 Virtual destructor.
virtual void load (const std::string &filename)
 Loads data from file.
virtual void save () const
 Saves values into same file as used during loading.
virtual void save (const std::string &filename) const
 Saves values into specified file.
template<typename C, typename D>
void read (const std::string &key, C *val, const D &def) const
 Reads stored value into variable, or default value if not found.
template<typename C>
read (const std::string &key, const C &def) const
 Overloaded version of Read function returning the value read.
template<typename T>
void write (const std::string &key, const T &val)
 Stores key/value pair.
void setPath (const std::string &dir)
 Sets current directory.
size_t size () const
 Returns number of elements stored by this instance.
void dump () const
 Dump the entire stored contents to std::cerr (for debugging).

Protected Types

typedef std::map< std::string,
std::string >::const_iterator 
CIter
 Constant iterator for internal data storage.

Protected Member Functions

CIter begin () const
 Access to begin iterator.
CIter end () const
 Access to end iterator.

Private Types

typedef std::map< std::string,
std::string >::iterator 
Iter

Private Attributes

std::string m_configFile
 File name used during loading.
std::string m_curPath
 Current directory.
std::map< std::string, std::string > m_values
 Actual data storage.

Member Typedef Documentation

typedef std::map<std::string, std::string>::const_iterator Config::CIter [protected]
 

Constant iterator for internal data storage.

Definition at line 188 of file config.h.

typedef std::map<std::string, std::string>::iterator Config::Iter [private]
 

Definition at line 198 of file config.h.


Constructor & Destructor Documentation

Config::Config  ) 
 

Default constructor.

Definition at line 28 of file config.cpp.

Config::Config const std::string &  filename  ) 
 

Constructs and loads from file.

Parameters:
filename File to read data from.

Definition at line 29 of file config.cpp.

References load().

Here is the call graph for this function:

Config::~Config  )  [virtual]
 

Virtual destructor.

Definition at line 32 of file config.cpp.


Member Function Documentation

CIter Config::begin  )  const [inline, protected]
 

Access to begin iterator.

Definition at line 190 of file config.h.

Referenced by Prefs::getData(), and Prefs::getFieldName().

void Config::dump  )  const
 

Dump the entire stored contents to std::cerr (for debugging).

Definition at line 159 of file config.cpp.

References logTrace(), m_values, and TRACE_CONFIG.

Here is the call graph for this function:

CIter Config::end  )  const [inline, protected]
 

Access to end iterator.

Definition at line 192 of file config.h.

void Config::load const std::string &  filename  )  [virtual]
 

Loads data from file.

Parameters:
filename File to read data from.

Definition at line 37 of file config.cpp.

References CHECK_THROW, logTrace(), logWarning(), m_configFile, m_curPath, m_values, setPath(), and TRACE_CONFIG.

Referenced by Config(), and HydraNode::initConfig().

Here is the call graph for this function:

template<typename C>
C Config::read const std::string &  key,
const C &  def
const [inline]
 

Overloaded version of Read function returning the value read.

Parameters:
key Key to search for.
def Default value to use if key is not found.
Returns:
Value of found key, or default value if not found.

Definition at line 143 of file config.h.

template<typename C, typename D>
void Config::read const std::string &  key,
C *  val,
const D &  def
const [inline]
 

Reads stored value into variable, or default value if not found.

Parameters:
key String key of value searched for.
val Pointer to variable to store found data in.
def Default value, used if no value was found in map.
The two template classes here are neccesery, because we can get input like float + double, and compilers don't seem to like that.

Definition at line 115 of file config.h.

References CHECK_THROW.

Referenced by FilesList::createDownload(), HydraNode::initFiles(), HydraNode::initModules(), and SchedBase::SchedBase().

void Config::save const std::string &  filename  )  const [virtual]
 

Saves values into specified file.

Parameters:
filename File to save values to.

Definition at line 96 of file config.cpp.

References CHECK_THROW, logError(), logTrace(), m_values, and TRACE_CONFIG.

Here is the call graph for this function:

virtual void Config::save  )  const [inline, virtual]
 

Saves values into same file as used during loading.

Definition at line 95 of file config.h.

Referenced by HydraNode::cleanup(), and HydraNode::onEvent().

void Config::setPath const std::string &  dir  ) 
 

Sets current directory.

Parameters:
dir Directory to change to.

Definition at line 146 of file config.cpp.

References CHECK_THROW, and m_curPath.

Referenced by FilesList::createDownload(), SchedBase::exit(), HydraNode::initFiles(), load(), and SchedBase::SchedBase().

size_t Config::size  )  const [inline]
 

Returns number of elements stored by this instance.

Returns:
Number of values stored by this instance.

Definition at line 179 of file config.h.

Referenced by SchedBase::getConnReqCount(), Prefs::getDataCount(), SchedBase::getDownloadReqCount(), SchedBase::getUploadReqCount(), SchedBase::handleDownloads(), and SchedBase::handleUploads().

template<typename T>
void Config::write const std::string &  key,
const T &  val
[inline]
 

Stores key/value pair.

Parameters:
key String key for later retrieval.
val Value to be stored.

Definition at line 156 of file config.h.

References CHECK_THROW.

Referenced by SchedBase::exit(), HydraNode::initConfig(), HydraNode::initModules(), and FilesList::scanSharedDir().


Member Data Documentation

std::string Config::m_configFile [private]
 

File name used during loading.

Definition at line 194 of file config.h.

Referenced by load().

std::string Config::m_curPath [private]
 

Current directory.

Definition at line 195 of file config.h.

Referenced by load(), and setPath().

std::map<std::string, std::string> Config::m_values [private]
 

Actual data storage.

Definition at line 196 of file config.h.

Referenced by dump(), load(), and save().


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