KSS Utility
C++ general utilities
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends
kss::util::iterators::ForwardIterator< Container, T > Class Template Reference

Base implementation of a forward iterator. More...

#include <iterator.hpp>

Inheritance diagram for kss::util::iterators::ForwardIterator< Container, T >:

Public Member Functions

 ForwardIterator ()
 
 ForwardIterator (Container &cont)
 
 ForwardIterator (const ForwardIterator &it)=default
 
 ForwardIterator (ForwardIterator &&it)
 
ForwardIteratoroperator= (const ForwardIterator &it)=default
 
ForwardIteratoroperator= (ForwardIterator &&it) noexcept
 
bool operator== (const ForwardIterator &it) const noexcept
 
bool operator!= (const ForwardIterator &it) const noexcept
 
const T & operator* () const
 
const T * operator-> () const
 
ForwardIteratoroperator++ ()
 
ForwardIterator operator++ (int)
 
void swap (ForwardIterator &b) noexcept
 

Detailed Description

template<typename Container, typename T>
class kss::util::iterators::ForwardIterator< Container, T >

Base implementation of a forward iterator.

The ForwardIterator class provides a forward iterator for any class that can move through its elements in a single direction. Specifically it needs to support the following operations:

void hasAnother(); // Returns true if there is at least one more item available.
void next(T& t); // Fill in the next value .

The type T needs to support a default constructor and copy and move semantics.

Typically the container begin() and end() methods would be defined along the lines of the following:

using iterator = kss::util::iterator::ForwardIterator<Container, T>;
iterator begin() { return iterator(*this); }
iterator end() { return iterator(); }

Definition at line 61 of file iterator.hpp.

Constructor & Destructor Documentation

template<typename Container, typename T>
kss::util::iterators::ForwardIterator< Container, T >::ForwardIterator ( )
inline

The default constructor creates an iterator that points to nothing other than a special "end" flag.

The container version of the constructor creates an iterator and attempts to obtain the next value from the container.

Exceptions
anyexception that next() may throw
anyexception that the T copy and move semantics may throw

Definition at line 76 of file iterator.hpp.

template<typename Container, typename T>
kss::util::iterators::ForwardIterator< Container, T >::ForwardIterator ( Container &  cont)
inlineexplicit

Definition at line 77 of file iterator.hpp.

template<typename Container, typename T>
kss::util::iterators::ForwardIterator< Container, T >::ForwardIterator ( const ForwardIterator< Container, T > &  it)
default
template<typename Container, typename T>
kss::util::iterators::ForwardIterator< Container, T >::ForwardIterator ( ForwardIterator< Container, T > &&  it)
inline

Definition at line 88 of file iterator.hpp.

Member Function Documentation

template<typename Container, typename T>
bool kss::util::iterators::ForwardIterator< Container, T >::operator!= ( const ForwardIterator< Container, T > &  it) const
inlinenoexcept

Definition at line 114 of file iterator.hpp.

template<typename Container, typename T>
const T& kss::util::iterators::ForwardIterator< Container, T >::operator* ( ) const
inline

Dereference the iterator. This is undefined if we try to dereference the end() value.

Definition at line 122 of file iterator.hpp.

template<typename Container, typename T>
ForwardIterator& kss::util::iterators::ForwardIterator< Container, T >::operator++ ( )
inline

Increment the iterator. If the containers eof() is true then this will become an instance of the end iterator. Otherwise we call the containers >> operator and store the result.

If for some reason eof() cannot be known until operator>> is called, then you should have your operator>> throw a kss::eof exception if it reaches the end. This allows us to avoid having to write "lookahead" code in these situations.

Exceptions
runtime_errorif this is already the end iterator.
anyexception that container::eof() or container::operator>>(T&) may throw

Definition at line 137 of file iterator.hpp.

template<typename Container, typename T>
ForwardIterator kss::util::iterators::ForwardIterator< Container, T >::operator++ ( int  )
inline

Definition at line 152 of file iterator.hpp.

template<typename Container, typename T>
const T* kss::util::iterators::ForwardIterator< Container, T >::operator-> ( ) const
inline

Definition at line 123 of file iterator.hpp.

template<typename Container, typename T>
ForwardIterator& kss::util::iterators::ForwardIterator< Container, T >::operator= ( const ForwardIterator< Container, T > &  it)
default
template<typename Container, typename T>
ForwardIterator& kss::util::iterators::ForwardIterator< Container, T >::operator= ( ForwardIterator< Container, T > &&  it)
inlinenoexcept

Definition at line 95 of file iterator.hpp.

template<typename Container, typename T>
bool kss::util::iterators::ForwardIterator< Container, T >::operator== ( const ForwardIterator< Container, T > &  it) const
inlinenoexcept

Determine iterator equality. Any "end" iterators are considered equal, even if they have different streams. Any other iterators are equal so long as they are referencing the same stream. And iterator that is not an "end" iterator is not equal to an "end" iterator.

Definition at line 109 of file iterator.hpp.

template<typename Container, typename T>
void kss::util::iterators::ForwardIterator< Container, T >::swap ( ForwardIterator< Container, T > &  b)
inlinenoexcept

Swap with another iterator.

Definition at line 161 of file iterator.hpp.


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