Jump to content
  • Advertisement
Sign in to follow this  
  • entries
  • comments
  • views


Sign in to follow this  


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

Btw: I kept my template code for now, to avoid duplicating the message codes.

Share this comment

Link to comment

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
  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!