You.i Engine
CYIAny Class Reference

Detailed Description

A class that holds instances of any type that is copy-constructible.

Example:

std::map<CYIString, CYIAny> CreateOverpoweredHammer()
{
CYIAny name = CYIString("Hammer of the Naaru");
CYIAny price(580);
CYIAny weight(45.9f);
std::map<CYIString, CYIAny> hammer;
hammer["name"] = name;
hammer["price"] = price;
hammer["weight"] = weight;
return hammer;
}
Warning
When accessing the content of a CYIAny via Get(), the (decayed) requested static type must exactly match the (decayed) static type given when storing the contained value. In particular, a cast cannot be performed to a base class of the contained static type, nor to an integer type that differs in size or signedness from the contained type.
A CYIAny will assert in debug mode if a cast is attempted to the incorrect type, but no runtime checks exist in release.
Note
References and const values are stripped when storing in a CYIAny (the value is copied in and owned by the CYIAny.) A CYIAny can contain a const pointer, or some other indirection containing const (such as a vector of const pointers); this is considered part of the unique type (separate from an equivalent non-const pointer), and the const-ness must be consistent in assignment and casts.
A CYIAny cannot directly contain a CYIAny by value, though it can contain one indirectly (for example a CYIAny could contain a vector of CYIAnys.)
A CYIAny cannot contain nullptr_t. A nullptr must be explicitly cast to a specific pointer type to be stored in a CYIAny. Remember that nullptr is an int, so nullptr should also be cast to a specific pointer type (otherwise the type contained by the CYIAny will be int32_t.)

#include <framework/YiAny.h>

Public Member Functions

 CYIAny ()
 
template<typename T , std::enable_if_t<!std::is_same< std::decay_t< T >, CYIAny >::value > * = nullptr>
 CYIAny (T &&value)
 
 CYIAny (const CYIAny &other)
 
 CYIAny (CYIAny &&other) noexcept
 
 ~CYIAny ()
 
CYIAnySwap (CYIAny &other)
 
CYIAnyoperator= (const CYIAny &other)
 
CYIAnyoperator= (CYIAny &&other)
 
template<typename T , std::enable_if_t<!std::is_same< std::decay_t< T >, CYIAny >::value > * = nullptr>
CYIAnyoperator= (T &&value)
 
bool Empty () const
 
void Clear ()
 
template<typename T >
bool ContainsType () const
 
bool MatchesType (const CYIAny &rOther) const
 
template<typename T >
std::decay_t< T > & Get ()
 
template<typename T >
const std::decay_t< T > & Get () const
 
CYIString ToString () const
 

Static Public Member Functions

template<typename T >
static size_t GetRequiredHeapMemory ()
 

Friends

class CYIAnyTest
 

Constructor & Destructor Documentation

◆ CYIAny() [1/4]

CYIAny::CYIAny ( )

Initializes the CYIAny as empty.

◆ CYIAny() [2/4]

template<typename T , std::enable_if_t<!std::is_same< std::decay_t< T >, CYIAny >::value > * = nullptr>
CYIAny::CYIAny ( T &&  value)

Initializes the CYIAny by copying or moving the passed value, stored as the template type.

◆ CYIAny() [3/4]

CYIAny::CYIAny ( const CYIAny other)

Initializes the CYIAny with a copy of the value stored in the given CYIAny, stored as the same type as the given CYIAny.

◆ CYIAny() [4/4]

CYIAny::CYIAny ( CYIAny &&  other)
noexcept

Initializes the CYIAny by moving the value stored in the given CYIAny, stored as the same type as the given CYIAny.

◆ ~CYIAny()

CYIAny::~CYIAny ( )

Destroys the CYIAny and its content.

Note
If the CYIAny contains a pointer, it is not deleted. The value itself is destroyed, not what it may point to. Store a std::unique_ptr if the pointed-to value should be deleted when the CYIAny is destroyed.

Member Function Documentation

◆ Clear()

void CYIAny::Clear ( )

Clears the CYIAny, destroying its content and leaving it empty.

Note
If the CYIAny contains a pointer, it is not deleted. The value itself is destroyed, not what it may point to.

◆ ContainsType()

template<typename T >
bool CYIAny::ContainsType ( ) const

