Swarm-NG  1.1
swarm::config Class Reference

Basic dictionary like data structure to hold configuration. More...

#include <config.hpp>

Inheritance diagram for swarm::config:

Public Member Functions

template<class T >
 config (const T &key_value_pairs)
 Simple constructur for inline creation of config objects form 2D arrays of strings. More...
 
void initialize (const char *const key_value_pairs[][2], const int n)
 Initialize the configuration from key_value_pairs. More...
 
bool valid (const std::string &key) const
 Query if the value for specified key exists and is a valid entry.
 
template<typename T >
bool valid (const std::string &key, T t) const
 Query if the value for specified key exists and is a valid value of type T. More...
 
template<typename T >
optional (const std::string &key, const T &default_value) const
 Value of an optional parameter key. More...
 
template<typename T >
require (const std::string &key, const T &=T()) const
 Value of a mandatory parameter key. More...
 

Static Public Member Functions

static config load (const std::string &fn, config cfg=config())
 Load a new configuration from a text file and merge it with cfg. More...
 
static bool valid_value (const std::string &v, std::string)
 Is v a valid string.
 
static std::string parse (const std::string &v, std::string)
 Parse a string.
 
static bool valid_value (const std::string &v, double)
 Is v a valid double.
 
static double parse (const std::string &v, double)
 Parse a double value.
 
static bool valid_value (const std::string &v, float)
 Is v a valid float.
 
static float parse (const std::string &v, float)
 Parse a float value.
 
static bool valid_value (const std::string &v, int)
 Is v a valid integer.
 
static int parse (const std::string &v, int)
 Parse an integer.
 

Additional Inherited Members

- Public Attributes inherited from std::map< K, T >
keys
 STL member.
 
elements
 STL member.
 

Detailed Description

Basic dictionary like data structure to hold configuration.

It is basically a standard C++ map with enhanced accessors. This features inline initialization, loading from a text file and automatic parsers to floating and integer types. In addition to specific accessors for optional and required parameters.

It is sometimes used in the code to simulate named parameter calling convention.

Definition at line 44 of file config.hpp.

Constructor & Destructor Documentation

template<class T >
swarm::config::config ( const T &  key_value_pairs)
inline

Simple constructur for inline creation of config objects form 2D arrays of strings.

Example:
const char* pairs[][2] = { { "nsys" , "8000" }, { "nbod", "4" } };
config inline_configuration( pairs );
\args key_value_pairs 2D array of key-value pairs for initializing the config object

Definition at line 60 of file config.hpp.

References initialize().

Member Function Documentation

void swarm::config::initialize ( const char *const  key_value_pairs[][2],
const int  n 
)

Initialize the configuration from key_value_pairs.

Initialize the config objects.

Parameters
[in]key_value_pairs2D array of key_value_pairs. key_value_pairs[i][0] is a key and key_value_pairs[i][1] is the corresponding value.
[in]nnumber of items in key_value_pairs array.

Definition at line 35 of file config.cpp.

Referenced by config().

config swarm::config::load ( const std::string &  fn,
config  cfg = config() 
)
static

Load a new configuration from a text file and merge it with cfg.

Load the configuration file and check for format errors.

Syntax:

key=value
key2=value2
.
.
.
Parameters
[out]cfgconfiguration class
[in]fnfile name sting

Definition at line 43 of file config.cpp.

References peyton::util::trim().

Referenced by main().

template<typename T >
T swarm::config::require ( const std::string &  key,
const T &  = T() 
) const
inline

Value of a mandatory parameter key.

If the key does not exists or the value is not a valid value for type T an error is raised. Otherwise text is parsed as a value of type T and is returned This function can be used in two ways. For retrieving the double value of a key "number" you can use either alternatives:

  • c.require("key", 0.0)
  • c.require<double>("key")
Parameters
[in]keyName of the key to retrieve
[in]tOptional value just for template matching.

Definition at line 181 of file config.hpp.

References parse(), and valid_value().

Referenced by swarm::log::bdb_writer::bdb_writer(), swarm::gpu::bppt::EulerPropagatorParams::EulerPropagatorParams(), generate_ensemble(), generate_ensemble_with_initial_conditions_cartesian_from_file(), generate_ensemble_with_initial_conditions_keplerian_from_file(), generate_ensemble_with_randomized_initial_conditions(), swarm::gpu::bppt::hermite< Monitor, Gravitation >::hermite(), swarm::gpu::bppt::hermite_adap< Monitor, Gravitation >::hermite_adap(), swarm::cpu::hermite_cpu< Monitor >::hermite_cpu(), swarm::gpu::bppt::HermitePropagatorParams::HermitePropagatorParams(), swarm::log::host_array_writer::host_array_writer(), swarm::monitors::log_time_interval_params::log_time_interval_params(), swarm::monitors::log_transit_params::log_transit_params(), swarm::gpu::bppt::MidpointPropagatorParams::MidpointPropagatorParams(), swarm::cpu::mvs_cpu< Monitor >::mvs_cpu(), swarm::gpu::bppt::MVSPropagatorParams::MVSPropagatorParams(), TutorialIntegrator< Monitor, Gravitation >::TutorialIntegrator(), and swarm::gpu::bppt::VerletPropagatorParams::VerletPropagatorParams().

template<typename T >
bool swarm::config::valid ( const std::string &  key,
t 
) const
inline

Query if the value for specified key exists and is a valid value of type T.

Example: vaild("log interval", 0.0) makes sure that "log interval" exists and is a valid double value ( same type as 0.0 )

Definition at line 148 of file config.hpp.

References valid(), and valid_value().


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