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> class ObjectIterator
29 {
30 public:
31  using iterator_category = std::random_access_iterator_tag;
32  using value_type = ElementType;
33  using difference_type = std::ptrdiff_t;
34  using pointer = typename ObjectType::DataPtrType;
35  using reference = ElementType&;
36 
37  ObjectIterator() = default;
38  ObjectIterator(const ObjectIterator&) = default;
42 
43  ObjectIterator(const ObjectType& object, unsigned index)
44  : data(pointer(object.data())), length(object.length()), index(index)
45  {
46  }
47 
49  {
50  }
51 
53  {
54  ++index;
55  return *this;
56  }
57 
59  {
60  ObjectIterator tmp(*this);
61  ++index;
62  return tmp;
63  }
64 
65  ObjectIterator operator+=(size_t distance)
66  {
67  ObjectIterator tmp(*this);
68  index += distance;
69  return tmp;
70  }
71 
72  bool operator==(const ObjectIterator& rhs) const
73  {
74  return data == rhs.data && index == rhs.index;
75  }
76 
77  bool operator!=(const ObjectIterator& rhs) const
78  {
79  return !operator==(rhs);
80  }
81 
85  template <typename T = ElementType>
86  typename std::enable_if<!std::is_pointer<T>::value, const ElementType>::type operator*() const
87  {
88  return readValue(data + index);
89  }
90 
94  template <typename T = ElementType>
95  typename std::enable_if<std::is_pointer<T>::value, const typename std::remove_pointer<ElementType>::type&>::type
96  operator*() const
97  {
98  auto ptr = data[index];
99  return ptr ? *ptr : std::remove_pointer<ElementType>::type::empty();
100  }
101 
102 private:
103  pointer data{};
104  size_t length{0};
105  unsigned index{0};
106 };
107 
108 } // namespace FSTR
Definition: ObjectIterator.hpp:29
ObjectIterator(const ObjectType &object, unsigned index)
Definition: ObjectIterator.hpp:43
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:96
ElementType & reference
Definition: ObjectIterator.hpp:35
ObjectIterator & operator=(ObjectIterator &&)=default
ObjectIterator & operator++()
Definition: ObjectIterator.hpp:52
~ObjectIterator()
Definition: ObjectIterator.hpp:48
bool operator!=(const ObjectIterator &rhs) const
Definition: ObjectIterator.hpp:77
ObjectIterator(ObjectIterator &&)=default
ObjectIterator()=default
std::ptrdiff_t difference_type
Definition: ObjectIterator.hpp:33
ElementType value_type
Definition: ObjectIterator.hpp:32
ObjectIterator operator++(int)
Definition: ObjectIterator.hpp:58
typename ObjectType::DataPtrType pointer
Definition: ObjectIterator.hpp:34
std::random_access_iterator_tag iterator_category
Definition: ObjectIterator.hpp:31
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:86
ObjectIterator(const ObjectIterator &)=default
bool operator==(const ObjectIterator &rhs) const
Definition: ObjectIterator.hpp:72
ObjectIterator & operator=(const ObjectIterator &)=default
ObjectIterator operator+=(size_t distance)
Definition: ObjectIterator.hpp:65
Definition: Array.hpp:108
std::enable_if< sizeof(T)==1, T >::type readValue(const T *ptr)
Read a typed value from flash memory ensuring correct alignment of access.
Definition: Utility.hpp:126
ObjectType
Definition: Libraries/jerryscript/src/include/Jerryscript/Types.h:34