Minecraft Community Edition 0.1.0
Loading...
Searching...
No Matches
mce::FunctionContainer Class Reference

Container for storing and invoking named functions. More...

#include <FunctionContainer.hpp>

Public Member Functions

 FunctionContainer ()=default
 Default constructor.
 FunctionContainer (QEventBus &qBus, std::initializer_list< std::pair< eastl::string, eastl::any > > initlist)
 Construct the container with an initializer list of name/value pairs.
template<typename Functor>
void addFunction (const eastl::string &name, Functor functor)
 Add a function to the container.
template<typename Return, typename... Arguments>
Return callFunction (const eastl::string &name, Arguments &&... args)
 Call a stored function by name with the provided arguments.
bool findFunction (const eastl::string &name)
 Check whether a function with the given name exists in the container.
template<typename Return, typename... Arguments>
eastl::function< Return(Arguments...)> operator[] (const eastl::string &name)
 Retrieve a stored function as an eastl::function via operator[].
template<typename Return, typename... Arguments>
eastl::function< Return(Arguments...)> getFunction (const eastl::string &name)
 Retrieve a stored function as an eastl::function.

Detailed Description

Container for storing and invoking named functions.

This class allows storing callables (wrapped as eastl::any) associated with string keys. Functions can be retrieved, cast to the expected signature, and invoked. The API supports adding functions, calling them by name, checking for existence, and retrieving a stored function as an eastl::function object.

A usage example: FunctionContainer fc = { { "foo", eastl::function<int(int)>([](int value) -> int{ return value + 1; }) } }; if(fc.findFunction("foo") fc.callFunction<int, int>("foo", 1);

Constructor & Destructor Documentation

◆ FunctionContainer()

mce::FunctionContainer::FunctionContainer ( QEventBus & qBus,
std::initializer_list< std::pair< eastl::string, eastl::any > > initlist )

Construct the container with an initializer list of name/value pairs.

The initializer list should contain pairs of function name and eastl::any containing the callable.

Parameters
initlistInitializer list of pairs (name, callable-as-eastl::any).

Member Function Documentation

◆ addFunction()

template<typename Functor>
void mce::FunctionContainer::addFunction ( const eastl::string & name,
Functor functor )
inline

Add a function to the container.

Add a function implementation.

The callable will be stored in the internal map under the provided name. The callable is stored as an eastl::any and should be retrievable later by using the same signature when casting.

Template Parameters
FunctorType of the callable being stored.
Parameters
nameThe name under which the function will be stored.
functorThe callable to store.

See addFunction documentation above.

◆ callFunction()

template<typename Return, typename ... Arguments>
Return mce::FunctionContainer::callFunction ( const eastl::string & name,
Arguments &&... args )
inline

Call a stored function by name with the provided arguments.

Call a stored function implementation.

The template parameters specify the expected return type and argument types of the stored function. If the named function exists it will be cast to eastl::function<Return(Arguments...)> and invoked. If the function is not found an error is logged via MCE_ERROR and behavior is undefined for the returned value.

Template Parameters
ReturnThe expected return type of the function.
ArgumentsThe expected argument types of the function.
Parameters
nameThe name of the function to call.
argsArguments to forward to the function.
Returns
Return The value returned by the invoked function.

See callFunction documentation above.

◆ findFunction()

bool mce::FunctionContainer::findFunction ( const eastl::string & name)

Check whether a function with the given name exists in the container.

Parameters
nameThe name to check for.
Returns
true if a function with the given name exists, false otherwise.

◆ getFunction()

template<typename Return, typename ... Arguments>
eastl::function< Return(Arguments...)> mce::FunctionContainer::getFunction ( const eastl::string & name)
inline

Retrieve a stored function as an eastl::function.

getFunction implementation to retrieve stored function.

Same behavior as operator[] but provided with an explicit named method.

Template Parameters
ReturnThe return type of the function.
ArgumentsThe argument types of the function.
Parameters
nameThe name of the stored function.
Returns
eastl::function<Return(Arguments...)> The callable if found; a null function otherwise.

See getFunction documentation above.

◆ operator[]()

template<typename Return, typename ... Arguments>
eastl::function< Return(Arguments...)> mce::FunctionContainer::operator[] ( const eastl::string & name)
inline

Retrieve a stored function as an eastl::function via operator[].

operator[] implementation to retrieve stored function.

The template specifies the expected signature of the function stored under the given name. If no function exists an error is logged and a null function is returned.

Template Parameters
ReturnThe return type of the function.
ArgumentsThe argument types of the function.
Parameters
nameThe name of the stored function.
Returns
eastl::function<Return(Arguments...)> The callable if found; a null function otherwise.

See operator[] documentation above.


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