-
Notifications
You must be signed in to change notification settings - Fork 3
/
configurable.h
91 lines (67 loc) · 1.83 KB
/
configurable.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
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
#ifndef LIBRESPONSE_CONFIGURABLE_H_
#define LIBRESPONSE_CONFIGURABLE_H_
/*!
* @file
*
* Inheritable configuration by wrapping a string->string map.
*/
#include <cstdlib>
#include <map>
#include <sstream>
#include <string>
#include <stdexcept>
typedef std::map< std::string, std::string > ssmap;
typedef ssmap::const_iterator ssmap_it_type;
namespace libresponse {
/*!
* Inheritable configuration by wrapping a string->string map.
*/
class configurable {
private:
ssmap m_params; //!< Key-value parameters
std::string m_empty; //!< Empty value
public:
/*!
* Default constructor
*/
configurable() { }
const std::string &get_param(const std::string &key) const;
void cfg(const std::string &key, const std::string &val);
template<typename T>
void get_param(const std::string &key, T &val) const;
template<typename T>
T get_param(const std::string &key) const;
template<typename T>
void cfg(const std::string &key, const T &val);
/*!
* Print all key-value pairs to stdout
*/
void dump_params() const;
bool has_param(const std::string &key) const;
};
template<typename T>
void configurable::cfg(const std::string &key, const T &val) {
std::ostringstream ss;
ss << val;
cfg(key, ss.str());
}
template<typename T>
void configurable::get_param(const std::string &key, T &val) const {
const std::string &par = get_param(key);
if (!par.empty()) {
std::stringstream ss;
ss << par; ss >> val;
}
}
template<typename T>
T configurable::get_param(const std::string &key) const {
if(m_params.count(key) == 0) {
throw std::logic_error(
"configurable::get_param(): no parameter with given name: " + key);
}
T t;
get_param(key, t);
return t;
}
} // namespace libresponse
#endif // LIBRESPONSE_CONFIGURABLE_H_