C/C++ API Reference
Loading...
Searching...
No Matches
inline_async_queue.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 <cstdint>
18
19#include "pw_async2/context.h"
20#include "pw_async2/poll.h"
21#include "pw_async2/try.h"
22#include "pw_containers/inline_async_deque.h"
23#include "pw_containers/inline_queue.h"
24#include "pw_containers/internal/async_count_and_capacity.h"
25
26namespace pw {
27
29
32
33// Forward declaration.
34template <typename ValueType, typename SizeType, size_t kCapacity>
35class BasicInlineAsyncQueue;
36
41template <typename ValueType,
42 size_t kCapacity = containers::internal::kGenericSized>
44
45template <typename ValueType,
46 typename SizeType,
47 size_t kCapacity = containers::internal::kGenericSized>
49 : public BasicInlineAsyncQueue<ValueType,
50 SizeType,
51 containers::internal::kGenericSized> {
52 private:
53 using Base = BasicInlineAsyncQueue<ValueType,
54 SizeType,
55 containers::internal::kGenericSized>;
57
58 public:
59 using typename Base::const_iterator;
60 using typename Base::const_pointer;
61 using typename Base::const_reference;
62 using typename Base::difference_type;
63 using typename Base::iterator;
64 using typename Base::pointer;
65 using typename Base::reference;
66 using typename Base::size_type;
67 using typename Base::value_type;
68
69 // Constructors
70
71 constexpr BasicInlineAsyncQueue() noexcept = default;
72
73 BasicInlineAsyncQueue(size_type count, const_reference value)
74 : deque_(count, value) {}
75
76 explicit BasicInlineAsyncQueue(size_type count) : deque_(count) {}
77
78 template <
79 typename InputIterator,
80 typename = containers::internal::EnableIfInputIterator<InputIterator>>
81 BasicInlineAsyncQueue(InputIterator start, InputIterator finish)
82 : deque_(start, finish) {}
83
84 BasicInlineAsyncQueue(const std::initializer_list<value_type>& list)
85 : deque_(list) {}
86
88 BasicInlineAsyncQueue(const BasicInlineAsyncQueue& other) : deque_(other) {}
89
93 template <size_t kOtherCapacity>
96 *this = other;
97 }
98
101 : deque_(std::move(other.deque_)) {}
102
106 template <size_t kOtherCapacity>
109 *this = std::move(other);
110 }
111
112 BasicInlineAsyncQueue& operator=(
113 const std::initializer_list<value_type>& list) {
114 deque_ = std::move(list);
115 return *this;
116 }
117
120 deque_ = other.deque_;
121 return *this;
122 }
123
127 template <size_t kOtherCapacity>
130 deque_ = other.deque_;
131 return *this;
132 }
133
136 deque_ = std::move(other.deque_);
137 return *this;
138 }
139
143 template <size_t kOtherCapacity>
146 deque_ = std::move(other.deque_);
147 return *this;
148 }
149
150 template <typename OtherValueType,
151 typename = containers::internal::EnableIfIterable<OtherValueType>>
152 BasicInlineAsyncQueue& operator=(const OtherValueType& other) {
153 deque_ = Deque(other.begin(), other.end());
154 return *this;
155 }
156
157 // Size
158
159 static constexpr size_type max_size() { return capacity(); }
160 static constexpr size_type capacity() { return kCapacity; }
161
162 private:
163 friend class BasicInlineAsyncQueue<ValueType,
164 SizeType,
165 containers::internal::kGenericSized>;
166
167 Deque deque_;
168};
169
170// Defines the generic-sized BasicInlineAsyncDeque<T> specialization, which
171// serves as the base class for BasicInlineAsyncDeque<T> of any capacity.
172//
173// Except for constructors and destructors, all other methods should be
174// implemented on this generic-sized specialization.
175//
176// NOTE: this size-polymorphic base class must not be used inside of
177// ``std::unique_ptr`` or ``delete``.
178template <typename ValueType, typename SizeType>
179class BasicInlineAsyncQueue<ValueType,
180 SizeType,
181 containers::internal::kGenericSized>
183 BasicInlineAsyncQueue<ValueType, SizeType>,
184 BasicInlineAsyncDeque<ValueType, SizeType>> {
185 private:
187 using Base = containers::internal::
188 BasicInlineQueueImpl<BasicInlineAsyncQueue<ValueType, SizeType>, Deque>;
189
190 public:
191 using typename Base::const_iterator;
192 using typename Base::const_pointer;
193 using typename Base::const_reference;
194 using typename Base::difference_type;
195 using typename Base::iterator;
196 using typename Base::pointer;
197 using typename Base::reference;
198 using typename Base::size_type;
199 using typename Base::value_type;
200
202 async2::Poll<> PendHasSpace(async2::Context& context, size_type num = 1) {
203 return deque().PendHasSpace(context, num);
204 }
205
208 return deque().PendNotEmpty(context);
209 }
210
211 protected:
212 constexpr BasicInlineAsyncQueue() = default;
213
214 private:
215 template <typename, typename>
216 friend class containers::internal::GenericQueue;
217
218 template <size_t kCapacity>
220
221 Deque& deque() { return static_cast<Derived<0>*>(this)->deque_; }
222 const Deque& deque() const {
223 return static_cast<const Derived<0>*>(this)->deque_;
224 }
225};
226
227} // namespace pw
Definition: inline_async_deque.h:51
Definition: inline_async_queue.h:51
Definition: context.h:55
Definition: poll.h:60
BasicInlineAsyncQueue(const BasicInlineAsyncQueue &other)
Copy constructs for matching capacity.
Definition: inline_async_queue.h:88
BasicInlineAsyncQueue(BasicInlineAsyncQueue< ValueType, SizeType, kOtherCapacity > &&other)
Definition: inline_async_queue.h:107
BasicInlineAsyncQueue & operator=(BasicInlineAsyncQueue< ValueType, SizeType, kOtherCapacity > &&other)
Definition: inline_async_queue.h:144
BasicInlineAsyncQueue & operator=(BasicInlineAsyncQueue &&other)
Move assigns from matching capacity.
Definition: inline_async_queue.h:135
BasicInlineAsyncQueue(const BasicInlineAsyncQueue< ValueType, SizeType, kOtherCapacity > &other)
Definition: inline_async_queue.h:94
async2::Poll PendHasSpace(async2::Context &context, size_type num=1)
Returns Pending until space for num elements is available.
Definition: inline_async_queue.h:202
BasicInlineAsyncQueue & operator=(const BasicInlineAsyncQueue< ValueType, SizeType, kOtherCapacity > &other)
Definition: inline_async_queue.h:128
BasicInlineAsyncQueue(BasicInlineAsyncQueue &&other)
Move constructs for matching capacity.
Definition: inline_async_queue.h:100
async2::Poll PendNotEmpty(async2::Context &context)
Returns Pending until an element is available.
Definition: inline_async_queue.h:207
BasicInlineAsyncQueue & operator=(const BasicInlineAsyncQueue &other)
Copy assigns from matching capacity.
Definition: inline_async_queue.h:119
The Pigweed namespace.
Definition: alignment.h:27