Dispatching

Published April 07, 2007
Advertisement
I wanted to write a dispatcher class, to use after demarshalling packets (using the Etwork library). It's pretty simple: I have an int based on the de-marshalled type, and a void pointer that points to a concrete instance of that de-marshalled type. I want to dispatch to one of a number of registered (member) functions, based on the int. The dispatch needs to include a cast to reference-to-demarshalled-type from the void pointer.

Ideally, I would also want static binding of int to pointer-to-member-function.

My first approach used several template helpers and virtual dispatch. It worked, but was much bigger than I'd like.

My second approach used one simple template helper, and a lookup array, and uses runtime binding. It works, and isn't too big, but doesn't help much, either.

I'm thinking that the solution to this problem really should be a switch() statement. Yes, I can build direct-dispatch object-oriented structures to solve this problem, but: why?

#define BEGIN_DISPATCH(code,data)   { void *_ptr = data; switch(code) {#define DISPATCH(type,code)         case code: On ## type(*(type *)_ptr); break;#define END_DISPATCH()              default: UnknownPacket(); break; } }


No muss, no fuss. The drawback is that I have to hard-code the code for each type, which is already coded in another table (used to define the serialization), and putting the same piece of data in two places goes against my better judgement.
0 likes 1 comments

Comments

hplus0603
Btw: I kept my template code for now, to avoid duplicating the message codes.
April 08, 2007 01:32 AM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Profile
Author
Advertisement
Advertisement