-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathconfig_manager.hpp
More file actions
188 lines (161 loc) · 4.81 KB
/
config_manager.hpp
File metadata and controls
188 lines (161 loc) · 4.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#pragma once
#include "fly/concepts/concepts.hpp"
#include "fly/config/config.hpp"
#include "fly/logger/logger.hpp"
#include "fly/types/json/json.hpp"
#include "fly/types/string/formatters.hpp"
#include <cstdint>
#include <filesystem>
#include <map>
#include <memory>
#include <mutex>
#include <string>
#include <type_traits>
namespace fly::parser {
class Parser;
} // namespace fly::parser
namespace fly::path {
class PathMonitor;
} // namespace fly::path
namespace fly::task {
class SequencedTaskRunner;
} // namespace fly::task
namespace fly::config {
/**
* Enumerated list of supported configuration file formats.
*/
enum class ConfigFileType : std::uint8_t
{
Ini,
Json,
};
/**
* Class to create and manage a set of configurations.
*
* @author Timothy Flynn (trflynn89@pm.me)
* @version July 21, 2016
*/
class ConfigManager : public std::enable_shared_from_this<ConfigManager>
{
public:
/**
* Map of configuration group names to configuration objects.
*/
using ConfigMap = std::map<std::string, std::weak_ptr<Config>>;
/**
* Create and start a configuration manager.
*
* @param task_runner Task runner for posting config-related tasks onto.
* @param file_type File format of the configuration file.
* @param path Path to the configuration file.
*
* @return The created configuration manager.
*/
static std::shared_ptr<ConfigManager> create(
std::shared_ptr<fly::task::SequencedTaskRunner> task_runner,
ConfigFileType file_type,
std::filesystem::path path);
/**
* Destructor. Stop the configuration manager and underlying objects.
*/
~ConfigManager();
/**
* Create a configuration object, or if one with the given type's name exists, fetch it.
*
* @tparam T Config type (must derive from or be fly::config::Config).
*
* @return A reference to the created/found configuration.
*/
template <fly::DerivedFrom<Config> T>
std::shared_ptr<T> create_config();
/**
* Erase any expired configuration objects.
*
* @return The remaining number of configurations.
*/
ConfigMap::size_type prune();
private:
/**
* Constructor.
*
* @param task_runner Task runner for posting config-related tasks onto.
* @param file_type File format of the configuration file.
* @param path Path to the configuration file.
*/
ConfigManager(
std::shared_ptr<fly::task::SequencedTaskRunner> task_runner,
ConfigFileType file_type,
std::filesystem::path path) noexcept;
/**
* Start the configuration manager and underlying objects.
*
* @return True if the manager could be started.
*/
bool start();
/**
* Parse the configuration file and store the parsed values in memory.
*/
void update_config();
std::shared_ptr<fly::task::SequencedTaskRunner> m_task_runner;
std::shared_ptr<fly::path::PathMonitor> m_monitor;
std::unique_ptr<fly::parser::Parser> m_parser;
fly::Json m_values;
std::filesystem::path const m_path;
mutable std::mutex m_configs_mutex;
ConfigMap m_configs;
};
//==================================================================================================
template <fly::DerivedFrom<Config> T>
std::shared_ptr<T> ConfigManager::create_config()
{
std::shared_ptr<T> config;
std::lock_guard<std::mutex> lock(m_configs_mutex);
ConfigMap::const_iterator it = m_configs.find(T::identifier);
if (it == m_configs.end())
{
config = std::make_shared<T>();
m_configs[T::identifier] = config;
}
else
{
std::shared_ptr<Config> base_config = it->second.lock();
if (base_config)
{
config = std::dynamic_pointer_cast<T>(base_config);
}
else
{
config = std::make_shared<T>();
m_configs[T::identifier] = config;
}
}
if (config)
{
config->update(m_values[T::identifier]);
}
else
{
LOGW("Could not create configuration for type {}", T::identifier);
}
return config;
}
} // namespace fly::config
//==================================================================================================
template <>
struct fly::string::Formatter<fly::config::ConfigFileType> :
public fly::string::Formatter<std::uint8_t>
{
/**
* Format a configuration file type.
*
* @tparam FormatContext The type of the formatting context.
*
* @param file_type The configuration file type to format.
* @param context The context holding the formatting state.
*/
template <typename FormatContext>
void format(fly::config::ConfigFileType file_type, FormatContext &context)
{
fly::string::Formatter<std::uint8_t>::format(static_cast<std::uint8_t>(file_type), context);
}
};