AutoIterator< C > Class Template Reference

#include <foreach.h>

Inheritance diagram for AutoIterator< C >:

Detailed Description

template<typename C>
class maxon::AutoIterator< C >

AutoIterator implements a foreach iterator based on two normal iterators. one for the current position and a constant one for the end. This simplifies cases like iterating over a whole array or list. Typical usage is:

for (AutoIterator<ARRAYTYPE> it(array); it; ++it)
{
// ... do something with *it or it->
}

You can specify the current and end iterator explicitly as in

for (AutoIterator<ARRAYTYPE> it(array.Begin() + 1, array.End()); it; ++it)
{
// ... do something with *it or it->
}

In addition to the basic foreach protocol, AutoIterator also supports the iterator arithmetic of the underlying iterator, but only in forward direction:

it++; // go to the next element
it += 5; // advance by 5 elements
cnt = itB - itA; // number of elements from itA to itB
Template Parameters
CEither a collection class with a valid Iterator, or the Iterator class itself.

Public Types

using Iterator = typename GetIteratorType< C >::type
 
using CollectionType = typename GetCollectionType< C >::type
 
using ValueType = decltype(*std::declval< Iterator >())
 
template<typename SUPER >
using ResultFunctions = ResultIteratorFunctions< SUPER >
 
using InvConstAutoIterator = AutoIterator< typename ConstIf< typename std::remove_const< C >::type, !STD_IS_REPLACEMENT(const, C)>::type >
 
- Public Types inherited from ForEachIterator< AutoIterator< C > >
using IsForEachIterator = std::true_type
 

Public Member Functions

MAXON_ATTRIBUTE_FORCE_INLINE AutoIterator (CollectionType &collection)
 
MAXON_ATTRIBUTE_FORCE_INLINE AutoIterator (const Iterator &s, const Iterator &e)
 
MAXON_ATTRIBUTE_FORCE_INLINE AutoIterator (CollectionType &collection, Int s, Int e=InvalidArrayIndex)
 
MAXON_ATTRIBUTE_FORCE_INLINE AutoIterator (AutoIterator &&src)
 
 MAXON_OPERATOR_MOVE_ASSIGNMENT (AutoIterator)
 
MAXON_ATTRIBUTE_FORCE_INLINE AutoIterator (const AutoIterator &src)
 
 MAXON_OPERATOR_COPY_ASSIGNMENT (AutoIterator)
 
MAXON_ATTRIBUTE_FORCE_INLINE AutoIterator ()
 
MAXON_ATTRIBUTE_FORCE_INLINE operator Bool () const
 
MAXON_ATTRIBUTE_FORCE_INLINE ValueType operator* () const
 
MAXON_ATTRIBUTE_FORCE_INLINE std::remove_reference< ValueType >::typeoperator-> () const
 
MAXON_ATTRIBUTE_FORCE_INLINE Bool operator== (const AutoIterator &b) const
 
MAXON_ATTRIBUTE_FORCE_INLINE Bool operator< (const AutoIterator &b) const
 
 MAXON_OPERATOR_COMPARISON (AutoIterator)
 
MAXON_ATTRIBUTE_FORCE_INLINE Bool operator== (const InvConstAutoIterator &b) const
 
MAXON_ATTRIBUTE_FORCE_INLINE Bool operator< (const InvConstAutoIterator &b) const
 
 MAXON_OPERATOR_COMPARISON (InvConstAutoIterator)
 
MAXON_ATTRIBUTE_FORCE_INLINE AutoIteratoroperator++ ()
 
MAXON_ATTRIBUTE_FORCE_INLINE AutoIterator operator++ (int)
 
MAXON_ATTRIBUTE_FORCE_INLINE AutoIteratoroperator+= (Int i)
 
MAXON_ATTRIBUTE_FORCE_INLINE AutoIterator operator+ (Int i) const
 
MAXON_ATTRIBUTE_FORCE_INLINE Int operator- (const AutoIterator &b) const
 
MAXON_ATTRIBUTE_FORCE_INLINE Int operator- (const Iterator &b) const
 
MAXON_ATTRIBUTE_FORCE_INLINE void Erase (CollectionType &collection)
 
MAXON_ATTRIBUTE_FORCE_INLINE operator Iterator & ()
 
MAXON_ATTRIBUTE_FORCE_INLINE IteratorGetIterator ()
 
- Public Member Functions inherited from ForEachIterator< AutoIterator< C > >
MAXON_ATTRIBUTE_FORCE_INLINE ForEachIterator (ARGS &&... args)
 
String ToString (const FormatStatement *formatStatement=nullptr)
 
AutoIterator< C > & Find (const T &v)
 
Int FindIndex (const T &v)
 
MAXON_ATTRIBUTE_FORCE_INLINE Bool Contains (const T &v)
 
