forked from google/ced
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.h
121 lines (101 loc) · 2.71 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
// Copyright 2017 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include <boost/filesystem/path.hpp>
#include "absl/synchronization/mutex.h"
#include "yaml-cpp/yaml.h"
namespace YAML {
template <>
struct convert<boost::filesystem::path> {
static bool decode(Node n, boost::filesystem::path& p) {
p = n.Scalar();
return true;
}
};
} // namespace YAML
class ConfigWatcher {
public:
~ConfigWatcher();
protected:
void RegisterWatcher(const std::string& path);
private:
friend class ConfigRegistry;
virtual void Set(YAML::Node node) = 0;
};
template <class T>
class Config final : public ConfigWatcher {
public:
Config(const std::string& path, const T& def = T())
: default_(def), value_(def) {
RegisterWatcher(path);
}
T get() const {
absl::MutexLock lock(&mu_);
return value_;
}
private:
mutable absl::Mutex mu_;
const T default_;
T value_ GUARDED_BY(mu_);
void Set(YAML::Node node) {
absl::MutexLock lock(&mu_);
value_ = node.as<T>(default_);
}
};
template <>
class Config<std::string> final : public ConfigWatcher {
public:
Config(const std::string& path, const std::string& def = std::string())
: default_(def), value_(def) {
RegisterWatcher(path);
}
std::string get() const {
absl::MutexLock lock(&mu_);
return value_;
}
private:
mutable absl::Mutex mu_;
const std::string default_;
std::string value_ GUARDED_BY(mu_);
void Set(YAML::Node node) {
absl::MutexLock lock(&mu_);
value_ = node.Scalar();
}
};
template <class K, class V>
class Config<std::map<K, V>> final : public ConfigWatcher {
public:
Config(const std::string& path, const V& def = V()) : def_(def) {
RegisterWatcher(path);
}
const V& get(const K& k) {
absl::MutexLock lock(&mu_);
auto it = value_.find(k);
if (it == value_.end()) return def_;
return it->second;
}
private:
mutable absl::Mutex mu_;
const V def_;
std::map<K, V> value_ GUARDED_BY(mu_);
void Set(YAML::Node node) {
absl::MutexLock lock(&mu_);
value_.clear();
for (const auto& kv : node) {
value_[kv.first.as<K>()] = kv.second.as<V>();
}
}
};
template <class K, class V>
using ConfigMap = Config<std::map<K, V>>;