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;
39 
40  ObjectIterator(const ObjectType& object, unsigned index)
41  : data(pointer(object.data())), length(object.length()), 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 data == rhs.data && index == rhs.index;
68  }
69 
70  bool operator!=(const ObjectIterator& rhs) const
71  {
72  return !operator==(rhs);
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 readValue(data + 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  auto ptr = data[index];
92  return ptr ? *ptr : std::remove_pointer<ElementType>::type::empty();
93  }
94 
95 private:
96  const pointer data;
97  size_t length;
98  unsigned index = 0;
99 };
100 
101 } // namespace FSTR
Definition: ObjectIterator.hpp:29
ObjectIterator(const ObjectType &object, unsigned index)
Definition: ObjectIterator.hpp:40
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:89
ElementType & reference
Definition: ObjectIterator.hpp:35
ObjectIterator & operator++()
Definition: ObjectIterator.hpp:45
bool operator!=(const ObjectIterator &rhs) const
Definition: ObjectIterator.hpp:70
ObjectIterator()=default
std::ptrdiff_t difference_type
Definition: ObjectIterator.hpp:33
ElementType value_type
Definition: ObjectIterator.hpp:32
ObjectIterator operator++(int)
Definition: ObjectIterator.hpp:51
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:79
ObjectIterator(const ObjectIterator &)=default
bool operator==(const ObjectIterator &rhs) const
Definition: ObjectIterator.hpp:65
ObjectIterator operator+=(size_t distance)
Definition: ObjectIterator.hpp:58
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