softwarecontainer  0.18.0-739e8d7 2017-05-04
mainconfigsource.h
1 /*
2  * Copyright (C) 2016-2017 Pelagicore AB
3  *
4  * Permission to use, copy, modify, and/or distribute this software for
5  * any purpose with or without fee is hereby granted, provided that the
6  * above copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
9  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
10  * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
11  * BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
12  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
13  * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
14  * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
15  * SOFTWARE.
16  *
17  * For further information see LICENSE
18  */
19 
20 #pragma once
21 
22 #include "softwarecontainer-common.h"
23 #include "configloader.h"
24 #include "configtypes.h"
25 #include "configsource.h"
26 #include "configdefinition.h"
27 
28 
29 namespace softwarecontainer {
30 
32 {
33 
34 LOG_DECLARE_CLASS_CONTEXT("CFGM", "Main config source");
35 
36 public:
43  MainConfigSource(std::unique_ptr<ConfigLoader> loader, TypeMap typeMapping);
44 
45  ~MainConfigSource() {}
46 
47  std::vector<StringConfig> stringConfigs() override;
48  std::vector<IntConfig> intConfigs() override;
49  std::vector<BoolConfig> boolConfigs() override;
50 
51 private:
52  /*
53  * Can throw ConfigFileError if the underlying Glib load operation fails
54  */
55  void loadConfig();
56 
57  /*
58  * Can throw ConfigFileError is the underlying Glib read operation fails
59  */
60  void readLoadedConfig();
61 
62  void createConfigItems();
63 
64  /*
65  * Can throw ConfigUnknownError if any config item found in the loaded config
66  * does not match what is defined in ConfigDefinition
67  */
68  ConfigType typeOf(const std::string &group, const std::string &key);
69 
70  void create(const std::string &group, const std::string &key, ConfigType type);
71 
72  template<typename T, typename U>
73  T createConfig(const std::string &group, const std::string &name);
74 
75  /*
76  * Can throw ConfigFileError if a config item can not be parsed
77  */
78  template<typename T>
79  T getGlibValue(const std::string &group, const std::string &key) const;
80 
81  std::unique_ptr<ConfigLoader> m_loader;
82  TypeMap m_typeMapping;
83  std::unique_ptr<Glib::KeyFile> m_config;
84  std::map<std::string, std::vector<std::string>> m_configsByGroup;
85  std::vector<StringConfig> m_stringConfigs;
86  std::vector<IntConfig> m_intConfigs;
87  std::vector<BoolConfig> m_boolConfigs;
88 };
89 
90 } // namespace softwarecontainer
ConfigType
Represents the type of a config value.
Definition: configtypes.h:32
Base class for all config sources.
Definition: configsource.h:32
MainConfigSource(std::unique_ptr< ConfigLoader > loader, TypeMap typeMapping)
Developers guide to adding a config item: