-
Notifications
You must be signed in to change notification settings - Fork 2
/
Config.h
132 lines (114 loc) · 2.79 KB
/
Config.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
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
/*
* Config.h
*
* Created on: Jul 16, 2013
* Author: sam
*/
#ifndef CONFIG_H_
#define CONFIG_H_
#if defined(ENERGIA)
#define EMBEDDED_MODE
#endif
#if defined(EMBEDDED_MODE)
#else
#include <map>
#include <iostream>
#include <sstream>
class Config
{
private:
typedef std::map<std::string, std::string> Values;
Values values;
std::string name;
std::string path;
bool modified;
bool persistenceMode;
public:
Config();
Config(const std::string& name, const std::string& path);
~Config();
private:
template<class T>
T getCfgValue(const std::string& name, T value)
{
Values::iterator iter = values.find(name);
std::string cfgValue;
if (iter != values.end())
{
cfgValue = iter->second;
std::stringstream ss(cfgValue);
ss >> value;
return value;
}
else
{
// New value has been found
modified = true;
std::stringstream ss;
ss << value;
values.insert(std::make_pair((std::string) name, ss.str()));
}
return value;
}
template<class T>
void setCfgValue(const std::string& name, T value)
{
Values::iterator iter = values.find(name);
if (iter != values.end())
{
values.erase(iter);
}
modified = true;
std::stringstream ss;
ss << value;
values.insert(std::make_pair((std::string) name, ss.str()));
}
public:
bool getValue(const std::string& name, const bool& defaultValue)
{
return getCfgValue(name, defaultValue);
}
int getValue(const std::string& name, const int& defaultValue)
{
return getCfgValue(name, defaultValue);
}
double getValue(const std::string& name, const double& defaultValue)
{
return getCfgValue(name, defaultValue);
}
std::string getValue(const std::string& name, const std::string& defaultValue)
{
return getCfgValue(name, defaultValue);
}
void setValue(const std::string& name, const bool& value)
{
setCfgValue(name, value);
}
void setValue(const std::string& name, const int& value)
{
setCfgValue(name, value);
}
void setValue(const std::string& name, const long& value)
{
setCfgValue(name, value);
}
void setValue(const std::string& name, const float& value)
{
setCfgValue(name, value);
}
void setValue(const std::string& name, const double& value)
{
setCfgValue(name, value);
}
void setValue(const std::string& name, const std::string& value)
{
setCfgValue(name, value);
}
void setName(const std::string& name);
void setPath(const std::string& path);
void persist();
void resurrect();
void setPersist(const bool& persistenceMode);
};
#endif
#endif /* CONFIG_H_ */