C/C++ API Reference
Loading...
Searching...
No Matches
chunks.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 "pw_multibuf/internal/chunk_iterator.h"
17
18namespace pw::multibuf {
19
20// Forward declaration for friending.
21namespace test {
22class IteratorTest;
23} // namespace test
24
25namespace internal {
26class GenericMultiBuf;
27
29template <typename Derived, typename Deque>
31 public:
32 using size_type = typename Deque::size_type;
33 using value_type = typename Deque::value_type;
34 using difference_type = typename Deque::difference_type;
35 using iterator = ChunkIterator<size_type, /*kIsConst=*/false>;
36 using const_iterator = ChunkIterator<size_type, /*kIsConst=*/true>;
37
38 constexpr ChunksImpl() = default;
39
40 constexpr size_type size() const { return deque().size() / depth(); }
41 constexpr size_type capacity() const { return deque().capacity() / depth(); }
42
43 constexpr const_iterator cbegin() const { return derived().begin(); }
44 constexpr const_iterator cend() const { return derived().end(); }
45
46 protected:
47 constexpr void Init(Deque& deque, size_type depth) {
48 derived().begin_.deque_ = &deque;
49 derived().begin_.depth_ = depth;
50 derived().end_.deque_ = &deque;
51 derived().end_.depth_ = depth;
52 derived().end_.index_ = deque.size();
53 }
54
55 constexpr Derived& derived() { return static_cast<Derived&>(*this); }
56 constexpr const Derived& derived() const {
57 return static_cast<const Derived&>(*this);
58 }
59
60 constexpr const Deque& deque() const { return *(derived().begin_.deque_); }
61 constexpr size_type depth() const { return derived().begin_.depth_; }
62};
63
64} // namespace internal
65
78template <typename Deque>
79class Chunks : public internal::ChunksImpl<Chunks<Deque>, Deque> {
80 private:
82
83 public:
84 using typename Base::const_iterator;
85 using typename Base::difference_type;
86 using typename Base::iterator;
87 using typename Base::size_type;
88 using typename Base::value_type;
89
90 constexpr Chunks() = default;
91
92 constexpr iterator begin() const { return begin_; }
93 constexpr iterator end() const { return end_; }
94
95 private:
96 template <typename, typename>
97 friend class ::pw::multibuf::internal::ChunksImpl;
98 friend class ::pw::multibuf::internal::GenericMultiBuf;
99
100 // For unit testing.
101 friend class ::pw::multibuf::test::IteratorTest;
102
103 constexpr Chunks(Deque& deque, size_type depth) { Base::Init(deque, depth); }
104
105 iterator begin_;
106 iterator end_;
107};
108
121template <typename Deque>
123 : public internal::ChunksImpl<ConstChunks<Deque>, const Deque> {
124 private:
125 using Base = internal::ChunksImpl<ConstChunks<Deque>, const Deque>;
126
127 public:
128 using typename Base::const_iterator;
129 using typename Base::difference_type;
130 using typename Base::size_type;
131 using typename Base::value_type;
132
133 constexpr ConstChunks() = default;
134
135 constexpr const_iterator begin() const { return begin_; }
136 constexpr const_iterator end() const { return end_; }
137
138 private:
139 template <typename, typename>
140 friend class ::pw::multibuf::internal::ChunksImpl;
141 friend class ::pw::multibuf::internal::GenericMultiBuf;
142
143 constexpr ConstChunks(const Deque& deque, size_type depth) {
144 Base::Init(deque, depth);
145 }
146
147 const_iterator begin_;
148 const_iterator end_;
149};
150
151} // namespace pw::multibuf
Definition: chunks.h:79
Definition: chunks.h:123
Definition: chunk_iterator.h:43
Base class for ranges of chunks.
Definition: chunks.h:30