C/C++ API Reference
Loading...
Searching...
No Matches
wrapped_iterator.h
1// Copyright 2021 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
19namespace pw::containers {
20
22
25
33template <typename Impl, typename Iterator, typename ValueType>
35 public:
36 using difference_type = std::ptrdiff_t;
37 using value_type = ValueType;
38 using pointer = ValueType*;
39 using reference = ValueType&;
40 using iterator_category = std::bidirectional_iterator_tag;
41
42 constexpr WrappedIterator(const WrappedIterator&) = default;
43 constexpr WrappedIterator& operator=(const WrappedIterator&) = default;
44
45 Impl& operator++() {
46 ++iterator_;
47 return static_cast<Impl&>(*this);
48 }
49
50 Impl operator++(int) {
51 Impl original = static_cast<const Impl&>(*this);
52 ++iterator_;
53 return original;
54 }
55
56 Impl& operator--() {
57 --iterator_;
58 return static_cast<Impl&>(*this);
59 }
60
61 Impl operator--(int) {
62 Impl original = static_cast<const Impl&>(*this);
63 --iterator_;
64 return original;
65 }
66
67 constexpr bool operator==(const WrappedIterator& other) const {
68 return iterator_ == other.iterator_;
69 }
70
71 constexpr bool operator!=(const WrappedIterator& other) const {
72 return !(*this == other);
73 }
74
75 protected:
76 constexpr WrappedIterator() = default;
77
78 constexpr WrappedIterator(const Iterator& it) : iterator_(it) {}
79
80 const auto& value() const { return *iterator_; }
81 const auto* ptr() const { return iterator_.operator->(); }
82
83 private:
84 Iterator iterator_;
85};
86
87} // namespace pw::containers
Definition: wrapped_iterator.h:34