cppcore
A small, portable C++ core library
Loading...
Searching...
No Matches
cppcore::Variant Class Reference

This class can be used to store integer or float data with a dynamical binding. More...

#include <Variant.h>

Public Types

enum  Type {
  Invalid = -1 , Byte , Int , Int3 ,
  Int4 , Float , Float3 , Float4 ,
  Float4x4 , String , Boolean , Count
}
 This enum describes the variable type of the variant instance. More...

Public Member Functions

 Variant ()=default
 The class default constructor.
 Variant (Type type, void *pData, size_t numItems)
 The class constructor with type information and data.
 Variant (bool value)
 The class constructor with a boolean value.
 Variant (const Variant &other)
 The class copy constructor.
 ~Variant ()
 The class destructor.
Type getType () const
 Returns the type of the instance.
size_t getSize () const
 Will return the size in bytes of the stored type.
void * getPtr () const
 Will return a raw pointer showing to the data.
void setByte (uint8_t value)
 Will set the payload to the given value.
uint8_t getByte () const
 Will return the current payload as a byte value.
void setInt (int32_t val)
 Sets a new integer value, old values will be released and destroyed.
int32_t getInt () const
 Returns the integer value of the instance.
void setInt3 (int32_t val1, int32_t val2, int32_t val3)
 Set a new integer vector, the old data will be released and destroyed.
int32_t * getInt3 () const
 Returns a pointer to the first element of the integer vector.
void setInt4 (int32_t val1, int32_t val2, int32_t val3, int32_t val4)
 Set a new integer vector, the old data will be released and destroyed.
int32_t * getInt4 () const
 Returns a pointer to the first element of the integer vector.
void setFloat (float val)
 Returns the float value of the instance.
float getFloat () const
 Returns the float value of the instance.
void setFloat3 (float val1, float val2, float val3)
 Set a new float vector, the old data will be released and destroyed.
float * getFloat3 () const
 Returns a pointer to the first element of the float vector.
void setFloat4 (float val1, float val2, float val3, float val4)
 Set a new float vector, the old data will be released and destroyed.
float * getFloat4 () const
 Returns a pointer to the first element of the float vector.
void setFloat4x4 (float *pData)
 Assigns a new float 4x4 item.
float * getFloat4x4 () const
 Returns the pointer to the float 4x4 value.
void setStdString (const std::string &value)
 Sets a string value.
const char * getString () const
 Returns a constant reference to the string value.
void setBool (bool value)
 Will set a new bool value.
bool getBool () const
 Will return the bool value.
void clear ()
 Clears the variant data, type will set back to None.
bool operator== (const Variant &rOther) const
 Operator implementations.
Variant & operator= (const Variant &rOther)

Static Public Member Functions

static Variant * createFromString (const std::string &value)
 Static helper method to generate a new variant storing a std::string.

Protected Member Functions

bool isValid (Type type, size_t numItems) const
 Performs a validation.
void reserve (Type type, size_t size)
 Will reserve a buffer for the requested types.

Detailed Description

This class can be used to store integer or float data with a dynamical binding.

It stores the type information at the runtime instead of checking this to compile-time. So it is possible to pass integer or float arguments dynamically to arbitrary customers without changing the calling signature of the used interface. If you are trying to get a float value even if the instance stores currently an integer value on a debug build an assertion will be thrown. On a release build it is possible to get values, but an error will be logged.

Definition at line 46 of file Variant.h.

Member Enumeration Documentation

◆ Type

This enum describes the variable type of the variant instance.

Enumerator
Invalid 

Initialization value.

Byte 

1 Byte type.

Int 

Integer value.

Int3 

Integer vector, 3 components.

Int4 

Integer vector, 4 components.

Float 

Float value.

Float3 

Float vector, 3 components.

Float4 

Float vector, 4 components.

Float4x4 

Float matrix, 4 x 4 components.

String 

String value.

Boolean 

Boolean type.

Count 

Upper limit.

Definition at line 50 of file Variant.h.

Constructor & Destructor Documentation

◆ Variant() [1/4]

cppcore::Variant::Variant ( )
default

The class default constructor.

References Invalid, and Variant().

Referenced by createFromString(), operator=(), operator==(), Variant(), and Variant().

◆ Variant() [2/4]

cppcore::Variant::Variant ( Type type,
void * pData,
size_t numItems )
inline

The class constructor with type information and data.

A data buffer will be used to pass the data. The size of the buffer will be check on runtime as well.

