Sign in to follow this  
  • entries
    45
  • comments
    48
  • views
    51690

Dispatching

Sign in to follow this  
hplus0603

176 views

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.
Sign in to follow this  


1 Comment


Recommended Comments

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now