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>
99 public Queue<T, SizeType> {
104 : containers::StorageBaseFor<T, kInlineCapacity>{},
122 template <
size_t kOtherCapacity,
123 typename = std::enable_if_t<kOtherCapacity <= kInlineCapacity>>
128 template <
size_t kOtherCapacity,
129 typename = std::enable_if_t<kOtherCapacity <= kInlineCapacity>>
138 template <
size_t kOtherCapacity>
140 this->SwapValuesWith(other);
150template <
typename T,
typename S>
152 :
public Queue<T, S> {
160 FixedQueue queue = TryAllocate(allocator, capacity);
161 PW_ASSERT(queue.capacity() == capacity);
173 std::byte* array =
static_cast<std::byte*
>(allocator.
Allocate(layout));
174 if (array ==
nullptr) {
175 return FixedQueue(Aligned(array, 0u),
nullptr);
177 return FixedQueue(Aligned(array, layout.size()), &allocator);
185 const size_t size = storage.size();
198 :
Queue<T, S>(unaligned_buffer), deallocator_(
nullptr) {}
204 template <
size_t kAlignment,
size_t kSizeBytes>
219 deallocator_(cpp20::exchange(other.deallocator_,
nullptr)) {
220 this->MoveBufferFrom(other);
224 if (
this == &other) {
227 T*
const data = this->data();
228 this->MoveBufferFrom(other);
230 if (deallocator_ !=
nullptr) {
231 deallocator_->Deallocate(data);
233 deallocator_ = cpp20::exchange(other.deallocator_,
nullptr);
241 if (deallocator_ !=
nullptr) {
242 deallocator_->Deallocate(this->data());
249 this->SwapBufferWith(other);
250 std::swap(deallocator_, other.deallocator_);
257 template <
size_t kInlineCapacity>
267 using Aligned =
typename Queue<T, S>::Aligned;
272 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:197
constexpr FixedQueue(containers::Storage< kAlignment, kSizeBytes > &buffer) noexcept
Definition: queue.h:205
constexpr FixedQueue()
Definition: queue.h:103
void swap(FixedQueue< T, kInlineCapacity, S > &other)
Definition: queue.h:258
FixedQueue(const FixedQueue &)=delete
Copying is not supported since it can fail.
static FixedQueue WithStorage(UniquePtr< std::byte[]> &&storage)
Definition: queue.h:184
static FixedQueue TryAllocate(Allocator &allocator, S capacity)
Definition: queue.h:171
void swap(FixedQueue &other) noexcept
Definition: queue.h:248
constexpr Deallocator * deallocator() const
Definition: queue.h:264
constexpr FixedQueue(FixedQueue &&other) noexcept
Definition: queue.h:217
constexpr Queue(containers::Storage< kAlignment, kSizeBytes > &storage) noexcept
Definition: queue.h:52
void swap(FixedQueue< T, kOtherCapacity, SizeType > &other)
Definition: queue.h:139
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:144
static FixedQueue Allocate(Allocator &allocator, const S capacity)
Definition: queue.h:159
constexpr size_t kExternalStorage
Reserved capacity value for container specializations with external storage.
Definition: storage.h:79
The Pigweed namespace.
Definition: alignment.h:27