Loading...
Searching...
No Matches
FunctionContainer.hpp
#pragma once
#include "EASTL/initializer_list.h"
#include "EASTL/string.h"
#include "EASTL/any.h"
#include "EASTL/unordered_map.h"
#include "EASTL/functional.h"
#include "..\IO\Logger.hpp"
#include <source_location>
namespace mce::core {
class FunctionContainer {
public:
FunctionContainer() = default;
FunctionContainer(QEventBus& qBus, std::initializer_list<std::pair<eastl::string, eastl::any>> initlist);
template<typename Functor>
void addFunction(const eastl::string& name, Functor functor);
template<typename Return, typename... Arguments>
Return callFunction(const eastl::string& name, Arguments&&... args);
bool findFunction(const eastl::string& name);
template<typename Return, typename... Arguments>
eastl::function<Return(Arguments...)> operator[](const eastl::string& name);
template<typename Return, typename... Arguments>
eastl::function<Return(Arguments...)> getFunction(const eastl::string& name);
private:
eastl::unordered_map<eastl::string, eastl::any> functions;
QEventBus& qBus;
};
// Template implementations
template<typename Functor>
inline void FunctionContainer::addFunction(const eastl::string& name, Functor functor) {
FunctionContainer::functions[name] = functor;
}
template<typename Return, typename ...Arguments>
inline Return FunctionContainer::callFunction(const eastl::string& name, Arguments && ...args) {
if (FunctionContainer::functions.find(name) != FunctionContainer::functions.end()) {
auto& func = FunctionContainer::functions[name];
auto castedFunc = eastl::any_cast<eastl::function<Return(Arguments...)>>(func);
return castedFunc(eastl::forward<Arguments>(args)...);
}
else {
MCE_ERROR("Function \"{}\" not found! File:", name);
}
}
template<typename Return, typename ...Arguments>
inline eastl::function<Return(Arguments...)> FunctionContainer::operator[](const eastl::string& name) {
if (FunctionContainer::functions.find(name) != FunctionContainer::functions.end()) {
return eastl::any_cast<eastl::function<Return(Arguments...)>>(FunctionContainer::functions[name]);
}
else {
MCE_ERROR("Function \"{}\" not found! File:", name);
return nullptr;
}
}
template<typename Return, typename ...Arguments>
inline eastl::function<Return(Arguments...)> FunctionContainer::getFunction(const eastl::string& name) {
if (FunctionContainer::functions.find(name) != FunctionContainer::functions.end()) {
return eastl::any_cast<eastl::function<Return(Arguments...)>>(FunctionContainer::functions[name]);
}
else {
MCE_ERROR("Function \"{}\" not found! File:", name);
return nullptr;
}
}
}
mce::core::FunctionContainer::findFunction
bool findFunction(const eastl::string &name)
Check whether a function with the given name exists in the container.
Definition FunctionContainer.cpp:10
mce::core::FunctionContainer::getFunction
eastl::function< Return(Arguments...)> getFunction(const eastl::string &name)
Retrieve a stored function as an eastl::function.
Definition FunctionContainer.hpp:174
mce::core::FunctionContainer::operator[]
eastl::function< Return(Arguments...)> operator[](const eastl::string &name)
Retrieve a stored function as an eastl::function via operator[].
Definition FunctionContainer.hpp:158
mce::core::FunctionContainer::callFunction
Return callFunction(const eastl::string &name, Arguments &&... args)
Call a stored function by name with the provided arguments.
Definition FunctionContainer.hpp:141
mce::core::FunctionContainer::addFunction
void addFunction(const eastl::string &name, Functor functor)
Add a function to the container.
Definition FunctionContainer.hpp:131
mce::core::FunctionContainer::FunctionContainer
FunctionContainer()=default
Default constructor.
mce::core::QEventBus
Thread-safe event bus.
Definition QEventBus.hpp:58