Common base class for input iterator adapter classes. More...
#include <Iterator.hpp>
Public Member Functions | |
Iterator () | |
Default constructor. | |
Iterator (const Iterator< T > &other) | |
Copy constructor. | |
virtual | ~Iterator () |
Destructor. | |
virtual Iterator & | operator= (const Iterator< T > &other) |
Assignment operator. | |
virtual T | operator* () const |
Dereference operator. | |
virtual Iterator & | operator++ () |
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 Iterator & | getIterator () 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 Iterator * | clone () 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. |
Common base class for input iterator adapter classes.
T | Value 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
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());
Definition at line 149 of file Iterator.hpp.
Iterator< T > * TRTK::Iterator< T >::clone | ( | ) | const [protected, virtual] |
Clones the current object (i.e. *this
).
Reimplemented in TRTK::IteratorAdapter< T, InputIterator >.
Definition at line 345 of file Iterator.hpp.
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.
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
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.
bool TRTK::Iterator< T >::operator== | ( | const Iterator< T > & | other ) | const [virtual] |
Comparison operator.
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.
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.
Documentation generated by Doxygen