Belle II Software light-2406-ragdoll
ArrayIterator< ArrayType, ValueType > Class Template Reference

Generic iterator class for arrays, allowing use of STL algorithms, range-based for etc. More...

#include <ArrayIterator.h>

Public Types

using iterator_category = std::forward_iterator_tag
 Iterator category.
 
using difference_type = std::ptrdiff_t
 Difference type.
 
using value_type = ValueType
 Value type.
 
using pointer = ValueType *
 Pointer.
 
using reference = ValueType &
 Reference.
 

Public Member Functions

 ArrayIterator ()
 Default constructor (not that you can dereference these).
 
 ArrayIterator (const ArrayType *array, int index)
 Constructor.
 
ArrayIterator< ArrayType, ValueType > & operator++ ()
 Prefix increment.
 
ArrayIterator< ArrayType, ValueType > operator++ (int)
 Postfix increment.
 
bool operator== (const ArrayIterator< ArrayType, ValueType > &rhs) const
 Check equality.
 
bool operator!= (const ArrayIterator< ArrayType, ValueType > &rhs) const
 Check inequality.
 
ValueType & operator* () const
 Dereference.
 
ValueType * operator-> () const
 Dereference.
 

Static Private Member Functions

static ValueType & deref_if_needed (ValueType &t)
 Dereference if argument is a pointer to ValueType.
 
static ValueType & deref_if_needed (ValueType *t)
 Dereference if argument is a pointer to ValueType.
 

Private Attributes

const ArrayType * m_array
 Array to iterate over.
 
int m_index
 Current index.
 

Detailed Description

template<class ArrayType, class ValueType>
class Belle2::ArrayIterator< ArrayType, ValueType >

Generic iterator class for arrays, allowing use of STL algorithms, range-based for etc.

Implements all operations required for a forward iterator. Note that wether objects can actually be added (i.e. extending the array) depends on the implementation of operator[].

It only requires that ArrayType provides operator[int] returning pointers or references to ValueType, you can then return ArrayIterator objects * with indices 0 and size() from begin() and end(). You should also define iterator and const_iterator typedefs as part of ArrayType. See StoreArray for examples.

Note that dereferencing an iterator via * returns a reference to ValueType, regardless of wether ArrayType::operator[] returns ValueType* or ValueType&.

Definition at line 102 of file ArrayIterator.h.

Member Typedef Documentation

◆ difference_type

using difference_type = std::ptrdiff_t

Difference type.

Definition at line 110 of file ArrayIterator.h.

◆ iterator_category

using iterator_category = std::forward_iterator_tag

Iterator category.

Definition at line 107 of file ArrayIterator.h.

◆ pointer

using pointer = ValueType*

Pointer.

Definition at line 116 of file ArrayIterator.h.

◆ reference

using reference = ValueType&

Reference.

Definition at line 119 of file ArrayIterator.h.

◆ value_type

using value_type = ValueType

Value type.

Definition at line 113 of file ArrayIterator.h.

Constructor & Destructor Documentation

◆ ArrayIterator() [1/2]

ArrayIterator ( )
inline

Default constructor (not that you can dereference these).

Definition at line 124 of file ArrayIterator.h.

124 : m_array(nullptr), m_index(-1)
125 {
126 }
int m_index
Current index.
const ArrayType * m_array
Array to iterate over.

◆ ArrayIterator() [2/2]

ArrayIterator ( const ArrayType *  array,
int  index 
)
inlineexplicit

Constructor.

Definition at line 129 of file ArrayIterator.h.

129 :
130 m_array(array), m_index(index)
131 {
132 }

Member Function Documentation

◆ deref_if_needed() [1/2]

static ValueType & deref_if_needed ( ValueType &  t)
inlinestaticprivate

Dereference if argument is a pointer to ValueType.

Definition at line 176 of file ArrayIterator.h.

177 {
178 return t;
179 }

◆ deref_if_needed() [2/2]

static ValueType & deref_if_needed ( ValueType *  t)
inlinestaticprivate

Dereference if argument is a pointer to ValueType.

Definition at line 182 of file ArrayIterator.h.

183 {
184 return *t;
185 }

◆ operator!=()

bool operator!= ( const ArrayIterator< ArrayType, ValueType > &  rhs) const
inline

Check inequality.

Definition at line 156 of file ArrayIterator.h.

157 {
158 return !(*this == rhs);
159 }

◆ operator*()

ValueType & operator* ( ) const
inline

Dereference.

Definition at line 162 of file ArrayIterator.h.

163 {
164 return deref_if_needed((*m_array)[m_index]);
165 }
static ValueType & deref_if_needed(ValueType &t)
Dereference if argument is a pointer to ValueType.

◆ operator++() [1/2]

ArrayIterator< ArrayType, ValueType > & operator++ ( )
inline

Prefix increment.

Definition at line 135 of file ArrayIterator.h.

136 {
137 ++m_index;
138 return *this;
139 }

◆ operator++() [2/2]

ArrayIterator< ArrayType, ValueType > operator++ ( int  )
inline

Postfix increment.

Definition at line 142 of file ArrayIterator.h.

143 {
144 ArrayIterator<ArrayType, ValueType> old(*this);
145 ++(*this);
146 return old;
147 }

◆ operator->()

ValueType * operator-> ( ) const
inline

Dereference.

Definition at line 168 of file ArrayIterator.h.

169 {
170 return &(operator*());
171 }
ValueType & operator*() const
Dereference.

◆ operator==()

bool operator== ( const ArrayIterator< ArrayType, ValueType > &  rhs) const
inline

Check equality.

Definition at line 150 of file ArrayIterator.h.

151 {
152 return m_index == rhs.m_index && m_array == rhs.m_array;
153 }

Member Data Documentation

◆ m_array

const ArrayType* m_array
private

Array to iterate over.

Definition at line 188 of file ArrayIterator.h.

◆ m_index

int m_index
private

Current index.

Definition at line 191 of file ArrayIterator.h.


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