59 #include <fmt/format.h>
85 explicit Conf(
Json data) : data_(std::move(data)) {}
87 Conf(
Json data, std::string
file) : file_(std::move(
file)), data_(std::move(data)) {}
89 : file_(std::move(
file)), root_(std::move(
root)), data_(std::move(data)) {}
105 [[nodiscard]]
const std::string&
file()
const {
return file_; }
110 [[nodiscard]]
bool is_empty()
const {
return data_.is_null(); }
154 [[nodiscard]] std::string
root()
const {
return (root_.empty() ?
"/" : root_); }
166 [[nodiscard]]
bool has(
const std::string& key)
const {
return data_.count(key) != 0; }
201 [[nodiscard]]
Conf at(
const std::string& key)
const;
236 size_t erase(
const std::string& key);
290 [[nodiscard]] std::vector<Conf>
to_array()
const;
297 template <
typename T>
298 [[nodiscard]] T
get()
const {
300 return data_.get<T>();
301 }
catch (Json::type_error&) {
312 template <
typename T>
313 [[nodiscard]] T
get(
const std::string& key)
const {
315 return data_.at(key).get<T>();
316 }
catch (Json::out_of_range&) {
318 }
catch (Json::type_error&) {
329 template <
typename T>
332 return data_.at(ptr).get<T>();
333 }
catch (Json::out_of_range&) {
335 }
catch (Json::type_error&) {
347 template <
typename T>
360 template <
typename T>
361 [[nodiscard]] T
get_or(
const std::string& key, T def)
const {
362 if (!data_.count(key)) {
366 return data_.at(key).get<T>();
367 }
catch (Json::type_error&) {
380 template <
typename T>
383 return data_.at(ptr).get<T>();
384 }
catch (Json::out_of_range&) {
386 }
catch (Json::type_error&) {
400 template <
typename T>
411 template <
typename T>
412 void with(
const std::string& key, std::function<
void(
const T&)> fn)
const {
413 if (data_.count(key)) {
424 template <
typename T>
428 }
catch (Json::out_of_range&) {
440 template <
typename T>
441 void with_pointer(
const std::string& ptr, std::function<
void(
const T&)> fn)
const {
454 template <
typename T>
455 void try_from(
const std::string& key, T& val)
const {
456 if (data_.count(key)) {
470 template <
typename T>
473 val = data_.at(ptr).get<T>();
474 }
catch (Json::out_of_range& e) {
476 }
catch (Json::type_error& e) {
491 template <
typename T>
502 void assert_has(
const std::string& key)
const;
561 void assert_has_not(
const std::string& key,
const std::string& msg =
"")
const;
595 [[nodiscard]] std::filesystem::path
resolve_file(
const std::filesystem::path& filename)
const;
607 [[nodiscard]] std::string
resolve_file(
const std::string& filename)
const;
618 template <
typename... Args>
619 [[noreturn]]
void throw_error(std::string_view format, Args&&... args)
const {
620 throw_error(fmt::format(fmt::runtime(format), std::forward<Args>(args)...));
627 [[noreturn]]
void throw_error(
const std::string& msg)
const;
636 [[noreturn]]
void throw_unexpected(
const std::string& key,
const std::string& msg =
"")
const;
653 [[noreturn]]
void throw_missing(
const std::string& key)
const;
void try_from(const JsonPointer &ptr, T &val) const
Definition: conf.hpp:471
bool has(const std::string &key) const
Definition: conf.hpp:166
void with_pointer(const std::string &ptr, std::function< void(const T &)> fn) const
Definition: conf.hpp:441
T get_or(const std::string &key, T def) const
Definition: conf.hpp:361
void throw_error(std::string_view format, Args &&... args) const
Definition: conf.hpp:619
void with(const std::string &key, std::function< void(const T &)> fn) const
Definition: conf.hpp:412
void throw_unexpected(const std::string &key, const std::string &msg="") const
Definition: conf.cpp:173
const Json * operator->() const
Definition: conf.hpp:131
Conf at_pointer(const std::string &ptr) const
Definition: conf.hpp:224
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:190
friend void to_json(Json &j, const Conf &c)
Definition: conf.hpp:702
std::string root() const
Definition: conf.hpp:154
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:709
void assert_has(const std::string &key) const
Definition: conf.cpp:110
T get_or(const JsonPointer &ptr, T def) const
Definition: conf.hpp:381
T get_pointer_or(const std::string &ptr, T def) const
Definition: conf.hpp:401
void try_from(const std::string &key, T &val) const
Definition: conf.hpp:455
void throw_missing(const std::string &key) const
Definition: conf.cpp:187
T get(const JsonPointer &ptr) const
Definition: conf.hpp:330
const std::string & file() const
Definition: conf.hpp:105
void try_from_pointer(const std::string &ptr, T &val) const
Definition: conf.hpp:492
const Json & operator*() const
Definition: conf.hpp:117
size_t erase(const std::string &key)
Definition: conf.cpp:74
bool is_from_file() const
Definition: conf.hpp:100
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:581
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:348
size_t erase_pointer(const std::string &ptr, bool preserve_empty=false)
Definition: conf.hpp:272
void assert_has_pointer(const std::string &key) const
Definition: conf.hpp:519
void assert_has_pointer_type(const std::string &ptr, JsonType t) const
Definition: conf.hpp:550
T get() const
Definition: conf.hpp:298
bool is_empty() const
Definition: conf.hpp:110
Json & operator*()
Definition: conf.hpp:124
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:425
T get(const std::string &key) const
Definition: conf.hpp:313
Json * operator->()
Definition: conf.hpp:138
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