metadata 0.2.0
Modern C++23 header-only metadata container (JSON-like)
Loading...
Searching...
No Matches
md::Value Class Reference

Discriminated union holding one of the JSON-like alternatives (null, bool, signed/unsigned integer, float/double, string, array, or nested object). More...

#include <value.hpp>

Public Types

using variant_type = std::variant< std::nullptr_t, bool, std::int64_t, std::uint64_t, float, double, std::string, Array, std::unique_ptr< Object > >
 Underlying std::variant type that stores the active alternative.
 

Public Member Functions

 Value () noexcept
 Construct a null-valued Value.
 
 Value (Value &&other) noexcept
 Move-construct from another Value, leaving the source in a valid but unspecified state.
 
Valueoperator= (Value &&other) noexcept
 Move-assign from another Value.
 
 Value (const Value &other)
 Deep-copy construct from another Value.
 
Valueoperator= (const Value &other)
 Deep-copy assign from another Value.
 
 ~Value ()
 Destructor.
 
Valueoperator= (std::initializer_list< Value > il)
 Assign an Array from a braced list of values.
 
Valueoperator= (std::initializer_list< std::pair< const std::string, Value > > il)
 Assign an Object from a braced list of key/value pairs.
 
 Value (std::nullptr_t) noexcept
 Construct a null-valued Value.
 
template<class B >
requires std::same_as<B, bool>
 Value (B b) noexcept
 Construct a boolean-valued Value.
 
template<detail::SignedIntLike T>
 Value (T x) noexcept
 Construct a signed-integer Value (stored as std::int64_t).
 
template<detail::UnsignedIntLike T>
 Value (T x) noexcept
 Construct an unsigned-integer Value (stored as std::uint64_t).
 
template<detail::FloatLike T>
 Value (T x) noexcept
 Construct a floating-point Value; float stays float, others widen to double.
 
 Value (std::string s)
 Construct a string-valued Value from an owning std::string.
 
 Value (std::string_view s)
 Construct a string-valued Value from a std::string_view.
 
 Value (const char *s)
 Construct a string-valued Value from a C string.
 
 Value (Array a)
 Construct an array-valued Value.
 
 Value (Object o)
 Construct an object-valued Value.
 
 Value (std::initializer_list< std::pair< const std::string, Value > > il)
 Construct an object-valued Value from a braced list of key/value pairs.
 
template<class T >
 Value (T *)=delete
 Deleted to prevent silent pointer-to-bool conversions.
 
bool is_null () const noexcept
 True if the value holds nullptr.
 
bool is_bool () const noexcept
 True if the value holds a bool.
 
bool is_int () const noexcept
 True if the value holds a signed integer (std::int64_t).
 
bool is_uint () const noexcept
 True if the value holds an unsigned integer (std::uint64_t).
 
bool is_float () const noexcept
 True if the value holds a float.
 
bool is_double () const noexcept
 True if the value holds a double.
 
bool is_number () const noexcept
 True if the value holds any numeric alternative.
 
bool is_string () const noexcept
 True if the value holds a std::string.
 
bool is_array () const noexcept
 True if the value holds an Array.
 
bool is_object () const noexcept
 True if the value holds a nested Object.
 
bool as_bool () const
 Return the bool; throws std::bad_variant_access on type mismatch.
 
std::int64_t as_int () const
 Return the signed integer; throws on type mismatch.
 
std::uint64_t as_uint () const
 Return the unsigned integer; throws on type mismatch.
 
float as_float () const
 Return the float; strict — throws if the value isn't a float.
 
double as_double () const
 Return as double, widening from int64/uint64/float as needed.
 
std::string & as_string ()
 Access the string alternative; throws on type mismatch.
 
const std::string & as_string () const
 Access the string alternative; throws on type mismatch.
 
Array & as_array ()
 Access the array alternative; throws on type mismatch.
 
const Array & as_array () const
 Access the array alternative; throws on type mismatch.
 
Objectas_object ()
 Access the nested object; throws on type mismatch.
 
const Objectas_object () const
 Access the nested object; throws on type mismatch.
 
bool * as_bool_if () noexcept
 Return a pointer to the bool, or nullptr if the value isn't a bool.
 
const bool * as_bool_if () const noexcept
 Return a pointer to the bool, or nullptr if the value isn't a bool.
 
std::int64_t * as_int_if () noexcept
 Return a pointer to the signed integer, or nullptr if not held.
 
const std::int64_t * as_int_if () const noexcept
 Return a pointer to the signed integer, or nullptr if not held.
 
std::uint64_t * as_uint_if () noexcept
 Return a pointer to the unsigned integer, or nullptr if not held.
 
const std::uint64_t * as_uint_if () const noexcept
 Return a pointer to the unsigned integer, or nullptr if not held.
 
float * as_float_if () noexcept
 Return a pointer to the float, or nullptr if not held.
 
const float * as_float_if () const noexcept
 Return a pointer to the float, or nullptr if not held.
 
double * as_double_if () noexcept
 Return a pointer to the double, or nullptr if not held.
 
const double * as_double_if () const noexcept
 Return a pointer to the double, or nullptr if not held.
 
std::string * as_string_if () noexcept
 Return a pointer to the string, or nullptr if not held.
 
const std::string * as_string_if () const noexcept
 Return a pointer to the string, or nullptr if not held.
 
Array * as_array_if () noexcept
 Return a pointer to the array, or nullptr if not held.
 
const Array * as_array_if () const noexcept
 Return a pointer to the array, or nullptr if not held.
 
Objectas_object_if () noexcept
 Return a pointer to the nested object, or nullptr if not held.
 
const Objectas_object_if () const noexcept
 Return a pointer to the nested object, or nullptr if not held.
 
template<class T >
T * get_if () noexcept
 Return a pointer to the alternative of type T, or nullptr if not held.
 
template<class T >
const T * get_if () const noexcept
 Return a pointer to the alternative of type T, or nullptr if not held.
 
template<class T >
value_or (T fallback) const
 Return the held T by value, or fallback if a different alternative is active.
 
variant_typeraw () noexcept
 Return the underlying std::variant for advanced access.
 
const variant_typeraw () const noexcept
 Return the underlying std::variant for advanced access.
 
std::size_t index () const noexcept
 Return the zero-based index of the active alternative.
 

Friends

bool operator== (const Value &a, const Value &b) noexcept
 Deep value-equality compare two Values.
 
bool operator!= (const Value &a, const Value &b) noexcept
 Negation of operator==.
 

Detailed Description

Discriminated union holding one of the JSON-like alternatives (null, bool, signed/unsigned integer, float/double, string, array, or nested object).


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