
Hello all,
I'm trying to create a base class that provides 2 iterators (begin/end).
These will be used to iterate over data structures that will be provided
by subclasses. These data structures are completely unrelated. Normally
this can be solved using virtual functions but the problem is that
iterators are normally returned by value so I cannot go that route.
My concrete problem:
struct Point { int x, y; };
class PointIterator : public boost::iterator_facade
From the PointCollection base class I want to derive concrete implementations:
class Collection1 : public PointCollection { public: Collection1(char* data, size_t size) : data_(data), size_(size) {}; private: char* data_; size_t size_; }; Collection1 would return a Point with x = data_[index] and y = index * 1000, for example. Other subclasses might contain 2 vectors or just generated values. In my first attempt I tried to subclass the iterator too and make it return the correct Point when the iterator is dereferenced. As the iterators are returned by value, however, the subclass was sliced right off. An iterator-less solution is easy: class PointCollection { public: virtual Point getPoint(size_t index) const = 0; virtual size_t size() const = 0; }; Easily subclassed but no iterator support. How would I solve this problem with iterators? Should I implement another iterator over them that doesn't know about the actual types of the iterators? Can indirect_iterator help me in any way? Thanks a lot in advance! Greets, Ben

AMDG Swerts, Benjamin wrote:
I'm trying to create a base class that provides 2 iterators (begin/end). These will be used to iterate over data structures that will be provided by subclasses. These data structures are completely unrelated. Normally this can be solved using virtual functions but the problem is that iterators are normally returned by value so I cannot go that route.
Look at Adobe's any_iterator http://stlab.adobe.com/classadobe_1_1any__iterator.html In Christ, Steven Watanabe

Swerts, Benjamin wrote:
Hello all,
I'm trying to create a base class that provides 2 iterators (begin/end). These will be used to iterate over data structures that will be provided by subclasses. These data structures are completely unrelated. Normally this can be solved using virtual functions but the problem is that iterators are normally returned by value so I cannot go that route.
My concrete problem:
struct Point { int x, y; }; class PointIterator : public boost::iterator_facade
; class PointCollection { public: typedef PointIterator const_iterator; const_iterator begin(); const_iterator end(); }; From the PointCollection base class I want to derive concrete implementations:
class Collection1 : public PointCollection { public: Collection1(char* data, size_t size) : data_(data), size_(size) {}; private: char* data_; size_t size_; };
Collection1 would return a Point with x = data_[index] and y = index * 1000, for example. Other subclasses might contain 2 vectors or just generated values. In my first attempt I tried to subclass the iterator too and make it return the correct Point when the iterator is dereferenced. As the iterators are returned by value, however, the subclass was sliced right off. An iterator-less solution is easy:
class PointCollection { public: virtual Point getPoint(size_t index) const = 0; virtual size_t size() const = 0; };
Can't your iterator just hold on to a "container" base class ref, an incrementable index and call the above virtual method in the iterator's deref implementation? Jeff

Can't your iterator just hold on to a "container" base class ref, an incrementable index and call the above virtual method in the iterator's deref implementation?
Jeff
Look at Adobe's any_iterator http://stlab.adobe.com/classadobe_1_1any__iterator.html
In Christ, Steven Watanabe
Jeff, Steven, thank you both for the excellent suggestions. I finally had some time to revisit this problem and currently have something working based on Jeff's idea. It does provide the simplest interface for people wanting to subclass (only 2 functions to implement instead of a complete subclass of boost::iterator_facade). AFAICS Adobe's any_iterator will not be more efficient because they mention a heap allocation is needed. I couldn't check its code, however, as the any_iterator class is mentioned nowhere in the source distribution (or I'm too stupid to find it in 1.0.42). Thanks again. Greets, Ben

AFAICS Adobe's any_iterator will not be more efficient because they mention a heap allocation is needed. I couldn't check its code, however, as the any_iterator class is mentioned nowhere in the source distribution (or I'm too stupid to find it in 1.0.42).
Thanks again. Greets,
Ben
I *think* any_iterator uses internal memory for small objects. Tony
participants (4)
-
Gottlob Frege
-
Jeff Flinn
-
Steven Watanabe
-
Swerts, Benjamin