Gorgon Game Engine
Any Class Reference

This class can hold any other information providing type erasure. More...

Public Member Functions

 Any ()
 Default constructor. More...
 
 Any (Any &&any)
 Move constructor. More...
 
 Any (const Any &any)
 Copy constructor. More...
 
template<class T_ >
 Any (const T_ &data)
 Creates a new Any from the given data. More...
 
 Any (const TMP::RTTS &typeinterface, void *data)
 Unsafe! Constructs any from give raw data. More...
 
 Any (void *data, const TMP::RTTS &typeinterface)
 Unsafe! Constructs any from give raw data. More...
 
 ~Any ()
 Destructor. More...
 
void AssumeRaw (TMP::RTTS &type, void *data)
 Unsafe! This function sets the raw data contained within any, while modifying its type data. More...
 
void Clear ()
 Clears the content of the any. More...
 
void * Disown ()
 Unsafe! Disowns the data contained in this any. More...
 
template<class T_ >
T_ & Get ()
 Returns the value contained with this any. More...
 
template<class T_ >
const T_ & Get () const
 Returns the value contained with this any. More...
 
void * GetRaw () const
 Unsafe! This function returns raw data contained within any. More...
 
std::string GetTypeName () const
 
bool IsPointer () const
 Checks if any contains a pointer. More...
 
bool IsSameType (const Any &other) const
 Checks whether the Any is the same type with the given type. More...
 
bool IsSet () const
 Checks whether the Any is set. More...
 
bool operator!= (const Any &content) const
 Compares two Any variables. More...
 
template<class T_ >
bool operator!= (const T_ &content) const
 Compares the contents of this Any to the given value. More...
 
Anyoperator= (Any &&any)
 Moves the information in the given Any to this Any. More...
 
Anyoperator= (const Any &any)
 Copies the information in the given Any. More...
 
template<class T_ >
Anyoperator= (const T_ &value)
 Set the content of the Any to the given value. More...
 
bool operator== (const Any &content) const
 Compares two Any variables. More...
 
template<class T_ >
bool operator== (const T_ &content) const
 Compares the contents of this Any to the given value. More...
 
void * Pointer () const
 Returns the pointer without type information. More...
 
template<class T_ >
void Set (const T_ &data)
 Set the content of the Any to the given value. More...
 
template<class T_ >
void Set (const T_ &data, TMP::RTTS *type)
 Unsafe! This function sets the data contained within any with regular means. More...
 
template<class T_ >
void Set (T_ &&data)
 Set the content of the Any to the given value. More...
 
void SetRaw (TMP::RTTS *type, void *data)
 Unsafe! This function sets the raw data contained within any, while modifying its type data. More...
 
void SetRaw (void *data)
 Unsafe! This function sets the raw data contained within any, without modifying its type data. More...
 
void SetType (const TMP::RTTS &type)
 Unsafe! This function modifies type information of the data content. More...
 
void Swap (Any &other)
 Swaps the contents of the current any with another. More...
 
template<class T_ >
bool TypeCheck () const
 Checks whether the Any is the same type with the given type. More...
 
TMP::RTTITypeInfo () const
 Returns TypeInfo used by current data. More...
 
TMP::RTTSTypeServices () const
 Returns TypeInterface used by this any. More...
 
template<class T_ >
T_ UnsafeGet () const
 Unsafe version of Get. More...
 

Protected Attributes

void * content
 Data stored in this any. More...
 
TMP::RTTStype
 Type of the data stored. More...
 

Detailed Description

This class can hold any other information providing type erasure.

It requires the type to be either copy constructible or move constructible. Type safety is ensured during debug using RTTI information.

This class only checks type safety in debug mode. In release it only checks for Any being empty. This check can also be removed if it causes bottleneck. Define GORGON_FAST_ANY to remove these checks.

Currently cannot work with object without copy constructor.

Constructor & Destructor Documentation

◆ Any() [1/6]

Any ( )

Default constructor.

Initializes and empty Any.

◆ Any() [2/6]

Any ( const TMP::RTTS typeinterface,
void *  data 
)