MAXON_ATTRIBUTE_FORCE_INLINE Wrapper begin ()
 

Protected Member Functions

MAXON_ATTRIBUTE_FORCE_INLINE AutoIterator (const Iterator &s, const Iterator &e, Bool removed)
 

Protected Attributes

Iterator _it
 
Iterator _end
 

Additional Inherited Members

- Static Public Member Functions inherited from ForEachIterator< AutoIterator< C > >
static MAXON_ATTRIBUTE_FORCE_INLINE PRIVATE_MAXON_RBF_SENTINEL (Wrapper) end()
 

Member Typedef Documentation

◆ Iterator

using Iterator = typename GetIteratorType<C>::type

◆ CollectionType

using CollectionType = typename GetCollectionType<C>::type

◆ ValueType

using ValueType = decltype(*std::declval<Iterator>())

◆ ResultFunctions

◆ InvConstAutoIterator

using InvConstAutoIterator = AutoIterator<typename ConstIf<typename std::remove_const<C>::type, !STD_IS_REPLACEMENT(const, C)>::type>

Constructor & Destructor Documentation

◆ AutoIterator() [1/7]

Use this constructor if you want to iterate over all elements of a collection. Please note that the end iterator of the AutoIterator is a constant. When you modify an array (via Insert(), Append() or Erase()) the value of its End() iterator will most likely change and trying to iterate over such an array with an AutoIterator while modifying it would crash. To safely erase the current element, use iterator.Erase(collection).

◆ AutoIterator() [2/7]

use this constructor if you want to iterate from s until e (excluding e)

◆ AutoIterator() [3/7]

◆ AutoIterator() [4/7]

◆ AutoIterator() [5/7]

◆ AutoIterator() [6/7]

◆ AutoIterator() [7/7]

MAXON_ATTRIBUTE_FORCE_INLINE AutoIterator ( const Iterator s,
const Iterator e,
Bool  removed 
)
protected

Member Function Documentation

◆ MAXON_OPERATOR_MOVE_ASSIGNMENT()

MAXON_OPERATOR_MOVE_ASSIGNMENT ( AutoIterator< C >  )

◆ MAXON_OPERATOR_COPY_ASSIGNMENT()

MAXON_OPERATOR_COPY_ASSIGNMENT ( AutoIterator< C >  )

◆ operator Bool()

MAXON_ATTRIBUTE_FORCE_INLINE operator Bool ( ) const
explicit

Operator that returns false when the end of the array has been reached. explicit operator bool() would be ambiguous because it can be used for integer arithmetic. For C++11 we use explicit bool to avoid that implicit conversion, for older compilers void* is returned because you can't do arithmetic with it.

Returns
False when the end of the array has been reached.

◆ operator*()

◆ operator->()

MAXON_ATTRIBUTE_FORCE_INLINE std::remove_reference<ValueType>::type* operator-> ( void  ) const

◆ operator==() [1/2]

MAXON_ATTRIBUTE_FORCE_INLINE Bool operator== ( const AutoIterator< C > &  b) const

◆ operator<() [1/2]

MAXON_ATTRIBUTE_FORCE_INLINE Bool operator< ( const AutoIterator< C > &  b) const

◆ MAXON_OPERATOR_COMPARISON() [1/2]

MAXON_OPERATOR_COMPARISON ( AutoIterator< C >  )

◆ operator==() [2/2]

MAXON_ATTRIBUTE_FORCE_INLINE Bool operator== ( const InvConstAutoIterator< C > &  b) const

◆ operator<() [2/2]

MAXON_ATTRIBUTE_FORCE_INLINE Bool operator< ( const InvConstAutoIterator< C > &  b) const

◆ MAXON_OPERATOR_COMPARISON() [2/2]

MAXON_OPERATOR_COMPARISON ( InvConstAutoIterator< C >  )

◆ operator++() [1/2]

◆ operator++() [2/2]

◆ operator+=()

◆ operator+()

◆ operator-() [1/2]

MAXON_ATTRIBUTE_FORCE_INLINE Int operator- ( const AutoIterator< C > &  b) const

◆ operator-() [2/2]

MAXON_ATTRIBUTE_FORCE_INLINE Int operator- ( const Iterator b) const

◆ Erase()

MAXON_ATTRIBUTE_FORCE_INLINE void Erase ( CollectionType collection)

Removes the current iteration value from the underlying collection. The AutoIterator will point to the element behind the removed one afterwards. Example:

for (AutoIterator<COLLECTION> it(coll); it; )
{
if (*it == valueToErase)
it.Erase(coll);
else
++it;
}
Parameters
[in]collectionThe collection to remove the value from. This has to be the collection which was used to construct this AutoIterator.

◆ operator Iterator &()

◆ GetIterator()

Member Data Documentation

◆ _it

Iterator _it
protected

◆ _end

Iterator _end
protected