Is there a way to store void functions with different parameters in a vector? The number of parameters is always one, only the type differs. The parameter type can be a default type like int, a pointer to my own object like example* or any other type.
What I do for now is using a vector of functions with a void pointer as parameter. So I can pass everything. But I want to get rid of the back casting in all the functions.
unordered_map<string, function<void(void*)> > List;
void Callback(string Name, function<void(void*)> Function)
{
List[Name].push_back(&Function);
}
Callback("event", [](void* x){
int value = *(int*)x;
cout << value << endl;
});
Here is an example to illustrate what I would like to have. Please note the template syntax I would prefer. Therefore I would need to store all the functions in a container.
vector<function<void(...)> > List; // need something other than a std vector
template <typename T>
void Callback(string Name, function<void(T)> Function)
{
List[Name].push_back(&Function);
}
Callback<int>([](int x){
cout << x << endl;
});
This application is performance related since it is an essential part of a realtime rendering engine.
Edit: I solved the point of storing functions without parameters, so this is not part of the question anymore what makes the question more clear and straightforward.
I developed a void pointer based event system and it works now. It uses templates for passing and receiving data. Functions with none or one parameter of any type both are supported. Since it uses void pointers to store the callback functions I suppose it is very fast compared to a solution using the any type from boost framework.
This is what came to my mind and it works quite well. However, please feel free to suggest improvements or use the code for your own projects.