softwarecontainer  0.18.0-739e8d7 2017-05-04
config.h
1 
2 /*
3  * Copyright (C) 2016-2017 Pelagicore AB
4  *
5  * Permission to use, copy, modify, and/or distribute this software for
6  * any purpose with or without fee is hereby granted, provided that the
7  * above copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
10  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
11  * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
12  * BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
13  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
14  * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
15  * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
16  * SOFTWARE.
17  *
18  * For further information see LICENSE
19  */
20 
21 #pragma once
22 
23 #include "softwarecontainer-common.h"
24 #include "configloader.h"
25 #include "configsource.h"
26 #include "configdefinition.h"
27 
28 namespace softwarecontainer {
29 
30 
67 class Config
68 {
69 
70 LOG_DECLARE_CLASS_CONTEXT("CFG", "Config");
71 
72 public:
90  Config(std::vector<std::unique_ptr<ConfigSource>> sources,
91  MandatoryConfigs mandatory,
92  ConfigDependencies dependencies);
93 
94  ~Config() {}
95 
107  std::string getStringValue(const std::string &group, const std::string &key) const;
108 
120  int getIntValue(const std::string &group, const std::string &key) const;
121 
133  bool getBoolValue(const std::string &group, const std::string &key) const;
134 
135 private:
136  /*
137  * Goes through all dependency relations and makes sure all dependencies for any
138  * found dependee have been found in any of the sources.
139  */
140  void assertDependencies();
141 
142  /*
143  * Goes through all mandatory configs and make sure they have been found in any
144  * of the sources.
145  */
146  void assertAllMandatoryPresent();
147 
148  /*
149  * Get all config items from all sources. A call to this method populates
150  * m_stringConfigs, m_intConfigs, m_boolConfigs, and m_allConfigs
151  */
152  void readConfigsFromSources();
153 
154  /*
155  * Returns a config item from 'configs' that matches 'group' and 'key'
156  */
157  template<typename T>
158  T getConfig(const std::string &group,
159  const std::string &key,
160  const std::vector<T> &configs) const;
161 
162  /*
163  * Returns the config item from 'configs' that has the highest prioritized source
164  */
165  template<typename T>
166  T prioritizedConfig(const std::vector<T> &configs) const;
167 
168  /*
169  * Returns true is all config items in 'dependencies' have been found in the sources
170  */
171  bool allDepsSatisfied(const std::vector<UniqueKey> &dependencies) const;
172 
173  std::vector<std::unique_ptr<ConfigSource>> m_sources;
174  MandatoryConfigs m_mandatory;
175  ConfigDependencies m_dependencies;
176  std::vector<StringConfig> m_stringConfigs;
177  std::vector<IntConfig> m_intConfigs;
178  std::vector<BoolConfig> m_boolConfigs;
179  std::vector<UniqueKey> m_allConfigs;
180 };
181 
182 } // namespace softwarecontainer
bool getBoolValue(const std::string &group, const std::string &key) const
getBoolValue Get a config value of type bool
Definition: config.cpp:139
std::string getStringValue(const std::string &group, const std::string &key) const
getStringValue Get a config value of type string
Definition: config.cpp:129
int getIntValue(const std::string &group, const std::string &key) const
getIntValue Get a config value of type int
Definition: config.cpp:134
Developers guide to adding a config item:
Represents the configuration of SoftwareContainer (the component)
Definition: config.h:67
Config(std::vector< std::unique_ptr< ConfigSource >> sources, MandatoryConfigs mandatory, ConfigDependencies dependencies)
Constructor - retrieves all configs from the available sources.
Definition: config.cpp:29