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 this library.
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
30 {
31 public:
32  using iterator_category = std::random_access_iterator_tag;
33  using value_type = ElementType;
34  using difference_type = std::ptrdiff_t;
35  using pointer = ElementType*;
36  using reference = ElementType&;
37 
38  ObjectIterator() = default;
39  ObjectIterator(const ObjectIterator&) = default;
40 
41  ObjectIterator(const ObjectType& object, unsigned index) : object(object), index(index)
42  {
43  }
44 
46  {
47  ++index;
48  return *this;
49  }
50 
52  {
53  ObjectIterator tmp(*this);
54  ++index;
55  return tmp;
56  }
57 
58  ObjectIterator operator+=(size_t distance)
59  {
60  ObjectIterator tmp(*this);
61  index += distance;
62  return tmp;
63  }
64 
65  bool operator==(const ObjectIterator& rhs) const
66  {
67  return index == rhs.index;
68  }
69 
70  bool operator!=(const ObjectIterator& rhs) const
71  {
72  return index != rhs.index;
73  }
74 
78  template <typename T = ElementType>
79  typename std::enable_if<!std::is_pointer<T>::value, const ElementType>::type operator*() const
80  {
81  return object.valueAt(index);
82  }
83 
87  template <typename T = ElementType>
88  typename std::enable_if<std::is_pointer<T>::value, const typename std::remove_pointer<ElementType>::type&>::type
89  operator*() const
90  {
91  return object.valueAt(index);
92  }
93 
94 private:
95  const ObjectType& object;
96  unsigned index = 0;
97 };
98 
99 } // namespace FSTR
std::ptrdiff_t difference_type
Definition: ObjectIterator.hpp:72
bool operator!=(const ObjectIterator &rhs) const
Definition: ObjectIterator.hpp:108
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:117
ObjectIterator operator+=(size_t distance)
Definition: ObjectIterator.hpp:96
ElementType & reference
Definition: ObjectIterator.hpp:74
std::random_access_iterator_tag iterator_category
Definition: ObjectIterator.hpp:70
Definition: Array.hpp:107
ElementType * pointer
Definition: ObjectIterator.hpp:73
ObjectIterator & operator++()
Definition: ObjectIterator.hpp:83
bool operator==(const ObjectIterator &rhs) const
Definition: ObjectIterator.hpp:103
Definition: ObjectIterator.hpp:48
ObjectType
Definition: Libraries/jerryscript/src/include/Jerryscript/Types.h:44
ElementType value_type
Definition: ObjectIterator.hpp:71