gramods
Classes | Public Member Functions | List of all members
gramods::gmCore::Configuration Class Reference

A wrapper for the XML parser that also creates and configures the system objects and holds temporary references to them for easy retrieval. More...

#include <Configuration.hh>

Public Member Functions

 Configuration ()
 Creates an empty configuration. More...
 
 Configuration (int &argc, char *argv[], std::vector< std::string > *error_list=nullptr)
 Consumes arguments –config <file>, –xml <string> and –param <identifier>=<value> from the provided command line. More...
 
 Configuration (std::string config, std::vector< std::string > *error_list=nullptr)
 Loads an XML string, create objects as specified by the XML data and configure the objects. More...
 
 Configuration (tinyxml2::XMLNode *node, std::vector< std::string > *error_list=nullptr)
 Read the XML data, create objects as specified by the XML data and configure the objects. More...
 
 ~Configuration ()
 Cleans up and checks that all configuration variables have been read. More...
 
bool hasParam (std::string name)
 Check if a specific parameter is available. More...
 
std::size_t getAllParamNames (std::vector< std::string > &name)
 Adds all currently available parameter names to the specified vector and returns the count. More...
 
std::size_t getAllObjectKeys (std::vector< std::string > &name)
 Adds all currently available object's keys to the specified vector and returns the count. More...
 
void addParam (std::string name, std::string value)
 Add a parameter value. More...
 
template<class T >
bool getParam (std::string name, T &value) const
 Retrieves a named parameter as a specified type. More...
 
template<class T >
std::size_t getAllParams (std::string name, std::vector< T > &value) const
 Retrieves all parameters with a specified name. More...
 
template<class T >
bool getObjectByKey (std::string key, std::shared_ptr< T > &ptr) const
 Retrieve an object by its key. More...
 
template<class T >
bool getObjectByDef (std::string def, std::shared_ptr< T > &ptr) const
 Retrieve an object by its DEF name. More...
 
template<class T >
bool getObject (std::shared_ptr< T > &ptr) const
 Retrieve an object by its type. More...
 
template<class T >
void addObject (std::shared_ptr< T > ptr, std::string key, std::string def)
 Set an object. More...
 
template<class T >
std::size_t getAllObjects (std::vector< std::shared_ptr< T >> &value) const
 Retrieve all objects of a specified type. More...
 
template<class T >
std::size_t getAllObjectsByKey (std::string key, std::vector< std::shared_ptr< T >> &ptr) const
 Retrieve all objects with the specified key. More...
 
template<>
bool getParam (std::string name, std::string &value) const
 
template<>
bool getParam (std::string name, bool &value) const
 
template<>
std::size_t getAllParams (std::string name, std::vector< std::string > &value) const
 
template<>
std::size_t getAllParams (std::string name, std::vector< bool > &value) const
 

Detailed Description

A wrapper for the XML parser that also creates and configures the system objects and holds temporary references to them for easy retrieval.

This unit will build an internal tree structure of objects that can be easily retrieved by name or by type, and lists of parameters for each object.

Typical usage:

int width = DEFAULT_WIDTH;
config->getParam("width", width);

In XML every node accepts attributes KEY, for specifying container, DEF, for specifying handle, and USE, for reusing node with specified handle.

Constructor & Destructor Documentation

◆ Configuration() [1/4]

gramods::gmCore::Configuration::Configuration ( )

Creates an empty configuration.

◆ Configuration() [2/4]

gramods::gmCore::Configuration::Configuration ( int &  argc,
char *  argv[],
std::vector< std::string > *  error_list = nullptr 
)

Consumes arguments –config <file>, –xml <string> and –param <identifier>=<value> from the provided command line.

Either –config or –xml is required, upon which the specified file or XML string will be parsed, or gmCore::InvalidArgument will be thrown. Command line argument –param <identifier>=<value> will also override configuration file parameters. For example –param head.connectionString=WAND@localhost

◆ Configuration() [3/4]

gramods::gmCore::Configuration::Configuration ( std::string  config,
std::vector< std::string > *  error_list = nullptr 
)

Loads an XML string, create objects as specified by the XML data and configure the objects.

◆ Configuration() [4/4]

