pstore2
pointer_based_iterator.hpp
Go to the documentation of this file.
1 //===- include/pstore/adt/pointer_based_iterator.hpp ------*- mode: C++ -*-===//
2 //* _ _ _ _ *
3 //* _ __ ___ (_)_ __ | |_ ___ _ __ | |__ __ _ ___ ___ __| | *
4 //* | '_ \ / _ \| | '_ \| __/ _ \ '__| | '_ \ / _` / __|/ _ \/ _` | *
5 //* | |_) | (_) | | | | | || __/ | | |_) | (_| \__ \ __/ (_| | *
6 //* | .__/ \___/|_|_| |_|\__\___|_| |_.__/ \__,_|___/\___|\__,_| *
7 //* |_| *
8 //* _ _ _ *
9 //* (_) |_ ___ _ __ __ _| |_ ___ _ __ *
10 //* | | __/ _ \ '__/ _` | __/ _ \| '__| *
11 //* | | || __/ | | (_| | || (_) | | *
12 //* |_|\__\___|_| \__,_|\__\___/|_| *
13 //* *
14 //===----------------------------------------------------------------------===//
15 //
16 // Part of the pstore project, under the Apache License v2.0 with LLVM Exceptions.
17 // See https://github.com/SNSystems/pstore/blob/master/LICENSE.txt for license
18 // information.
19 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
20 //
21 //===----------------------------------------------------------------------===//
42 
43 #ifndef PSTORE_ADT_POINTER_BASED_ITERATOR_HPP
44 #define PSTORE_ADT_POINTER_BASED_ITERATOR_HPP
45 
46 #include <cstddef>
47 #include <iterator>
48 #include <type_traits>
49 
50 namespace pstore {
51 
52  template <typename T>
54  public:
55  using value_type = T;
56  using difference_type = std::ptrdiff_t;
57  using pointer = value_type *;
58  using reference = value_type &;
59  using iterator_category = std::random_access_iterator_tag;
60 
61  template <typename Other, typename = typename std::enable_if_t<
62  std::is_const<T>::value && !std::is_const<Other>::value>>
63  explicit constexpr pointer_based_iterator (Other const * const pos) noexcept
64  : pos_{pos} {}
65 
66  template <typename Other, typename = typename std::enable_if_t<std::is_const<T>::value ==
67  std::is_const<Other>::value>>
68  explicit constexpr pointer_based_iterator (Other * const pos) noexcept
69  : pos_{pos} {}
70 
71  template <typename Other>
72  constexpr bool operator== (pointer_based_iterator<Other> const & other) const noexcept {
73  return pos_ == &*other;
74  }
75  template <typename Other>
76  constexpr bool operator!= (pointer_based_iterator<Other> const & other) const noexcept {
77  return pos_ != &*other;
78  }
79 
80  template <typename Other>
81  constexpr pointer_based_iterator &
82  operator= (pointer_based_iterator<Other> const & other) noexcept {
83  pos_ = &*other;
84  return *this;
85  }
86 
87  constexpr value_type * operator-> () noexcept { return pos_; }
88  constexpr value_type const * operator-> () const noexcept { return pos_; }
89  constexpr value_type & operator* () noexcept { return *pos_; }
90  constexpr value_type const & operator* () const noexcept { return *pos_; }
91 
92  constexpr value_type & operator[] (std::size_t const n) noexcept { return *(pos_ + n); }
93  constexpr value_type const & operator[] (std::size_t const n) const noexcept {
94  return *(pos_ + n);
95  }
96 
97  pointer_based_iterator & operator++ () noexcept {
98  ++pos_;
99  return *this;
100  }
101  pointer_based_iterator operator++ (int) noexcept {
102  auto const prev = *this;
103  ++*this;
104  return prev;
105  }
106  pointer_based_iterator & operator-- () noexcept {
107  --pos_;
108  return *this;
109  }
110  pointer_based_iterator operator-- (int) noexcept {
111  auto const prev = *this;
112  --*this;
113  return prev;
114  }
115 
116  pointer_based_iterator & operator+= (difference_type const n) noexcept {
117  pos_ += n;
118  return *this;
119  }
120  pointer_based_iterator & operator-= (difference_type const n) noexcept {
121  pos_ -= n;
122  return *this;
123  }
124 
125  template <typename Other>
126  constexpr bool operator< (pointer_based_iterator<Other> const & other) const noexcept {
127  return pos_ < &*other;
128  }
129  template <typename Other>
130  constexpr bool operator> (pointer_based_iterator<Other> const & other) const noexcept {
131  return pos_ > &*other;
132  }
133  template <typename Other>
134  constexpr bool operator<= (pointer_based_iterator<Other> const & other) const noexcept {
135  return pos_ <= &*other;
136  }
137  template <typename Other>
138  constexpr bool operator>= (pointer_based_iterator<Other> const & other) const noexcept {
139  return pos_ >= &*other;
140  }
141 
142  private:
143  pointer pos_;
144  };
145 
150  template <typename T>
152  operator+ (pointer_based_iterator<T> const i,
153  typename pointer_based_iterator<T>::difference_type const n) noexcept {
154  auto temp = i;
155  return temp += n;
156  }
161  template <typename T>
163  operator+ (typename pointer_based_iterator<T>::difference_type const n,
164  pointer_based_iterator<T> const i) noexcept {
165  auto temp = i;
166  return temp += n;
167  }
168 
173  template <typename T>
175  operator- (pointer_based_iterator<T> const i,
176  typename pointer_based_iterator<T>::difference_type const n) noexcept {
177  auto temp = i;
178  return temp -= n;
179  }
180 
181  template <typename T>
182  inline typename pointer_based_iterator<T>::difference_type
183  operator- (pointer_based_iterator<T> b, pointer_based_iterator<T> a) noexcept {
184  return &*b - &*a;
185  }
186 
187 } // end namespace pstore
188 
189 #endif // PSTORE_ADT_POINTER_BASED_ITERATOR_HPP
Definition: pointer_based_iterator.hpp:53
Definition: nonpod2.cpp:40