Pigweed
C/C++ API Reference
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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
28// Forward declaration.
29template <typename ValueType, typename SizeType, size_t kCapacity>
30class BasicInlineAsyncQueue;
31
36template <typename ValueType,
37 size_t kCapacity = containers::internal::kGenericSized>
39
40template <typename ValueType,
41 typename SizeType,
42 size_t kCapacity = containers::internal::kGenericSized>
44 : public BasicInlineAsyncQueue<ValueType,
45 SizeType,
46 containers::internal::kGenericSized> {
47 private:
48 using Base = BasicInlineAsyncQueue<ValueType,
49 SizeType,
50 containers::internal::kGenericSized>;
52
53 public:
54 using typename Base::const_iterator;
55 using typename Base::const_pointer;
56 using typename Base::const_reference;
57 using typename Base::difference_type;
58 using typename Base::iterator;
59 using typename Base::pointer;
60 using typename Base::reference;
61 using typename Base::size_type;
62 using typename Base::value_type;
63
64 // Constructors
65
66 constexpr BasicInlineAsyncQueue() noexcept = default;
67
68 BasicInlineAsyncQueue(size_type count, const_reference value)
69 : deque_(count, value) {}
70
71 explicit BasicInlineAsyncQueue(size_type count) : deque_(count) {}
72
73 template <
74 typename InputIterator,
75 typename = containers::internal::EnableIfInputIterator<InputIterator>>
76 BasicInlineAsyncQueue(InputIterator start, InputIterator finish)
77 : deque_(start, finish) {}
78
79 BasicInlineAsyncQueue(const std::initializer_list<value_type>& list)
80 : deque_(list) {}
81
83 BasicInlineAsyncQueue(const BasicInlineAsyncQueue& other) : deque_(other) {}
84
88 template <size_t kOtherCapacity>
91 *this = other;
92 }
93
96 : deque_(std::move(other.deque_)) {}
97
101 template <size_t kOtherCapacity>
104 *this = std::move(other);
105 }
106
107 BasicInlineAsyncQueue& operator=(
108 const std::initializer_list<value_type>& list) {
109 deque_ = std::move(list);
110 return *this;
111 }
112
115 deque_ = other.deque_;
116 return *this;
117 }
118
122 template <size_t kOtherCapacity>
125 deque_ = other.deque_;
126 return *this;
127 }
128
131 deque_ = std::move(other.deque_);
132 return *this;
133 }
134
138 template <size_t kOtherCapacity>
141 deque_ = std::move(other.deque_);
142 return *this;
143 }
144
145 template <typename OtherValueType,
146 typename = containers::internal::EnableIfIterable<OtherValueType>>
147 BasicInlineAsyncQueue& operator=(const OtherValueType& other) {
148 deque_ = Deque(other.begin(), other.end());
149 return *this;
150 }
151
152 // Size
153
154 static constexpr size_type max_size() { return capacity(); }
155 static constexpr size_type capacity() { return kCapacity; }
156
157 private:
158 friend class BasicInlineAsyncQueue<ValueType,
159 SizeType,
160 containers::internal::kGenericSized>;
161
162 Deque deque_;
163};
164
165// Defines the generic-sized BasicInlineAsyncDeque<T> specialization, which
166// serves as the base class for BasicInlineAsyncDeque<T> of any capacity.
167//
168// Except for constructors and destructors, all other methods should be
169// implemented on this generic-sized specialization.
170//
171// NOTE: this size-polymorphic base class must not be used inside of
172// ``std::unique_ptr`` or ``delete``.
173template <typename ValueType, typename SizeType>
174class BasicInlineAsyncQueue<ValueType,
175 SizeType,
176 containers::internal::kGenericSized>
178 BasicInlineAsyncQueue<ValueType, SizeType>,
179 BasicInlineAsyncDeque<ValueType, SizeType>> {
180 private:
182 using Base = containers::internal::
183 BasicInlineQueueImpl<BasicInlineAsyncQueue<ValueType, SizeType>, Deque>;
184
185 public:
186 using typename Base::const_iterator;
187 using typename Base::const_pointer;
188 using typename Base::const_reference;
189 using typename Base::difference_type;
190 using typename Base::iterator;
191 using typename Base::pointer;
192 using typename Base::reference;
193 using typename Base::size_type;
194 using typename Base::value_type;
195
197 async2::Poll<> PendHasSpace(async2::Context& context, size_type num = 1) {
198 return deque().PendHasSpace(context, num);
199 }
200
203 return deque().PendNotEmpty(context);
204 }
205
206 protected:
207 constexpr BasicInlineAsyncQueue() = default;
208
209 private:
210 template <typename, typename>
211 friend class containers::internal::GenericQueue;
212
213 template <size_t kCapacity>
215
216 Deque& deque() { return static_cast<Derived<0>*>(this)->deque_; }
217 const Deque& deque() const {
218 return static_cast<const Derived<0>*>(this)->deque_;
219 }
220};
221
222} // namespace pw
Definition: inline_async_deque.h:49
async2::Poll PendHasSpace(async2::Context &context, size_type num=1)
Returns Pending until space for num elements is available.
Definition: inline_async_queue.h:197
async2::Poll PendNotEmpty(async2::Context &context)
Returns Pending until an element is available.
Definition: inline_async_queue.h:202
Definition: inline_async_queue.h:46
BasicInlineAsyncQueue(const BasicInlineAsyncQueue &other)
Copy constructs for matching capacity.
Definition: inline_async_queue.h:83
BasicInlineAsyncQueue(BasicInlineAsyncQueue< ValueType, SizeType, kOtherCapacity > &&other)
Definition: inline_async_queue.h:102
BasicInlineAsyncQueue & operator=(BasicInlineAsyncQueue< ValueType, SizeType, kOtherCapacity > &&other)
Definition: inline_async_queue.h:139
BasicInlineAsyncQueue & operator=(BasicInlineAsyncQueue &&other)
Move assigns from matching capacity.
Definition: inline_async_queue.h:130
BasicInlineAsyncQueue(const BasicInlineAsyncQueue< ValueType, SizeType, kOtherCapacity > &other)
Definition: inline_async_queue.h:89
BasicInlineAsyncQueue & operator=(const BasicInlineAsyncQueue< ValueType, SizeType, kOtherCapacity > &other)
Definition: inline_async_queue.h:123
BasicInlineAsyncQueue(BasicInlineAsyncQueue &&other)
Move constructs for matching capacity.
Definition: inline_async_queue.h:95
BasicInlineAsyncQueue & operator=(const BasicInlineAsyncQueue &other)
Copy assigns from matching capacity.
Definition: inline_async_queue.h:114
Definition: context.h:53
Definition: poll.h:54
Provides basic helpers for reading and writing UTF-8 encoded strings.
Definition: alignment.h:27