I have some functions that read various types from serialized data, for example:
class DataDeserializer { int getInt(); std::string getString(); MyClass getMyClass(); }
Then I have various callback functions that take arbitrary parameters, for example:
void callbackA (int, int, int); void callbackB (int, std::string); void callbackC (std::string, int, MyClass, int);
I want to call various callbacks with arguments read from a deserialized data stream. I would like to automate the template code as much as possible. I was thinking maybe I can use patterns. If I had some Dispatcher class, for example:
template <SOMETHING??> class Dispatcher { void dispatch() {
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 send, I simply call:
myDispatcherB.dispatch();
which underneath will expand like this:
void dispatch() { callback (myDeserializer.getString(), myDeserializer.getInt(), myDeserializer.getMyClass(), myDeserializer.getInt()); }
Is this possible with C ++ 11 variation patterns? I read them a bit, and it seems that recursion is used a lot.