cppfig 0.1.0
Modern C++20 compile-time type-safe configuration library
Loading...
Searching...
No Matches
cppfig::Configuration< Schema, SerializerT, ThreadPolicy > Class Template Reference

Main configuration manager. More...

#include <configuration.h>

Inheritance diagram for cppfig::Configuration< Schema, SerializerT, ThreadPolicy >:
[legend]
Collaboration diagram for cppfig::Configuration< Schema, SerializerT, ThreadPolicy >:
[legend]

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
 

Detailed Description

template<typename Schema, Serializer SerializerT = ConfSerializer, typename ThreadPolicy = SingleThreadedPolicy>
class cppfig::Configuration< Schema, SerializerT, ThreadPolicy >

Main configuration manager.

This class manages configuration values with:

  • Compile-time type-safe access via setting types
  • Environment variable overrides
  • Validation
  • Automatic file creation with defaults
  • Schema migration (adding new settings)
  • Optional thread safety via a pluggable ThreadPolicy

Thread Safety: By default, the class uses SingleThreadedPolicy (zero overhead). For concurrent access from multiple threads, specify MultiThreadedPolicy:

// Single-threaded (default, zero overhead):
cppfig::Configuration<MySchema> config("config.json");
// Thread-safe (reader-writer locking):
config("config.json");
Main configuration manager.
Definition configuration.h:88

With MultiThreadedPolicy:

  • Multiple threads may call Get concurrently (shared/reader lock).
  • Calls to 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.
  • Validation in Set is performed before acquiring the exclusive lock.

Usage:

// Define settings
struct AppName {
static constexpr std::string_view path = "app.name";
using value_type = std::string;
static auto default_value() -> std::string { return "MyApp"; }
};
struct ServerPort {
static constexpr std::string_view path = "server.port";
static constexpr std::string_view env_override = "SERVER_PORT";
using value_type = int;
static auto default_value() -> int { return 8080; }
static auto validator() -> Validator<int> { return Range(1, 65535); }
};
// Create schema and configuration
using MySchema = ConfigSchema<AppName, ServerPort>;
Configuration<MySchema> config("config.json");
// Load and use
config.Load();
std::string name = config.Get<AppName>();
int port = config.Get<ServerPort>();
config.Set<ServerPort>(9000);
config.Save();
auto Range(T min_value, T max_value) -> Validator< T >
Creates a validator that checks if a numeric value is within [min, max].
Definition validator.h:114
Template Parameters
SchemaThe ConfigSchema type defining all settings.
SerializerTThe serializer to use (defaults to ConfSerializer).
ThreadPolicyThe 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.

Member Typedef Documentation

◆ data_type

template<typename Schema , Serializer SerializerT = ConfSerializer, typename ThreadPolicy = SingleThreadedPolicy>
using cppfig::Configuration< Schema, SerializerT, ThreadPolicy >::data_type = Value

◆ serializer_type

template<typename Schema , Serializer SerializerT = ConfSerializer, typename ThreadPolicy = SingleThreadedPolicy>
using cppfig::Configuration< Schema, SerializerT, ThreadPolicy >::serializer_type = SerializerT

Constructor & Destructor Documentation

◆ Configuration()

template<typename Schema , Serializer SerializerT = ConfSerializer, typename ThreadPolicy = SingleThreadedPolicy>
cppfig::Configuration< Schema, SerializerT, ThreadPolicy >::Configuration ( std::string  file_path)
inlineexplicit

Creates a configuration manager with a file path.

Parameters
file_pathPath to the configuration file.

Member Function Documentation

◆ DiffImpl()

template<typename Schema , Serializer SerializerT = ConfSerializer, typename ThreadPolicy = SingleThreadedPolicy>
auto cppfig::Configuration< Schema, SerializerT, ThreadPolicy >::DiffImpl ( ) const -> ConfigDiff
inline

Returns the diff between file values and defaults.

Thread safety: acquires a shared (reader) lock.

◆ GetDefaults()

template<typename Schema , Serializer SerializerT = ConfSerializer, typename ThreadPolicy = SingleThreadedPolicy>
auto cppfig::Configuration< Schema, SerializerT, ThreadPolicy >::GetDefaults ( ) const -> Value
inline

Returns a snapshot of the default values.

Thread safety: defaults_ is immutable after construction — safe to call concurrently without synchronization.

◆ GetFilePathImpl()

template<typename Schema , Serializer SerializerT = ConfSerializer, typename ThreadPolicy = SingleThreadedPolicy>
auto cppfig::Configuration< Schema, SerializerT, ThreadPolicy >::GetFilePathImpl ( ) const -> std::string_view
inline

Returns the file path.

Thread safety: file_path_ is immutable after construction — no lock needed.

◆ GetFileValues()

template<typename Schema , Serializer SerializerT = ConfSerializer, typename ThreadPolicy = SingleThreadedPolicy>
auto cppfig::Configuration< Schema, SerializerT, ThreadPolicy >::GetFileValues ( ) const -> Value
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.

◆ GetImpl()

template<typename Schema , Serializer SerializerT = ConfSerializer, typename ThreadPolicy = SingleThreadedPolicy>
template<IsSetting S>
requires (Schema::template has_setting<S>)
auto cppfig::Configuration< Schema, SerializerT, ThreadPolicy >::GetImpl ( ) const -> typename S::value_type
inline

Gets the value for a setting type.

Resolution order:

  1. Environment variable (if configured)
  2. File value (if present)
  3. Default value

Thread safety: acquires a shared (reader) lock when reading file values.

◆ IsOverriddenImpl()

template<typename Schema , Serializer SerializerT = ConfSerializer, typename ThreadPolicy = SingleThreadedPolicy>
template<IsSetting S>
requires (Schema::template has_setting<S>)
auto cppfig::Configuration< Schema, SerializerT, ThreadPolicy >::IsOverriddenImpl ( ) const -> bool
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.

◆ LoadImpl()

template<typename Schema , Serializer SerializerT = ConfSerializer, typename ThreadPolicy = SingleThreadedPolicy>
auto cppfig::Configuration< Schema, SerializerT, ThreadPolicy >::LoadImpl ( ) -> Status
inline

Loads configuration from the file.

Thread safety: acquires an exclusive (writer) lock for the entire operation because it mutates file_values_.

◆ SaveImpl()

template<typename Schema , Serializer SerializerT = ConfSerializer, typename ThreadPolicy = SingleThreadedPolicy>
auto cppfig::Configuration< Schema, SerializerT, ThreadPolicy >::SaveImpl ( ) const -> Status
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.

◆ SetImpl()

template<typename Schema , Serializer SerializerT = ConfSerializer, typename ThreadPolicy = SingleThreadedPolicy>
template<IsSetting S>
requires (Schema::template has_setting<S>)
auto cppfig::Configuration< Schema, SerializerT, ThreadPolicy >::SetImpl ( typename S::value_type  value) -> Status
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.

◆ ValidateAllImpl()

template<typename Schema , Serializer SerializerT = ConfSerializer, typename ThreadPolicy = SingleThreadedPolicy>
auto cppfig::Configuration< Schema, SerializerT, ThreadPolicy >::ValidateAllImpl ( ) const -> Status
inline

Validates all current values against their validators.

Thread safety: acquires a shared (reader) lock.


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