Here is an idea for you:
- Create sequence of types supporting
random access (boost::mpl::vector
will fit)
- Each element in a sequence will have
its unique identifier, which its
position (0, 1, 2...)
- Create a template analog of virtual
table with functions accepting a
predicate that has an operator
overloaded for each type in a list.
This template should be instantiated
for each predicate. For example,
function at index 0 will call a
“Predicate::operator<Type1>()” etc.
Use recursive template instantiation
to initialize that table.
So, to get a unique identifier in compile-time, use something like:
boost::mpl::find<type_list, T>::type::pos::value
To get a type for a given identifier at compile-time:
boost::mpl::at_c<type_list, which /* unsigned int known at compile-time */>::type
To get a type at run-time using a number, create a functor with all possible overloads:
struct Visitor {
template <typename T>
void operator () ();
};
template <>
void operator ()<Type1> ();
template <>
void operator ()<TypeN> ();
And invoke it using instantiation of the virtual table analog described in p.3, something like:
void applyVisitor (int index, Visitor & v) {
DispatchTable<Visitor>::functions[ index].call (v);
}
The idea is simple, but generic implementation is a bit tricky. It took me 377 lines of code to implement.