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

  initlist | Initializer 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

  Functor | Type of the callable being stored.

Parameters

  name | The name under which the function will be stored.
  functor | The 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

  Return | The expected return type of the function.
  Arguments | The expected argument types of the function.

Parameters

  name | The name of the function to call.
  args | Arguments 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

  name | The 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

  Return | The return type of the function.
  Arguments | The argument types of the function.

Parameters

  name | The 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

  Return | The return type of the function.
  Arguments | The argument types of the function.

Parameters

  name | The 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:

- Core/FunctionContainer.hpp
- Core/FunctionContainer.cpp