LogCabin
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Classes | Public Member Functions | Private Types | Private Member Functions | Static Private Member Functions | Private Attributes | Friends
LogCabin::Core::Config Class Reference

Reads and writes configuration files. More...

#include <Config.h>

List of all members.

Classes

struct  ConversionError
struct  Exception
 Base class for Config exceptions. More...
struct  FileNotFound
struct  KeyNotFound

Public Member Functions

 Config (const string &delimiter="=", const string &comment="#")
 Construct an empty Config.
 Config (const std::map< string, string > &options)
 Construct a Config from the given map of options.
void readFile (const string &filename)
 Load a Config from a file.
template<class T = string>
read (const string &key) const
 Read the value corresponding to a key.
template<class T = string>
read (const string &key, const T &value) const
 Return the value corresponding to key or given default value if key is not found.
bool keyExists (const string &key) const
 Check whether key exists in configuration.
template<class T >
void set (const string &key, const T &value)
 Set a key to the given value.
void set (const string &key, const string &value)
 Set a key to the given string value.
void remove (const string &key)
 Remove a key and its value.

Private Types

typedef std::string string

Private Member Functions

std::string readLine (std::istream &is) const
 Read a line, strip comments, and trim it.

Static Private Member Functions

template<class T >
static string toString (const T &t)
 Convert from a T to a string.
template<class T >
static T fromString (const string &key, const string &s)
 Convert from a string to a T.

Private Attributes

const string delimiter
 Separator between key and value, usually "=".
const string comment
 Starts a comment, usually "#".
std::map< string, stringcontents
 Extracted keys and values.

Friends

std::istream & operator>> (std::istream &is, Config &cf)
 Read configuration.
std::ostream & operator<< (std::ostream &os, const Config &cf)
 Write configuration.

Detailed Description

Reads and writes configuration files.

Definition at line 61 of file Config.h.


Member Typedef Documentation

typedef std::string LogCabin::Core::Config::string [private]

Definition at line 62 of file Config.h.


Constructor & Destructor Documentation

LogCabin::Core::Config::Config ( const string delimiter = "=",
const string comment = "#" 
) [explicit]

Construct an empty Config.

Definition at line 138 of file Config.cc.

LogCabin::Core::Config::Config ( const std::map< string, string > &  options) [explicit]

Construct a Config from the given map of options.

Definition at line 146 of file Config.cc.


Member Function Documentation

void LogCabin::Core::Config::readFile ( const string filename)

Load a Config from a file.

This is a convenience wrapper around operator>>.

Exceptions:
FileNotFound

Definition at line 155 of file Config.cc.

template<class T >
T LogCabin::Core::Config::read ( const string key) const

Read the value corresponding to a key.

Returns:
The desired value, converted to a T.
Exceptions:
KeyNotFoundIf the key does not exist.
ConversionErrorIf the value could not be converted to a T.

Definition at line 234 of file Config.h.

template<class T >
T LogCabin::Core::Config::read ( const string key,
const T &  value 
) const

Return the value corresponding to key or given default value if key is not found.

Exceptions:
ConversionErrorIf the value could not be converted to a T.

Definition at line 244 of file Config.h.

bool LogCabin::Core::Config::keyExists ( const string key) const

Check whether key exists in configuration.

Definition at line 230 of file Config.cc.

template<class T >
void LogCabin::Core::Config::set ( const string key,
const T &  value 
)

Set a key to the given value.

Definition at line 254 of file Config.h.

void LogCabin::Core::Config::set ( const string key,
const string value 
)

Set a key to the given string value.

Definition at line 236 of file Config.cc.

void LogCabin::Core::Config::remove ( const string key)

Remove a key and its value.

If the key does not exist, this will do nothing.

Definition at line 246 of file Config.cc.

template<class T >
std::string LogCabin::Core::Config::toString ( const T &  t) [static, private]

Convert from a T to a string.

Type T must support << operator.

Definition at line 203 of file Config.h.

bool LogCabin::Core::Config::fromString< bool > ( const string key,
const string s 
) [static, private]

Convert from a string to a T.

Type T must support >> operator.

For boolean conversions, "false", "f", "no", "n", "0" are false, and "true", "t", "yes", "y", "1" are true.

Exceptions:
ConversionError

Definition at line 264 of file Config.cc.

std::string LogCabin::Core::Config::readLine ( std::istream &  is) const [private]

Read a line, strip comments, and trim it.

Definition at line 296 of file Config.cc.


Friends And Related Function Documentation

std::istream& operator>> ( std::istream &  is,
Config cf 
) [friend]

Read configuration.

Definition at line 164 of file Config.cc.

std::ostream& operator<< ( std::ostream &  os,
const Config cf 
) [friend]

Write configuration.

Definition at line 219 of file Config.cc.


Member Data Documentation

Separator between key and value, usually "=".

Definition at line 192 of file Config.h.

Starts a comment, usually "#".

Definition at line 195 of file Config.h.

Extracted keys and values.

Definition at line 198 of file Config.h.


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines