cppfig 0.1.0
Modern C++20 compile-time type-safe configuration library
Loading...
Searching...
No Matches
cppfig::Value Class Reference

A self-contained, recursive value type for configuration data. More...

#include <value.h>

Public Types

using ObjectType = std::map< std::string, Value, std::less<> >
 Ordered map of string keys to Value children.
 
using ArrayType = std::vector< Value >
 Ordered sequence of Value elements.
 

Public Member Functions

 Value ()
 Constructs a null value.
 
 Value (std::nullptr_t)
 Constructs a null value.
 
 Value (bool b)
 Constructs a boolean value.
 
 Value (int i)
 Constructs an integer value from int.
 
 Value (std::int64_t i)
 Constructs an integer value.
 
 Value (double d)
 Constructs a double value.
 
 Value (float f)
 Constructs a double value from float.
 
 Value (const char *s)
 Constructs a string value from a C string.
 
 Value (std::string s)
 Constructs a string value.
 
 Value (std::string_view s)
 Constructs a string value from string_view.
 
 Value (const Value &other)
 Deep-copies the value (recursive for objects / arrays).
 
auto operator= (const Value &other) -> Value &
 Deep-copy assignment.
 
 Value (Value &&) noexcept=default
 Move constructor (default, transfers ownership).
 
auto operator= (Value &&) noexcept -> Value &=default
 Move assignment (default).
 
 ~Value ()=default
 Destructor (default, unique_ptr handles cleanup).
 
auto IsNull () const -> bool
 Returns true if this value is null.
 
auto IsBoolean () const -> bool
 Returns true if this value is a boolean.
 
auto IsInteger () const -> bool
 Returns true if this value is an integer.
 
auto IsDouble () const -> bool
 Returns true if this value is a double.
 
auto IsNumber () const -> bool
 Returns true if this value is any numeric type (integer or double).
 
auto IsString () const -> bool
 Returns true if this value is a string.
 
auto IsObject () const -> bool
 Returns true if this value is an object (key-value map).
 
auto IsArray () const -> bool
 Returns true if this value is an array.
 
template<typename T >
auto Fits () const -> bool
 Returns true if the stored value is representable as T.
 
template<typename T >
auto Get () const -> T
 Extracts the stored value as the requested type.
 
auto TryToText () const -> std::optional< std::string >
 Renders a scalar value as text, or nullopt for objects/arrays.
 
template<typename T >
auto TryGet () const -> std::optional< T >
 Extracts the stored value as T, or nullopt if it is not one.
 
auto Contains (std::string_view key) const -> bool
 Checks whether the given key exists in an object value.
 
auto operator[] (const std::string &key) -> Value &
 Accesses or creates a child by key, promoting null → object.
 
auto operator[] (const std::string &key) const -> const Value &
 Read-only access to a child by key (returns static null for missing keys).
 
auto Items () const -> const ObjectType &
 Returns const reference to the object entries.
 
auto Items () -> ObjectType &
 Returns mutable reference to the object entries, promoting null → object.
 
auto Elements () const -> const ArrayType &
 Returns const reference to the array elements.
 
auto Elements () -> ArrayType &
 Returns mutable reference to the array elements, promoting null → array.
 
auto GetAtPath (std::string_view path) const -> StatusOr< Value >
 Gets a value at a dot-separated path.
 
void SetAtPath (std::string_view path, const Value &value)
 Sets a value at a dot-separated path, creating intermediate objects.
 
auto HasPath (std::string_view path) const -> bool
 Checks if a path exists in the data.
 
auto Dump (int indent=0) const -> std::string
 Produces a JSON-like string representation.
 
auto operator== (const Value &other) const -> bool
 Value equality (deep comparison for objects/arrays).
 
auto operator!= (const Value &other) const -> bool
 Value inequality.
 

Static Public Member Functions

static auto Object () -> Value
 Creates an empty object value.
 
static auto Array () -> Value
 Creates an empty array value.
 
static auto SplitPath (std::string_view path) -> std::vector< std::string >
 Splits a dot-separated path into its segments.
 
static auto Merge (const Value &base, const Value &overlay) -> Value
 Deep-merges two object values; overlay takes precedence.
 

Detailed Description

A self-contained, recursive value type for configuration data.

This type replaces external JSON dependencies in the core library. It supports: null, bool, int64, double, string, object (map), and array.

Objects use std::map with transparent comparison for efficient std::string_view lookups. Recursive containers are heap-allocated via std::unique_ptr to keep the variant's inline size small; a custom copy constructor deep-copies them, so the pointer is never actually shared and the type has plain value semantics.

Member Typedef Documentation

◆ ArrayType

using cppfig::Value::ArrayType = std::vector<Value>

Ordered sequence of Value elements.

◆ ObjectType

using cppfig::Value::ObjectType = std::map<std::string, Value, std::less<> >

Ordered map of string keys to Value children.

Constructor & Destructor Documentation

◆ Value() [1/12]

cppfig::Value::Value ( )
inline

Constructs a null value.

◆ Value() [2/12]

cppfig::Value::Value ( std::nullptr_t  )
inline

Constructs a null value.

◆ Value() [3/12]

cppfig::Value::Value ( bool  b)
inline

Constructs a boolean value.

◆ Value() [4/12]

cppfig::Value::Value ( int  i)
inline

Constructs an integer value from int.

◆ Value() [5/12]

cppfig::Value::Value ( std::int64_t  i)
inline

Constructs an integer value.

◆ Value() [6/12]

cppfig::Value::Value ( double  d)
inline

Constructs a double value.

◆ Value() [7/12]

cppfig::Value::Value ( float  f)
inline

Constructs a double value from float.

◆ Value() [8/12]

cppfig::Value::Value ( const char *  s)
inline

Constructs a string value from a C string.

◆ Value() [9/12]

cppfig::Value::Value ( std::string  s)
inline

Constructs a string value.

◆ Value() [10/12]

cppfig::Value::Value ( std::string_view  s)
inline

Constructs a string value from string_view.

◆ Value() [11/12]

cppfig::Value::Value ( const Value &  other)
inline

Deep-copies the value (recursive for objects / arrays).

◆ Value() [12/12]

cppfig::Value::Value ( Value &&  )
defaultnoexcept

Move constructor (default, transfers ownership).

◆ ~Value()

cppfig::Value::~Value ( )
default

Destructor (default, unique_ptr handles cleanup).

Member Function Documentation

◆ Array()

static auto cppfig::Value::Array ( ) -> Value
inlinestatic

Creates an empty array value.

◆ Contains()

auto cppfig::Value::Contains ( std::string_view  key) const -> bool
inline

Checks whether the given key exists in an object value.

◆ Dump()

auto cppfig::Value::Dump ( int  indent = 0) const -> std::string
inline

Produces a JSON-like string representation.

Parameters
indentNumber of spaces per indentation level (0 = compact).

◆ Elements() [1/2]

auto cppfig::Value::Elements ( ) -> ArrayType&
inline

Returns mutable reference to the array elements, promoting null → array.

◆ Elements() [2/2]

auto cppfig::Value::Elements ( ) const -> const ArrayType&
inline

Returns const reference to the array elements.

◆ Fits()

template<typename T >
auto cppfig::Value::Fits ( ) const -> bool
inline

Returns true if the stored value is representable as T.

Reports false both when the stored alternative is the wrong kind and when it is numerically out of range for T (an int64 too large for an int, a double too large for a float).

◆ Get()

template<typename T >
auto cppfig::Value::Get ( ) const -> T
inline

Extracts the stored value as the requested type.

Supported types: bool, int, std::int64_t, double, float, std::string. Integer↔double conversions are performed with static_cast when the underlying storage differs from the requested type.

Exceptions
std::bad_variant_accessif the stored value is of another kind.
std::out_of_rangeif the stored value does not fit in T. Discarding the high bits of an out-of-range value would turn a plainly wrong configuration into a plausible-looking one, so callers that cannot guarantee the range should check Fits first (ConfigTraits does).

◆ GetAtPath()

auto cppfig::Value::GetAtPath ( std::string_view  path) const -> StatusOr<Value>
inline

Gets a value at a dot-separated path.

Returns
The value at the path, or a NotFound error if the path is ill-formed or does not resolve.

