Boost logo

Boost :

From: graham.shanks_at_[hidden]
Date: 2002-02-01 12:09:14


Without wishing to gainsay the development of the Cyclic Iterator
Adaptor in this thread I think that this is missing an important
point. All the examples in the documentation of the iterator
adaptor library show adapators of iterators (excluding the
existing counting_iterator example which as Dave says may not be
that useful). The Cyclic Iterator Adapator (although good and
useful in its own right) is just another example of adapting an
existing iterator. IMHO what is missing from the documentation is
an example of how to use the iterator adaptor library to transform
a base type which is *not* an iterator into a standard conforming
iterator .

One of the things that many of us find ourselves having to do is
to create an iterator for a class from a third party library,
which provides the functionality required to implements iterators
but not the iterators themselves. The major constraint is that we
cannot change the interface of the class itself and do not even
have access to the implementation of the class (which is contained
within the object library).

For instance consider the following class, which defines a tree of
Nodes (for those who are interested this is a subset of the XML
DOM interface, simplified to remove details not germain to this
discussion)

class Node
{
public:
    Node();
    Node(const char* const Name);
    ~Node();

    Node& operator=(const Node &other);

    std::string getNodeName() const;

    Node getFirstChild() const;

    Node getNextSibling() const;
    Node getPreviousSibling() const;

    Node appendChild(const Node &newChild);

    bool operator==(const Node &other) const;

private:
    class Implementation;
    Implementation* pImplementation;
};

If I tell you that past-the-end can be tested by comparing to a
default constructed Node, then I believe I'm right in saying that
this has everything we need to create a bi-directional iterator
for this class.

Having done this once (the hard way, i.e without boost ) I am now
looking to simplify this using iterator adaptors (after all the
documentation says that it can be done and Dave reiterated this
earlier in this thread).

I return for some guidance from boosters I would be prepared to
develop this as an example for inclusion in the documentation.
My idea on how to do this is to provide a wrapper class

class NodeWrap
{
public:
    NodeWrap(Node& Node);

    typedef boost::iterator_adaptor<???> iterator;

    iterator begin();
    iterator end();

};

Is this the correct way to go? How can I provide a const_iterator?

Graham Shanks


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk