
I've put together a class called 'AnyVector' for doing type-based iteration
on a vector. The basic idea is that AnyVector wraps a
std::vector<boost::any>. The AnyVector lets you iterate across a subset of
the underlying vector by using filter_iterators. The filter iterators, in
turn, dereference to the requested type for ease of use. For instance, you
can do the following:
I've done something similar to this in the past, but found it notoriously
slow to filter on types with boost::any - (on top of this I also added the
capability to cast into another type, and throw if it was not possible).
Not to disuade you, however, I would really be interested to know if you've
profiled your code for a mix of types (say 10 or more atomic types).
Nope, I haven't done any profiling... I imagine it doesn't run too quickly if you have a large number of a lot of different types. But the tradeof from a design perspective is pretty cool (having a single container store all sorts of types!). The other way to design the container (for performance purposes) is to store a map<typeinfo, vector<boost::any> >. Any particular vector in the map only has boost::any's corresponding to the matching typeinfo. When you want an iterator, the container looks like the type requested in the map and returns an iterator to the corresponding vector. It works faster, but it's less elegant. --Steve