|
cppfig 0.1.0
Modern C++20 compile-time type-safe configuration library
|
Main configuration manager. More...
#include <configuration.h>
Public Types | |
| using | serializer_type = SerializerT |
| using | data_type = Value |
Public Types inherited from cppfig::IConfigurationProvider< Derived, Schema > | |
| using | schema_type = Schema |
Public Member Functions | |
| Configuration (std::string file_path) | |
| Creates a configuration manager with a file path. | |
| template<IsSetting S> requires (Schema::template has_setting<S>) | |
| auto | GetImpl () const -> typename S::value_type |
| Gets the value for a setting type. | |
| template<IsSetting S> requires (Schema::template has_setting<S>) | |
| auto | IsOverriddenImpl () const -> bool |
| Reports whether an environment variable is overriding a setting. | |
| template<IsSetting S> requires (Schema::template has_setting<S>) | |
| auto | SetImpl (typename S::value_type value) -> Status |
| Sets the value for a setting type. | |
| auto | LoadImpl () -> Status |
| Loads configuration from the file. | |
| auto | SaveImpl () const -> Status |
| Saves the current configuration to the file. | |
| auto | DiffImpl () const -> ConfigDiff |
| Returns the diff between file values and defaults. | |
| auto | ValidateAllImpl () const -> Status |
| Validates all current values against their validators. | |
| auto | GetFilePathImpl () const -> std::string_view |
| Returns the file path. | |
| auto | GetFileValues () const -> Value |
| Returns a snapshot of the current file values. | |
| auto | GetDefaults () const -> Value |
| Returns a snapshot of the default values. | |
Public Member Functions inherited from cppfig::IConfigurationProvider< Derived, Schema > | |
| template<IsSetting S> requires (Schema::template has_setting<S>) | |
| auto | Get () const -> typename S::value_type |
| Gets the value for a setting type. | |
| template<IsSetting S> requires (Schema::template has_setting<S>) | |
| auto | Set (typename S::value_type value) -> Status |
| Sets the value for a setting type. | |
| template<IsSetting S> requires (Schema::template has_setting<S>) | |
| auto | IsOverridden () const -> bool |
| Reports whether an environment variable is overriding a setting. | |
| auto | Load () -> Status |
| Loads configuration from the file. | |
| auto | Save () const -> Status |
| Saves the current configuration to the file. | |
| auto | Diff () const -> ConfigDiff |
| Returns the diff between file values and defaults. | |
| auto | ValidateAll () const -> Status |
| Validates all current values against their validators. | |
| auto | GetFilePath () const -> std::string_view |
| Returns the file path. | |
Additional Inherited Members | |
Protected Member Functions inherited from cppfig::IConfigurationProvider< Derived, Schema > | |
| IConfigurationProvider ()=default | |
| ~IConfigurationProvider ()=default | |
| IConfigurationProvider (const IConfigurationProvider &)=default | |
| IConfigurationProvider (IConfigurationProvider &&)=default | |
| auto | operator= (const IConfigurationProvider &) -> IConfigurationProvider &=default |
| auto | operator= (IConfigurationProvider &&) -> IConfigurationProvider &=default |
Main configuration manager.
This class manages configuration values with:
Thread Safety: By default, the class uses SingleThreadedPolicy (zero overhead). For concurrent access from multiple threads, specify MultiThreadedPolicy:
With MultiThreadedPolicy:
Get concurrently (shared/reader lock).Set, Load mutate internal state under an exclusive/writer lock.Save also takes the exclusive lock: it rewrites the file, so concurrent savers would race over the same path.Diff and ValidateAll acquire a shared/reader lock.Set is performed before acquiring the exclusive lock.Usage:
| Schema | The ConfigSchema type defining all settings. |
| SerializerT | The serializer to use (defaults to ConfSerializer). |
| ThreadPolicy | The threading policy (defaults to SingleThreadedPolicy). For type-erased access, wrap an instance in VirtualConfigAdapter rather than deriving from IConfigurationProviderVirtual here: a virtual base would put a vptr on every configuration, including the single-threaded, non-polymorphic default. |
| using cppfig::Configuration< Schema, SerializerT, ThreadPolicy >::data_type = Value |
| using cppfig::Configuration< Schema, SerializerT, ThreadPolicy >::serializer_type = SerializerT |
|
inlineexplicit |
Creates a configuration manager with a file path.
| file_path | Path to the configuration file. |
|
inline |
Returns the diff between file values and defaults.
Thread safety: acquires a shared (reader) lock.
|
inline |
Returns a snapshot of the default values.
Thread safety: defaults_ is immutable after construction — safe to call concurrently without synchronization.
|
inline |
Returns the file path.
Thread safety: file_path_ is immutable after construction — no lock needed.
|
inline |
Returns a snapshot of the current file values.
Returned by value: handing out a reference to file_values_ let it escape the lock that guards it, so a reader could observe a tree being mutated by Set or Load.
Thread safety: acquires a shared (reader) lock.
|
inline |
Gets the value for a setting type.
Resolution order:
Thread safety: acquires a shared (reader) lock when reading file values.
|
inline |
Reports whether an environment variable is overriding a setting.
An override wins over the file value, so Set on such a setting is recorded and saved but will not be what Get returns. Callers that need to know can ask before writing.
|
inline |
Loads configuration from the file.
Thread safety: acquires an exclusive (writer) lock for the entire operation because it mutates file_values_.
|
inline |
Saves the current configuration to the file.
Thread safety: acquires an exclusive (writer) lock. Although only file_values_ is read, the call also rewrites the file, and two threads holding a shared lock would race over the same path. Serializing saves costs nothing next to the file I/O they perform.
|
inline |
Sets the value for a setting type.
The value is stored and will be written by Save. If an environment override is shadowing the setting, Get will keep returning the override; that is logged, and IsOverridden reports it up front.
Thread safety: validation runs without holding any lock; the actual mutation of internal state acquires an exclusive (writer) lock.
|
inline |
Validates all current values against their validators.
Thread safety: acquires a shared (reader) lock.