Parameters
typeType enumeration.
pDataPointer to the data buffer.
numItemsSize of the data buffer.

Definition at line 222 of file Variant.h.

References isValid(), reserve(), setStdString(), and String.

◆ Variant() [3/4]

cppcore::Variant::Variant ( bool value)
inlineexplicit

The class constructor with a boolean value.

Parameters
value[in] The boolean value.

Definition at line 237 of file Variant.h.

References Boolean, and reserve().

◆ Variant() [4/4]

cppcore::Variant::Variant ( const Variant & other)
inline

The class copy constructor.

Parameters
other[in] Other instance to copy from.

Definition at line 242 of file Variant.h.

References getString(), Invalid, reserve(), setStdString(), String, and Variant().

◆ ~Variant()

cppcore::Variant::~Variant ( )
inline

The class destructor.

Definition at line 255 of file Variant.h.

References clear().

Member Function Documentation

◆ clear()

void cppcore::Variant::clear ( )
inline

Clears the variant data, type will set back to None.

Definition at line 421 of file Variant.h.

References Invalid.

Referenced by setBool(), setByte(), setFloat(), setFloat3(), setFloat4(), setFloat4x4(), setInt(), setInt3(), setInt4(), setStdString(), and ~Variant().

◆ createFromString()

Variant * cppcore::Variant::createFromString ( const std::string & value)
inlinestatic

Static helper method to generate a new variant storing a std::string.

Parameters
valueThe std::string data.
Returns
Thew new created variant as a pointer.

Definition at line 431 of file Variant.h.

References setStdString(), and Variant().

◆ getBool()

bool cppcore::Variant::getBool ( ) const
inline

Will return the bool value.

Returns
The bool value.

Definition at line 415 of file Variant.h.

References Boolean.

◆ getByte()

unsigned char cppcore::Variant::getByte ( ) const
inline

Will return the current payload as a byte value.

Returns
The byte value.

Definition at line 277 of file Variant.h.

◆ getFloat()

float cppcore::Variant::getFloat ( ) const
inline

Returns the float value of the instance.

Returns
The stored float value will be returned.

Definition at line 334 of file Variant.h.

References Float.

◆ getFloat3()

float * cppcore::Variant::getFloat3 ( ) const
inline

Returns a pointer to the first element of the float vector.

You can access the 2 following with the index operator for instance.

Returns
Pointer to the first component of th vector array.

Definition at line 350 of file Variant.h.

References Float3.

◆ getFloat4()

float * cppcore::Variant::getFloat4 ( ) const
inline

Returns a pointer to the first element of the float vector.

You can access the 3 following with the index operator for instance.

Returns
Pointer to the first component of th vector array.

Definition at line 368 of file Variant.h.

References Float4.

◆ getFloat4x4()

float * cppcore::Variant::getFloat4x4 ( ) const
inline

Returns the pointer to the float 4x4 value.

Returns
A pointer showing to the 4x4 float item.

Definition at line 379 of file Variant.h.

References Float4x4.

◆ getInt()

int cppcore::Variant::getInt ( ) const
inline

Returns the integer value of the instance.

Returns
The stored integer value will be returned.

Definition at line 287 of file Variant.h.

References Int.

◆ getInt3()

int * cppcore::Variant::getInt3 ( ) const
inline

Returns a pointer to the first element of the integer vector.

You can access the 2 following with the index operator for instance.

Returns
Pointer to the first component of th vector array.

Definition at line 304 of file Variant.h.

References Int3.

◆ getInt4()

int * cppcore::Variant::getInt4 ( ) const
inline

Returns a pointer to the first element of the integer vector.

You can access the 3 following with the index operator for instance.

Returns
Pointer to the first component of th vector array.

Definition at line 322 of file Variant.h.

References Int4.

◆ getPtr()

void * cppcore::Variant::getPtr ( ) const
inline

Will return a raw pointer showing to the data.

Returns
The pointer.

Definition at line 267 of file Variant.h.

◆ getSize()

size_t cppcore::Variant::getSize ( ) const
inline

Will return the size in bytes of the stored type.

Returns
The size in bytes.

Definition at line 263 of file Variant.h.

◆ getString()

const char * cppcore::Variant::getString ( ) const
inline

Returns a constant reference to the string value.

Returns
A pointer showing to the data buffer of the string.

Definition at line 403 of file Variant.h.

References String.

Referenced by Variant().

◆ getType()

Variant::Type cppcore::Variant::getType ( ) const
inline

