siplasplas
A library for C++ reflection and introspection
cpp::SimpleAny< Storage > Class Template Reference

Implements a type-erased value container with minimal value semantics requirements. More...

#include <siplasplas/typeerasure/simpleany.hpp>

Inheritance diagram for cpp::SimpleAny< Storage >:
cpp::Any< Storage, FunctionsStorage, FunctionArgsStorage, AttributesStorage >

Classes

struct  EmptyTag
 Type used to represent empty state. More...
 

Public Member Functions

 SimpleAny ()
 Constructs an empty SimpleAny.
 
 SimpleAny (const cpp::typeerasure::TypeInfo &typeInfo)
 Default constructs a value of the given type. More...
 
bool empty () const
 Checks whether the any has an object hosted in or if is empty. More...
 
template<typename T , typename = std::enable_if_t< !std::is_base_of< SimpleAny, std::decay_t<T> >::value >>
 SimpleAny (const T &value)
 Constructs an any of type T from an lvalue of type T. More...
 
template<typename OtherStorage >
 SimpleAny (const SimpleAny< OtherStorage > &other)
 
template<typename OtherStorage >
 SimpleAny (SimpleAny< OtherStorage > &&other)
 
 SimpleAny (const SimpleAny &other)
 
 SimpleAny (SimpleAny &&other)
 
template<typename T >
bool hasType () const
 Checks if the any has a value of type T. More...
 
template<typename T >
const std::decay_t< T > & get () const
 Returns a readonly reference to the hosted object. More...
 
template<typename T >
std::decay_t< T > & get ()
 Returns a reference to the hosted object. More...
 
cpp::typeerasure::TypeInfo typeInfo () const
 Returns the type information of the hosted type.
 
cpp::SimpleAny< cpp::NonOwningStoragegetReference ()
 Returns a reference any to the hosted object.
 
cpp::SimpleAny< cpp::ConstNonOwningStoragegetReference () const
 Returns a const reference any to the hosted object.
 
template<typename T >
SimpleAnyoperator= (const T &value)
 Assigns a value of type T. More...
 
SimpleAnyoperator= (const SimpleAny &other)
 
SimpleAnyoperator= (SimpleAny &&other)
 
const Storage & getStorage () const
 Returns the storage backend of the SimpleAny object.
 

Static Public Member Functions

template<typename T , typename... Args>
static SimpleAny create (Args &&...args)
 Constructs a SimpleAny with an in-place constructed value of type T. More...
 

Detailed Description

template<typename Storage>
class cpp::SimpleAny< Storage >

Implements a type-erased value container with minimal value semantics requirements.

The template SimpleAny implements a type-erased value container, that is, a type that can hold values of different types at runtime.

SimpleAny assumes that types hosted on it satisfy the CopyConstructible, MoveConstructible, CopyAssignable, MoveAssignable, and Destructible. If any of those concepts are not satisfied, an exception would be thrown if the corresponding feature is used. This means you can use non-satisfying types as long as the operation is not needed (Invoked). For example:

cpp::SimpleAny<64> any{NoCopyAssignable()}: // Ok, NoCopyAssignable is move constructible
auto any2 = cpp::SimpleAny<64>::create<NoCopyAssignable>(); // Ok, NoCopyAssignable is default constructible
any2 = any; // EXCEPTION THROWN: NoCopyAssignable is not copy assignable (Noooo, really?)
any2 = std::move(any); // Ok, NoCopyAssignable is move assignable

Note this behavior is undefined and may be disabled in release builds. In general, using semantics not supporeted by the hosted type has undefined behavior.

Template Parameters
StorageStorage policy. See any storage

Constructor & Destructor Documentation

template<typename Storage>
cpp::SimpleAny< Storage >::SimpleAny ( const cpp::typeerasure::TypeInfo typeInfo)
inline

Default constructs a value of the given type.

The behavior is undefined if the given type is not default constructible

Parameters
typeInfoType of the value
template<typename Storage>
template<typename T , typename = std::enable_if_t< !std::is_base_of< SimpleAny, std::decay_t<T> >::value >>
cpp::SimpleAny< Storage >::SimpleAny ( const T &  value)
inline

Constructs an any of type T from an lvalue of type T.

A copy is done from the T lvalue argument to the any storage

Parameters
valueA value of type T to store in the any

Member Function Documentation

template<typename Storage>
template<typename T , typename... Args>
static SimpleAny cpp::SimpleAny< Storage >::create ( Args &&...  args)
inlinestatic

Constructs a SimpleAny with an in-place constructed value of type T.

The value is constructed directly on the any storage, no extra copy operations are done. Arguments are passed as-is to the object constructor.

Template Parameters
TType of the value to be constructed.
Parameters
argsConstructor arguments
Returns
A SimpleAny instance hosting an object of type T
template<typename Storage>
bool cpp::SimpleAny< Storage >::empty ( ) const
inline

Checks whether the any has an object hosted in or if is empty.

Returns
True if the any is empty (There's no hosted object), false instead
template<typename Storage>
template<typename T >
const std::decay_t<T>& cpp::SimpleAny< Storage >::get ( ) const
inline

Returns a readonly reference to the hosted object.

Template Parameters
TType of the returned object. If T is different from the hosted object type, the behavior is undefined (See hasType()).
Returns
A reference to the hosted object it the hosted object is not a pointer. If the hosted object is a pointer, returns a reference to the pointed object
template<typename Storage>
template<typename T >
std::decay_t<T>& cpp::SimpleAny< Storage >::get ( )
inline

Returns a reference to the hosted object.

Template Parameters
TType of the returned object. If T is different from the hosted object type, the behavior is undefined (See hasType()).
Returns
A reference to the hosted object it the hosted object is not a pointer. If the hosted object is a pointer, returns a reference to the pointed object
template<typename Storage>
template<typename T >
bool cpp::SimpleAny< Storage >::hasType ( ) const
inline

Checks if the any has a value of type T.

Returns
True if the hosted type is exactly T, false otherwise
template<typename Storage>
template<typename T >
SimpleAny& cpp::SimpleAny< Storage >::operator= ( const T &  value)
inline

Assigns a value of type T.

If the current hosted type is T, performs a copy assignment of value into the hosted object. Else, the destructor of the hosted object is invoked and the value is copy-constructed into the storage.

Parameters
valueValue to be assigned to the any
Returns
A reference to *this

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