59#ifndef GUM_CONFIGURATION_H
60#define GUM_CONFIGURATION_H
74#include <initializer_list>
75#include <shared_mutex>
77#include <unordered_map>
108 enum class Kind :
unsigned char { INT, FLOAT, BOOL, STR, ENUM, DEPRECATED };
111 struct GUM_SHARED_PUBLIC
Meta {
141 using Value = std::variant< bool, std::int64_t, double, std::string >;
156 enum class Kind :
unsigned char { DEPRECATION, FILE };
180 explicit Configuration(std::string_view defaultsText, std::string_view origin =
"defaults.ini");
197 explicit Configuration(std::initializer_list< DefaultsSource > sources);
225 std::string
get(std::string_view section, std::string_view key)
const;
234 template <
typename T >
235 T
typed(std::string_view section, std::string_view key)
const;
244 Value getValue(std::string_view section, std::string_view key)
const;
247 bool has(std::string_view section, std::string_view key)
const;
250 Meta meta(std::string_view section, std::string_view key)
const;
253 std::string
doc(std::string_view section, std::string_view key)
const;
256 std::vector< SchemaEntry >
schema()
const;
259 std::vector< std::string >
sections()
const;
276 void set(std::string_view section,
277 std::string_view key,
278 std::string_view value,
279 bool noHook =
false);
308 void load(
const std::string& filename);
314 void save(
const std::string& filename)
const;
318 std::string
grep(std::string_view search)
const;
321 std::string
diff()
const;
383 std::unordered_map< std::string, std::size_t >
399 void _parse_(std::string_view text, std::string_view origin);
405 static constexpr std::size_t
_npos_ =
static_cast< std::size_t
>(-1);
407 static std::string
_indexKey_(std::string_view section, std::string_view key);
409 std::size_t
_tryFind_(std::string_view section, std::string_view key)
const;
411 std::size_t
_find_(std::string_view section, std::string_view key)
const;
415 std::size_t
_resolve_(std::size_t pos, std::vector< Notice >* sink =
nullptr)
const;
417 std::pair< std::string, Kind >
_typedRaw_(std::string_view section, std::string_view key)
const;
421 std::string_view value,
422 std::string& canonical,
426 void _setAt_(std::size_t pos, std::string_view value);
429 std::string
_render_(
const std::function<
bool(
const Item&) >& keep)
const;
Sectioned key/value configuration with typed and documented keys.
std::string describe() const
static Configuration & instance()
The global configuration.
static constexpr std::size_t _npos_
std::unordered_map< std::string, std::size_t > _index_
Configuration(std::string_view defaultsText, std::string_view origin="defaults.ini")
Builds a configuration from a defaults text.
bool has(std::string_view section, std::string_view key) const
std::size_t _find_(std::string_view section, std::string_view key) const
std::shared_mutex _mutex_
void reset()
Back to the defaults. Runs the hooks.
double epsilonStandard() const noexcept
Configuration & operator=(const Configuration &)=delete
The global configuration.
std::vector< Notice > _notices_
std::string _render_(const std::function< bool(const Item &) > &keep) const
static std::string _indexKey_(std::string_view section, std::string_view key)
T typed(std::string_view section, std::string_view key) const
Value of section.key converted according to its declared type.
std::vector< Notice > takeNotices()
std::array< std::size_t, _nbEpsilons_ > _epsilonPos_
std::vector< Snapshot > _stack_
void _queue_(Notice::Kind kind, std::string message) const
static bool _validate_(const Item &item, std::string_view value, std::string &canonical, std::string &error)
std::size_t _resolve_(std::size_t pos, std::vector< Notice > *sink=nullptr) const
void save(const std::string &filename) const
Writes the difference with the defaults (see diff()) in a file.
void addHook(Hook hook)
Adds a function called after each change (see set(), reset(), pop(), load()).
std::vector< std::string > _sections_
std::vector< Hook > _hooks_
std::array< std::atomic< double >, _nbEpsilons_ > _epsilons_
void runHooks() const
Runs the hooks (outside of any lock).
void push()
Pushes the current values on a stack.
void _parse_(std::string_view text, std::string_view origin)
static std::string kindName(Kind kind)
void pop()
Restores the values on top of the stack.
std::function< void(const Configuration &) > Hook
std::string toString() const
std::string grep(std::string_view search) const
std::string doc(std::string_view section, std::string_view key) const
Snapshot _snapshot_() const
std::vector< Item > _items_
std::pair< std::string, Kind > _typedRaw_(std::string_view section, std::string_view key) const
Configuration(const Configuration &)=delete
The global configuration.
static std::string userFileName()
std::variant< bool, std::int64_t, double, std::string > Value
a value converted according to the declared type of its key (see getValue())
void _setAt_(std::size_t pos, std::string_view value)
std::size_t _tryFind_(std::string_view section, std::string_view key) const
Value getValue(std::string_view section, std::string_view key) const
Value of section.key converted according to its declared type (following a deprecation).
bool _hasSection_(std::string_view section) const
void set(std::string_view section, std::string_view key, std::string_view value, bool noHook=false)
Sets section.key.
double epsilonCoarse() const noexcept
The coarse tolerance (key core.epsilonCoarse, 1e-2 by default).
static constexpr std::size_t _nbEpsilons_
void load(const std::string &filename)
Loads a user file and changes the values it contains.
std::vector< SchemaEntry > schema() const
Kind
the declared type of a key
void _restore_(const Snapshot &snapshot)
~Configuration()=default
The global configuration.
Meta meta(std::string_view section, std::string_view key) const
std::vector< std::string > sections() const
std::string get(std::string_view section, std::string_view key) const
Value of section.key, as a string.
double epsilonFine() const noexcept
std::vector< std::string > Snapshot
Inline definitions of gum::Configuration.
Template definitions of gum::Configuration.
gum is the global namespace for all aGrUM entities
Configuration & config()
A short name for Configuration::instance() : gum::config().epsilonStandard(). It is not inline so tha...
a text of defaults, with the name designating it in error messages
domain of validity of an int or float key, written after the type in the ### line: float]0,...
std::string text
the domain as written, without blanks (e.g. ]0,1[) ; empty if none
bool contains(double x) const
a message that cannot be an exception (the operation went on), see takeNotices()
one line of the schema (see schema())
std::string defaultValue
empty for a deprecated key