Re: Multiple dispatch using templates - too good to be true?
Tigera wrote:
Greetings,
I too have succumbed to the perhaps foolish urge to write a video
game, and I have been struggling with the implementation of multiple
dispatch. I read through "More Effective C++" by Scott Meyers, and
though I am impressed with his implementation, I wanted to find a way
to use multiple dispatch in a way that was less complicated.
Forgive me if the result, below, has been posted before or written of
before - I haven't read the "Purple Book" at all, so maybe it's all
been done before. What I'd like to know is what I'm missing - this
compiles on my machine, but am I doing something horribly wrong by
trying this? If I add a class later, like one out of a dynamically-
linked library, am I going to have to recompile everything? It just
seems to be too easy to be true.
//File: base_dispatch.h
#ifndef __BASEDISPATCH__
#define __BASEDISPATCH__
class BaseDispatcher
{
public:
virtual void Dispatch(void* A) = 0;
};
#endif
//File: dispatchany.cpp
#ifndef __DISPATCHANY__
#define __DISPATCHANY__
#include "base_dispatcher.h"
template<class T=BaseDispatcher, class U=BaseDispatcher>
class DispatchAny
{
public:
static void Dispatch(T* dispatcher, void* arg)
{
dispatcher->Dispatch(arg);
}
static void Dispatch(T* dispatcher, U* arg)
{
dispatcher->Dispatch(arg);
}
};
#endif
//File: testdispatchany.cpp
#include "dispatchany.cpp"
#include <iostream>
using std::cout;
using std::endl;
class Attack;
class Ability
{
public:
virtual void Dispatch(void* A) { cout<<"Dispatched to void in
ability"<<endl; }
virtual void Dispatch(Attack* a) { cout<<"Ability dispatches
attack"<<endl; }
};
class Attack
{
public:
virtual void Dispatch(void* A) { cout<<"Dispatched to void in
attack"<<endl; }
virtual void Dispatch(Ability* a) { cout<<"Attack dispatches
ability"<<endl; }
};
class Melee : public Attack
{
public:
virtual void Dispatch(void* A) { cout<<"Dispatched to void in
melee"<<endl; }
virtual void Dispatch(Ability* a) { cout<<"Melee dispatches
ability"<<endl; }
};
int main()
{
Ability* a = new Ability();
Attack* b = new Attack();
int* test = new int(5);
Melee* melee = new Melee();
DispatchAny<Ability, Attack> c;
DispatchAny<Attack,Ability> d;
DispatchAny<Melee,Ability> e;
c.Dispatch(a,b);
d.Dispatch(b,a);
e.Dispatch(melee,a);
d.Dispatch(melee,a);
c.Dispatch(a,a);
c.Dispatch(a,test);
delete a;
delete b;
delete melee;
delete test;
return 0;
}
I seek the wisdom of the opinions of those more experienced than I,
and I thank you for them.
I fail to see where you actually do multiple dispatch. The dispatch
seems to only occur on the first parameter. If you want to, say
dispatch on Attack and Ability the setup would be something like:
struct Ability;
struct Attack
{
virtual void Dispatch(Ability *) = 0;
};
struct Melee;
struct Projectile;
struct Ability
{
virtual void Dispatch(Melee *) = 0;
virtual void Dispatch(Projectile *) = 0;
};
void Dispatch(Attack *attack, Ability *ability)
{
attack->Dispatch(ability);
}
With Melee and Projectile looking something like:
struct Melee : Attack
{
void Dispatch(Ability *ability)
{
ability->Dispatch(this);
}
};
struct Projectile : Attack
{
void Dispatch(Ability *ability)
{
ability->Dispatch(this);
}
};
To avoid repeating the boiler plate the curiously recurring template
pattern can be used:
template<class T>
struct AttackDispatch : Attack
{
void Dispatch(Ability *ability)
{
ability->Dispatch(static_cast<T *>(this));
}
};
struct Melee : AttackDispatch<Melee>
{
};
struct Projectile : AttackDispatch<Projectile>
{
};
This will be even more useful if there are multiple multiple dispatch
functions (no pun intended).
But maybe I am just misunderstanding what you are trying to do.
--
Markus