C/C++ API Reference
Loading...
Searching...
No Matches
var_len_entry_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#include <iterator>
18#include <type_traits>
19
20namespace pw::containers::internal {
21
22// Forward declarations.
23template <typename T>
24class VarLenEntry;
25
29template <typename T>
31 public:
32 using difference_type = std::ptrdiff_t;
33 using value_type = std::remove_cv_t<T>;
34 using pointer = T*;
35 using reference = T&;
36 using iterator_category = std::forward_iterator_tag;
37
38 constexpr VarLenEntryIterator() = default;
39
40 constexpr VarLenEntryIterator(const VarLenEntryIterator&) = default;
41 constexpr VarLenEntryIterator& operator=(const VarLenEntryIterator&) =
42 default;
43
44 constexpr VarLenEntryIterator& operator++() {
45 index_ += 1;
46 return *this;
47 }
48 constexpr VarLenEntryIterator operator++(int) {
49 VarLenEntryIterator previous_value(*this);
50 operator++();
51 return previous_value;
52 }
53
54 constexpr VarLenEntryIterator& operator+=(difference_type n) {
55 index_ += static_cast<size_t>(n);
56 return *this;
57 }
58
59 constexpr reference operator*() const { return entry_->at(index_); }
60
61 constexpr pointer operator->() const { return &entry_->at(index_); }
62
63 friend constexpr VarLenEntryIterator operator+(const VarLenEntryIterator& it,
64 difference_type n) {
65 return VarLenEntryIterator(*it.entry_, it.index_ + static_cast<size_t>(n));
66 }
67
68 friend constexpr VarLenEntryIterator operator+(
69 difference_type n, const VarLenEntryIterator& it) {
70 return VarLenEntryIterator(*it.entry_, it.index_ + static_cast<size_t>(n));
71 }
72
73 [[nodiscard]] friend constexpr bool operator==(
74 const VarLenEntryIterator& lhs, const VarLenEntryIterator& rhs) {
75 if (lhs.entry_ != nullptr && rhs.entry_ != nullptr) {
76 return *lhs.entry_ == *rhs.entry_ && lhs.index_ == rhs.index_;
77 }
78 return lhs.entry_ == rhs.entry_;
79 }
80 [[nodiscard]] friend constexpr bool operator!=(
81 const VarLenEntryIterator& lhs, const VarLenEntryIterator& rhs) {
82 return !(lhs == rhs);
83 }
84
85 private:
86 template <typename>
87 friend class VarLenEntry;
88
89 constexpr VarLenEntryIterator(const VarLenEntry<T>& entry, size_t index)
90 : entry_(&entry), index_(index) {}
91
92 const VarLenEntry<T>* entry_ = nullptr;
93 size_t index_ = 0;
94};
95
96} // namespace pw::containers::internal
Refers to an entry in-place in the queue. Entries may be discontiguous.
Definition: var_len_entry.h:65
Definition: var_len_entry_iterator.h:30