C/C++ API Reference
Loading...
Searching...
No Matches
dynamic_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 <utility>
17
18#include "pw_allocator/allocator.h"
19#include "pw_containers/dynamic_deque.h"
20#include "pw_containers/internal/generic_queue.h"
21
22namespace pw {
23
25
28
46template <typename T, typename SizeType = uint16_t>
48 : public containers::internal::GenericQueue<DynamicQueue<T, SizeType>,
49 DynamicDeque<T, SizeType>> {
50 private:
53
54 public:
55 using const_iterator = typename Deque::const_iterator;
56 using const_pointer = typename Deque::const_pointer;
57 using const_reference = typename Deque::const_reference;
58 using difference_type = typename Deque::difference_type;
59 using iterator = typename Deque::iterator;
60 using pointer = typename Deque::pointer;
61 using reference = typename Deque::reference;
62 using size_type = typename Deque::size_type;
63 using value_type = typename Deque::value_type;
64
66 explicit constexpr DynamicQueue(pw::Allocator& allocator)
67 : deque_(allocator) {}
68
69 // Disable copy since it can fail.
70 DynamicQueue(const DynamicQueue&) = delete;
71 DynamicQueue& operator=(const DynamicQueue&) = delete;
72
74 constexpr DynamicQueue(DynamicQueue&&) = default;
75 DynamicQueue& operator=(DynamicQueue&&) = default;
76
78 [[nodiscard]] bool try_push(const value_type& value) {
79 return deque_.try_push_back(value);
80 }
81
83 [[nodiscard]] bool try_push(value_type&& value) {
84 return deque_.try_push_back(std::move(value));
85 }
86
88 template <typename... Args>
89 [[nodiscard]] bool try_emplace(Args&&... args) {
90 return deque_.try_emplace_back(std::forward<Args>(args)...);
91 }
92
94 void reserve(size_type capacity) { deque_.reserve(capacity); }
95
98 [[nodiscard]] bool try_reserve(size_type capacity) {
99 return deque_.try_reserve(capacity);
100 }
101
103 void reserve_exact(size_type capacity) { deque_.reserve_exact(capacity); }
104
106 [[nodiscard]] bool try_reserve_exact(size_type capacity) {
107 return deque_.try_reserve_exact(capacity);
108 }
109
111 void shrink_to_fit() { deque_.shrink_to_fit(); }
112
114 void reset() { deque_.reset(); }
115
117 void swap(DynamicQueue& other) { deque_.swap(other.deque_); }
118
119 private:
120 friend Base;
121
122 // Hide full() since the capacity can grow.
123 using Base::full;
124
125 Deque& deque() { return deque_; }
126 const Deque& deque() const { return deque_; }
127
128 Deque deque_;
129};
130
131} // namespace pw
Definition: allocator.h:42
Definition: deque.h:57
Definition: dynamic_deque.h:68
Definition: dynamic_queue.h:49
Definition: generic_queue.h:25
bool try_reserve_exact(size_type capacity)
Attempts to set the queue capacity to max(capacity, size()) elements.
Definition: dynamic_queue.h:106
void reserve(size_type new_capacity)
Increases capacity() to at least new_capacity. Crashes on failure.
Definition: dynamic_deque.h:157
void reserve(size_type capacity)
Sets the queue capacity to at least max(capacity, size()) elements.
Definition: dynamic_queue.h:94
constexpr DynamicQueue(pw::Allocator &allocator)
Constructs a DynamicQueue using the provided allocator.
Definition: dynamic_queue.h:66
constexpr DynamicQueue(DynamicQueue &&)=default
Move operations are supported and incur no allocations.
void reserve_exact(size_type new_capacity)
Increases capacity() to exactly new_capacity. Crashes on failure.
Definition: dynamic_deque.h:173
void swap(DynamicDeque &other) noexcept
Swaps the contents of two deques. No allocations occur.
Definition: dynamic_deque.h:203
bool try_reserve(size_type new_capacity)
Definition: dynamic_deque.h:269
void swap(DynamicQueue &other)
Swaps the contents with another queue.
Definition: dynamic_queue.h:117
void shrink_to_fit()
Definition: dynamic_deque.h:290
void reset()
Clears the deque and deallocates its buffer.
Definition: dynamic_deque.h:190
bool try_reserve_exact(size_type new_capacity)
Definition: dynamic_deque.h:168
bool try_push(const value_type &value)
Attempts to add an element to the back of the queue.
Definition: dynamic_queue.h:78
bool try_push(value_type &&value)
Attempts to add an element to the back of the queue (move version).
Definition: dynamic_queue.h:83
bool try_reserve(size_type capacity)
Definition: dynamic_queue.h:98
void shrink_to_fit()
Reduces memory usage by releasing unused capacity, if possible.
Definition: dynamic_queue.h:111
bool try_emplace(Args &&... args)
Attempts to construct an element in place at the back of the queue.
Definition: dynamic_queue.h:89
void reserve_exact(size_type capacity)
Sets the queue capacity to max(capacity, size()) elements.
Definition: dynamic_queue.h:103
void reset()
Clears the deque and deallocates its buffer.
Definition: dynamic_queue.h:114
The Pigweed namespace.
Definition: alignment.h:27