Unsafe! Constructs any from give raw data.

Both typeinterface and data are duplicated.

Warning
Using this constructor might be dangerous

References RTTS::Clone(), Any::content, RTTS::Duplicate(), and Any::type.

◆ Any() [3/6]

Any ( void *  data,
const TMP::RTTS typeinterface 
)

Unsafe! Constructs any from give raw data.

typeinterface is duplicated. Ownership of data is taken.

Warning
Using this constructor might be dangerous

References RTTS::Clone(), Any::content, RTTS::Duplicate(), and Any::type.

◆ Any() [4/6]

Any ( const T_ &  data)

Creates a new Any from the given data.

This constructor duplicates the given data. Requires type in the copied Any to be copy constructible.

◆ Any() [5/6]

Any ( const Any any)

Copy constructor.

Requires type in the copied Any to be copy constructible.

References RTTS::Clone(), Any::content, RTTS::Duplicate(), and Any::type.

◆ Any() [6/6]

Any ( Any &&  any)

Move constructor.

Requires type in the copied Any to be move constructible.

References Any::Swap().

◆ ~Any()

~Any ( )

Destructor.

References Any::Clear().

Member Function Documentation

◆ AssumeRaw()

void AssumeRaw ( TMP::RTTS type,
void *  data 
)

Unsafe! This function sets the raw data contained within any, while modifying its type data.

type is duplicated, whereas data ownership is assumed.

Warning
this function is unsafe

References Any::Clear(), Any::content, RTTS::Duplicate(), and Any::type.

◆ Clear()

void Clear ( )

Clears the content of the any.

References Any::content, RTTS::Delete(), and Any::type.

◆ Disown()

void* Disown ( )

Unsafe! Disowns the data contained in this any.

The data is not freed. This function returns the raw pointer that is disowned

References Any::content, and Any::type.

◆ Get() [1/2]

T_& Get ( )

Returns the value contained with this any.

If this Any is not const, this method could be used to move the object out of Any. It can also be used to modify the value contained within this Any.

Exceptions
std::runtime_errorif Any is empty
std::bad_cast(debug only) if types do not match

References ASSERT, Any::content, RTTI::IsSameType(), RTTI::Name(), and Any::type.

◆ Get() [2/2]

const T_& Get ( ) const

Returns the value contained with this any.

If this Any is not const, this method could be used to move the object out of Any. It can also be used to modify the value contained within this Any.

Exceptions
std::runtime_errorif Any is empty
std::bad_cast(debug only) if types do not match

References ASSERT, Any::content, RTTI::IsSameType(), RTTI::Name(), and Any::type.

◆ GetRaw()

void* GetRaw ( ) const

Unsafe! This function returns raw data contained within any.

Warning
this function is unsafe

References Any::content.

◆ GetTypeName()

std::string GetTypeName ( ) const

◆ IsPointer()

bool IsPointer ( ) const

Checks if any contains a pointer.

References RTTI::IsPointer(), and Any::type.

◆ IsSameType()

bool IsSameType ( const Any other) const

Checks whether the Any is the same type with the given type.

References RTTI::IsSameType(), Any::type, and RTTI::TypeInfo().

◆ IsSet()

bool IsSet ( ) const

Checks whether the Any is set.

References Any::content.

◆ operator!=() [1/2]

bool operator!= ( const Any content) const

Compares two Any variables.

Returns
true if both are empty or have their contents equal.
Exceptions
std::bad_castif types do not match

References Any::content, and Any::operator==().

◆ operator!=() [2/2]

bool operator!= ( const T_ &  content) const

Compares the contents of this Any to the given value.

The value should be the same type as this Any. Even if it is possible, no cross type comparison will be performed. You may use any.Get<originaltype>() == othertype_variable

Exceptions
std::runtime_errorif Any is empty
std::bad_cast(debug only) if types do not match

References Any::content, and Any::operator==().

◆ operator=() [1/3]

Any& operator= ( Any &&  any)

Moves the information in the given Any to this Any.

This function directly moves the pointer therefore, no copying or moving performed on the original type.

