libpromeki main
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
VariantImpl< Types > Class Template Reference

Type-safe tagged union that can hold any of the types listed in PROMEKI_VARIANT_TYPES. More...

#include <variant.h>

Public Types

enum  Type {
  TypeInvalid , TypeBool , TypeU8 , TypeS8 ,
  TypeU16 , TypeS16 , TypeU32 , TypeS32 ,
  TypeU64 , TypeS64 , TypeFloat , TypeDouble ,
  TypeString , TypeDateTime , TypeTimeStamp , TypeSize2D ,
  TypeUUID , TypeTimecode , TypeRational
}
 Enumerates every type the variant can hold. More...
 

Public Member Functions

 VariantImpl ()=default
 Default-constructs an invalid (empty) variant holding std::monostate.
 
template<typename T >
 VariantImpl (const T &value)
 Constructs a variant holding a copy of value.
 
bool isValid () const
 Returns true if the variant holds a value other than std::monostate.
 
template<typename T >
void set (const T &value)
 Replaces the currently held value with value.
 
template<typename To >
To get (Error *err=nullptr) const
 Converts the stored value to the requested type To.
 
Type type () const
 Returns the Type enumerator for the currently held value.
 
const chartypeName () const
 Returns the human-readable type name of the currently held value.
 
VariantImpl toStandardType () const
 Converts complex types to their String representation, leaving simple types unchanged.
 

Static Public Member Functions

static const chartypeName (Type id)
 Returns the human-readable C++ type name for the given Type enumerator.
 
static VariantImpl fromJson (const nlohmann::json &val)
 Constructs a VariantImpl from a JSON value, inferring the best native type.
 

Detailed Description

template<typename... Types>
class VariantImpl< Types >

Type-safe tagged union that can hold any of the types listed in PROMEKI_VARIANT_TYPES.

VariantImpl is a thin wrapper around std::variant that adds a Type enum, human-readable type names, and automatic type-conversion logic via the templated get() method. It is not intended to be used directly; instead use

Example
Variant v = 42;
String s = v.get<String>(); // "42"
v.set(String("hello"));
bool valid = v.isValid(); // true
Variant::Type t = v.type(); // Variant::TypeString
Dynamic array container wrapping std::vector.
Definition list.h:40
bool set(size_t index, const T &val)
Sets an item in the list by index.
Definition list.h:541
Encoding-aware string class with copy-on-write semantics.
Definition string.h:35
the Variant type alias which is instantiated with the concrete type list.
Template Parameters
TypesThe set of types the variant can hold (generated from PROMEKI_VARIANT_TYPES).

Member Enumeration Documentation

◆ Type

template<typename... Types>
enum VariantImpl::Type

Enumerates every type the variant can hold.

The enumerator order matches the template argument order so that std::variant::index() can be cast directly to a Type value.

Constructor & Destructor Documentation

◆ VariantImpl()

template<typename... Types>
template<typename T >
VariantImpl< Types >::VariantImpl ( const T value)
inline

Constructs a variant holding a copy of value.

Template Parameters
TThe type of the value; must be one of the supported variant types.
Parameters
valueThe value to store.

Member Function Documentation

◆ fromJson()

template<typename... Types>
static VariantImpl VariantImpl< Types >::fromJson ( const nlohmann::json val)
inlinestatic

Constructs a VariantImpl from a JSON value, inferring the best native type.

Mapping rules:

  • null -> invalid (default-constructed) variant
  • boolean -> bool
  • unsigned integer -> uint64_t
  • signed integer -> int64_t
  • floating-point -> double
  • string -> String
  • anything else -> String (via json::dump())
Parameters
valThe JSON value to convert.
Returns
A VariantImpl holding the converted value.

◆ get()

template<typename... Types>
template<typename To >
To VariantImpl< Types >::get ( Error err = nullptr) const
inline

Converts the stored value to the requested type To.

The method uses std::visit with a compile-time conversion matrix. If the stored type is already To, the value is returned directly. Otherwise an appropriate conversion is attempted (numeric casts, string parsing, toString() calls, etc.). When no conversion path exists, a default-constructed To is returned and err (if non-null) is set to Error::Invalid.

Template Parameters
ToThe desired result type.
Parameters
errOptional pointer to an Error that receives Error::Ok on success or Error::Invalid when the conversion is not possible.
Returns
The converted value, or a default-constructed To on failure.

◆ set()

template<typename... Types>
template<typename T >
void VariantImpl< Types >::set ( const T value)
inline

Replaces the currently held value with value.

Template Parameters
TThe type of the new value; must be one of the supported variant types.
Parameters
valueThe value to store.

◆ toStandardType()

template<typename... Types>
VariantImpl VariantImpl< Types >::toStandardType ( ) const
inline

Converts complex types to their String representation, leaving simple types unchanged.

Types such as String, DateTime, TimeStamp, Size2Du32, UUID, Timecode, and Rational are converted to String via get<std::string>(). All other types (numeric, bool, invalid) are returned as-is.

Returns
A new VariantImpl containing either the original value or its String representation.

◆ typeName()

template<typename... Types>
static const char * VariantImpl< Types >::typeName ( Type  id)
inlinestatic

Returns the human-readable C++ type name for the given Type enumerator.

Parameters
[in]idThe Type enumerator value.
Returns
A null-terminated string such as "bool", "String", etc.

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