[Iterators] Filtering and conversion

Hi there, I'm playing with the iterators library at the moment. Say I have a std::vector<boost::shared_ptr<base> > , with base being the base class of derived1 and derived2. Pointers to derived1 and derived2 are stored in arbitrary order in the vector. Would it be possible to create an iterator that filters out the derived1 objects (including implicit conversion and possibly dereferencing of the resulting shared_ptr<derivedx>) ? So what I'm looking for is something like this: /*******************************************************/ main() { vector<base> x; for (int i=0; i<10; i++) { if(i%2==0) x.push_back(shared_ptr<derived1>(new derived1(i))); else x.push_back(shared_ptr<derived2>(new derived2(i))); } conversion_iterator<derived1> it(x.end()); // ++it will skip over derived2 objects and set itself // to x.end() if no derived1 is remaining for(it=x.begin(); it!=x.end(); ++it) { // Implies implicit conversion and dereferencing // of shared_ptr<base> it->someDerived1SpecificCall(); } } /*******************************************************/ The filter_iterator already does part of what I'd want here, but cannot help with the conversion part. I'm not asking for a solution to the above problem, just whether you think this can be done at all. I had the impression that the iterators library assumes that the types of the sequence being iterated over and the type being returned are identical. Also, while it=x.begin() can be handled by a conversion constructor. it!=x.end() does not appear to be accessible with the predefined comparison operators. In any case thanks and have a nice Christmas time, Ruediger

Ruediger Berlich wrote:
The filter_iterator already does part of what I'd want here, but cannot help with the conversion part.
I'm not asking for a solution to the above problem, just whether you think this can be done at all. I had the impression that the iterators library assumes that the types of the sequence being iterated over and the type being returned are identical.
I don't know if this helps. But the serialization library has a section called dataflow iterators. It's really about composition of iterators. So you can a) start with an original sequence o_i a) make a derivation from transform iterator - call it t_i b) make a deriveration from filter iterator - call it f-i c) make a composition of the two with - f-i(t_i(o_i). This composite returns a the original sequence filtered and transformed. It's all done at compile time so when you compile for release - the final result is very fast. Robert Ramey
participants (2)
-
Robert Ramey
-
Ruediger Berlich