-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonconfig.hpp
188 lines (169 loc) · 5.3 KB
/
jsonconfig.hpp
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
#pragma once
#include <vxldollar/lib/configbase.hpp>
#include <vxldollar/lib/errors.hpp>
#include <vxldollar/lib/utility.hpp>
#include <boost/filesystem/path.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/property_tree/ptree.hpp>
#include <fstream>
namespace boost
{
namespace asio
{
namespace ip
{
class address_v6;
}
}
}
namespace vxldollar
{
/** Manages a node in a boost configuration tree. */
class jsonconfig : public vxldollar::configbase
{
public:
jsonconfig ();
jsonconfig (boost::property_tree::ptree & tree_a, std::shared_ptr<vxldollar::error> const & error_a = nullptr);
vxldollar::error & read (boost::filesystem::path const & path_a);
void write (boost::filesystem::path const & path_a);
void write (std::ostream & stream_a) const;
void read (std::istream & stream_a);
void open_or_create (std::fstream & stream_a, std::string const & path_a);
void create_backup_file (boost::filesystem::path const & filepath_a);
boost::property_tree::ptree const & get_tree ();
bool empty () const;
boost::optional<jsonconfig> get_optional_child (std::string const & key_a);
jsonconfig get_required_child (std::string const & key_a);
jsonconfig & put_child (std::string const & key_a, vxldollar::jsonconfig & conf_a);
jsonconfig & replace_child (std::string const & key_a, vxldollar::jsonconfig & conf_a);
bool has_key (std::string const & key_a);
jsonconfig & erase (std::string const & key_a);
/** Set value for the given key. Any existing value will be overwritten. */
template <typename T>
jsonconfig & put (std::string const & key, T const & value)
{
tree.put (key, value);
return *this;
}
/** Push array element */
template <typename T>
jsonconfig & push (T const & value)
{
boost::property_tree::ptree entry;
entry.put ("", value);
tree.push_back (std::make_pair ("", entry));
return *this;
}
/** Iterate array entries */
template <typename T>
jsonconfig & array_entries (std::function<void (T)> callback)
{
for (auto & entry : tree)
{
callback (entry.second.get<T> (""));
}
return *this;
}
/** Get optional, using \p default_value if \p key is missing. */
template <typename T>
jsonconfig & get_optional (std::string const & key, T & target, T default_value)
{
get_config (true, key, target, default_value);
return *this;
}
/**
* Get optional value, using the current value of \p target as the default if \p key is missing.
* @return May return vxldollar::error_config::invalid_value
*/
template <typename T>
jsonconfig & get_optional (std::string const & key, T & target)
{
get_config (true, key, target, target);
return *this;
}
/** Return a boost::optional<T> for the given key */
template <typename T>
boost::optional<T> get_optional (std::string const & key)
{
boost::optional<T> res;
if (has_key (key))
{
T target{};
get_config (true, key, target, target);
res = target;
}
return res;
}
/** Get value, using the current value of \p target as the default if \p key is missing. */
template <typename T>
jsonconfig & get (std::string const & key, T & target)
{
get_config (true, key, target, target);
return *this;
}
/**
* Get value of optional key. Use default value of data type if missing.
*/
template <typename T>
T get (std::string const & key)
{
T target{};
get_config (true, key, target, target);
return target;
}
/**
* Get required value.
* @note May set vxldollar::error_config::missing_value if \p key is missing, vxldollar::error_config::invalid_value if value is invalid.
*/
template <typename T>
jsonconfig & get_required (std::string const & key, T & target)
{
get_config (false, key, target);
return *this;
}
template <typename T>
jsonconfig & get_required (std::string const & key, T & target, T const & default_value)
{
get_config (false, key, target, default_value);
return *this;
}
protected:
template <typename T, typename = std::enable_if_t<vxldollar::is_lexical_castable<T>::value>>
jsonconfig & get_config (bool optional, std::string key, T & target, T default_value = T ())
{
try
{
auto val (tree.get<std::string> (key));
if (!boost::conversion::try_lexical_convert<T> (val, target))
{
conditionally_set_error<T> (vxldollar::error_config::invalid_value, optional, key);
}
}
catch (boost::property_tree::ptree_bad_path const &)
{
if (!optional)
{
conditionally_set_error<T> (vxldollar::error_config::missing_value, optional, key);
}
else
{
target = default_value;
}
}
catch (std::runtime_error & ex)
{
conditionally_set_error<T> (ex, optional, key);
}
return *this;
}
// boost's lexical cast doesn't handle (u)int8_t
jsonconfig & get_config (bool optional, std::string key, uint8_t & target, uint8_t default_value = uint8_t ());
jsonconfig & get_config (bool optional, std::string key, bool & target, bool default_value = false);
jsonconfig & get_config (bool optional, std::string key, boost::asio::ip::address_v6 & target, boost::asio::ip::address_v6 const & default_value);
private:
/** The property node being managed */
boost::property_tree::ptree & tree;
boost::property_tree::ptree tree_default;
void write_json (std::fstream & stream);
};
}