[ VIGRA Homepage | Function Index | Class Index | Namespaces | File List | Main Page ]

Public Member Functions
IteratorAdaptor< Policy > Class Template Reference

Quickly create 1-dimensional iterator adapters. More...

#include <vigra/iteratoradapter.hxx>

List of all members.

Public Member Functions

 IteratorAdaptor (BaseType const &o)

Detailed Description

template<class Policy>
class vigra::IteratorAdaptor< Policy >

Quickly create 1-dimensional iterator adapters.

This class supports the easy creation of 1D iterator adpaters out of existing iterators. To use it, you must first implement a policy class that defines the iterator's behavior. The policy is used to instantiate the IteratorAdapter template, which thus automatically obtains all required functions of an STL-compatible iterator. General information on how this works can be found on the Boost Iterator Adaptor page, although there are some differences in the details of the boost and VIGRA implementations. Here is an example policy class that just exports the behaviour of the underlying iterator:

    template <class Iterator>
    class TrivialIteratorAdaptorPolicy
    {
      public:
        // the underlying iterator
        typedef Iterator                               BaseType;

        // the adaptor's value type
        typedef typename Iterator::value_type          value_type;

        // the adaptor's difference type (result of 'iter1 - iter2',
        //                                argument of 'iter[n]')
        typedef typename Iterator::difference_type     difference_type;

        // the adaptor's reference type (result of '*iter')
        typedef typename Iterator::reference           reference;

        // the adaptor's index_reference type (result of 'iter[n]')
        typedef typename Iterator::index_reference     index_reference;

        // the adaptor's pointer type (result of 'iter.operator->()')
        typedef typename Iterator::pointer             pointer;

        // the adaptor's iterator category
        typedef typename Iterator::iterator_category   iterator_category;

        // do some additional initialization in the adaptor's constructor
        static void initialize(BaseType & d) {}

        // called by '*iter', 'iter->'
        static reference dereference(BaseType const & d)
            { return *d; }

        // called by 'iter[n]'
        static index_reference dereference(BaseType d, difference_type n)
            { return d[n]; }

        // called by 'iter1 == iter2', 'iter1 != iter2'
        static bool equal(BaseType const & d1, BaseType const & d2)
            { return d1 == d2; }

        // called by 'iter1 < iter2', 'iter1 <= iter2', 'iter1 > iter2', 'iter1 >= iter2'
        static bool less(BaseType const & d1, BaseType const & d2)
            { return d1 < d2; }

        // called by 'iter1 - iter2'
        static difference_type difference(BaseType const & d1, BaseType const & d2)
            { return d1 - d2; }

        // called by '++iter', 'iter++'
        static void increment(BaseType & d)
            { ++d; }

        // called by '--iter', 'iter--'
        static void decrement(BaseType & d)
            { --d; }

        // called by 'iter += n', 'iter -= n'
        static void advance(BaseType & d, difference_type n)
            { d += n; }
    };

This policy class is used like this:

    SomeIterator iter = ...;

    vigra::IteratorAdaptor<vigra::TrivialIteratorAdaptorPolicy<SomeIterator> > iter_adaptor(iter);

By changing the definition of the policy members, a wide range of adaptor behaviors can be achieved. If the base iterator isn't a random access iterator, just drop the functions that cannot be implemented. This simply means that some adaptor functions may not be called, as one would expect from an iterator that doesn't support random access. Note also that the BaseType needs not be an iterator - it can be any type that contains the information necessary for the adaptor to do it's work.

#include <vigra/iteratoradapter.hxx>
Namespace: vigra


Constructor & Destructor Documentation

IteratorAdaptor ( BaseType const &  o) [explicit]

Construct from an instance of the policy class' BaseType Note that the functions of the adaptor implement the interface of an random access iterator as defined in the C++ standard, so there is no need for explicit documentation.


The documentation for this class was generated from the following file:

© Ullrich Köthe (ullrich.koethe@iwr.uni-heidelberg.de)
Heidelberg Collaboratory for Image Processing, University of Heidelberg, Germany

html generated using doxygen and Python
vigra 1.7.0 (Thu Aug 25 2011)