$darkmode
fable::Conf Class Reference

#include <conf.hpp>

Public Member Functions

 Conf (Json data)
 
 Conf (std::string file)
 
 Conf (Json data, std::string file)
 
 Conf (Json data, std::string file, std::string root)
 
bool is_from_file () const
 
const std::string & file () const
 
bool is_empty () const
 
const Jsonoperator* () const
 
Jsonoperator* ()
 
const Jsonoperator-> () const
 
Jsonoperator-> ()
 
std::string root () const
 
bool has (const std::string &key) const
 
bool has (const JsonPointer &key) const
 
bool has_pointer (const std::string &key) const
 
Conf at (const std::string &key) const
 
Conf at (const JsonPointer &key) const
 
Conf at_pointer (const std::string &key) const
 
size_t erase (const std::string &key)
 
size_t erase (const JsonPointer &key)
 
size_t erase_pointer (const std::string &key)
 
std::vector< Confto_array () const
 
template<typename T >
get () const
 
template<typename T >
get (const std::string &key) const
 
template<typename T >
get (const JsonPointer &key) const
 
template<typename T >
get_pointer (const std::string &key) const
 
template<typename T >
get_or (const std::string &key, T def) const
 
template<typename T >
get_or (const JsonPointer &key, T def) const
 
template<typename T >
get_pointer_or (const std::string &key, T def) const
 
template<typename T >
void with (const std::string &key, std::function< void(const T &)> fn) const
 
template<typename T >
void with (const JsonPointer &key, std::function< void(const T &)> fn) const
 
template<typename T >
void with_pointer (const std::string &key, std::function< void(const T &)> fn) const
 
template<typename T >
void try_from (const std::string &key, T &val) const
 
template<typename T >
void try_from (const JsonPointer &key, T &val) const
 
template<typename T >
void try_from_pointer (const std::string &key, T &val) const
 
void assert_has (const std::string &key) const
 
void assert_has (const JsonPointer &key) const
 
void assert_has_pointer (const std::string &key) const
 
void assert_has_type (const std::string &key, JsonType t) const
 
void assert_has_type (const JsonPointer &key, JsonType t) const
 
void assert_has_pointer_type (const std::string &key, JsonType t) const
 
void assert_has_not (const std::string &key, const std::string &msg="") const
 
void assert_has_not (const JsonPointer &key, const std::string &msg="") const
 
void assert_has_pointer_not (const std::string &key, const std::string &msg="") const
 
std::filesystem::path resolve_file (const std::filesystem::path &filename) const
 
std::string resolve_file (const std::string &filename) const
 
template<typename... Args>
void throw_error (std::string_view format, Args &&... args) const
 
void throw_error (const std::string &msg) const
 
void throw_unexpected (const std::string &key, const std::string &msg="") const
 
void throw_missing (const std::string &key) const
 
void throw_wrong_type (const std::string &key="") const
 
void throw_wrong_type (const std::string &key, JsonType type) const
 

Friends

void to_json (Json &j, const Conf &c)
 
void from_json (const Json &j, Conf &c)
 

Detailed Description

Conf wraps a JSON with context that allows for more user-friendly errors.

In general, a Conf wraps JSON content that comes from the filesystem and is used for configuration purposes. Even if a class requires only a subset of the JSON, Conf maintains the connection to the original file, which allows errors to be returned that refer to the file and location.

Conf is best used together with the Confable and Schema types.

Member Function Documentation

◆ assert_has()

void fable::Conf::assert_has ( const std::string &  key) const

Assert that the pointer key resolves, throw a ConfError otherwise.

Here is the caller graph for this function:

◆ assert_has_not()

void fable::Conf::assert_has_not ( const std::string &  key,
const std::string &  msg = "" 
) const

Assert that the pointer key is not available, throw a ConfError otherwise.

◆ assert_has_type()

void fable::Conf::assert_has_type ( const std::string &  key,
JsonType  t 
) const

Assert that the pointer key exists and resolves to the type t, throw a ConfError otherwise.

Here is the caller graph for this function:

◆ at()

Conf fable::Conf::at ( const std::string &  key) const

Return a new Conf basing off the JSON pointer.

  • Throws a ConfError if the key cannot be found.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ erase()

size_t fable::Conf::erase ( const std::string &  key)

Erase a key from the Conf if it exists and return the number of elements removed.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ file()

const std::string& fable::Conf::file ( ) const
inline

Return the file associated with this configuration.

◆ get() [1/2]

template<typename T >
T fable::Conf::get ( ) const
inline

Return a value of type T.

  • Throws a ConfError if the key is of the wrong type.
Here is the caller graph for this function:

◆ get() [2/2]

template<typename T >
T fable::Conf::get ( const std::string &  key) const
inline

Return a value of type T at the position key.

  • Throws a ConfError if the key cannot be found or is wrong type.

◆ get_or()

template<typename T >
T fable::Conf::get_or ( const std::string &  key,
def 
) const
inline

Return a value of type T at the position key, returning def if the key cannot be found.

◆ has()

bool fable::Conf::has ( const std::string &  key) const
inline

Return whether the key is present in the configuration.

  • This method should not throw.
Here is the caller graph for this function:

◆ is_empty()

bool fable::Conf::is_empty ( ) const
inline

Return whether this configuration is empty.

◆ is_from_file()

bool fable::Conf::is_from_file ( ) const
inline

Return whether this configuration was read from a file.

  • If not from a file, then the configuration must have come from one of: a) stdin b) command line c) network
  • This method should not throw.

◆ operator*()

const Json& fable::Conf::operator* ( ) const
inline

Return a reference to the JSON.

This allows operations to be performed on the underlying type.

◆ operator->()

const Json* fable::Conf::operator-> ( ) const
inline

Return a pointer to the JSON.

This allows operations to be performed with the underlying type.

◆ resolve_file()

std::filesystem::path fable::Conf::resolve_file ( const std::filesystem::path &  filename) const

Resolve a path to an absolute path by taking the configuration file into account.

  • If the path is absolute, return as is.
  • If the path is relative, but file is stdin, return relative to execution.
  • If the path is relative and file is not stdin, return relative to the file.

◆ root()

std::string fable::Conf::root ( ) const
inline

Return the root of the current JSON as a JSON pointer.

◆ to_array()

std::vector< Conf > fable::Conf::to_array ( ) const

Return an array of Conf values.

  • Throws a ConfError if the object is not an array.
Here is the caller graph for this function:

◆ try_from()

template<typename T >
void fable::Conf::try_from ( const std::string &  key,
T &  val 
) const
inline

Write the value at the pointer key to val, if the key can be found.

  • Throws a ConfError if the value is of the wrong type.
Here is the caller graph for this function:

◆ with()

template<typename T >
void fable::Conf::with ( const std::string &  key,
std::function< void(const T &)>  fn 
) const
inline

Perform the function on the value of the pointer key, if the key can be found; otherwise the function is not executed.

  • Throws a ConfError if the value is of the wrong type.

The documentation for this class was generated from the following files: