-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfiguration.h
60 lines (54 loc) · 1.46 KB
/
Configuration.h
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
/*
* Configuration.h
*
* Created on: Jul 11, 2016
* Author: mlaakso
*/
#ifndef CONFIGURATION_H_
#define CONFIGURATION_H_
#include <string>
#include <utility>
#include <vector>
#include <boost/property_tree/json_parser.hpp>
#include <boost/property_tree/ptree.hpp>
namespace PowerMonitor
{
class Configuration
{
public:
//! Creates a new configuration object.
//! \param configFile path to configuration file to read settings from.
Configuration(const std::string& configFile)
{
boost::property_tree::read_json(configFile, _tree);
}
//! Gets a configuration option.
//! \param option option name.
//! \return Option value.
template<class T> T get(const std::string& option)
{
return _tree.get<T>(option);
}
//! Gets a configuration option.
//! \param option option name.
//! \param def default value to return if option is not set.
//! \return Option value.
template<class T> T get(const std::string& option, T def)
{
return _tree.get(option, def);
}
//! Gets a configuration option array.
//! \param option option name.
//! \return Option values as vector.
template<class T> std::vector<T> getArray(const std::string& option)
{
std::vector<T> r;
for (auto&& item : _tree.get_child(option))
r.push_back(item.second.get_value<T>());
return r;
}
private:
boost::property_tree::ptree _tree;
};
}
#endif /* CONFIGURATION_H_ */