Returns true if the CYIAny contains a value of exactly the given template type, if the given template type is a const and/or reference type of exactly the type contained by the CYIAny.

A return value of true means a Get() to the given type is safe.

The template parameter cannot be CYIAny. Const and reference qualifiers are stripped away.

Compatible types are not supported. This will return false even if the given template type is a base class of the contained type. This will also return false even if the given template type is an integer type that differs only in size or signedness from the contained type.

◆ Empty()

bool CYIAny::Empty ( ) const

Returns true if the CYIAny is empty.

Note
Empty is not the same as nullptr or nullptr. A CYIAny may contain a null value of a specific pointer type, in which case it is not empty.

◆ Get() [1/2]

template<typename T >
std::decay_t<T>& CYIAny::Get ( )

Gets a reference to the contained value. The given template type must exactly match the type of the contained value, excluding const and reference qualifiers.

The returned reference is invalidated if the CYIAny is modified other than through the returned reference.

The template parameter cannot CYIAny.

Example:

CYIAny any(3.14f)
float value = any.Get<float>(); // value is 3.14f
float &rValue = any.Get<float>(); // rValue refers to the content of any
rValue = 6.28f; // any now contains 6.28f
Warning
An assertion is triggered in debug mode if the given template type does not match that of the contained value. No type-checking is performed in release.
See also
ContainsType()

◆ Get() [2/2]

template<typename T >
const std::decay_t<T>& CYIAny::Get ( ) const

Gets a const reference to the contained value. The given template type must exactly match the type of the contained value, excluding const and reference qualifiers.

The returned reference is invalidated if the CYIAny is modified.

The template parameter cannot CYIAny.

Warning
An assertion is triggered in debug mode if the given template type does not match that of the contained value. No type-checking is performed in release.
See also
ContainsType()

◆ GetRequiredHeapMemory()

template<typename T >
static size_t CYIAny::GetRequiredHeapMemory ( )
static

Returns the total amount of heap memory needed to store type T in a CYIAny instance. This does not include allocation overheads (such as metadata), and does not include heap allocations performed internally by type T. Returns 0 if the type T can be stored in a CYIAny instance without any heap allocations.

For example, calling GetRequiredHeapMemory<int>() will return 0, and calling GetRequiredHeapMemory<glm::mat4> will return sizeof(glm::mat4).

◆ MatchesType()

bool CYIAny::MatchesType ( const CYIAny rOther) const

Returns true if this CYIAny contains a value of exactly the same type as the given CYIAny.

Also returns true if both this CYIAny and the given CYIAny are empty.

Compatible types are not supported. This will return false even if the type of one CYIAny is a base class of the other. This will also return false even if the types are integers that differ only in size or signedness.

◆ operator=() [1/3]

CYIAny& CYIAny::operator= ( const CYIAny other)

Replaces the content of this CYIAny with a copy of the value stored in the given CYIAny, stored as the same type as the given CYIAny.

◆ operator=() [2/3]

CYIAny& CYIAny::operator= ( CYIAny &&  other)

Replaces the content of this CYIAny by moving the value stored in the given CYIAny, stored as the same type as the given CYIAny.

◆ operator=() [3/3]

template<typename T , std::enable_if_t<!std::is_same< std::decay_t< T >, CYIAny >::value > * = nullptr>
CYIAny& CYIAny::operator= ( T &&  value)

Replaces the content of this CYIAny by copying or moving the passed value, stored as the given template type.

The template parameter cannot be CYIAny or nullptr_t. If you want the CYIAny to contain a null pointer, it must be explicitly cast to a specific pointer type.

◆ Swap()

CYIAny& CYIAny::Swap ( CYIAny other)

Exchanges the content with another CYIAny.

◆ ToString()

CYIString CYIAny::ToString ( ) const

Returns the string representation of the value held in this CYIAny. If this CYIAny is empty, (empty) is returned. For detailed information related to how different types are represented, see CYIObjectPrinter.

Note
This function is mostly used for debugging purposes.
Warning
This function should not be used to retrieve the value contained in a CYIAny when the stored type is CYIString. Get<CYIString>() should be used instead. Using ToString() would result in 'extra' quotes being added to the returned string.
See also
Get()

Friends And Related Function Documentation

◆ CYIAnyTest

friend class CYIAnyTest
friend

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