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 
48  ~ObjectIterator() = default;
49 
51  {
52  ++index;
53  return *this;
54  }
55 
57  {
58  ObjectIterator tmp(*this);
59  ++index;
60  return tmp;
61  }
62 
63  ObjectIterator operator+=(size_t distance)
64  {
65  ObjectIterator tmp(*this);
66  index += distance;
67  return tmp;
68  }
69 
70  bool operator==(const ObjectIterator& rhs) const
71  {
72  return data == rhs.data && index == rhs.index;
73  }
74 
75  bool operator!=(const ObjectIterator& rhs) const
76  {
77  return !operator==(rhs);
78  }
79 
83  template <typename T = ElementType>
84  typename std::enable_if<!std::is_pointer<T>::value, const ElementType>::type operator*() const
85  {
86  return readValue(data + index);
87  }
88 
92  template <typename T = ElementType>
94  typename std::enable_if<std::is_pointer<T>::value, const typename std::remove_pointer<ElementType>::type&>::type
95  operator*() const
96  {
97  auto ptr = data[index];
98  return ptr ? *ptr : std::remove_pointer<ElementType>::type::empty();
99  }
100 
101 private:
102  pointer data{};
103  size_t length{0};
104  unsigned index{0};
105 };
106 
107 } // namespace FSTR
Definition: ObjectIterator.hpp:29
ObjectIterator(const ObjectType &object, unsigned index)
Definition: ObjectIterator.hpp:43
ElementType & reference
Definition: ObjectIterator.hpp:35
ObjectIterator & operator=(ObjectIterator &&)=default
ObjectIterator & operator++()
Definition: ObjectIterator.hpp:50
bool operator!=(const ObjectIterator &rhs) const
Definition: ObjectIterator.hpp:75
ObjectIterator(ObjectIterator &&)=default
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:56
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:84
FSTR_ALIGN32 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:95
ObjectIterator(const ObjectIterator &)=default
bool operator==(const ObjectIterator &rhs) const
Definition: ObjectIterator.hpp:70
ObjectIterator & operator=(const ObjectIterator &)=default
ObjectIterator operator+=(size_t distance)
Definition: ObjectIterator.hpp:63
#define FSTR_ALIGN32
Definition: config.hpp:32
ObjectType
Definition: ObjectInfo.h:32
Definition: Array.hpp:118
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:134