Pigweed
C/C++ API Reference
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Loading...
Searching...
No Matches
ptr_iterator.h
1// Copyright 2025 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
4// use this file except in compliance with the License. You may obtain a copy of
5// the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12// License for the specific language governing permissions and limitations under
13// the License.
14#pragma once
15
16#include <cstddef>
17
18#include "pw_containers/iterator.h"
19
20namespace pw::containers {
21namespace internal {
22
23// Generic PtrIterator for const or non-const iterators. Extended by
24// pw::containers::PtrIterator and pw::containers::ConstPtrIterator.
25template <typename Iterator, typename Container>
27 public:
28 using value_type = typename Container::value_type;
29 using difference_type = std::ptrdiff_t;
30 using reference =
31 typename std::conditional_t<std::is_const_v<Container>,
32 typename Container::const_reference,
33 typename Container::reference>;
34 using pointer = typename std::conditional_t<std::is_const_v<Container>,
35 typename Container::const_pointer,
36 typename Container::pointer>;
37 using iterator_category = containers::contiguous_iterator_tag;
38
39 constexpr reference operator*() const { return *ptr_; }
40 constexpr pointer operator->() const { return ptr_; }
41
42 constexpr Iterator& operator++() {
43 ++ptr_;
44 return static_cast<Iterator&>(*this);
45 }
46
47 constexpr Iterator& operator--() {
48 --ptr_;
49 return static_cast<Iterator&>(*this);
50 }
51
52 constexpr Iterator operator++(int) {
53 Iterator original(ptr_);
54 ++(*this);
55 return original;
56 }
57
58 constexpr Iterator operator--(int) {
59 Iterator original{ptr_};
60 --(*this);
61 return original;
62 }
63
64 constexpr Iterator& operator+=(difference_type n) {
65 ptr_ += n;
66 return static_cast<Iterator&>(*this);
67 }
68
69 constexpr Iterator& operator-=(difference_type n) {
70 ptr_ -= n;
71 return static_cast<Iterator&>(*this);
72 }
73
74 friend constexpr Iterator operator+(Iterator it, difference_type n) {
75 return it.Add(n);
76 }
77
78 friend constexpr Iterator operator+(difference_type n, Iterator it) {
79 return it.Add(n);
80 }
81
82 friend constexpr Iterator operator-(Iterator it, difference_type n) {
83 return it.Add(-n);
84 }
85
86 friend constexpr difference_type operator-(Iterator lhs, Iterator rhs) {
87 return lhs.ptr_ - rhs.ptr_;
88 }
89
90 friend constexpr bool operator==(Iterator lhs, Iterator rhs) {
91 return lhs.ptr_ == rhs.ptr_;
92 }
93 friend constexpr bool operator!=(Iterator lhs, Iterator rhs) {
94 return lhs.ptr_ != rhs.ptr_;
95 }
96 friend constexpr bool operator<(Iterator lhs, Iterator rhs) {
97 return lhs.ptr_ < rhs.ptr_;
98 }
99 friend constexpr bool operator<=(Iterator lhs, Iterator rhs) {
100 return lhs.ptr_ <= rhs.ptr_;
101 }
102 friend constexpr bool operator>(Iterator lhs, Iterator rhs) {
103 return lhs.ptr_ > rhs.ptr_;
104 }
105 friend constexpr bool operator>=(Iterator lhs, Iterator rhs) {
106 return lhs.ptr_ >= rhs.ptr_;
107 }
108
109 constexpr reference operator[](difference_type n) const { return ptr_[n]; }
110
111 protected:
112 constexpr PtrIterator() : ptr_(nullptr) {}
113
114 constexpr explicit PtrIterator(pointer ptr) : ptr_(ptr) {}
115
116 private:
117 constexpr Iterator Add(difference_type n) const { return Iterator(ptr_ + n); }
118
119 pointer ptr_;
120};
121
122} // namespace internal
123
135template <typename Container>
137 : public internal::PtrIterator<PtrIterator<Container>, Container> {
138 public:
139 constexpr PtrIterator() = default;
140
141 private:
143
144 friend Base;
145 friend Container;
146
147 explicit constexpr PtrIterator(typename Base::pointer ptr) : Base(ptr) {}
148};
149
156template <typename Container>
158 : public internal::PtrIterator<ConstPtrIterator<Container>,
159 const Container> {
160 public:
161 constexpr ConstPtrIterator() = default;
162
163 // Implicit conversion from non-const iterators.
165 : Base(other.operator->()) {}
166
167 private:
169
170 friend Base;
171 friend Container;
172
173 explicit constexpr ConstPtrIterator(typename Base::pointer ptr) : Base(ptr) {}
174};
175
176} // namespace pw::containers
Definition: ptr_iterator.h:159
Definition: ptr_iterator.h:137
Definition: ptr_iterator.h:26