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
30// Forward declaration.
31template <typename ValueType, typename SizeType, size_t kCapacity>
32class BasicInlineAsyncQueue;
33
41template <typename ValueType,
42 size_t kCapacity = containers::internal::kGenericSized>
44
56template <typename ValueType,
57 typename SizeType,
58 size_t kCapacity = containers::internal::kGenericSized>
60 : public BasicInlineAsyncQueue<ValueType,
61 SizeType,
62 containers::internal::kGenericSized> {
63 private:
64 using Base = BasicInlineAsyncQueue<ValueType,
65 SizeType,
66 containers::internal::kGenericSized>;
68
69 public:
70 using typename Base::const_iterator;
71 using typename Base::const_pointer;
72 using typename Base::const_reference;
73 using typename Base::difference_type;
74 using typename Base::iterator;
75 using typename Base::pointer;
76 using typename Base::reference;
77 using typename Base::size_type;
78 using typename Base::value_type;
79
80 // Constructors
81
82 constexpr BasicInlineAsyncQueue() noexcept = default;
83
84 BasicInlineAsyncQueue(size_type count, const_reference value)
85 : deque_(count, value) {}
86
87 explicit BasicInlineAsyncQueue(size_type count) : deque_(count) {}
88
89 template <
90 typename InputIterator,
91 typename = containers::internal::EnableIfInputIterator<InputIterator>>
92 BasicInlineAsyncQueue(InputIterator start, InputIterator finish)
93 : deque_(start, finish) {}
94
95 BasicInlineAsyncQueue(const std::initializer_list<value_type>& list)
96 : deque_(list) {}
97
99 BasicInlineAsyncQueue(const BasicInlineAsyncQueue& other) : deque_(other) {}
100
104 template <size_t kOtherCapacity>
107 *this = other;
108 }
109
112 : deque_(std::move(other.deque_)) {}
113
117 template <size_t kOtherCapacity>
120 *this = std::move(other);
121 }
122
123 BasicInlineAsyncQueue& operator=(
124 const std::initializer_list<value_type>& list) {
125 deque_ = std::move(list);
126 return *this;
127 }
128
131 deque_ = other.deque_;
132 return *this;
133 }
134
138 template <size_t kOtherCapacity>
141 deque_ = other.deque_;
142 return *this;
143 }
144
147 deque_ = std::move(other.deque_);
148 return *this;
149 }
150
154 template <size_t kOtherCapacity>
157 deque_ = std::move(other.deque_);
158 return *this;
159 }
160
161 template <typename OtherValueType,
162 typename = containers::internal::EnableIfIterable<OtherValueType>>
163 BasicInlineAsyncQueue& operator=(const OtherValueType& other) {
164 deque_ = Deque(other.begin(), other.end());
165 return *this;
166 }
167
168 // Size
169
170 static constexpr size_type max_size() { return capacity(); }
171 static constexpr size_type capacity() { return kCapacity; }
172
173 // Async
174 // Document these functions here since the template specialization base isn't
175 // included in Doxygen.
176
178 using Base::PendHasSpace;
179
181 using Base::PendNotEmpty;
182
183 private:
184 friend class BasicInlineAsyncQueue<ValueType,
185 SizeType,
186 containers::internal::kGenericSized>;
187
188 Deque deque_;
189};
190
192
193// Defines the generic-sized BasicInlineAsyncDeque<T> specialization, which
194// serves as the base class for BasicInlineAsyncDeque<T> of any capacity.
195//
196// Except for constructors and destructors, all other methods should be
197// implemented on this generic-sized specialization.
198//
199// NOTE: this size-polymorphic base class must not be used inside of
200// ``std::unique_ptr`` or ``delete``.
201template <typename ValueType, typename SizeType>
202class BasicInlineAsyncQueue<ValueType,
203 SizeType,
204 containers::internal::kGenericSized>
206 BasicInlineAsyncQueue<ValueType, SizeType>,
207 BasicInlineAsyncDeque<ValueType, SizeType>> {
208 private:
210 using Base = containers::internal::
211 BasicInlineQueueImpl<BasicInlineAsyncQueue<ValueType, SizeType>, Deque>;
212
213 public:
214 using typename Base::const_iterator;
215 using typename Base::const_pointer;
216 using typename Base::const_reference;
217 using typename Base::difference_type;
218 using typename Base::iterator;
219 using typename Base::pointer;
220 using typename Base::reference;
221 using typename Base::size_type;
222 using typename Base::value_type;
223
224 async2::Poll<> PendHasSpace(async2::Context& context, size_type num = 1) {
225 return deque().PendHasSpace(context, num);
226 }
227
229 return deque().PendNotEmpty(context);
230 }
231
232 protected:
233 constexpr BasicInlineAsyncQueue() = default;
234
235 private:
236 template <typename, typename>
237 friend class containers::internal::GenericQueue;
238
239 template <size_t kCapacity>
241
242 Deque& deque() { return static_cast<Derived<0>*>(this)->deque_; }
243 const Deque& deque() const {
244 return static_cast<const Derived<0>*>(this)->deque_;
245 }
246};
247
248} // namespace pw
Definition: inline_async_deque.h:65
Definition: inline_async_queue.h:62
BasicInlineAsyncQueue(const BasicInlineAsyncQueue &other)
Copy constructs for matching capacity.
Definition: inline_async_queue.h:99
BasicInlineAsyncQueue(BasicInlineAsyncQueue< ValueType, SizeType, kOtherCapacity > &&other)
Definition: inline_async_queue.h:118
BasicInlineAsyncQueue & operator=(BasicInlineAsyncQueue< ValueType, SizeType, kOtherCapacity > &&other)
Definition: inline_async_queue.h:155
BasicInlineAsyncQueue & operator=(BasicInlineAsyncQueue &&other)
Move assigns from matching capacity.
Definition: inline_async_queue.h:146
BasicInlineAsyncQueue(const BasicInlineAsyncQueue< ValueType, SizeType, kOtherCapacity > &other)
Definition: inline_async_queue.h:105
async2::Poll PendHasSpace(async2::Context &context, size_type num=1)
Returns pw::async2::Pending until space for num elements is available.
Definition: inline_async_queue.h:224
BasicInlineAsyncQueue & operator=(const BasicInlineAsyncQueue< ValueType, SizeType, kOtherCapacity > &other)
Definition: inline_async_queue.h:139
BasicInlineAsyncQueue(BasicInlineAsyncQueue &&other)
Move constructs for matching capacity.
Definition: inline_async_queue.h:111
async2::Poll PendNotEmpty(async2::Context &context)
Returns pw::async2::Pending until an element is available.
Definition: inline_async_queue.h:228
BasicInlineAsyncQueue & operator=(const BasicInlineAsyncQueue &other)
Copy assigns from matching capacity.
Definition: inline_async_queue.h:130
Definition: context.h:55
Definition: poll.h:60
The Pigweed namespace.
Definition: alignment.h:27