The class Functions
can store/load any functions. It does this by assigning a unique name to every function that gets added. You can then call the function by specifying its name and return type.
Here is a sample usage
//Create class
Functions funcs;
//Add 2 functions (foo -> void(int, int) | add -> int(int, int))
funcs.add("foo", foo);
funcs.add("add", add);
//Call them
int r = funcs.call<int>("add", 3, 2);
funcs.call<void>("foo", r, 2);
Here is the actual class
class Functions
{
public:
template<typename T, typename... Ts>
void add(std::string name, T(*func)(Ts...))
{
mFunctions.insert(std::make_pair(name, func));
}
template<typename T, typename... Ts>
T call(std::string name, Ts... args)
{
try
{
return boost::any_cast<T(*)(Ts...)>(mFunctions.at(name))(args...);
}
catch (const std::out_of_range&)
{
throw std::invalid_argument("Function doesn't exist!");
}
}
private:
std::map<std::string, boost::any> mFunctions;
};
Is there anything to be improved, like maybe naming, programming techniques, ...? Some general feedback would be appreciated.