Public Member Functions | List of all members
aurora::Any Class Reference

Type-erased class holding any value. More...

Public Member Functions

 Any ()
 Construct empty value.
 
template<typename T >
 Any (const T &value)
 Construct from arbitrary value.
 
 Any (const Any &origin)
 Copy constructor.
 
 Any (Any &&source)
 Move constructor.
 
template<typename T >
Anyoperator= (const T &value)
 Assignment operator from value.
 
Anyoperator= (const Any &origin)
 Copy assignment operator.
 
Anyoperator= (Any &&source)
 Move assignment operator.
 
 ~Any ()
 Destructor.
 
void swap (Any &other)
 Swaps the Any with another Any of the same type.
 
template<typename T >
T & get ()
 Returns a reference to the contained value. More...
 
template<typename T >
T * check ()
 Returns a pointer to the contained value. More...
 
 operator SafeBool () const
 Check if the object is not empty.
 

Detailed Description

Type-erased class holding any value.

This class can either hold a single value of arbitrary type, or it can be empty. Any erases the contained type at compile time while preserving value semantics. It is thus a type-safe and RAII-managed alternative to void* pointers.

Usage examples:

aurora::Any any = 32; // store int
any = 44l; // store long
long& ref = any.get<long>(); // extract reference (UB if wrong type)
long* ptr = any.check<long>(); // extract pointer (nullptr if wrong type)
Warning
Types must match exactly when extracted – implicit conversions are not supported.

Member Function Documentation

template<typename T >
T* aurora::Any::check ( )

Returns a pointer to the contained value.

Returns
Address of contained value, if this instance is not empty and its value is of type T; nullptr otherwise. The behavior is always well-defined.
See also
get()
template<typename T >
T& aurora::Any::get ( )

Returns a reference to the contained value.

Warning
You must be certain that the contained value is actually of type T. If this assumption does not hold, the behavior will be undefined.
See also
check()

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