Belle II Software  release-08-01-10
ArrayIterator.h
1 /**************************************************************************
2  * basf2 (Belle II Analysis Software Framework) *
3  * Author: The Belle II Collaboration *
4  * *
5  * See git log for contributors and copyright holders. *
6  * This file is licensed under LGPL-3.0, see LICENSE.md. *
7  **************************************************************************/
8 
9 #pragma once
10 
11 #include <iterator>
12 
13 class TObject;
14 
15 namespace Belle2 {
23  template <class ArrayType, class ValueType> class ObjArrayIterator: public std::iterator<std::forward_iterator_tag, ValueType> {
24  public:
26  ObjArrayIterator() = default;
31  ObjArrayIterator(ArrayType& array, size_t index): m_array{array.GetObjectRef() + index} {}
32 
40  explicit ObjArrayIterator(ArrayType const* array, bool end = false)
41  {
42  if (array) {
43  m_array = array->GetObjectRef() + (end ? array->GetEntriesFast() : 0);
44  }
45  }
46 
54  explicit ObjArrayIterator(ArrayType const* const* array, bool end = false): ObjArrayIterator(array ? * array : nullptr, end) {}
58  ObjArrayIterator<ArrayType, ValueType> operator++(int) { auto old = *this; ++(*this); return old; }
60  bool operator==(const ObjArrayIterator<ArrayType, ValueType>& rhs) const { return m_array == rhs.m_array; }
62  bool operator!=(const ObjArrayIterator<ArrayType, ValueType>& rhs) const { return !(*this == rhs); }
63 
65  ValueType& operator*() const { return *static_cast<ValueType*>(*m_array); }
67  ValueType* operator->() const { return &(operator*()); }
68  private:
70  TObject** m_array{nullptr};
71  };
72 
85  template <class ArrayType, class ValueType>
86  class ArrayIterator : public std::iterator<std::forward_iterator_tag, ValueType> {
88  static ValueType& deref_if_needed(ValueType& t) { return t; }
90  static ValueType& deref_if_needed(ValueType* t) { return *t; }
91  public:
93  ArrayIterator(): m_array(nullptr), m_index(-1) {}
94 
96  explicit ArrayIterator(const ArrayType* array, int index):
97  m_array(array),
98  m_index(index)
99  {}
100 
103 
106  {
108  ++(*this);
109  return old;
110  }
111 
113  bool operator==(const ArrayIterator<ArrayType, ValueType>& rhs) const { return m_index == rhs.m_index && m_array == rhs.m_array; }
115  bool operator!=(const ArrayIterator<ArrayType, ValueType>& rhs) const { return !(*this == rhs); }
116 
118  ValueType& operator*() const { return deref_if_needed((*m_array)[m_index]); }
120  ValueType* operator->() const { return &(operator*()); }
121 
122  private:
123  const ArrayType* m_array;
124  int m_index;
125  };
127 }
Generic iterator class for arrays, allowing use of STL algorithms, range-based for etc.
Definition: ArrayIterator.h:86
ValueType & operator*() const
dereference.
static ValueType & deref_if_needed(ValueType &t)
dereference if argument is a pointer to ValueType.
Definition: ArrayIterator.h:88
ArrayIterator< ArrayType, ValueType > & operator++()
prefix increment.
ValueType * operator->() const
dereference.
static ValueType & deref_if_needed(ValueType *t)
dereference if argument is a pointer to ValueType.
Definition: ArrayIterator.h:90
bool operator!=(const ArrayIterator< ArrayType, ValueType > &rhs) const
check inequality.
ArrayIterator(const ArrayType *array, int index)
Constructor.
Definition: ArrayIterator.h:96
bool operator==(const ArrayIterator< ArrayType, ValueType > &rhs) const
check equality.
int m_index
Current index.
ArrayIterator()
Default constructor (not that you can dereference these).
Definition: ArrayIterator.h:93
ArrayIterator< ArrayType, ValueType > operator++(int)
postfix increment.
const ArrayType * m_array
Array to iterate over.
Optimizes class to iterate over TObjArray and classes inheriting from it.
Definition: ArrayIterator.h:23
ObjArrayIterator()=default
default constructor
ValueType & operator*() const
dereference.
Definition: ArrayIterator.h:65
ValueType * operator->() const
dereference.
Definition: ArrayIterator.h:67
bool operator!=(const ObjArrayIterator< ArrayType, ValueType > &rhs) const
check inequality.
Definition: ArrayIterator.h:62
ObjArrayIterator(ArrayType &array, size_t index)
real constructor
Definition: ArrayIterator.h:31
bool operator==(const ObjArrayIterator< ArrayType, ValueType > &rhs) const
check equality.
Definition: ArrayIterator.h:60
ObjArrayIterator< ArrayType, ValueType > & operator++()
prefix increment
Definition: ArrayIterator.h:56
ObjArrayIterator(ArrayType const *const *array, bool end=false)
Convenience constructor because we usually have a TClonesArray** member so this takes cares of the ch...
Definition: ArrayIterator.h:54
ObjArrayIterator< ArrayType, ValueType > operator++(int)
postfix increment
Definition: ArrayIterator.h:58
TObject ** m_array
pointer to the fCont member of the TObjArray
Definition: ArrayIterator.h:70
ObjArrayIterator(ArrayType const *array, bool end=false)
Convenience constructor in case of a TClonesArray pointer.
Definition: ArrayIterator.h:40
Abstract base class for different kinds of events.