I have a std::map where I store some arbitrary methods that I want to call later.
map<string, function<void(void)>> methods;
template<typename R, typename ...P>
void storeMethod(const string& name, const function<R(P...)>& method) {
methods[name] = method;
}
A the time of calling, I get the parameters to call the method with in a vector<void*>
, where the first element is a pointer where I will store the return value.
How do I automatically cast these parameters to the corresponding types of the method I want to call?
Should I store the types in some way maybe?
void callMethod(const string& name, vector<void*> parameters) {
auto method = methods[name];
// How to call 'method' with all the parameters casted to the required types?
}
For example, if I orignally called storeMethod()
with a function<int(string a, float b)>
, I need a generic way to call it in callMethod()
like this:
*((int*)parameters[0]) = method(*((string*)parameters[1]), *((float*)parameters[2]));