52 #ifndef FABLE_CONF_HPP_ 53 #define FABLE_CONF_HPP_ 59 #include <fmt/format.h> 65 namespace filesystem {
77 explicit Conf(
const Json& data) : data_(data) {}
78 explicit Conf(
const std::string& file);
79 Conf(
const Json& data,
const std::string& file) : file_(file), data_(data) {}
80 Conf(
const Json& data,
const std::string& file,
const std::string& root)
81 : file_(file), root_(root), data_(data) {}
97 const std::string&
file()
const {
return file_; }
110 Json& operator*() {
return data_; }
118 Json* operator->() {
return &data_; }
123 std::string
root()
const {
return (root_.empty() ?
"/" : root_); }
130 bool has(
const std::string& key)
const {
return data_.count(key) != 0; }
132 bool has_pointer(
const std::string& key)
const {
return has(
JsonPointer(key)); }
139 Conf at(
const std::string& key)
const;
141 Conf at_pointer(
const std::string& key)
const {
return at(
JsonPointer(key)); }
147 size_t erase(
const std::string& key);
149 size_t erase_pointer(
const std::string& key) {
return erase(
JsonPointer(key)); }
156 std::vector<Conf> to_array()
const;
163 template <
typename T>
166 return data_.get<T>();
167 }
catch (nlohmann::detail::type_error&) {
177 template <
typename T>
178 T
get(
const std::string& key)
const {
180 return data_.at(key).get<T>();
181 }
catch (nlohmann::detail::out_of_range&) {
183 }
catch (nlohmann::detail::type_error&) {
184 throw_wrong_type(key);
188 template <
typename T>
191 return data_.at(key).get<T>();
192 }
catch (nlohmann::detail::out_of_range&) {
194 }
catch (nlohmann::detail::type_error&) {
195 throw_wrong_type(key);
199 template <
typename T>
200 T get_pointer(
const std::string& key)
const {
208 template <
typename T>
209 T
get_or(
const std::string& key, T def)
const {
210 if (!data_.count(key)) {
214 return data_.at(key).get<T>();
215 }
catch (nlohmann::detail::type_error&) {
216 throw_wrong_type(key);
220 template <
typename T>
223 return data_.at(key).get<T>();
224 }
catch (nlohmann::detail::out_of_range&) {
226 }
catch (nlohmann::detail::type_error&) {
227 throw_wrong_type(key);
231 template <
typename T>
232 T get_pointer_or(
const std::string& key, T def)
const {
233 return get_or(key, def);
242 template <
typename T>
243 void with(
const std::string& key, std::function<
void(
const T&)> fn)
const {
244 if (data_.count(key)) {
249 template <
typename T>
250 void with(
const JsonPointer& key, std::function<
void(
const T&)> fn)
const {
253 }
catch (nlohmann::detail::out_of_range&) {
258 template <
typename T>
259 void with_pointer(
const std::string& key, std::function<
void(
const T&)> fn)
const {
268 template <
typename T>
269 void try_from(
const std::string& key, T* val)
const {
270 if (data_.count(key)) {
275 template <
typename T>
276 void try_from(
const JsonPointer& key, T* val)
const {
278 *val = data_.at(key).get<T>();
279 }
catch (nlohmann::detail::out_of_range& e) {
281 }
catch (nlohmann::detail::type_error& e) {
282 throw_wrong_type(key);
286 template <
typename T>
287 void try_from_pointer(
const std::string& key, T* val)
const {
294 void assert_has(
const std::string& key)
const;
296 void assert_has_pointer(
const std::string& key)
const { assert_has(
JsonPointer(key)); }
302 void assert_has_type(
const std::string& key,
JsonType t)
const;
304 void assert_has_pointer_type(
const std::string& key,
JsonType t)
const {
312 void assert_has_not(
const std::string& key,
const std::string& msg =
"")
const;
313 void assert_has_not(
const JsonPointer& key,
const std::string& msg =
"")
const;
314 void assert_has_pointer_not(
const std::string& key,
const std::string& msg =
"")
const {
328 boost::filesystem::path resolve_file(
const boost::filesystem::path& filename)
const;
329 std::string resolve_file(
const std::string& filename)
const;
331 template <
typename... Args>
332 [[noreturn]]
void throw_error(
const char* msg,
const Args&... args)
const {
333 throw_error(fmt::format(msg, args...));
335 [[noreturn]]
void throw_error(
const std::string& msg)
const;
336 [[noreturn]]
void throw_unexpected(
const std::string& key,
const std::string& msg =
"")
const;
337 [[noreturn]]
void throw_missing(
const std::string& key)
const;
338 [[noreturn]]
void throw_wrong_type(
const std::string& key =
"")
const;
339 [[noreturn]]
void throw_wrong_type(
const std::string& key,
JsonType type)
const;
341 friend void to_json(
Json& j,
const Conf& c) { j = *c; }
342 friend void from_json(
const Json& j,
Conf& c) { *c = j; }
352 #endif // FABLE_CONF_HPP_
std::string root() const
Definition: conf.hpp:123
const Json * operator->() const
Definition: conf.hpp:117
bool is_empty() const
Definition: conf.hpp:102
nlohmann::json Json
Definition: json.hpp:62
bool has(const std::string &key) const
Definition: conf.hpp:130
Json::value_t JsonType
Definition: json.hpp:78
const std::string & file() const
Definition: conf.hpp:97
void try_from(const std::string &key, T *val) const
Definition: conf.hpp:269
Json::json_pointer JsonPointer
Definition: json.hpp:70
const Json & operator*() const
Definition: conf.hpp:109
T get_or(const std::string &key, T def) const
Definition: conf.hpp:209
void with(const std::string &key, std::function< void(const T &)> fn) const
Definition: conf.hpp:243
bool is_from_file() const
Definition: conf.hpp:92