|
Minecraft Community Edition 0.1.0
|
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. | |
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);
| 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.
| initlist | Initializer list of pairs (name, callable-as-eastl::any). |
|
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.
| Functor | Type of the callable being stored. |
| name | The name under which the function will be stored. |
| functor | The callable to store. |
See addFunction documentation above.
|
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.
| Return | The expected return type of the function. |
| Arguments | The expected argument types of the function. |
| name | The name of the function to call. |
| args | Arguments to forward to the function. |
See callFunction documentation above.
| bool mce::FunctionContainer::findFunction | ( | const eastl::string & | name | ) |
Check whether a function with the given name exists in the container.
| name | The name to check for. |
|
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.
| Return | The return type of the function. |
| Arguments | The argument types of the function. |
| name | The name of the stored function. |
See getFunction documentation above.
|
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.
| Return | The return type of the function. |
| Arguments | The argument types of the function. |
| name | The name of the stored function. |
See operator[] documentation above.