Public Member Functions | Protected Types | Protected Member Functions | Friends

TRTK::Iterator< T > Class Template Reference

Common base class for input iterator adapter classes. More...

#include <Iterator.hpp>

Inheritance diagram for TRTK::Iterator< T >:

List of all members.

Public Member Functions

 Iterator ()
 Default constructor.
 Iterator (const Iterator< T > &other)
 Copy constructor.
virtual ~Iterator ()
 Destructor.
virtual Iteratoroperator= (const Iterator< T > &other)
 Assignment operator.
virtual T operator* () const
 Dereference operator.
virtual Iteratoroperator++ ()
 Pre-increment operator.
virtual Iterator operator++ (int)
 Post-increment operator.
virtual bool operator== (const Iterator< T > &other) const
 Comparison operator.
virtual bool operator!= (const Iterator< T > &other) const
 Comparison operator.
virtual operator bool_type () const
virtual bool isValid () const
 Returns true if the instance references an iterator.
const IteratorgetIterator () const
 Returns a reference to the internally stored IteratorAdapter.

Protected Types

typedef void(Iterator< T >::* bool_type )() const
 Used in the safe bool idiom implementation.

Protected Member Functions

void safe_bool_idiom_helper_function () const
 Used in the safe bool idiom implementation.
virtual Iteratorclone () const
 Clones the current object (i.e. *this).
virtual unsigned long distance (const Iterator< T > &other) const
 Returns the distance to the given iterator.

Friends

unsigned long distance (Iterator< T > first, Iterator< T > last)
 Returns the distance between to iterators.

Detailed Description

template<class T>
class TRTK::Iterator< T >

Common base class for input iterator adapter classes.

Template Parameters:
TValue type of the dereferenced iterator (note: this is not necessarily the type of the container element)

In C++ templated virtual member functions are not allowed. Thus, classes offering a generic iterator interface are not directly feasible (the STL does not offer a common base class). However, an adapter can be used to delegate the required operations to the particular iterator class.

This class is the common base class for the derived templated input iterator adapter classes (see IteratorAdapter).

Example:

 #include <iostream>
 #include <vector>

 #include <TRTK/Iterator.hpp>

 using namespace std;
 using namespace TRTK;

 class Interface
 {
 public:
     virtual ~Interface() {}
     virtual void doSomething(Iterator<double> begin, Iterator<double> end) = 0;
 };


 class Implementation
 {
 public:
     void doSomething(Iterator<double> begin, Iterator<double> end)
     {
         // Print the container's content.

         Iterator<double> it = begin;

         while (it != end)
         {
             cout << *it;
             ++it;
         }
     }
 };

 int main()
 {
     vector<double> vec;
     vec.push_back(1);
     vec.push_back(2);

     Implementation impl;

     Iterator<double> itBegin = make_iterator(vec.begin());
     Iterator<double> itEnd = make_iterator(vec.end());

     impl.doSomething(itBegin, itEnd);
 }

Output:

 12
Note:

You might have noticed the use of make_iterator. This avoids the need of explicitly stating the template arguments:

 Iterator<double> it = make_iterator(vec.begin());

Instead of:

 Iterator<double> it = IteratorAdapter<double, vector<double>::iterator>(vec.begin());

However, the advantage of the latter form is that it enables you to mix different container types with different value types (provided, these are convertible to each other):

 Iterator<int> it = IteratorAdapter<int, vector<double>::iterator>(vec.begin());
See also:
IteratorAdapter, make_iterator, Range
Author:
Christoph Haenisch
Version:
0.4.0
Date:
last changed on 2019-06-24

Definition at line 149 of file Iterator.hpp.


Member Function Documentation

template<class T >
Iterator< T > * TRTK::Iterator< T >::clone (  ) const [protected, virtual]

Clones the current object (i.e. *this).

Returns:
Returns a clone of the wrapped iterator created on the heap.

Reimplemented in TRTK::IteratorAdapter< T, InputIterator >.

Definition at line 345 of file Iterator.hpp.

template<class T >
unsigned long TRTK::Iterator< T >::distance ( const Iterator< T > &  other ) const [protected, virtual]

Returns the distance to the given iterator.

The given iterator must be from the same container otherwise the behaviour is undefined.

Reimplemented in TRTK::IteratorAdapter< T, InputIterator >.

Definition at line 399 of file Iterator.hpp.

template<class T >
bool TRTK::Iterator< T >::isValid (  ) const [virtual]

Returns true if the instance references an iterator.

The default instantiation does not reference an iterator and thus returns false.

 Iterator<double> it = Iterator<double>();
 it.isValid(); // returns false
Note:
For convenience Iterator<T> also provides a boolean conversion operator that allows you to write code like this:
       Iterator<double> it = make_iterator(vec.begin());
       if (it) doSomething(it); // instead of using it.isValid()

Reimplemented in TRTK::IteratorAdapter< T, InputIterator >.

Definition at line 386 of file Iterator.hpp.

template<class T >
bool TRTK::Iterator< T >::operator== ( const Iterator< T > &  other ) const [virtual]

Comparison operator.

Note:
The internally stored iterators must belong to the same sequence and must be of the same type, otherwise the behavior is undefined.
Example:
         vector<double> vec;
         vec.push_back(1);
         vector<double> vec2 = vec;
         vec.begin() == vec2.begin(); // undefined behavior

Reimplemented in TRTK::IteratorAdapter< T, InputIterator >.

Definition at line 286 of file Iterator.hpp.


Friends And Related Function Documentation

template<class T>
unsigned long distance ( Iterator< T >  first,
Iterator< T >  last 
) [friend]

Returns the distance between to iterators.

This function call is delegated to the distance function specifically defined for the internally stored iterator type.

Definition at line 421 of file Iterator.hpp.


The documentation for this class was generated from the following file:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines