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#include "pw_preprocessor/compiler.h"
26
28PW_MODIFY_DIAGNOSTIC(ignored, "-Wdeprecated-declarations");
29
30namespace pw {
31
33
34// Forward declaration.
35template <typename ValueType, typename SizeType, size_t kCapacity>
36class BasicInlineAsyncQueue;
37
48template <typename ValueType,
49 size_t kCapacity = containers::internal::kGenericSized>
51
68template <typename ValueType,
69 typename SizeType,
70 size_t kCapacity = containers::internal::kGenericSized>
71class [[deprecated("Use pw_async2 Channels instead (pw_async2/channel.h)")]]
73 : public BasicInlineAsyncQueue<ValueType,
74 SizeType,
75 containers::internal::kGenericSized> {
76 private:
77 using Base = BasicInlineAsyncQueue<ValueType,
78 SizeType,
79 containers::internal::kGenericSized>;
81
82 public:
83 using typename Base::const_iterator;
84 using typename Base::const_pointer;
85 using typename Base::const_reference;
86 using typename Base::difference_type;
87 using typename Base::iterator;
88 using typename Base::pointer;
89 using typename Base::reference;
90 using typename Base::size_type;
91 using typename Base::value_type;
92
93 // Constructors
94
95 constexpr BasicInlineAsyncQueue() noexcept = default;
96
97 BasicInlineAsyncQueue(size_type count, const_reference value)
98 : deque_(count, value) {}
99
100 explicit BasicInlineAsyncQueue(size_type count) : deque_(count) {}
101
102 template <
103 typename InputIterator,
104 typename = containers::internal::EnableIfInputIterator<InputIterator>>
105 BasicInlineAsyncQueue(InputIterator start, InputIterator finish)
106 : deque_(start, finish) {}
107
108 BasicInlineAsyncQueue(const std::initializer_list<value_type>& list)
109 : deque_(list) {}
110
112 BasicInlineAsyncQueue(const BasicInlineAsyncQueue& other) : deque_(other) {}
113
117 template <size_t kOtherCapacity>
120 *this = other;
121 }
122
125 : deque_(std::move(other.deque_)) {}
126
130 template <size_t kOtherCapacity>
133 *this = std::move(other);
134 }
135
136 BasicInlineAsyncQueue& operator=(
137 const std::initializer_list<value_type>& list) {
138 deque_ = std::move(list);
139 return *this;
140 }
141
144 deque_ = other.deque_;
145 return *this;
146 }
147
151 template <size_t kOtherCapacity>
154 deque_ = other.deque_;
155 return *this;
156 }
157
160 deque_ = std::move(other.deque_);
161 return *this;
162 }
163
167 template <size_t kOtherCapacity>
170 deque_ = std::move(other.deque_);
171 return *this;
172 }
173
174 template <typename OtherValueType,
175 typename = containers::internal::EnableIfIterable<OtherValueType>>
176 BasicInlineAsyncQueue& operator=(const OtherValueType& other) {
177 deque_ = Deque(other.begin(), other.end());
178 return *this;
179 }
180
181 // Size
182
183 static constexpr size_type max_size() { return capacity(); }
184 static constexpr size_type capacity() { return kCapacity; }
185
186 // Async
187 // Document these functions here since the template specialization base isn't
188 // included in Doxygen.
189
191 using Base::PendHasSpace;
192
194 using Base::PendNotEmpty;
195
196 private:
197 friend Base;
198
199 Deque deque_;
200};
201
203
204// Defines the generic-sized BasicInlineAsyncDeque<T> specialization, which
205// serves as the base class for BasicInlineAsyncDeque<T> of any capacity.
206//
207// Except for constructors and destructors, all other methods should be
208// implemented on this generic-sized specialization.
209//
210// NOTE: this size-polymorphic base class must not be used inside of
211// ``std::unique_ptr`` or ``delete``.
212template <typename ValueType, typename SizeType>
213class BasicInlineAsyncQueue<ValueType,
214 SizeType,
215 containers::internal::kGenericSized>
217 BasicInlineAsyncQueue<ValueType, SizeType>,
218 BasicInlineAsyncDeque<ValueType, SizeType>> {
219 private:
221 using Base = containers::internal::
222 BasicInlineQueueImpl<BasicInlineAsyncQueue<ValueType, SizeType>, Deque>;
223
224 public:
225 using typename Base::const_iterator;
226 using typename Base::const_pointer;
227 using typename Base::const_reference;
228 using typename Base::difference_type;
229 using typename Base::iterator;
230 using typename Base::pointer;
231 using typename Base::reference;
232 using typename Base::size_type;
233 using typename Base::value_type;
234
235 async2::Poll<> PendHasSpace(async2::Context& context, size_type num = 1) {
236 return deque().PendHasSpace(context, num);
237 }
238
240 return deque().PendNotEmpty(context);
241 }
242
243 protected:
244 constexpr BasicInlineAsyncQueue() = default;
245
246 private:
247 template <typename, typename>
249
250 template <size_t kCapacity>
252
253 Deque& deque() { return static_cast<Derived<0>*>(this)->deque_; }
254 const Deque& deque() const {
255 return static_cast<const Derived<0>*>(this)->deque_;
256 }
257};
258
259} // namespace pw
260
Definition: inline_async_deque.h:79
Definition: inline_async_queue.h:75
BasicInlineAsyncQueue(const BasicInlineAsyncQueue &other)
Copy constructs for matching capacity.
Definition: inline_async_queue.h:112
BasicInlineAsyncQueue(BasicInlineAsyncQueue< ValueType, SizeType, kOtherCapacity > &&other)
Definition: inline_async_queue.h:131
BasicInlineAsyncQueue & operator=(BasicInlineAsyncQueue< ValueType, SizeType, kOtherCapacity > &&other)
Definition: inline_async_queue.h:168
BasicInlineAsyncQueue & operator=(BasicInlineAsyncQueue &&other)
Move assigns from matching capacity.
Definition: inline_async_queue.h:159
BasicInlineAsyncQueue(const BasicInlineAsyncQueue< ValueType, SizeType, kOtherCapacity > &other)
Definition: inline_async_queue.h:118
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:235
BasicInlineAsyncQueue & operator=(const BasicInlineAsyncQueue< ValueType, SizeType, kOtherCapacity > &other)
Definition: inline_async_queue.h:152
BasicInlineAsyncQueue(BasicInlineAsyncQueue &&other)
Move constructs for matching capacity.
Definition: inline_async_queue.h:124
async2::Poll PendNotEmpty(async2::Context &context)
Returns pw::async2::Pending until an element is available.
Definition: inline_async_queue.h:239
BasicInlineAsyncQueue & operator=(const BasicInlineAsyncQueue &other)
Copy assigns from matching capacity.
Definition: inline_async_queue.h:143
Definition: deque.h:60
Definition: context.h:54
Definition: poll.h:60
Definition: generic_queue.h:25
#define PW_MODIFY_DIAGNOSTICS_POP()
Definition: compiler.h:194
#define PW_MODIFY_DIAGNOSTIC(kind, option)
Definition: compiler.h:203
#define PW_MODIFY_DIAGNOSTICS_PUSH()
Definition: compiler.h:189
The Pigweed namespace.
Definition: alignment.h:27