I have some functions that read various types from serialized data, eg:
class DataDeserializer
{
int getInt();
std::string getString();
MyClass getMyClass();
}
I then have various callback functions that take arbitrary parameters, eg:
void callbackA (int, int, int);
void callbackB (int, std::string);
void callbackC (std::string, int, MyClass, int);
I want to call the various callbacks with arguments read from the deserialized data stream. What I would like is to automate the boilerplate code as much as possible. I was thinking maybe I could use templates. If I had some sort of Dispatcher class, eg:
template <SOMETHING??> class Dispatcher
{
void dispatch()
{
// ????
}
SOMEFUNCTIONTYPE callback;
DataDeserializer myDeserializer;
};
Then declare various specific dispatchers:
Dispatcher<int,int,int> myDispatcherA (deserializer, callbackA);
Dispatcher<int,std::string> myDispatcherB (deserializer, callbackB);
Dispatcher<std::string,int,MyClass,int> myDispatcherC (deserializer, callbackC);
Then when I want to dispatch, I just call:
myDispatcherB.dispatch();
which underneath would expand to something like this:
void dispatch()
{
callback (myDeserializer.getString(), myDeserializer.getInt(), myDeserializer.getMyClass(), myDeserializer.getInt());
}
Is this possible with C++11 variadic templates? I've read up a little on them, and it seems recursion is used a lot.