Configuru
Classes | Public Types | Public Member Functions | Static Public Member Functions | List of all members
configuru::Config Class Reference

#include <configuru.hpp>

Collaboration diagram for configuru::Config:
Collaboration graph
[legend]

Classes

struct  ConfigArray
 
struct  ConfigObject
 

Public Types

enum  Type {
  Uninitialized, BadLookupType, Null, Bool,
  Int, Float, String, Array,
  Object
}
 
using ObjectEntry = Config_Entry< Config >
 
using ConfigArrayImpl = std::vector< Config >
 
using ConfigObjectImpl = std::map< std::string, ObjectEntry >
 

Public Member Functions

 Config ()
 Creates an uninitialized Config.
 
 Config (std::nullptr_t)
 
 Config (float f)
 
 Config (double f)
 
 Config (bool b)
 
 Config (int i)
 
 Config (unsigned int i)
 
 Config (long i)
 
 Config (unsigned long i)
 
 Config (long long i)
 
 Config (unsigned long long i)
 
 Config (const char *str)
 
 Config (std::string str)
 
 Config (std::initializer_list< std::pair< std::string, Config >> values)
 
template<typename T >
 Config (const std::vector< T > &values)
 Array constructor.
 
 Config (const std::vector< bool > &values)
 Array constructor.
 
template<typename T >
 Config (const std::map< std::string, T > &values)
 Object constructor.
 
void make_object ()
 Used by the parser - no need to use directly.
 
void make_array ()
 Used by the parser - no need to use directly.
 
void tag (const DocInfo_SP &doc, Index line, Index column)
 Used by the parser - no need to use directly.
 
 Config (const Config &o)
 
 Config (Config &&o) noexcept
 
Configoperator= (const Config &o)
 
Configoperator= (Config &&o) noexcept
 Will still remember file/line when assigned an object which has no file/line.
 
void swap (Config &o) noexcept
 Swaps file/line too.
 
Type type () const
 
bool is_uninitialized () const
 
bool is_null () const
 
bool is_bool () const
 
bool is_int () const
 
bool is_float () const
 
bool is_string () const
 
bool is_object () const
 
bool is_array () const
 
bool is_number () const
 
std::string where () const
 Returns file:line iff available.
 
Index line () const
 BAD_INDEX if not set.
 
const DocInfo_SP & doc () const
 Handle to document.
 
void set_doc (const DocInfo_SP &doc)
 
template<typename T >
 operator T () const
 Explicit casting, since C++ handles implicit casts real badly.
 
template<typename T >
 operator std::vector< T > () const
 Convenience conversion to std::vector.
 
template<typename T , size_t N>
 operator std::array< T, N > () const
 Convenience conversion to std::array.
 
template<typename Left , typename Right >
 operator std::pair< Left, Right > () const
 
const std::string & as_string () const
 
const char * c_str () const
 
bool as_bool () const
 The Config must be a boolean.
 
template<typename IntT >
IntT as_integer () const
 
float as_float () const
 
double as_double () const
 
template<typename T >
get () const
 Extract the value of this Config.
 
template<typename T >
get_or (const T &default_value) const
 Returns the value or default_value if this is the result of a bad lookup.
 
size_t array_size () const
 Length of an array.
 
ConfigArrayImpl & as_array ()
 Only use this for iterating over an array: for (Config& e : cfg.as_array()) { ... }
 
const ConfigArrayImpl & as_array () const
 Only use this for iterating over an array: for (Config& e : cfg.as_array()) { ... }
 
Configoperator[] (size_t ix)
 Array indexing.
 
const Configoperator[] (size_t ix) const
 Array indexing.
 
void push_back (Config value)
 Append a value to this array.
 
size_t object_size () const
 Number of elementsi n this object.
 
ConfigObjectas_object ()
 
const ConfigObjectas_object () const
 
const Configoperator[] (const std::string &key) const
 Look up a value in an Object. Returns a BadLookupType Config if the key does not exist.
 
Configoperator[] (const std::string &key)
 Prefer obj.insert_or_assign(key, value); to obj[key] = value; when inserting and performance is important!
 
template<std::size_t N>
Configoperator[] (const char(&key)[N])
 For indexing with string literals:
 
template<std::size_t N>
const Configoperator[] (const char(&key)[N]) const
 
bool has_key (const std::string &key) const
 Check if an object has a specific key.
 
size_t count (const std::string &key) const
 Like has_key, but STL compatible.
 
bool emplace (std::string key, Config value)
 Returns true iff the value was inserted, false if they key was already there.
 
