18#include "pw_allocator/allocator.h"
19#include "pw_allocator/unique_ptr.h"
20#include "pw_containers/deque.h"
21#include "pw_containers/internal/generic_queue.h"
22#include "pw_containers/storage.h"
36template <
typename T,
typename SizeType = u
int16_t>
47 :
Queue(Aligned::Align(unaligned_buffer)) {}
51 template <
size_t kAlignment,
size_t kSizeBytes>
64 template <
typename,
typename>
67 template <
typename,
size_t,
typename>
73 explicit constexpr Queue(Aligned buffer) noexcept : deque_(buffer) {}
75 void MoveItemsFrom(Queue& other) { deque_.MoveItemsFrom(other.deque_); }
76 void MoveBufferFrom(Queue& other) { deque_.MoveBufferFrom(other.deque_); }
77 void SwapValuesWith(Queue& other) { deque_.SwapValuesWith(other.deque_); }
78 void SwapBufferWith(Queue& other) { deque_.SwapBufferWith(other.deque_); }
80 T* data() {
return deque_.data(); }
82 Deque<T, SizeType>& deque() {
return deque_; }
83 const Deque<T, SizeType>& deque()
const {
return deque_; }
85 Deque<T, SizeType> deque_;
97 typename SizeType =
typename Queue<T>::size_type>
100 public Queue<T, SizeType> {
105 : containers::internal::ArrayStorage<T, kInlineCapacity>{},
123 template <
size_t kOtherCapacity,
124 typename = std::enable_if_t<kOtherCapacity <= kInlineCapacity>>
129 template <
size_t kOtherCapacity,
130 typename = std::enable_if_t<kOtherCapacity <= kInlineCapacity>>
139 template <
size_t kOtherCapacity>
141 this->SwapValuesWith(other);
151template <
typename T,
typename S>
153 :
public Queue<T, S> {
161 FixedQueue queue = TryAllocate(allocator, capacity);
162 PW_ASSERT(queue.capacity() == capacity);
174 std::byte* array =
static_cast<std::byte*
>(allocator.
Allocate(layout));
175 if (array ==
nullptr) {
176 return FixedQueue(Aligned(array, 0u),
nullptr);
178 return FixedQueue(Aligned(array, layout.size()), &allocator);
186 const size_t size = storage.size();
199 :
Queue<T, S>(unaligned_buffer), deallocator_(
nullptr) {}
205 template <
size_t kAlignment,
size_t kSizeBytes>
220 deallocator_(cpp20::exchange(other.deallocator_,
nullptr)) {
221 this->MoveBufferFrom(other);
225 if (
this == &other) {
228 T*
const data = this->data();
229 this->MoveBufferFrom(other);
231 if (deallocator_ !=
nullptr) {
232 deallocator_->Deallocate(data);
234 deallocator_ = cpp20::exchange(other.deallocator_,
nullptr);
242 if (deallocator_ !=
nullptr) {
243 deallocator_->Deallocate(this->data());
250 this->SwapBufferWith(other);
251 std::swap(deallocator_, other.deallocator_);
258 template <
size_t kInlineCapacity>
268 using Aligned =
typename Queue<T, S>::Aligned;
273 Deallocator* deallocator_;
Definition: allocator.h:36
void * Allocate(Layout layout)
Definition: allocator.h:44
Abstract interface for releasing memory.
Definition: deallocator.h:29
Definition: unique_ptr.h:43
Definition: generic_queue.h:25
Definition: span_impl.h:235
constexpr FixedQueue(span< std::byte > unaligned_buffer) noexcept
Definition: queue.h:198
constexpr FixedQueue(containers::Storage< kAlignment, kSizeBytes > &buffer) noexcept
Definition: queue.h:206
constexpr FixedQueue()
Definition: queue.h:104
void swap(FixedQueue< T, kInlineCapacity, S > &other)
Definition: queue.h:259
FixedQueue(const FixedQueue &)=delete
Copying is not supported since it can fail.
static FixedQueue WithStorage(UniquePtr< std::byte[]> &&storage)
Definition: queue.h:185
static FixedQueue TryAllocate(Allocator &allocator, S capacity)
Definition: queue.h:172
void swap(FixedQueue &other) noexcept
Definition: queue.h:249
constexpr Deallocator * deallocator() const
Definition: queue.h:265
constexpr FixedQueue(FixedQueue &&other) noexcept
Definition: queue.h:218
constexpr Queue(containers::Storage< kAlignment, kSizeBytes > &storage) noexcept
Definition: queue.h:52
void swap(FixedQueue< T, kOtherCapacity, SizeType > &other)
Definition: queue.h:140
constexpr Queue(span< std::byte > unaligned_buffer) noexcept
Definition: queue.h:46
constexpr Deallocator * deallocator() const
Returns nullptr; a FixedQueue with static storage never allocates.
Definition: queue.h:145
static FixedQueue Allocate(Allocator &allocator, const S capacity)
Definition: queue.h:160
constexpr size_t kExternalStorage
Reserved capacity value for container specializations with external storage.
Definition: storage.h:76
The Pigweed namespace.
Definition: alignment.h:27