LArSoft  v06_85_00
Liquid Argon Software toolkit - http://larsoft.org/
DereferenceIterator.h
Go to the documentation of this file.
1 
26 #ifndef LARCORE_COREUTILS_DEREFERENCEITERATOR_H
27 #define LARCORE_COREUTILS_DEREFERENCEITERATOR_H
28 
29 // C/C++ standard libraries
30 #include <utility> // std::move(), std::declval()
31 #include <type_traits> // std::add_lvalue_reference_t<>, std::add_pointer_t<>
32 
33 
34 namespace lar {
35  namespace util {
36 
37  namespace detail {
38 
39  template <typename BeginIter, typename EndIter>
40  class IteratorBox {
41  BeginIter b;
42  EndIter e;
43 
44  public:
45  IteratorBox(BeginIter b, EndIter e): b(b), e(e) {}
46 
47  BeginIter const& begin() const { return b; }
48  EndIter const& end() const { return e; }
49 
50  }; // IteratorBox<>
51 
52  template <typename BeginIter, typename EndIter>
53  auto makeIteratorBox(BeginIter b, EndIter e)
54  { return IteratorBox<BeginIter, EndIter>(b, e); }
55 
56 
57 
59  template <typename Iter, typename Value>
61  using iterator_t = Iter;
63 
65 
66  public:
68  struct initialize_tag {};
69 
72 
73  using difference_type = typename iterator_t::difference_type;
74  using value_type = Value;
75  using pointer = std::add_pointer_t<value_type>;
76  using reference = std::add_lvalue_reference_t<value_type>;
77  using iterator_category = typename iterator_t::iterator_category;
78 
80 
81 
83  DereferenceIteratorBase() = default;
84 
87  : iter(iter) {}
88 
91  : iter(std::move(iter)) {}
92 
96  template <typename OtherIter, typename OtherValue>
99  : iter(other.iter)
100  {
102  "Copying from a iterator with incompatible value"
103  );
104  }
105 
109  template <typename OtherIter, typename OtherValue>
112  : iter(std::move(other.iter))
113  {
115  "Moving from a iterator with incompatible value"
116  );
117  }
118 
119 
121  reference operator* () const { return **iter; }
122 
124  pointer operator-> () const { return *iter; }
125 
127  reference operator[] (difference_type i) const { return *(iter[i]); }
128 
130  this_t& operator++() { ++iter; return *this; }
131 
133  this_t& operator--() { --iter; return *this; }
134 
136  this_t operator++(int) { return this_t(iter++, {}); }
137 
139  this_t operator--(int) { return this_t(iter--, {}); }
140 
144  { return { iter + offset }; }
146  { return { iter - offset }; }
147 
149  { iter += offset; return *this; }
150  this_t& operator-= (difference_type offset)
151  { iter -= offset; return *this; }
152 
154 
156  difference_type operator- (this_t const& other) const
157  { return iter - other.iter; }
158 
160  bool is_null() const { return iterator_t::operator* () == nullptr; }
161 
164 
165  bool operator== (this_t const& other) const
166  { return other.iter == iter; }
167  bool operator!= (this_t const& other) const
168  { return other.iter != iter; }
169  bool operator<= (this_t const& other) const
170  { return other.iter <= iter; }
171  bool operator>= (this_t const& other) const
172  { return other.iter >= iter; }
173  bool operator< (this_t const& other) const
174  { return other.iter < iter; }
175  bool operator> (this_t const& other) const
176  { return other.iter < iter; }
177 
179 
180  }; // class DereferenceIteratorBase
181 
183  template <typename Iter, typename Value>
187  )
188  { return iter + offset; }
189 
190 
191  } // namespace detail
192 
193 
210  template <typename Iter>
212  Iter,
213  std::add_const_t<std::decay_t<decltype(**(std::declval<Iter>()))>>
214  >;
215 
224  template <typename Iter>
226  Iter,
227  std::decay_t<decltype(**(std::declval<Iter>()))>
228  >;
229 
230 
231 
245  template <typename Iter>
246  auto makeDereferenceIterator(Iter&& iter)
247  { return DereferenceIterator<Iter>(std::forward<Iter>(iter), {}); }
248 
259  template <typename Cont>
260  auto beginDereferenceIterator(Cont& cont)
261  { return makeDereferenceIterator(cont.begin()); }
262 
273  template <typename Cont>
274  auto endDereferenceIterator(Cont& cont)
275  { return makeDereferenceIterator(cont.end()); }
276 
287  template <typename Cont>
288  auto rbeginDereferenceIterator(Cont& cont)
289  { return makeDereferenceIterator(cont.rbegin()); }
290 
301  template <typename Cont>
302  auto rendDereferenceIterator(Cont& cont)
303  { return makeDereferenceIterator(cont.rend()); }
304 
330  template <typename Cont>
331  auto dereferenceIteratorLoop(Cont& cont)
332  {
335  } // dereferenceIteratorLoop()
336 
337 
366  template <typename Cont>
368  {
371  } // dereferenceIteratorReverseLoop()
372 
373 
378 
380  template <typename Iter>
382  { return DereferenceConstIterator<Iter>(std::forward<Iter>(iter), {}); }
383 
385  template <typename Cont>
386  auto cbeginDereferenceIterator(Cont& cont)
387  { return makeDereferenceConstIterator(cont.cbegin()); }
388 
390  template <typename Cont>
391  auto cendDereferenceIterator(Cont& cont)
392  { return makeDereferenceConstIterator(cont.cend()); }
393 
395  template <typename Cont>
396  auto crbeginDereferenceIterator(Cont& cont)
397  { return makeDereferenceConstIterator(cont.crbegin()); }
398 
400  template <typename Cont>
401  auto crendDereferenceIterator(Cont& cont)
402  { return makeDereferenceConstIterator(cont.crend()); }
403 
426  template <typename Cont>
428  {
431  } // dereferenceConstIteratorLoop()
432 
433 
462  template <typename Cont>
464  {
467  } // dereferenceConstIteratorReverseLoop()
468 
470 
471  } // namespace util
472 } // namespace lar
473 
474 
475 #endif // LARCORE_COREUTILS_DEREFERENCEITERATOR_H
typename iterator_t::difference_type difference_type
auto operator+(typename DereferenceIteratorBase< Iter, Value >::difference_type offset, DereferenceIteratorBase< Iter, Value > const &iter)
Swapped addition operator.
auto crbeginDereferenceIterator(Cont &cont)
Namespace for general, non-LArSoft-specific utilities.
Definition: PIDAAlg.h:17
auto crendDereferenceIterator(Cont &cont)
DereferenceIteratorBase(Iter const &iter, initialize_tag)
Constructor: copies the specified iterator in.
bool operator<(CryostatID const &a, CryostatID const &b)
Order cryostats with increasing ID.
Definition: geo_types.h:413
constexpr bool operator>(Index_t left, Flag_t< Storage > right)
Definition: BitMask.h:158
constexpr bool operator<=(Index_t left, Flag_t< Storage > right)
Definition: BitMask.h:165
BeginIter const & begin() const
auto dereferenceConstIteratorLoop(Cont &cont)
Returns an object enabling a dereferencing range-for loop.
auto dereferenceIteratorLoop(Cont &cont)
Returns an object enabling a dereferencing range-for loop.
EndIter const & end() const
STL namespace.
auto rendDereferenceIterator(Cont &cont)
Returns a dereference reverse iterator to the end of container.
auto dereferenceIteratorReverseLoop(Cont &cont)
Returns an object enabling a dereferencing reverse range-for loop.
auto beginDereferenceIterator(Cont &cont)
Returns a dereference iterator to the begin of specified container.
this_t & operator++()
Prefix increment operator.
Base class for dereferencing iterators.
auto makeIteratorBox(BeginIter b, EndIter e)
constexpr bool operator>=(Index_t left, Flag_t< Storage > right)
Definition: BitMask.h:172
QuadExpr operator-(double v, const QuadExpr &e)
Definition: QuadExpr.h:38
std::add_lvalue_reference_t< value_type > reference
std::add_pointer_t< value_type > pointer
auto makeDereferenceConstIterator(Iter &&iter)
this_t & operator--()
Prefix decrement operator.
auto makeDereferenceIterator(Iter &&iter)
Returns a dereference iterator to the begin of specified container.
String & operator+=(String &s, VectorDumper< Vector > const &manip)
Appends a string rendering of a vector to the specified string.
Definition: DumpUtils.h:424
typename iterator_t::iterator_category iterator_category
bool is_null() const
Bonus: returns true if the pointer is not dereferentiable.
auto dereferenceConstIteratorReverseLoop(Cont &cont)
Returns an object enabling a dereferencing reverse range-for loop.
bool operator!=(geometry_element_iterator< GEOIDITER > const &iter, GEOIDITER const &id_iter)
Comparison operator: geometry ID and element point to different IDs.
std::string value(boost::any const &)
auto rbeginDereferenceIterator(Cont &cont)
Returns a dereference reverse iterator to the begin of container.
this_t operator--(int)
Postfix decrement operator.
DereferenceIteratorBase(Iter &&iter, initialize_tag)
Constructor: acquires the specified iterator.
LArSoft-specific namespace.
auto cendDereferenceIterator(Cont &cont)
auto cbeginDereferenceIterator(Cont &cont)
QuadExpr operator*(double v, const QuadExpr &e)
Definition: QuadExpr.h:39
this_t operator++(int)
Postfix increment operator.
bool operator==(geometry_element_iterator< GEOIDITER > const &iter, GEOIDITER const &id_iter)
Comparison operator: geometry ID and element point to the same ID.
auto endDereferenceIterator(Cont &cont)
Returns a dereference iterator to the end of specified container.
IteratorBox(BeginIter b, EndIter e)