TDME2  1.9.200
UniquePtrSequenceIterator.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <memory>
4 #include <iterator>
5 #include <cstddef>
6 
7 #include <tdme/tdme.h>
8 
9 using std::forward_iterator_tag;
10 using std::ptrdiff_t;
11 using std::unique_ptr;
12 
13 namespace tdme {
14 namespace utilities {
15 
16 /**
17  * unique_ptr sequence iterator
18  * @author Andreas Drewke
19  */
20 template<typename T>
22 
23  /**
24  * Iterator for unique_ptr sequence iterator
25  * @author Andreas Drewke
26  */
27  template<typename IT, typename IV>
28  struct Iterator
29  {
30  // see: https://www.internalpointers.com/post/writing-custom-iterators-modern-cpp
31  using iterator_category = forward_iterator_tag;
32  using difference_type = ptrdiff_t;
33  using value_type = IV*;
34  using pointer = IV*;
35  using reference = IV*;
36 
37  /**
38  * Constructor
39  * @param ptr pointer to container element
40  */
41  inline Iterator(IT* ptr) : ptr(ptr) {}
42 
43  /**
44  * @return access iterator '*'
45  */
46  inline reference operator*() { return ptr->get(); }
47 
48  /**
49  * @return access iterator '->'
50  */
51  inline pointer operator->() { return ptr->get(); }
52 
53  /**
54  * @return prefix incremented iterator
55  */
56  inline Iterator<IT, IV>& operator++() { ptr++; return *this; }
57 
58  /**
59  * @return postfix incremented iterator
60  */
61  inline Iterator<IT, IV> operator++(int) { auto tmp = *this; ++(*this); return tmp; }
62 
63  /**
64  * Equality comparison operator
65  * @param a a iterator
66  * @param b b iterator
67  */
68  inline friend bool operator== (const Iterator<IT, IV>& a, const Iterator<IT, IV>& b) { return a.ptr == b.ptr; };
69 
70  /**
71  * Inequality comparison operator
72  * @param a a iterator
73  * @param b b iterator
74  */
75  inline friend bool operator!= (const Iterator<IT, IV>& a, const Iterator<IT, IV>& b) { return a.ptr != b.ptr; };
76 
77  private:
78  IT* ptr;
79  };
80 
81  /**
82  * unique_ptr sequence iterator
83  * @param sequenceBegin pointer to first element
84  * @param sequenceEnd pointer to last + 1 element
85  */
87 
88  /**
89  * @return begin iterator
90  */
92 
93  /**
94  * @return end iterator
95  */
97 
98 private:
99  unique_ptr<T>* sequenceBegin;
100  unique_ptr<T>* sequenceEnd;
101 };
102 
103 /**
104  * Const unique_ptr sequence iterator
105  * @author Andreas Drewke
106  */
107 template<typename T>
109 
110  /**
111  * Iterator for unique_ptr sequence iterator
112  * @author Andreas Drewke
113  */
114  template<typename IT, typename IV>
115  struct Iterator
116  {
117  // see: https://www.internalpointers.com/post/writing-custom-iterators-modern-cpp
118  using iterator_category = forward_iterator_tag;
119  using difference_type = ptrdiff_t;
120  using value_type = IV*;
121  using pointer = IV*;
122  using reference = IV*;
123 
124  /**
125  * Constructor
126  * @param ptr pointer to container element
127  */
128  inline Iterator(const IT* ptr) : ptr(ptr) {}
129 
130  /**
131  * @return access iterator '*'
132  */
133  inline reference operator*() const { return ptr->get(); }
134 
135  /**
136  * @return access iterator '->'
137  */
138  inline pointer operator->() const { return ptr->get(); }
139 
140  /**
141  * @return prefix incremented iterator
142  */
143  inline Iterator<IT, IV>& operator++() { ptr++; return *this; }
144 
145  /**
146  * @return postfix incremented iterator
147  */
148  inline Iterator<IT, IV> operator++(int) { auto tmp = *this; ++(*this); return tmp; }
149 
150  /**
151  * Equality comparison operator
152  * @param a a iterator
153  * @param b b iterator
154  */
155  inline friend bool operator== (const Iterator<IT, IV>& a, const Iterator<IT, IV>& b) { return a.ptr == b.ptr; };
156 
157  /**
158  * Inequality comparison operator
159  * @param a a iterator
160  * @param b b iterator
161  */
162  inline friend bool operator!= (const Iterator<IT, IV>& a, const Iterator<IT, IV>& b) { return a.ptr != b.ptr; };
163 
164  private:
165  const IT* ptr;
166  };
167 
168  /**
169  * Const unique_ptr sequence iterator
170  * @param sequenceBegin pointer to first element
171  * @param sequenceEnd pointer to last + 1 element
172  */
174 
175  /**
176  * @return begin iterator
177  */
179 
180  /**
181  * @return end iterator
182  */
184 
185 private:
186  const unique_ptr<T>* sequenceBegin;
187  const unique_ptr<T>* sequenceEnd;
188 };
189 
190 };
191 };
Definition: fwd-tdme.h:4
friend bool operator==(const Iterator< IT, IV > &a, const Iterator< IT, IV > &b)
Equality comparison operator.
friend bool operator!=(const Iterator< IT, IV > &a, const Iterator< IT, IV > &b)
Inequality comparison operator.
Iterator< const unique_ptr< T >, const T > begin()
ConstUniquePtrSequenceIterator(const unique_ptr< T > *sequenceBegin, const unique_ptr< T > *sequenceEnd)
Const unique_ptr sequence iterator.
Iterator< const unique_ptr< T >, const T > end()
friend bool operator==(const Iterator< IT, IV > &a, const Iterator< IT, IV > &b)
Equality comparison operator.
friend bool operator!=(const Iterator< IT, IV > &a, const Iterator< IT, IV > &b)
Inequality comparison operator.
UniquePtrSequenceIterator(unique_ptr< T > *sequenceBegin, unique_ptr< T > *sequenceEnd)
unique_ptr sequence iterator