gramods::gmCore::Configuration::Configuration ( tinyxml2::XMLNode *  node,
std::vector< std::string > *  error_list = nullptr 
)

Read the XML data, create objects as specified by the XML data and configure the objects.

◆ ~Configuration()

gramods::gmCore::Configuration::~Configuration ( )

Cleans up and checks that all configuration variables have been read.

Member Function Documentation

◆ addObject()

template<class T >
void gramods::gmCore::Configuration::addObject ( std::shared_ptr< T >  ptr,
std::string  key,
std::string  def 
)

Set an object.

◆ addParam()

void gramods::gmCore::Configuration::addParam ( std::string  name,
std::string  value 
)

Add a parameter value.

◆ getAllObjectKeys()

size_t gramods::gmCore::Configuration::getAllObjectKeys ( std::vector< std::string > &  name)

Adds all currently available object's keys to the specified vector and returns the count.

Keys used for multiple objects will only appear once and be counted once.

◆ getAllObjects()

template<class T >
std::size_t gramods::gmCore::Configuration::getAllObjects ( std::vector< std::shared_ptr< T >> &  value) const

Retrieve all objects of a specified type.

This function will append the retrieved objects to the specified list. Use gmCore::Object as type to retrieve all objects.

Parameters
[out]valuea vector of pointer to fill with objects
Returns
the number of objects added to the list.

◆ getAllObjectsByKey()

template<class T >
std::size_t gramods::gmCore::Configuration::getAllObjectsByKey ( std::string  key,
std::vector< std::shared_ptr< T >> &  ptr 
) const

Retrieve all objects with the specified key.

Parameters
[in]keyThe key of the objects to retrieve
[out]ptrA pointer to the retrieved object, if set
Returns
False if no object was found by that name or if it could not be casted to the specified type.

◆ getAllParamNames()

size_t gramods::gmCore::Configuration::getAllParamNames ( std::vector< std::string > &  name)

Adds all currently available parameter names to the specified vector and returns the count.

◆ getAllParams()

template<class T >
std::size_t gramods::gmCore::Configuration::getAllParams ( std::string  name,
std::vector< T > &  value 
) const
inline

Retrieves all parameters with a specified name.

If the value type is not string, then this function will use stringstream to parse most types but will need specializations to parse non-standard types.

◆ getObject()

template<class T >
bool gramods::gmCore::Configuration::getObject ( std::shared_ptr< T > &  ptr) const

Retrieve an object by its type.

If there are multiple objects of the same type, then the first object will be retrieved with this function.

Parameters
[out]ptrA pointer to the retrieved object, if set
Returns
False if no object was found by that name or if it could not be casted to the specified type.

◆ getObjectByDef()

template<class T >
bool gramods::gmCore::Configuration::getObjectByDef ( std::string  def,
std::shared_ptr< T > &  ptr 
) const

Retrieve an object by its DEF name.

Parameters
[in]defThe DEF name of the object to retrieve
[out]ptrA pointer to the object, if set
Returns
False if no object was found by that name or if it could not be casted to the specified type.

◆ getObjectByKey()

template<class T >
bool gramods::gmCore::Configuration::getObjectByKey ( std::string  key,
std::shared_ptr< T > &  ptr 
) const

Retrieve an object by its key.

If there are multiple objects of the same type, then the first object will be retrieved with this function.

Parameters
[in]keyThe key of the object to retrieve
[out]ptrA pointer to the object, if set
Returns
False if no object was found by that name or if it could not be casted to the specified type.

◆ getParam()

template<class T >
bool gramods::gmCore::Configuration::getParam ( std::string  name,
T &  value 
) const

Retrieves a named parameter as a specified type.

If there are multiple parameters with the same name, there is no saying which will be retrieved with this function.

If the value type is not string, then this function will use stringstream to parse most types but will need specializations to parse non-standard types. If the parameter is not found, the value argument will not be changed.

Parameters
[in]namename of the parameter to retrieve
[out]valuethe parameter value, if set
Returns
false if the parameter is not set or if the value cannot be casted to the specified type.

◆ hasParam()

bool gramods::gmCore::Configuration::hasParam ( std::string  name)

Check if a specific parameter is available.


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