-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.h
377 lines (344 loc) · 9.29 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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#ifndef __SYLAR_CONFIG_H__
#define __SYLAR_CONFIG_H__
#include <memory.h>
#include <sstream>
#include <boost/lexical_cast.hpp>
#include <string>
#include <list>
#include <map>
#include <vector>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <functional>
#include <yaml-cpp/yaml.h>
#include "log.h"
#include "thread.h"
#include "util.h"
namespace sylar{
class ConfigVarBase {
public:
typedef std::shared_ptr<ConfigVarBase> ptr;
ConfigVarBase(const std::string name, const std::string& description)
:m_name(name)
,m_description(description) {
}
virtual ~ConfigVarBase() {}
const std::string& GetName() { return m_name; }
const std::string& GetDescription() { return m_description; }
virtual std::string toString() = 0;
virtual bool fromString(const std::string& val) = 0;
virtual std::string getTypeid() const = 0;
protected:
std::string m_name;
std::string m_description;
};
//F from_type T to_type
template <class F, class T>
class LexicalCast {
public:
T operator()(const F& v) {
return boost::lexical_cast<T>(v);
}
};
template<class T>
class LexicalCast<std::string, std::vector<T> > {
public:
std::vector<T> operator()(const std::string& v) {
YAML::Node node = YAML::Load(v);
typename std::vector<T> vec;
std::stringstream ss;
for(size_t i = 0; i < node.size(); ++i) {
ss.str("");
ss << node[i];
vec.push_back(LexicalCast<std::string, T>()(ss.str()));
}
return vec;
}
};
template<class T>
class LexicalCast<std::vector<T>, std::string> {
public:
std::string operator()(const std::vector<T>& vec) {
YAML::Node node;
for(auto& i : vec) {
node.push_back(YAML::Load(LexicalCast<T, std::string>()(i)));
}
std::stringstream ss;
ss << node;
return ss.str();
}
};
template<class T>
class LexicalCast<std::string, std::list<T> > {
public:
std::list<T> operator()(const std::string& v) {
YAML::Node node = YAML::Load(v);
typename std::list<T> vec;
std::stringstream ss;
for(size_t i = 0; i < node.size(); ++i) {
ss.str("");
ss << node[i];
vec.push_back(LexicalCast<std::string, T>()(ss.str()));
}
return vec;
}
};
template<class T>
class LexicalCast<std::list<T>, std::string> {
public:
std::string operator()(const std::list<T>& vec) {
YAML::Node node;
for(auto& i : vec) {
node.push_back(YAML::Load(LexicalCast<T, std::string>()(i)));
}
std::stringstream ss;
ss << node;
return ss.str();
}
};
template<class T>
class LexicalCast<std::string, std::set<T> > {
public:
std::set<T> operator()(const std::string& v) {
YAML::Node node = YAML::Load(v);
typename std::set<T> vec;
std::stringstream ss;
for(size_t i = 0; i < node.size(); ++i) {
ss.str("");
ss << node[i];
vec.insert(LexicalCast<std::string, T>()(ss.str()));
}
return vec;
}
};
template<class T>
class LexicalCast<std::set<T>, std::string> {
public:
std::string operator()(const std::set<T>& vec) {
YAML::Node node;
for(auto& i : vec) {
node.push_back(YAML::Load(LexicalCast<T, std::string>()(i)));
}
std::stringstream ss;
ss << node;
return ss.str();
}
};
template<class T>
class LexicalCast<std::string, std::unordered_set<T> > {
public:
std::unordered_set<T> operator()(const std::string& v) {
YAML::Node node = YAML::Load(v);
typename std::unordered_set<T> vec;
std::stringstream ss;
for(size_t i = 0; i < node.size(); ++i) {
ss.str("");
ss << node[i];
vec.insert(LexicalCast<std::string, T>()(ss.str()));
}
return vec;
}
};
template<class T>
class LexicalCast<std::unordered_set<T>, std::string> {
public:
std::string operator()(const std::unordered_set<T>& vec) {
YAML::Node node;
for(auto& i : vec) {
node.push_back(YAML::Load(LexicalCast<T, std::string>()(i)));
}
std::stringstream ss;
ss << node;
return ss.str();
}
};
template<class T>
class LexicalCast<std::string, std::map<std::string, T> > {
public:
std::map<std::string, T> operator()(const std::string& v) {
YAML::Node node = YAML::Load(v);
typename std::map<std::string, T> vec;
std::stringstream ss;
for(auto it = node.begin();
it != node.end(); ++it) {
ss.str("");
ss << it->second;
vec.insert(std::make_pair(it->first.Scalar(),
LexicalCast<std::string, T>()(ss.str())));
}
return vec;
}
};
template<class T>
class LexicalCast<std::map<std::string, T>, std::string> {
public:
std::string operator()(const std::map<std::string, T>& vec) {
YAML::Node node;
for(auto& i : vec) {
node[i.first] = (YAML::Load(LexicalCast<T, std::string>()(i.second)));
}
std::stringstream ss;
ss << node;
return ss.str();
}
};
template<class T>
class LexicalCast<std::string, std::unordered_map<std::string, T> > {
public:
std::unordered_map<std::string, T> operator()(const std::string& v) {
YAML::Node node = YAML::Load(v);
typename std::unordered_map<std::string, T> vec;
std::stringstream ss;
for(auto it = node.begin();
it != node.end(); ++it) {
ss.str("");
ss << it->second;
vec.insert(std::make_pair(it->first.Scalar(),
LexicalCast<std::string, T>()(ss.str())));
}
return vec;
}
};
template<class T>
class LexicalCast<std::unordered_map<std::string, T>, std::string> {
public:
std::string operator()(const std::unordered_map<std::string, T>& vec) {
YAML::Node node;
for(auto& i : vec) {
node[i.first] = (YAML::Load(LexicalCast<T, std::string>()(i.second)));
}
std::stringstream ss;
ss << node;
return ss.str();
}
};
template<class T, class FromStr = LexicalCast<std::string, T>,
class ToStr = LexicalCast<T, std::string> >
class ConfigVar : public ConfigVarBase {
public:
typedef RWMutex RWMutexType;
typedef std::shared_ptr<ConfigVar> ptr;
typedef std::function<void (const T& old_value, const T& new_value)> on_change_cb;
ConfigVar(const std::string& name,
const T& default_value, const std::string& description = "")
:ConfigVarBase(name, description)
,m_val(default_value) {}
std::string toString() override {
try {
//return boost::lexical_cast<std::string>(m_val);
RWMutexType::ReadLock lock(m_mutex);
return ToStr()(m_val);
}catch(std::exception e) {
SYLAR_LOG_ERROR(SYLAR_LOG_ROOT()) << "ConfigVar::toString exception" << e.what()
<< " conver: " << typeid(m_val).name() << " to string ";
}
return "";
}
bool fromString(const std::string& val) override {
try {
//m_val = boost::lexical_cast<T>(val);
setValue(FromStr()(val));
}catch(std::exception e) {
SYLAR_LOG_ERROR(SYLAR_LOG_ROOT()) << "ConfigVar::toString exception" << e.what()
<< " conver: " << typeid(m_val).name();
}
return false;
}
std::string getTypeid() const override { return typeid(T).name(); }
uint64_t addListener(on_change_cb cb) {
static uint64_t s_fun_id = 0;
RWMutexType::WriteLock lock(m_mutex);
++s_fun_id;
m_cbs[s_fun_id] = cb;
return s_fun_id;
}
void delListener(uint64_t type) {
RWMutexType::WriteLock lock(m_mutex);
m_cbs.earse(type);
}
on_change_cb getListener(uint64_t type) {
RWMutexType::ReadLock lock(m_mutex);
auto it = m_cbs.find(type);
return it == m_cbs.end() ? nullptr : it->second;
}
void clearListener() {
RWMutexType::WriteLock lock(m_mutex);
m_cbs.clear();
}
const T getValue() {
RWMutexType::ReadLock lock(m_mutex);
return m_val;
}
void setValue(const T& v) {
{
RWMutexType::ReadLock lock(m_mutex);
if(v == m_val) {
return;
}
for(auto i : m_cbs) {
i.second(m_val, v);
}
}
RWMutexType::WriteLock lock(m_mutex);
m_val = v;
}
private:
T m_val;
RWMutexType m_mutex;
std::map<uint64_t, on_change_cb> m_cbs;
};
class Config
{
public:
typedef std::unordered_map<std::string, ConfigVarBase::ptr> ConfigVarMap;
typedef RWMutex RWMutexType;
template<class T>
static typename ConfigVar<T>::ptr Lookup(const std::string& name,
const T& default_value, const std::string& description = "") {
RWMutexType::ReadLock lock(GetMutex());
auto it = GetDatas().find(name);
if(it != GetDatas().end()) {
auto tmp = std::dynamic_pointer_cast<ConfigVar<T> >(it->second);
if(tmp) {
SYLAR_LOG_INFO(SYLAR_LOG_ROOT()) << "Lookup name = " << name << " Exists!";
} else {
SYLAR_LOG_ERROR(SYLAR_LOG_ROOT()) << "Lookup name = " << name << " Exists but type not "
<< typeid(T).name() << " real type = " << it->second->getTypeid();
}
}
if(name.find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVXYZ._012345678")
!= std::string::npos) {
SYLAR_LOG_ERROR(SYLAR_LOG_ROOT()) << "Lookup name Invalid " << name;
throw std::invalid_argument(name);
}
typename ConfigVar<T>::ptr v(new ConfigVar<T>(name, default_value, description));
GetDatas()[name] = v;
//s_datas.insert(std::pair<std::string, ConfigVarBase::ptr>(name, v));
return v;
}
template<class T>
static typename ConfigVar<T>::ptr Lookup(const std::string& name) {
RWMutexType::ReadLock lock(GetMutex());
auto it = GetDatas().find(name);
if(it == GetDatas().end()) {
return nullptr;
}
return std::dynamic_pointer_cast<ConfigVar<T> >(it->second);
}
static void LoadFromYaml(const YAML::Node& root);
static void LoadFromConfDir(const std::string& path, bool force);
static ConfigVarBase::ptr LookupBase(const std::string& name);
static void Visit(std::function<void(ConfigVarBase::ptr)>& cb);
private:
static RWMutexType& GetMutex() {
static RWMutexType m_mutex;
return m_mutex;
}
static ConfigVarMap& GetDatas (){
static ConfigVarMap s_datas;
return s_datas;
}
};
}
#endif