I am looking for a way to call different functions by a string input.
I have a map that ties each unique string to a function pointer and a lookup function to search the map and return a pointer if found.
Now the trick is, I need a way to store and return pointers to functions with at least different return types, if possible, also with different signatures.
The usage would be:
Get a string input from a network socket -> find and execute the found function -> shove the result straight back into the socket to be serialized and sent, not caring what actually happened.
Is this doable? If not, how would one approach this task?
Instead of storing the function pointers themselves, which are too different from one another to be accommodated into the same data structure, you can store adaptors that take care of bridging the mismatch. This is a form of type-erasure. An example:
In this example the adaptors are not stateful so you can actually use
void (*)(context_type, socket_type&)
as thecallback_type
.Do note that this kind of design is a bit brittle in that the
context_type
needs to know about every kind of parameter a stored callback might ever need. If at some later point you need to store a callback which needs a new kind of parameter, you need to modifycontext_type
-- if you improve the above design not to use magic numbers like0
and1
as parameters tostd::get
you could save yourself some pains (especially in the reverse situation of removing types fromcontext_type
). This is not an issue if all callbacks take the same parameters, in which case you can dispense yourself with thecontext_type
altogether and pass those parameters to the callbacks directly.Demonstration on LWS.