siplasplas
A library for C++ reflection and introspection
cpp::Any< Storage, FunctionsStorage, FunctionArgsStorage > Class Template Reference

Implements a type-erased object container with support for dynamic method and attribute invokation. More...

#include <siplasplas/typeerasure/any.hpp>

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

Public Types

using Base = SimpleAny< Storage >
 
using Method = cpp::typeerasure::Function< FunctionsStorage, FunctionArgsStorage >
 
using Attribute = cpp::typeerasure::Function< FunctionsStorage, FunctionArgsStorage, cpp::NonOwningStorage >
 

Public Member Functions

MethodProxy operator() (const std::string &name)
 Gives access to an object method. More...
 
ConstMethodProxy operator() (const std::string &name) const
 Gives access to an object method. More...
 
AttributeProxy operator[] (const std::string &name)
 
ConstAttributeProxy operator[] (const std::string &name) const
 
bool hasMethod (const std::string &name) const
 Checks if the object has a method with a given name. More...
 
bool hasAttribute (const std::string &name) const
 Checks if the object has an attribute with a given name. More...
 
- Public Member Functions inherited from cpp::SimpleAny< Storage >
 SimpleAny ()
 Constructs an empty SimpleAny.
 
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)
 

Static Public Member Functions

template<typename Class , typename... Args>
static Any create (Args &&...args)
 
- Static Public Member Functions inherited from cpp::SimpleAny< Storage >
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, typename FunctionsStorage = Storage, typename FunctionArgsStorage = FunctionsStorage>
class cpp::Any< Storage, FunctionsStorage, FunctionArgsStorage >

Implements a type-erased object container with support for dynamic method and attribute invokation.

cpp::Any extends the interface of cpp::SimpleAny to add dynamic manipulation of the object's interface. cpp::Any keeps track of a set of methods and attributes the instace has assigned, so it can behave as an instance of a class with its interface and associated data members:

auto any = cpp::Any::create<MyClass>("hello");
any("method") = &MyCass::method;
any("method")("hello, "s, "world"s); // Invokes <MyClass object>.method("hello, ", "world")
any["field"] = &MyClass::field;
any["field"] = 42; // <MyClass object>.field = 42

The behavior is undefined if the user access a method or attribute the any object doesn't have

Template Parameters
StorageStorage for the type erased object
FunctionsStorageStorage for the type erased methods. Storage by default
FunctionArgsStorageStorage for the type erased method call arguments. FunctionsStorage by default
AttributesStorageStorage for the type erased attributes. Storage by default

Member Function Documentation

template<typename Storage , typename FunctionsStorage = Storage, typename FunctionArgsStorage = FunctionsStorage>
bool cpp::Any< Storage, FunctionsStorage, FunctionArgsStorage >::hasAttribute ( const std::string &  name) const
inline

Checks if the object has an attribute with a given name.

Parameters
namename of the attribute
Returns
True if there's an attribute associated to that name, false otherwise
template<typename Storage , typename FunctionsStorage = Storage, typename FunctionArgsStorage = FunctionsStorage>
bool cpp::Any< Storage, FunctionsStorage, FunctionArgsStorage >::hasMethod ( const std::string &  name) const
inline

Checks if the object has a method with a given name.

Parameters
namename of the method
Returns
True if there's a method associated to that name, false otherwise
template<typename Storage , typename FunctionsStorage = Storage, typename FunctionArgsStorage = FunctionsStorage>
MethodProxy cpp::Any< Storage, FunctionsStorage, FunctionArgsStorage >::operator() ( const std::string &  name)
inline

Gives access to an object method.

Parameters
nameName of the object. If the object has no method with that name, the behavior is undefined
Returns
A proxy to the method. This proxy can be assigned, to change the method, or invoked.
template<typename Storage , typename FunctionsStorage = Storage, typename FunctionArgsStorage = FunctionsStorage>
ConstMethodProxy cpp::Any< Storage, FunctionsStorage, FunctionArgsStorage >::operator() ( const std::string &  name) const
inline

Gives access to an object method.

Parameters
nameName of the object. If the object has no method with that name, the behavior is undefined
Returns
A proxy to the method. This proxy can be assigned, to change the method, or invoked.

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