11#ifndef __Configuration_h
12#define __Configuration_h
36 void load (
const std::string& filename);
45 template<
typename T> T
get (
const std::string& key, T default_value)
const;
48 template<
typename T> T
get (
const std::string& key)
const;
51 Entry*
find (
const std::string& key)
const;
54 const std::vector<std::string>&
get_filenames ()
const {
return filenames; }
64 std::vector<std::string> filenames;
65 std::vector<Entry> entries;
69 mutable unsigned find_count;
72class Configuration::Entry
75 Entry (std::string& k, std::string& v) { key=k; value=v; }
91 Parameter (
const T& val)
97 Parameter (
const std::string& _key,
Configuration* config, T default_value)
100 loader =
new DirectLoader (default_value);
101 loader->parameter =
this;
102 loader->configuration = config;
108 loader =
new DirectLoader;
109 loader->parameter =
this;
110 loader->configuration = config;
113 template<
typename Parser>
115 Parser* parser,
const std::string& default_value)
117 DEBUG(
"Configuration::Parameter<T> ctor parser=" << parser);
120 loader->parameter =
this;
121 loader->configuration = config;
169 template<
class P>
class ParseLoader;
171 std::string get_key ()
const {
return key; }
172 void set_key (
const std::string& _key) { key = _key;}
174 void set_loader (Loader* _load)
176 DEBUG(
"Configuration::Parameter::set_loader this=" <<
this \
177 <<
" key=" << key <<
" loader=" << _load);
182 loader->parameter =
this;
199 virtual void load () = 0;
203 parameter->loader = 0;
208 Parameter* parameter;
216 DirectLoader () { default_value_set =
false; }
218 DirectLoader (
const T& _default)
219 { default_value = _default; default_value_set =
true; }
224 Parameter<T>* param = this->parameter;
227 if (default_value_set)
229 param->value =
config->get (param->key, default_value);
231 DEBUG(
"Configuration::Parameter<T>::Loader::load key=" << param->key \
232 <<
" default=" << default_value <<
" value=" << param->value);
234 else if (
config->find (param->key))
236 param->value =
config->get<T> (param->key);
238 DEBUG(
"Configuration::Parameter<T>::Loader::load key=" << param->key \
239 <<
" value=" << param->value);
247 bool default_value_set;
256 ParseLoader (P* _parser,
const std::string& _default)
258 DEBUG(
"Configuration::Parameter<T>::ParseLoader ctor parser=" << _parser);
260 default_value = _default;
267 = this->configuration->get (this->parameter->key, default_value);
269 DEBUG(
"Configuration::Parameter<T>::ParseLoader::load" \
270 " parser=" << (
void*) this->parser.ptr() << \
271 " key=" << this->parameter->key << \
272 " default=" << this->default_value << \
275 parser->parse( value );
277 DEBUG(
"Configuration::Parameter<T>::ParseLoader::load parse method called");
285 std::string default_value;
292 Entry* entry =
find (key);
295 DEBUG(
"Configuration::get found entry->value=" << entry->value);
296 return fromstring<T> (entry->value);
299 DEBUG(
"Configuration::get default " << key <<
" = " << default_value);
301 return default_value;
308 Entry* entry =
find (key);
311 DEBUG(
"Configuration::get found entry->value=" << entry->value);
312 return fromstring<T> (entry->value);
315 throw Error (InvalidParam,
"Configuration::get",
"entry not found");
Uses an intermediate parser to load from configuration.
Definition Configuration.h:253
virtual void load()
Load the parameter value.
Definition Configuration.h:264
A configurable parameter.
Definition Configuration.h:83
void set_value(const T &val)
Set the value.
Definition Configuration.h:149
bool operator==(const T &t) const
Equality test operator.
Definition Configuration.h:160
T get_value() const
Get a copy of the value.
Definition Configuration.h:140
T & operator=(const T &t)
Set equal to T operator.
Definition Configuration.h:157
T get(const std::string &key, T default_value) const
Get the value for the specified key.
Definition Configuration.h:290
Entry * find(const std::string &key) const
Find the entry with the specified key.
Definition Configuration.C:82
unsigned get_find_count() const
Get the number of times that the find method has been called.
Definition Configuration.h:57
const std::vector< std::string > & get_filenames() const
Get the names of the configuration files in the order they were parsed.
Definition Configuration.h:54
Configuration(const char *filename=0)
Construct from the specified file.
Definition Configuration.C:19
virtual void load()
Allow derived types to implement lazy evaluation of file loading.
Definition Configuration.h:62
A convenient exception handling class.
Definition Error.h:54
Manages Reference::To references to the instance.
Definition ReferenceAble.h:35
Template class manages Reference::Able objects.
Definition ReferenceTo.h:25
int config()
Call this global method to ensure that configuration is loaded.