Returns the type of the instance.

Returns
TYpe enum of the current dynamic type of the instance.

Definition at line 259 of file Variant.h.

◆ isValid()

bool cppcore::Variant::isValid ( Type type,
size_t numItems ) const
inlineprotected

Performs a validation.

Parameters
typeThe type to validate for
numItemsThe number of items to validate.
Returns
true, if data is valid.

Definition at line 464 of file Variant.h.

References Byte, Float, Float3, Float4, Int, Int3, Int4, Invalid, and String.

Referenced by Variant().

◆ operator=()

Variant & cppcore::Variant::operator= ( const Variant & rOther)
inline

Definition at line 454 of file Variant.h.

References reserve(), and Variant().

◆ operator==()

bool cppcore::Variant::operator== ( const Variant & rOther) const
inline

Operator implementations.

Definition at line 438 of file Variant.h.

References Variant().

◆ reserve()

void cppcore::Variant::reserve ( Type type,
size_t size )
inlineprotected

Will reserve a buffer for the requested types.

Parameters
typeThe requested type.
sizeThe number of items.

Definition at line 493 of file Variant.h.

References Boolean, Byte, Float, Float3, Float4, Float4x4, Int, Int3, and Int4.

Referenced by operator=(), setBool(), setByte(), setFloat(), setFloat3(), setFloat4(), setFloat4x4(), setInt(), setInt3(), setInt4(), Variant(), Variant(), and Variant().

◆ setBool()

void cppcore::Variant::setBool ( bool value)
inline

Will set a new bool value.

Parameters
valueThe new bool value.

Definition at line 409 of file Variant.h.

References Boolean, clear(), and reserve().

◆ setByte()

void cppcore::Variant::setByte ( uint8_t value)
inline

Will set the payload to the given value.

Parameters
value[in] The new given payload.

Definition at line 271 of file Variant.h.

References Byte, clear(), and reserve().

◆ setFloat()

void cppcore::Variant::setFloat ( float val)
inline

Returns the float value of the instance.

Parameters
valA new float value.

Definition at line 328 of file Variant.h.

References clear(), Float, and reserve().

◆ setFloat3()

void cppcore::Variant::setFloat3 ( float val1,
float val2,
float val3 )
inline

Set a new float vector, the old data will be released and destroyed.

Parameters
val1Component 1, float.
val2Component 2, float.
val3Component 3, float.

Definition at line 339 of file Variant.h.

References clear(), Float3, and reserve().

◆ setFloat4()

void cppcore::Variant::setFloat4 ( float val1,
float val2,
float val3,
float val4 )
inline

Set a new float vector, the old data will be released and destroyed.

Parameters
val1Component 1, float.
val2Component 2, float.
val3Component 3, float.
val4Component 4, float.

Definition at line 355 of file Variant.h.

References clear(), Float4, and reserve().

◆ setFloat4x4()

void cppcore::Variant::setFloat4x4 ( float * pData)
inline

Assigns a new float 4x4 item.

Parameters
pData[in] A float pointer to the 4x4 buffer.

Definition at line 373 of file Variant.h.

References clear(), Float4x4, and reserve().

◆ setInt()

void cppcore::Variant::setInt ( int32_t val)
inline

Sets a new integer value, old values will be released and destroyed.

Parameters
val[in] A new integer value.

Definition at line 281 of file Variant.h.

References clear(), Int, and reserve().

◆ setInt3()

void cppcore::Variant::setInt3 ( int32_t val1,
int32_t val2,
int32_t val3 )
inline

Set a new integer vector, the old data will be released and destroyed.

Parameters
val1Component 1, integer.
val2Component 2, integer.
val3Component 3, integer.

Definition at line 293 of file Variant.h.

References clear(), Int3, and reserve().

◆ setInt4()

void cppcore::Variant::setInt4 ( int32_t val1,
int32_t val2,
int32_t val3,
int32_t val4 )
inline

Set a new integer vector, the old data will be released and destroyed.

Parameters
val1Component 1, integer.
val2Component 2, integer.
val3Component 3, integer.
val4Component 3, integer.

Definition at line 309 of file Variant.h.

References clear(), Int4, and reserve().

◆ setStdString()

void cppcore::Variant::setStdString ( const std::string & value)
inline

Sets a string value.

Parameters
valueThe new string value.

Definition at line 384 of file Variant.h.

References clear(), and String.

Referenced by createFromString(), Variant(), and Variant().


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