◆ HasPath()

auto cppfig::Value::HasPath ( std::string_view  path) const -> bool
inline

Checks if a path exists in the data.

◆ IsArray()

auto cppfig::Value::IsArray ( ) const -> bool
inline

Returns true if this value is an array.

◆ IsBoolean()

auto cppfig::Value::IsBoolean ( ) const -> bool
inline

Returns true if this value is a boolean.

◆ IsDouble()

auto cppfig::Value::IsDouble ( ) const -> bool
inline

Returns true if this value is a double.

◆ IsInteger()

auto cppfig::Value::IsInteger ( ) const -> bool
inline

Returns true if this value is an integer.

◆ IsNull()

auto cppfig::Value::IsNull ( ) const -> bool
inline

Returns true if this value is null.

◆ IsNumber()

auto cppfig::Value::IsNumber ( ) const -> bool
inline

Returns true if this value is any numeric type (integer or double).

◆ IsObject()

auto cppfig::Value::IsObject ( ) const -> bool
inline

Returns true if this value is an object (key-value map).

◆ IsString()

auto cppfig::Value::IsString ( ) const -> bool
inline

Returns true if this value is a string.

◆ Items() [1/2]

auto cppfig::Value::Items ( ) -> ObjectType&
inline

Returns mutable reference to the object entries, promoting null → object.

◆ Items() [2/2]

auto cppfig::Value::Items ( ) const -> const ObjectType&
inline

Returns const reference to the object entries.

◆ Merge()

static auto cppfig::Value::Merge ( const Value &  base,
const Value &  overlay 
) -> Value
inlinestatic

Deep-merges two object values; overlay takes precedence.

  • Objects are merged recursively.
  • Arrays and primitives from overlay replace base entirely.

◆ Object()

static auto cppfig::Value::Object ( ) -> Value
inlinestatic

Creates an empty object value.

◆ operator!=()

auto cppfig::Value::operator!= ( const Value &  other) const -> bool
inline

Value inequality.

◆ operator=() [1/2]

auto cppfig::Value::operator= ( const Value &  other) -> Value&
inline

Deep-copy assignment.

◆ operator=() [2/2]

auto cppfig::Value::operator= ( Value &&  ) -> Value &=default
defaultnoexcept

Move assignment (default).

◆ operator==()

auto cppfig::Value::operator== ( const Value &  other) const -> bool
inline

Value equality (deep comparison for objects/arrays).

◆ operator[]() [1/2]

auto cppfig::Value::operator[] ( const std::string &  key) -> Value&
inline

Accesses or creates a child by key, promoting null → object.

◆ operator[]() [2/2]

auto cppfig::Value::operator[] ( const std::string &  key) const -> const Value&
inline

Read-only access to a child by key (returns static null for missing keys).

◆ SetAtPath()

void cppfig::Value::SetAtPath ( std::string_view  path,
const Value &  value 
)
inline

Sets a value at a dot-separated path, creating intermediate objects.

Ill-formed paths are ignored; use SplitPath to validate a path before calling if the caller needs to report the failure.

◆ SplitPath()

static auto cppfig::Value::SplitPath ( std::string_view  path) -> std::vector<std::string>
inlinestatic

Splits a dot-separated path into its segments.

A path is well-formed only if it is non-empty and every segment between the dots is non-empty. Ill-formed paths ("", "a.", ".a", "a..b") yield an empty result and are rejected by the path accessors rather than silently creating blank keys.

◆ TryGet()

template<typename T >
auto cppfig::Value::TryGet ( ) const -> std::optional<T>
inline

Extracts the stored value as T, or nullopt if it is not one.

The library's error model is Status and std::optional everywhere else; this is the accessor to reach for in ConfigTraits specialisations and anywhere the stored kind is not already known, so a type mismatch does not have to be handled as an exception.

◆ TryToText()

auto cppfig::Value::TryToText ( ) const -> std::optional<std::string>
inline

Renders a scalar value as text, or nullopt for objects/arrays.

Flat formats store every leaf as text, so this is the bridge back to ConfigTraits::FromString when a parsed leaf has to be reinterpreted as the type the schema declares.


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