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 <type_traits>
17
18#include "pw_multibuf/internal/chunk_iterator.h"
19
20namespace pw::multibuf {
21
22// Forward declaration for friending.
23namespace test {
24class IteratorTest;
25} // namespace test
26
27namespace internal {
28
30template <typename Deque,
31 ChunkContiguity kContiguity,
32 ChunkMutability kMutability>
34 public:
35 using size_type = typename Deque::size_type;
36 using value_type = typename Deque::value_type;
37 using difference_type = typename Deque::difference_type;
39 using const_iterator =
41
42 constexpr ChunksImpl() = default;
43
44 constexpr size_type size() const {
45 if constexpr (kContiguity == ChunkContiguity::kKeepAll) {
46 return deque().size() / begin_.entries_per_chunk_;
47 }
48 return static_cast<size_type>(std::distance(begin_, end_));
49 }
50
51 constexpr iterator begin() const { return begin_; }
52 constexpr iterator end() const { return end_; }
53
54 constexpr const_iterator cbegin() const { return begin_; }
55 constexpr const_iterator cend() const { return end_; }
56
57 private:
58 friend class GenericMultiBuf;
59
60 // For unit testing.
61 friend class ::pw::multibuf::test::IteratorTest;
62
63 using DequeRefType =
64 std::conditional_t<kMutability == ChunkMutability::kConst,
65 const Deque&,
66 Deque&>;
67
68 constexpr ChunksImpl(DequeRefType& deque, size_type entries_per_chunk) {
69 begin_.deque_ = &deque;
70 begin_.entries_per_chunk_ = entries_per_chunk;
71 end_.deque_ = &deque;
72 end_.entries_per_chunk_ = entries_per_chunk;
73 end_.chunk_ = deque.size() / entries_per_chunk;
74 }
75
76 constexpr const Deque& deque() const { return *(begin_.deque_); }
77
78 iterator begin_;
79 iterator end_;
80};
81
95template <typename Deque>
96using Chunks =
98
112template <typename Deque>
113using ConstChunks =
115
128template <typename Deque>
129using RawChunks =
131
144template <typename Deque>
145using RawConstChunks =
147
148} // namespace internal
149} // namespace pw::multibuf
Definition: deque.h:60
constexpr size_type size() const noexcept
Returns the number of elements in the deque.
Definition: generic_deque.h:69
Base class for ranges of chunks.
Definition: chunks.h:33
Definition: multibuf_v2.h:1072