References Any::Clear(), and Any::Swap().

◆ operator=() [2/3]

Any& operator= ( const Any any)

Copies the information in the given Any.

It requires type in the copied Any to be copy constructible.

References Any::Clear(), RTTS::Clone(), Any::content, RTTS::Duplicate(), and Any::type.

◆ operator=() [3/3]

Any& operator= ( const T_ &  value)

Set the content of the Any to the given value.

The value is duplicated. Requires T_ to be copy constructible.

◆ operator==() [1/2]

bool operator== ( const Any content) const

Compares two Any variables.

Returns
true if both are empty or have their contents equal.
Exceptions
std::bad_cast(debug only) if types do not match

References ASSERT, Any::content, RTTI::GetSize(), RTTI::Name(), Any::type, and RTTI::TypeInfo().

◆ operator==() [2/2]

bool operator== ( const T_ &  content) const

Compares the contents of this Any to the given value.

The value should be the same type as this Any. Even if it is possible, no cross type comparison will be performed. You may use any.Get<originaltype>() == othertype_variable

Exceptions
std::runtime_errorif Any is empty
std::bad_cast(debug only) if types do not match

References ASSERT, Any::content, RTTI::IsSameType(), RTTI::Name(), and Any::type.

◆ Pointer()

void* Pointer ( ) const

Returns the pointer without type information.

References Any::content.

◆ Set() [1/3]

void Set ( const T_ &  data)

Set the content of the Any to the given value.

The value is duplicated. Requires T_ to be copy constructible.

References Any::Clear().

◆ Set() [2/3]

void Set ( const T_ &  data,
TMP::RTTS type 
)

Unsafe! This function sets the data contained within any with regular means.

However, the type info is user supplied. Its primary aim is to be able to set pointer types from from void pointers. This can also be used as type casting.

Warning
this function is unsafe

References Any::Clear(), RTTS::Clone(), Any::content, RTTS::Duplicate(), and Any::type.

◆ Set() [3/3]

void Set ( T_ &&  data)

Set the content of the Any to the given value.

The value is moved. Requires T_ to be move constructible.

References Any::Clear().

◆ SetRaw() [1/2]

void SetRaw ( TMP::RTTS type,
void *  data 
)

Unsafe! This function sets the raw data contained within any, while modifying its type data.

type and data are duplicated.

Warning
this function is unsafe

References Any::Clear(), RTTS::Clone(), Any::content, RTTS::Duplicate(), and Any::type.

◆ SetRaw() [2/2]

void SetRaw ( void *  data)

Unsafe! This function sets the raw data contained within any, without modifying its type data.

data is duplicated.

Warning
this function is unsafe

References RTTS::Clone(), Any::content, RTTS::Delete(), and Any::type.

◆ SetType()

void SetType ( const TMP::RTTS type)

Unsafe! This function modifies type information of the data content.

type is duplicated

Warning
this function is extremely unsafe, basically, it performs reinterpret_cast

References RTTS::Duplicate(), and Any::type.

◆ Swap()

void Swap ( Any other)

Swaps the contents of the current any with another.

Does not perform copying or moving.

References Any::content, Gorgon::swap(), and Any::type.

◆ TypeCheck()

bool TypeCheck ( ) const

Checks whether the Any is the same type with the given type.

References RTTI::IsSameType(), and Any::type.

◆ TypeInfo()

TMP::RTTI& TypeInfo ( ) const

Returns TypeInfo used by current data.

References ASSERT, and Any::type.

◆ TypeServices()

TMP::RTTS* TypeServices ( ) const

Returns TypeInterface used by this any.

References Any::type.

◆ UnsafeGet()

T_ UnsafeGet ( ) const

Unsafe version of Get.

Should be used as last resort. Only const version is available.

Warning
Does not perform type check even in debug mode
Exceptions
std::runtime_errorif Any is empty

References Any::content, and Any::type.

Member Data Documentation

◆ content

void* content
protected

Data stored in this any.

◆ type

TMP::RTTS* type
protected

Type of the data stored.


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