void insert_or_assign (const std::string &key, Config &&value)
 Like foo[key] = value, but faster.
 
bool erase (const std::string &key)
 Erase a key from an object.
 
template<typename T >
get (const std::string &key) const
 Get the given value in this object.
 
template<typename T >
get_or (const std::string &key, const T &default_value) const
 Look for the given key in this object, and return default_value on failure.
 
std::string get_or (const std::string &key, const char *default_value) const
 Look for the given key in this object, and return default_value on failure.
 
template<typename T >
get_or (std::initializer_list< std::string > keys, const T &default_value) const
 obj.get_or({"a", "b". "c"}, 42) - like obj["a"]["b"]["c"], but returns 42 if any of the keys are missing.
 
std::string get_or (std::initializer_list< std::string > keys, const char *default_value) const
 obj.get_or({"a", "b". "c"}, 42) - like obj["a"]["b"]["c"], but returns 42 if any of the keys are missing.
 
Config deep_clone () const
 Copy this Config value recursively.
 
void visit_dangling (const std::function< void(const std::string &key, const Config &value)> &visitor) const
 Visit dangling (unaccessed) object keys recursively.
 
void check_dangling () const
 Will check for dangling (unaccessed) object keys recursively and call CONFIGURU_ON_DANGLING on all found.
 
void mark_accessed (bool v) const
 Set the 'access' flag recursively,.
 
bool has_comments () const
 Was there any comments about this value in the input?
 
ConfigCommentscomments ()
 Read/write of comments.
 
const ConfigCommentscomments () const
 Read comments.
 
const char * debug_descr () const
 Returns either "true", "false", the constained string, or the type name.
 
void check (bool b, const char *msg) const
 
void assert_type (Type t) const
 
void on_error (const std::string &msg) const CONFIGURU_NORETURN
 
template<>
bool get () const
 
template<>
signed char get () const
 
template<>
unsigned char get () const
 
template<>
signed short get () const
 
template<>
unsigned short get () const
 
template<>
signed int get () const
 
template<>
unsigned int get () const
 
template<>
signed long get () const
 
template<>
unsigned long get () const
 
template<>
signed long long get () const
 
template<>
unsigned long long get () const
 
template<>
float get () const
 
template<>
double get () const
 
template<>
const std::string & get () const
 
template<>
std::string get () const
 
template<>
const Config::ConfigArrayImpl & get () const
 

Static Public Member Functions

static Config object ()
 Preferred way to create an empty object.
 
static Config object (std::initializer_list< std::pair< std::string, Config >> values)
 Preferred way to create an object.
 
static Config array ()
 Preferred way to create an empty array.
 
static Config array (std::initializer_list< Config > values)
 Preferred way to create an array.
 
template<typename Container >
static Config array (const Container &container)
 Preferred way to create an array from an STL container.
 
static bool deep_eq (const Config &a, const Config &b)
 Compare Config values recursively.
 
static const char * type_str (Type t)
 Human-readable version of the type ("integer", "bool", etc).
 

Detailed Description

A dynamic config variable. Acts like something out of Python or Lua. If CONFIGURU_VALUE_SEMANTICS all copies of this will be deep copies. If not, it will use reference-counting for objects and arrays, meaning all copies will be shallow copies.

Member Enumeration Documentation

Enumerator
Uninitialized 

Accessing a Config of this type is always an error.

BadLookupType 

We are the result of a key-lookup in a Object with no hit. We are in effect write-only.

Constructor & Destructor Documentation

configuru::Config::Config ( std::initializer_list< std::pair< std::string, Config >>  values)

This constructor is a short-form for Config::object(...). We have no short-form for Config::array(...), as that is less common and can lead to ambiguities. Usage:

Config cfg {
{ "key", "value" },
{ "empty_array", Config::array() },
{ "array", Config::array({1, 2, 3}) },
{ "empty_object", Config::object() },
{ "object", Config::object({
{ "nested_key", "nested_value" },
})},
{ "another_object", {
{ "nested_key", "nested_value" },
}},
};

Member Function Documentation

ConfigObject& configuru::Config::as_object ( )
inline

Only use this for iterating over an object: for (auto& p : cfg.as_object()) { p.value() = p.key(); }

const ConfigObject& configuru::Config::as_object ( ) const
inline

Only use this for iterating over an object: for (const auto& p : cfg.as_object()) { cout << p.key() << ": " << p.value(); }

template<typename Left , typename Right >
configuru::Config::operator std::pair< Left, Right > ( ) const
inlineexplicit

Convenience conversion of an array of length 2 to an std::pair. TODO: generalize for tuples.


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