Message dispatching in C++ (Part 0 – Problem)

So you want to send a serialized class trough a socket? That’s not so hard, just use boost::serialization or even roll your own.

My question is: what do you do on the other end?

Let’s say you have some messages that you serialize somehow and send them trough a socket. On the other end you get some sort of buffer full of chars, how do you know what’s what, and which bytes correspond to which class?

Would you do this?

To send one of these things trough the socket you would have to write something like this:

So far so good, the only thing that’s bothering me is the enum, but maybe that’s just me.

Still with me? Great! Let’s look at the receiving side:

Hmm, that’s actually pretty horrible. And no matter what you do, no matter how many classes and design patterns you bury it under, some of it ugliness will still show trough in the end (I’m looking at you enum!).

What can we do to make this a little cleaner? The first thing we should do is get rid of that ugly enum. We can do that either by using RTTI or a type list.

I don’t like to depend on RTTI so I’ll go with the type list (a type list is a compile-time generic list of types).

For now, that’s it, but stay tuned, we’ll start to build a type list in the next part.