siplasplas
A library for C++ reflection and introspection
cpp::typeerasure::Function< Storage, ArgsStorage, ReturnStorage > Class Template Reference

Stores a type-erased callable of any signature and kind. More...

#include <siplasplas/typeerasure/function.hpp>

Public Member Functions

template<typename Callable >
 Function (Callable &&callable)
 Constructs a Function from a Callable object. More...
 
bool empty () const
 Checks if the object is empty (No callable assigned)
 
template<typename... Args>
SimpleAny< ReturnStorage > operator() (Args &&...args)
 Invokes the callable with the given arguments. More...
 
template<typename... Args>
SimpleAny< ReturnStorage > operator() (Args &&...args) const
 Invokes the callable with the given arguments. More...
 
template<typename ArgsVector >
SimpleAny< ReturnStorage > invoke (ArgsVector &&args)
 Invokes the callable with the given arguments The arguments are passed as a vector of SimpleAny. More...
 
template<typename ArgsVector >
SimpleAny< ReturnStorage > invoke (ArgsVector &&args) const
 Invokes the callable with the given arguments The arguments are passed as a vector of SimpleAny. More...
 
SimpleAny< ReturnStorage > invoke (AnyArg *args)
 Invokes the callable with the given arguments The arguments are passed as a vector of SimpleAny. More...
 
SimpleAny< ReturnStorage > invoke (AnyArg *args) const
 Invokes the callable with the given arguments The arguments are passed as a vector of SimpleAny. More...
 
template<typename Callable >
Functionoperator= (Callable &&callable)
 Assigns a new callable to the function.
 
cpp::FunctionKind kind () const
 Returns the function kind (free function, pointer to member function, functor, etc) of the callable.
 

Static Public Member Functions

template<typename Callable , typename... Args>
static Function< Storage > create (Args &&...args)
 Creates a Function by instancing in-place the given callable. More...
 

Detailed Description

template<typename Storage, typename ArgsStorage = Storage, typename ReturnStorage = Storage>
class cpp::typeerasure::Function< Storage, ArgsStorage, ReturnStorage >

Stores a type-erased callable of any signature and kind.

The Function class goes one step beyond std::function and provides a unique class to store any kind of C++ callable object

class MyClass
{
public:
int method(const std::vector<int>&);
};
void freeFunction(int i);
MyClass object;
std::vector<Function> functions = {
&MyClass::method,
[](int i, const std::string& str) {
return std::to_string(i) + str;
},
freeFunction
};
int i = functions[0](object, std::vector<int>{1, 2, 3, 4});
const std::string& str = functions[1](42, "hello");
functions[3](42);

Invoking a Function objects with the wrong arguments has undefined behavior

Template Parameters
StorageStorage for the hosted callable
ArgsStorageStorage for type-erased call arguments
ReturnStorageStorage for the return value

Constructor & Destructor Documentation

template<typename Storage , typename ArgsStorage = Storage, typename ReturnStorage = Storage>
template<typename Callable >
cpp::typeerasure::Function< Storage, ArgsStorage, ReturnStorage >::Function ( Callable &&  callable)
inline

Constructs a Function from a Callable object.

This constructor takes any kind of callable and stores it in a Function. The behavior is undefined if Function is initialized with a type that is not callable.

Parameters
callableCallable entity to store. Could be a function pointer, a member function pointer, a lambda expression, etc.

Member Function Documentation

template<typename Storage , typename ArgsStorage = Storage, typename ReturnStorage = Storage>
template<typename Callable , typename... Args>
static Function<Storage> cpp::typeerasure::Function< Storage, ArgsStorage, ReturnStorage >::create ( Args &&...  args)
inlinestatic

Creates a Function by instancing in-place the given callable.

Template Parameters
CallableCallable type to store
Parameters
argsCallable constructor arguments
Returns
A Function instance with the given callable type
template<typename Storage , typename ArgsStorage = Storage, typename ReturnStorage = Storage>
template<typename ArgsVector >
SimpleAny<ReturnStorage> cpp::typeerasure::Function< Storage, ArgsStorage, ReturnStorage >::invoke ( ArgsVector &&  args)
inline

Invokes the callable with the given arguments The arguments are passed as a vector of SimpleAny.

Parameters
argsForwarding reference to vector of arguments
Returns
the return value of the callable as in cpp::invoke(<underlying callable>, <args>)
template<typename Storage , typename ArgsStorage = Storage, typename ReturnStorage = Storage>
template<typename ArgsVector >
SimpleAny<ReturnStorage> cpp::typeerasure::Function< Storage, ArgsStorage, ReturnStorage >::invoke ( ArgsVector &&  args) const
inline

Invokes the callable with the given arguments The arguments are passed as a vector of SimpleAny.

Parameters
argsForwarding reference to vector of arguments
Returns
the return value of the callable as in cpp::invoke(<underlying callable>, <args>)
template<typename Storage , typename ArgsStorage = Storage, typename ReturnStorage = Storage>
SimpleAny<ReturnStorage> cpp::typeerasure::Function< Storage, ArgsStorage, ReturnStorage >::invoke ( AnyArg args)
inline

Invokes the callable with the given arguments The arguments are passed as a vector of SimpleAny.

Parameters
argsPointer to the sequence of arguments
Returns
the return value of the callable as in cpp::invoke(<underlying callable>, <args>)
template<typename Storage , typename ArgsStorage = Storage, typename ReturnStorage = Storage>
SimpleAny<ReturnStorage> cpp::typeerasure::Function< Storage, ArgsStorage, ReturnStorage >::invoke ( AnyArg args) const
inline

Invokes the callable with the given arguments The arguments are passed as a vector of SimpleAny.

Parameters
argsPointer to the sequence of arguments
Returns
the return value of the callable as in cpp::invoke(<underlying callable>, <args>)
template<typename Storage , typename ArgsStorage = Storage, typename ReturnStorage = Storage>
template<typename... Args>
SimpleAny<ReturnStorage> cpp::typeerasure::Function< Storage, ArgsStorage, ReturnStorage >::operator() ( Args &&...  args)
inline

Invokes the callable with the given arguments.

The behavior is undefined if the arguments don't match the underlying callable signature

Parameters
argsfunction arguments. If the underlying callable is a pointer to to member function, the first argument is assumed to be the caller object
Returns
the return value of the callable as in cpp::invoke(<underlying callable>, std::forward(args)...)
template<typename Storage , typename ArgsStorage = Storage, typename ReturnStorage = Storage>
template<typename... Args>
SimpleAny<ReturnStorage> cpp::typeerasure::Function< Storage, ArgsStorage, ReturnStorage >::operator() ( Args &&...  args) const
inline

Invokes the callable with the given arguments.

The behavior is undefined if the arguments don't match the underlying callable signature

Parameters
argsfunction arguments. If the underlying callable is a pointer to to member function, the first argument is assumed to be the caller object
Returns
the return value of the callable as in cpp::invoke(<underlying callable>, std::forward(args)...)

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