softwarecontainer  0.18.0-739e8d7 2017-05-04
softwarecontainer::Config Class Reference

Represents the configuration of SoftwareContainer (the component) More...

#include <config.h>

Public Member Functions

 Config (std::vector< std::unique_ptr< ConfigSource >> sources, MandatoryConfigs mandatory, ConfigDependencies dependencies)
 Constructor - retrieves all configs from the available sources. More...
 
std::string getStringValue (const std::string &group, const std::string &key) const
 getStringValue Get a config value of type string More...
 
int getIntValue (const std::string &group, const std::string &key) const
 getIntValue Get a config value of type int More...
 
bool getBoolValue (const std::string &group, const std::string &key) const
 getBoolValue Get a config value of type bool More...
 

Detailed Description

Represents the configuration of SoftwareContainer (the component)

Config gathers all config items present from config sources, and provides the values for these configs when asked. Config handles logic for what is to be considered mandatory or optional, and dependencies between configs.

Config reads all config items from the sources on creation, and stores them for retrieval when users of Config asks for a config value.

Config item uniqueness is based on the combination of config group and config key. Therefore, it is OK to have the same key in multiple groups, but a key must be unique within a group.

Some configs are optional and some are mandatory. A config can become mandatory if some other specified config depends on it. E.g. if an optional config in the main config file is uncommented and thus used, it might mean that other configs are mandatory as a consequence.

It is an error if a mandatory config can not be found or if one or more dependencies to a config can not be found.

There are three different config sources:

  • Command line options
  • Main config file
  • Defaults

The config sources as considered in the above order, i.e. first command line options, then the main config file etc. If a config was retrieved from more than one source, i.e. duplicates, only the value of the highest prioritized source will be returned, and the other(s) ignored.

It is an error if a config requested by the user of Config is not found, even if the config is optional.

Definition at line 67 of file config.h.

Constructor & Destructor Documentation

softwarecontainer::Config::Config ( std::vector< std::unique_ptr< ConfigSource >>  sources,
MandatoryConfigs  mandatory,
ConfigDependencies  dependencies 
)

Constructor - retrieves all configs from the available sources.

This constructor retrieves all ConfigItem objects from the available config sources, and keeps them for later access by users of Config.

Parameters
sourcesA list of config sources implementing the ConfigSource interface
mandatoryConfigs that are to be considered mandatory
dependenciesDependee configs and their respective dependencies
Exceptions
ConfigMandatoryErrorIf any mandatory config is missing, i.e. it is not returned by any config source.
ConfigDependencyErrorIf one of more dependencies are not met, i.e. there is a config present that has dependencies and all of those dependencies could not be found.

Definition at line 29 of file config.cpp.

31  :
32  m_sources(std::move(sources)),
33  m_mandatory(mandatory),
34  m_dependencies(dependencies),
35  m_stringConfigs(std::vector<StringConfig>()),
36  m_intConfigs(std::vector<IntConfig>()),
37  m_boolConfigs(std::vector<BoolConfig>()),
38  m_allConfigs(std::vector<UniqueKey>())
39 {
40  /*
41  * The order of the calls below is important since some methods modifies members that are
42  * needed in subsequent method calls.
43  */
44 
45  // Read all ConfigItems from all sources and store them
46  readConfigsFromSources();
47 
48  // If any ConfigItem has one or more dependencies which are not present, it is a fatal error
49  assertDependencies();
50 
51  // If any group-key combo is mandatory and not present, it is a fatal error
52  assertAllMandatoryPresent();
53 }

Member Function Documentation

std::string softwarecontainer::Config::getStringValue ( const std::string &  group,
const std::string &  key 
) const

getStringValue Get a config value of type string

Parameters
groupThe name of the config group the value belongs to
keyThe key for the config value
Returns
A string with the config value
Exceptions
ConfigNotFoundErrorIf the config identified with 'group' and 'key' couldn't be found

Definition at line 129 of file config.cpp.

130 {
131  return getConfig(group, key, m_stringConfigs).value();
132 }
int softwarecontainer::Config::getIntValue ( const std::string &  group,
const std::string &  key 
) const

getIntValue Get a config value of type int

Parameters
groupThe name of the config group the value belongs to
keyThe key for the config value
Returns
An int with the config value
Exceptions
ConfigNotFoundErrorIf the config identified with 'group' and 'key' couldn't be found

Definition at line 134 of file config.cpp.

135 {
136  return getConfig(group, key, m_intConfigs).value();
137 }
bool softwarecontainer::Config::getBoolValue ( const std::string &  group,
const std::string &  key 
) const

getBoolValue Get a config value of type bool

Parameters
groupThe name of the config group the value belongs to
keyThe key for the config value
Returns
A bool with the config value
Exceptions
ConfigNotFoundErrorIf the config identified with 'group' and 'key' couldn't be found

Definition at line 139 of file config.cpp.

140 {
141  return getConfig(group, key, m_boolConfigs).value();
142 }

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