Pigweed
C/C++ API Reference
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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
31template <typename T, typename SizeType = uint16_t>
33 : public containers::internal::GenericQueue<DynamicQueue<T, SizeType>,
34 DynamicDeque<T, SizeType>> {
35 private:
37
38 public:
39 using const_iterator = typename Deque::const_iterator;
40 using const_pointer = typename Deque::const_pointer;
41 using const_reference = typename Deque::const_reference;
42 using difference_type = typename Deque::difference_type;
43 using iterator = typename Deque::iterator;
44 using pointer = typename Deque::pointer;
45 using reference = typename Deque::reference;
46 using size_type = typename Deque::size_type;
47 using value_type = typename Deque::value_type;
48
50 explicit constexpr DynamicQueue(pw::Allocator& allocator)
51 : deque_(allocator) {}
52
53 // Disable copy since it can fail.
54 DynamicQueue(const DynamicQueue&) = delete;
55 DynamicQueue& operator=(const DynamicQueue&) = delete;
56
58 constexpr DynamicQueue(DynamicQueue&&) = default;
59 DynamicQueue& operator=(DynamicQueue&&) = default;
60
62 void clear() { deque_.clear(); }
63
65 [[nodiscard]] bool try_push(const value_type& value) {
66 return deque_.try_push_back(value);
67 }
68
70 [[nodiscard]] bool try_push(value_type&& value) {
71 return deque_.try_push_back(std::move(value));
72 }
73
75 template <typename... Args>
76 [[nodiscard]] bool try_emplace(Args&&... args) {
77 return deque_.try_emplace_back(std::forward<Args>(args)...);
78 }
79
81 void reserve(size_type capacity) { deque_.reserve(capacity); }
82
85 [[nodiscard]] bool try_reserve(size_type capacity) {
86 return deque_.try_reserve(capacity);
87 }
88
90 void reserve_exact(size_type capacity) { deque_.reserve_exact(capacity); }
91
93 [[nodiscard]] bool try_reserve_exact(size_type capacity) {
94 return deque_.try_reserve_exact(capacity);
95 }
96
98 void shrink_to_fit() { deque_.shrink_to_fit(); }
99
101 void swap(DynamicQueue& other) { deque_.swap(other.deque_); }
102
103 private:
104 template <typename, typename>
105 friend class containers::internal::GenericQueue;
106
107 Deque& deque() { return deque_; }
108 const Deque& deque() const { return deque_; }
109
110 Deque deque_;
111};
112
113} // namespace pw
Definition: allocator.h:34
Definition: dynamic_deque.h:55
void reserve(size_type new_capacity)
Increases capacity() to at least new_capacity. Crashes on failure.
Definition: dynamic_deque.h:146
void reserve_exact(size_type new_capacity)
Increases capacity() to exactly new_capacity. Crashes on failure.
Definition: dynamic_deque.h:162
void swap(DynamicDeque &other) noexcept
Swaps the contents of two deques. No allocations occur.
Definition: dynamic_deque.h:177
bool try_reserve(size_type new_capacity)
Definition: dynamic_deque.h:235
void shrink_to_fit()
Attempts to reduce capacity() to size(). Not guaranteed to succeed.
Definition: dynamic_deque.h:256
bool try_reserve_exact(size_type new_capacity)
Definition: dynamic_deque.h:157
Definition: dynamic_queue.h:34
void clear()
Removes all elements from the queue.
Definition: dynamic_queue.h:62
bool try_reserve_exact(size_type capacity)
Attempts to set the queue capacity to max(capacity, size()) elements.
Definition: dynamic_queue.h:93
void reserve(size_type capacity)
Sets the queue capacity to at least max(capacity, size()) elements.
Definition: dynamic_queue.h:81
constexpr DynamicQueue(pw::Allocator &allocator)
Constructs a DynamicQueue using the provided allocator.
Definition: dynamic_queue.h:50
constexpr DynamicQueue(DynamicQueue &&)=default
Move operations are supported and incur no allocations.
void swap(DynamicQueue &other)
Swaps the contents with another queue.
Definition: dynamic_queue.h:101
bool try_push(const value_type &value)
Attempts to add an element to the back of the queue.
Definition: dynamic_queue.h:65
bool try_push(value_type &&value)
Attempts to add an element to the back of the queue (move version).
Definition: dynamic_queue.h:70
bool try_reserve(size_type capacity)
Definition: dynamic_queue.h:85
void shrink_to_fit()
Reduces memory usage by releasing unused capacity, if possible.
Definition: dynamic_queue.h:98
bool try_emplace(Args &&... args)
Attempts to construct an element in place at the back of the queue.
Definition: dynamic_queue.h:76
void reserve_exact(size_type capacity)
Sets the queue capacity to max(capacity, size()) elements.
Definition: dynamic_queue.h:90
Provides basic helpers for reading and writing UTF-8 encoded strings.
Definition: alignment.h:27