array_range.h
Go to the documentation of this file.
1 
2 /************************************************************************************
3 * *
4 * Copyright (c) 2014 - 2018 Axel Menzel <info@rttr.org> *
5 * *
6 * This file is part of RTTR (Run Time Type Reflection) *
7 * License: MIT License *
8 * *
9 * Permission is hereby granted, free of charge, to any person obtaining *
10 * a copy of this software and associated documentation files (the "Software"), *
11 * to deal in the Software without restriction, including without limitation *
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense, *
13 * and/or sell copies of the Software, and to permit persons to whom the *
14 * Software is furnished to do so, subject to the following conditions: *
15 * *
16 * The above copyright notice and this permission notice shall be included in *
17 * all copies or substantial portions of the Software. *
18 * *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, *
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE *
25 * SOFTWARE. *
26 * *
27 *************************************************************************************/
28 
29 #ifndef RTTR_ARRAY_RANGE_H_
30 #define RTTR_ARRAY_RANGE_H_
31 
32 #include "rttr/detail/base/core_prerequisites.h"
33 #include <vector>
34 #include <cstddef>
35 
36 namespace rttr
37 {
38 class property;
39 class method;
40 class constructor;
41 class enumeration;
42 class parameter_info;
43 
44 namespace detail
45 {
46 template<typename T>
47 struct default_predicate;
48 
49 } // end namespace detail
50 
51 
62 template<typename T, typename Predicate = detail::default_predicate<T>>
64 {
65 public:
66  using value_type = T;
67  using bounds_type = T*;
68  using size_type = std::size_t;
69 
73  array_range();
74 
82  array_range(const T* begin, size_type size, const Predicate& pred = Predicate());
83 
84 #ifndef DOXYGEN
85 
88  template<typename DataType>
89  class array_iterator_base
90  {
91  public:
92  using self_type = array_iterator_base<DataType>;
93  using value_type = DataType;
94  using reference = DataType&;
95  using pointer = DataType*;
96  using iterator_category = std::forward_iterator_tag;
97  using difference_type = std::ptrdiff_t;
98 
99  bool operator==(const self_type& rhs) const;
100  bool operator!=(const self_type& rhs) const;
101 
102  array_iterator_base& operator=(const self_type& other);
103 
104  protected:
105  friend class array_range<T, Predicate>;
106  array_iterator_base();
107  array_iterator_base(pointer ptr, const array_range<T, Predicate>* const range);
108 
109  pointer m_ptr;
110  const array_range<T, Predicate>* m_range;
111  };
112 
116  template<typename DataType>
117  class array_iterator : public array_iterator_base<DataType>
118  {
119  public:
120  using self_type = array_iterator<DataType>;
121  using reference = typename array_iterator_base<DataType>::reference;
122  using pointer = typename array_iterator_base<DataType>::pointer;
123 
124  array_iterator();
125  array_iterator(const array_iterator<DataType>& other);
126 
127 
128  reference operator*() const;
129  pointer operator->();
130 
131  self_type& operator++();
132  self_type operator++(int index);
133 
134  private:
135  array_iterator(typename array_iterator_base<DataType>::pointer ptr,
136  const array_range<T, Predicate>* const range);
137  friend class array_range<T, Predicate>;
138  };
139 
143  template<typename DataType>
144  class array_reverse_iterator : public array_iterator_base<DataType>
145  {
146  public:
147  using self_type = array_reverse_iterator<DataType>;
148  using reference = typename array_iterator_base<DataType>::reference;
149  using pointer = typename array_iterator_base<DataType>::pointer;
150 
151  array_reverse_iterator();
152  array_reverse_iterator(const array_reverse_iterator<DataType>& other);
153 
154  reference operator*() const;
155  pointer operator->();
156 
157  self_type& operator++();
158  self_type operator++(int index);
159 
160  private:
161  array_reverse_iterator(typename array_iterator_base<DataType>::pointer ptr,
162  const array_range<T, Predicate>* const range);
163  friend class array_range<T, Predicate>;
164  };
165 #endif
166 
168  using iterator = array_iterator<T>;
170  using const_iterator = array_iterator<const T>;
171 
173  using reverse_iterator = array_reverse_iterator<T>;
175  using const_reverse_iterator = array_reverse_iterator<const T>;
176 
185 
195 
203  const_iterator begin() const;
204 
213  const_iterator end() const;
214 
222  const_iterator cbegin() const;
223 
232  const_iterator cend() const;
233 
243 
253 
263 
273 
283 
293 
304  size_t size() const;
305 
316  bool empty() const;
317 
318 private:
319  template<typename DataType>
320  void next(array_iterator<DataType>& itr) const;
321 
322  template<typename DataType>
323  void prev(array_reverse_iterator<DataType>& itr) const;
324 
325  bool empty_() const;
326  array_range<T, Predicate>& operator=(const array_range<T, Predicate>& other);
327 
328 private:
329  const T* const m_begin;
330  const T* const m_end;
331  const Predicate m_pred;
332 };
333 
335 
336 } // end namespace rttr
337 
338 #include "rttr/detail/impl/array_range_impl.h"
339 
340 #endif // RTTR_ARRAY_RANGE_H_
Definition: access_levels.h:33
array_reverse_iterator< const T > const_reverse_iterator
A constant forward iterator that reverses the direction.
Definition: array_range.h:175
constexpr bool operator==(basic_string_view< CharT, Traits > lhs, basic_string_view< CharT, Traits > rhs) noexcept
Compares the two views lhs and rhs.
T value_type
Definition: array_range.h:66
The array_range class provides a view into an underlying data structure with lower and upper limits...
Definition: array_range.h:63
size_t size() const
Returns the number of elements in the range.
const_iterator cend() const
Returns a constant iterator to the element following the last element of the range.
array_iterator< const T > const_iterator
A constant forward iterator.
Definition: array_range.h:170
const_iterator cbegin() const
Returns a constant iterator to the first element of the range.
constexpr bool operator!=(basic_string_view< CharT, Traits > lhs, basic_string_view< CharT, Traits > rhs) noexcept
Compares the two views lhs and rhs.
std::size_t size_type
Definition: array_range.h:68
array_reverse_iterator< T > reverse_iterator
A forward iterator that reverses the direction.
Definition: array_range.h:173
array_iterator< T > iterator
A forward iterator.
Definition: array_range.h:168
const_reverse_iterator crbegin() const
Returns a constant reverse iterator to the first element of the reversed range.
const_reverse_iterator rbegin()
Returns a reverse iterator to the first element of the reversed range.
const_iterator begin()
Returns an iterator to the first element of the range.
array_range()
Default constructor.
const_reverse_iterator rend()
Returns a reverse iterator to the element following the last element of the reversed range...
const_iterator end()
Returns an iterator to the element following the last element of the range.
const_reverse_iterator crend() const
Returns a constant reverse iterator to the element following the last element of the reversed range...
bool empty() const
Checks if the range has no elements, i.e.
T * bounds_type
Definition: array_range.h:67