59 #include <fmt/format.h>
84 explicit Conf(
Json data) : data_(std::move(data)) {}
86 Conf(
Json data, std::string
file) : file_(std::move(
file)), data_(std::move(data)) {}
88 : file_(std::move(
file)), root_(std::move(
root)), data_(std::move(data)) {}
104 [[nodiscard]]
const std::string&
file()
const {
return file_; }
109 [[nodiscard]]
bool is_empty()
const {
return data_.is_null(); }
153 [[nodiscard]] std::string
root()
const {
return (root_.empty() ?
"/" : root_); }
165 [[nodiscard]]
bool has(
const std::string& key)
const {
return data_.count(key) != 0; }
200 [[nodiscard]]
Conf at(
const std::string& key)
const;
235 size_t erase(
const std::string& key);
289 [[nodiscard]] std::vector<Conf>
to_array()
const;
296 template <
typename T>
297 [[nodiscard]] T
get()
const {
299 return data_.get<T>();
300 }
catch (Json::type_error&) {
311 template <
typename T>
312 [[nodiscard]] T
get(
const std::string& key)
const {
314 return data_.at(key).get<T>();
315 }
catch (Json::out_of_range&) {
317 }
catch (Json::type_error&) {
328 template <
typename T>
331 return data_.at(ptr).get<T>();
332 }
catch (Json::out_of_range&) {
334 }
catch (Json::type_error&) {
346 template <
typename T>
359 template <
typename T>
360 [[nodiscard]] T
get_or(
const std::string& key, T def)
const {
361 if (!data_.count(key)) {
365 return data_.at(key).get<T>();
366 }
catch (Json::type_error&) {
379 template <
typename T>
382 return data_.at(ptr).get<T>();
383 }
catch (Json::out_of_range&) {
385 }
catch (Json::type_error&) {
399 template <
typename T>
410 template <
typename T>
411 void with(
const std::string& key, std::function<
void(
const T&)> fn)
const {
412 if (data_.count(key)) {
423 template <
typename T>
427 }
catch (Json::out_of_range&) {
439 template <
typename T>
440 void with_pointer(
const std::string& ptr, std::function<
void(
const T&)> fn)
const {
453 template <
typename T>
454 void try_from(
const std::string& key, T& val)
const {
455 if (data_.count(key)) {
469 template <
typename T>
472 val = data_.at(ptr).get<T>();
473 }
catch (Json::out_of_range& e) {
475 }
catch (Json::type_error& e) {
490 template <
typename T>
501 void assert_has(
const std::string& key)
const;
560 void assert_has_not(
const std::string& key,
const std::string& msg =
"")
const;
594 [[nodiscard]] std::filesystem::path
resolve_file(
const std::filesystem::path& filename)
const;
606 [[nodiscard]] std::string
resolve_file(
const std::string& filename)
const;
617 template <
typename... Args>
618 [[noreturn]]
void throw_error(std::string_view format, Args&&... args)
const {
619 throw_error(fmt::format(fmt::runtime(format), std::forward<Args>(args)...));
626 [[noreturn]]
void throw_error(
const std::string& msg)
const;
635 [[noreturn]]
void throw_unexpected(
const std::string& key,
const std::string& msg =
"")
const;
652 [[noreturn]]
void throw_missing(
const std::string& key)
const;
void try_from(const JsonPointer &ptr, T &val) const
Definition: conf.hpp:470
bool has(const std::string &key) const
Definition: conf.hpp:165
void with_pointer(const std::string &ptr, std::function< void(const T &)> fn) const
Definition: conf.hpp:440
T get_or(const std::string &key, T def) const
Definition: conf.hpp:360
void throw_error(std::string_view format, Args &&... args) const
Definition: conf.hpp:618
void with(const std::string &key, std::function< void(const T &)> fn) const
Definition: conf.hpp:411
void throw_unexpected(const std::string &key, const std::string &msg="") const
Definition: conf.cpp:173
const Json * operator->() const
Definition: conf.hpp:130
Conf at_pointer(const std::string &ptr) const
Definition: conf.hpp:223
std::filesystem::path resolve_file(const std::filesystem::path &filename) const
Definition: conf.cpp:152
bool has_pointer(const std::string &ptr) const
Definition: conf.hpp:189
friend void to_json(Json &j, const Conf &c)
Definition: conf.hpp:701
std::string root() const
Definition: conf.hpp:153
void assert_has_type(const std::string &key, JsonType t) const
Definition: conf.cpp:138
friend void from_json(const Json &j, Conf &c)
Definition: conf.hpp:708
void assert_has(const std::string &key) const
Definition: conf.cpp:110
T get_or(const JsonPointer &ptr, T def) const
Definition: conf.hpp:380
T get_pointer_or(const std::string &ptr, T def) const
Definition: conf.hpp:400
void try_from(const std::string &key, T &val) const
Definition: conf.hpp:454
void throw_missing(const std::string &key) const
Definition: conf.cpp:187
T get(const JsonPointer &ptr) const
Definition: conf.hpp:329
const std::string & file() const
Definition: conf.hpp:104
void try_from_pointer(const std::string &ptr, T &val) const
Definition: conf.hpp:491
const Json & operator*() const
Definition: conf.hpp:116
size_t erase(const std::string &key)
Definition: conf.cpp:74
bool is_from_file() const
Definition: conf.hpp:99
void throw_wrong_type(const std::string &key="") const
Definition: conf.cpp:195
Conf at(const std::string &key) const
Definition: conf.cpp:58
void assert_has_pointer_not(const std::string &ptr, const std::string &msg="") const
Definition: conf.hpp:580
void assert_has_not(const std::string &key, const std::string &msg="") const
Definition: conf.cpp:122
T get_pointer(const std::string &ptr) const
Definition: conf.hpp:347
size_t erase_pointer(const std::string &ptr, bool preserve_empty=false)
Definition: conf.hpp:271
void assert_has_pointer(const std::string &key) const
Definition: conf.hpp:518
void assert_has_pointer_type(const std::string &ptr, JsonType t) const
Definition: conf.hpp:549
T get() const
Definition: conf.hpp:297
bool is_empty() const
Definition: conf.hpp:109
Json & operator*()
Definition: conf.hpp:123
std::vector< Conf > to_array() const
Definition: conf.cpp:93
void with(const JsonPointer &ptr, std::function< void(const T &)> fn) const
Definition: conf.hpp:424
T get(const std::string &key) const
Definition: conf.hpp:312
Json * operator->()
Definition: conf.hpp:137
nlohmann::json Json
Definition: fable_fwd.hpp:35
nlohmann::json_pointer< std::string > JsonPointer
Definition: fable_fwd.hpp:36
nlohmann::json::value_t JsonType
Definition: json.hpp:78