ObjectIterator.hpp
Go to the documentation of this file.
1 /****
2  * ObjectIterator.hpp - STL iterator support
3  *
4  * Copyright 2019 mikee47 <mike@sillyhouse.net>
5  *
6  * This file is part of the FlashString Library
7  *
8  * This library is free software: you can redistribute it and/or modify it under the terms of the
9  * GNU General Public License as published by the Free Software Foundation, version 3 or later.
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  * See the GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along with FlashString.
16  * If not, see <https://www.gnu.org/licenses/>.
17  *
18  * @author: Nov 2019 - Mikee47 <mike@sillyhouse.net>
19  *
20  ****/
21 
22 #pragma once
23 
24 #include <iterator>
25 
26 namespace FSTR
27 {
28 template <class ObjectType, typename ElementType>
29 class ObjectIterator : public std::iterator<std::random_access_iterator_tag, ElementType>
30 {
31 public:
32  ObjectIterator() = default;
33  ObjectIterator(const ObjectIterator&) = default;
34 
35  ObjectIterator(const ObjectType& object, unsigned index) : object(object), index(index)
36  {
37  }
38 
40  {
41  ++index;
42  return *this;
43  }
44 
46  {
47  ObjectIterator tmp(*this);
48  ++index;
49  return tmp;
50  }
51 
52  ObjectIterator operator+=(size_t distance)
53  {
54  ObjectIterator tmp(*this);
55  index += distance;
56  return tmp;
57  }
58 
59  bool operator==(const ObjectIterator& rhs) const
60  {
61  return index == rhs.index;
62  }
63 
64  bool operator!=(const ObjectIterator& rhs) const
65  {
66  return index != rhs.index;
67  }
68 
72  template <typename T = ElementType>
73  typename std::enable_if<!std::is_pointer<T>::value, const ElementType>::type operator*() const
74  {
75  return object.valueAt(index);
76  }
77 
81  template <typename T = ElementType>
82  typename std::enable_if<std::is_pointer<T>::value, const typename std::remove_pointer<ElementType>::type&>::type
83  operator*() const
84  {
85  return object.valueAt(index);
86  }
87 
88 private:
89  const ObjectType& object;
90  unsigned index = 0;
91 };
92 
93 } // namespace FSTR
bool operator==(const ObjectIterator &rhs) const
Definition: ObjectIterator.hpp:59
ObjectIterator(const ObjectType &object, unsigned index)
Definition: ObjectIterator.hpp:35
Definition: ObjectIterator.hpp:29
ObjectIterator operator+=(size_t distance)
Definition: ObjectIterator.hpp:52
bool operator!=(const ObjectIterator &rhs) const
Definition: ObjectIterator.hpp:64
ObjectIterator()=default
std::enable_if<!std::is_pointer< T >::value, const ElementType >::type operator*() const
Accessor returns a copy for non-pointer-type elements.
Definition: ObjectIterator.hpp:73
std::enable_if< std::is_pointer< T >::value, const typename std::remove_pointer< ElementType >::type & >::type operator*() const
Accessor returns a reference for pointer-type elements.
Definition: ObjectIterator.hpp:83
ObjectIterator operator++(int)
Definition: ObjectIterator.hpp:45
Definition: Array.hpp:107
ObjectIterator & operator++()
Definition: ObjectIterator.hpp:39