Hello Victor
It is similar to a type_info but returning something what could be used
easily in a switch statement.
Why does it have to be a switch statement? Couldn't it be, say,
a 'map<whatever, function_pointer>'? 'whatever' could be a string
or something else.
It must not be a switch, and I will keep in mind that a map might be a
possible solution to replace a switch.
In this case "whatever" should be a type_info (or the string name out of
typeinfo), what IMO is the only unique "feature" for any class. It seems
quite expensive to me to carry around the type_info so I would prefer, e.g.
an int. With an int I could use either switch, map (Alexandrescus
BasicDispatcher) or vector (Alexandrescus BasicFastDispatcher).
I did not test yet, but with map it should be _possible_ to use the
type_info (oh - I have to look if it is comparable). Anyway, all the
structures mentioned above must be 2D for double-dispatching, means
nested-ifs, nested switch/if, 2D-map or 2D-vector/matrix.
The bottom line is that if you relax the requirement to do it in
compile-time and allow it to be done in run-time, you could get
more and easier solutions.
This indeed might be true, but like described in my OP I reduce it to
"how-to get a unique ID", what is independent which structure to take.
Alexandrescu for example is defining a macro which must be included in each
class dispatched by the BasicFastDispatcher to define this ID.
// Andrei Alexandrescus Macro
#define IMPLEMENT_INDEXABLE_CLASS(SomeClass) \
static int& GetClassIndexStatic() \
{\
static int index = -1;\
return index;\
}\
virtual int& GetClassIndex()\
{\
assert(typeid(*this) == typeid(SomeClass));\
return GetClassIndexStatic();\
}
The variable index is used to indicate if the class was used in a dipatcher
and if, which slot was used. This is working quite nice and putting together
a single-dispatcher could look like this:
template <typename T>
void Add(std::vector<void(*)(const BASE &)> & callbacks)
{
int& index = T::GetClassIndexStatic();
if (index < 0) index = callbacks.size(); // define slot where to go
if ( callbacks.size() < index + 1 ) callbacks.resize( index + 1 ); //
assure that vector is big enough
void(* fooT)( const T & ) = foo;
callbacks[index] = (void(*)(const BASE &))fooT;
}
//the calls somewhere
std::vector<void(*)(const BASE &)> callbacks;
Add<A>(callbacks);
Add<B>(callbacks);
for (unsigned int i = 0; i < vec.size(); ++i)
{
callbacks[vec[i]->GetClassIndex()](*vec[i]);
}
So I still search a solution to define an ID, but I think I will refine
Alexandrescus method and adopt it to my needs.
Thanks,
Patrick