Repost (putting together boost::any, mpl::vector, and filter_iterator)

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: === CODE === AnyVector<> a; a.push_back(1); a.push_back(std::string("hello")); a.push_back(2); a.push_back(std::string("goodbye")); for(AnyVector<>::Iterator<int> i = a.begin<int>(); i != a.end<int>(); ++i) std::cout << (*i + 5); for(AnyVector<>::Iterator<std::string> i = a.begin<std::string>(); i != a.end<std::string>(); ++i) std::cout << i->length(); ============ Furthermore, you can restrict (at compile-time) which objects are allowed to be inserted into the AnyVector. This is done by listing those types when you create your AnyVector: === CODE ==== AnyVector<int, std::string> a; // Only allows int's and string's a.push_back(3.5); // Fails at compile time - double's not allowed! ============= Would this be a useful addition to boost, or does it already exist somewhere in boost and I just missed it? --Steve Stephen Gross Case Western School of Medicine Cleveland, OH "By Grabthar's hammer, by the sons of Worvan, you shall be avenged." - Dr. Lazarus -- Stephen Gross Case Western School of Medicine Cleveland, OH "By Grabthar's hammer, by the sons of Worvan, you shall be avenged." - Dr. Lazarus

Stephen Gross wrote:
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). -Again, my experience with this sort of thing meant runtimes suffered.
<snip .. example code>
Cheers, -- Manfred Doudar MetOcean Engineers www.metoceanengineers.com

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
participants (2)
-
Manfred Doudar
-
Stephen Gross