StormByte 3.0.0
C++26 foundation of the StormByte suite
Loading...
Searching...
No Matches
queue.hxx
Go to the documentation of this file.
1/*
2 * Copyright (C) 2024-2026 David C. Manuelda (StormBytePP)
3 *
4 * This is the base for StormByte Suite libraries.
5 *
6 * StormByte original source is dual-licensed:
7 *
8 * 1. GNU Lesser General Public License v3.0 (or later)
9 * You may redistribute and/or modify this file under the terms of the
10 * GNU Lesser General Public License as published by the Free Software
11 * Foundation, either version 3 of the License, or (at your option)
12 * any later version.
13 *
14 * 2. Commercial license
15 * Alternatively, this file may be used under the terms of a commercial
16 * license agreement with the copyright holder
17 * (David C. Manuelda <StormByte@gmail.com>).
18 *
19 * Both licenses apply only to original StormByte source in this repository.
20 * They do not cover other StormByte modules or any third-party material
21 * shipped with this repository (including everything under thirdparty/),
22 * which remains under its own license.
23 *
24 * Neither license grants any patent rights. Any patent licenses required
25 * to use this software or third-party components must be obtained separately
26 * from the patent holders.
27 *
28 * StormByte is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU Lesser General Public License for more details.
32 *
33 * You should have received a copy of the GNU Lesser General Public License
34 * version 3 along with StormByte. If not, see
35 * <https://www.gnu.org/licenses/lgpl-3.0.html>.
36 *
37 * SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-StormByte-Commercial
38 */
39
40#pragma once
41
42#include <StormByte/size.hxx>
46
47#include <compare>
48#include <cstddef>
49#include <queue>
50#include <ranges>
51#include <utility>
52
57namespace StormByte {
62 namespace Safe {
68
76 template<Type::SafeValue T>
78 public:
79 using value_type = T;
81 using size_type = Size;
82 using difference_type = std::ptrdiff_t;
83 using reference = T&;
84 using const_reference = const T&;
85 using iterator = typename Vector<T>::iterator;
87
91 Queue() noexcept;
92
98 STORMBYTE_FORCE_INLINE explicit Queue(const std::queue<T>& values): Queue() {
99 std::queue<T> copy(values);
100 while (!copy.empty()) {
101 push(std::move(copy.front()));
102 copy.pop();
103 }
104 }
105
111 STORMBYTE_FORCE_INLINE explicit Queue(std::queue<T>&& values): Queue() {
112 while (!values.empty()) {
113 push(std::move(values.front()));
114 values.pop();
115 }
116 }
117
123 explicit Queue(const container_type& values);
124
129 explicit Queue(container_type&& values) noexcept;
130
137 template<std::ranges::input_range R>
138 requires std::convertible_to<std::ranges::range_reference_t<R>, T>
139 STORMBYTE_FORCE_INLINE explicit Queue(std::from_range_t, R&& range): Queue() {
140 push_range(std::forward<R>(range));
141 }
142
148 Queue(const Queue& other);
149
154 Queue(Queue&& other) noexcept;
155
159 ~Queue() noexcept;
160
167 Queue& operator=(const Queue& other);
168
174 Queue& operator=(Queue&& other) noexcept;
175
182 STORMBYTE_FORCE_INLINE Queue& operator=(const std::queue<T>& values) {
183 Queue replacement(values);
184 swap(replacement);
185 return *this;
186 }
187
194 STORMBYTE_FORCE_INLINE Queue& operator=(std::queue<T>&& values) {
195 Queue replacement(std::move(values));
196 swap(replacement);
197 return *this;
198 }
199
204 iterator begin() noexcept;
205
210 iterator end() noexcept;
211
216 const_iterator begin() const noexcept;
217
222 const_iterator end() const noexcept;
223
228 const_iterator cbegin() const noexcept;
229
234 const_iterator cend() const noexcept;
235
240 bool empty() const noexcept;
241
246 size_type size() const noexcept;
247
254
261
268
275
281 void push(const T& value);
282
288 void push(T&& value);
289
296 template<std::ranges::input_range R>
297 requires std::convertible_to<std::ranges::range_reference_t<R>, T>
299 for (auto&& value : range)
300 push(T(std::forward<decltype(value)>(value)));
301 }
302
310 template<class... Args>
311 reference emplace(Args&&... args);
312
317 void pop();
318
325
333
338 void swap(Queue& other) noexcept;
339
345 bool operator==(const Queue& other) const requires Type::EqualityComparable<T>;
346
352 STORMBYTE_FORCE_INLINE std::strong_ordering operator<=>(const Queue& other) const requires std::three_way_comparable<T>;
353
359 bool operator<(const Queue& other) const requires requires(const T& left, const T& right) { left < right; };
360
366 bool operator<=(const Queue& other) const requires requires(const T& left, const T& right) { left < right; };
367
373 bool operator>(const Queue& other) const requires requires(const T& left, const T& right) { left < right; };
374
380 bool operator>=(const Queue& other) const requires requires(const T& left, const T& right) { left < right; };
381
386 STORMBYTE_FORCE_INLINE explicit operator std::queue<T>() const {
387 std::queue<T> exported;
388 for (const T& value : *this)
389 exported.push(value);
390 return exported;
391 }
392
393 private:
394 Vector<T> m_values;
395 };
396
403 template<Type::SafeValue T>
404 inline void swap(Queue<T>& left, Queue<T>& right) noexcept {
405 left.swap(right);
406 }
407 }
408
413 namespace Type {
418 template<SafeValue T>
419 requires IsSafe<T>::value
420 struct IsSafe<Safe::Queue<T>>: std::true_type {};
421
426 template<SafeValue T>
427 requires MaybeSafe<T>
428 struct IsMaybeSafe<Safe::Queue<T>>: std::true_type {};
429
434 template<SafeValue T>
435 struct IsSafeValue<Safe::Queue<T>>: std::true_type {};
436
441 template<SafeValue T>
442 struct IsSafeQueue<Safe::Queue<T>>: std::true_type {};
443 }
444}
445
464
FIFO sequence stored on Base's heap.
Definition queue.hxx:77
const_iterator cend() const noexcept
Return the read-only end iterator.
bool operator==(const Queue &other) const
Compare elements in FIFO order.
Size size_type
Element count. Size, not std::size_t.
Definition queue.hxx:81
iterator erase(const_iterator position)
Erase one element.
size_type size() const noexcept
Return the element count.
bool operator>=(const Queue &other) const
Order queues lexicographically.
typename Vector< T >::iterator iterator
Mutable iterator, front to back.
Definition queue.hxx:85
T value_type
Element type.
Definition queue.hxx:79
~Queue() noexcept
Destroy every element and release the block.
STORMBYTE_FORCE_INLINE Queue(std::queue< T > &&values)
Move elements from a caller-owned STL queue and leave it empty.
Definition queue.hxx:111
std::ptrdiff_t difference_type
Iterator distance type.
Definition queue.hxx:82
typename Vector< T >::const_iterator const_iterator
Read-only iterator, front to back.
Definition queue.hxx:86
STORMBYTE_FORCE_INLINE Queue & operator=(std::queue< T > &&values)
Move-assign a caller-owned STL queue and leave it empty.
Definition queue.hxx:194
Vector< T > container_type
Underlying Base-owned sequence.
Definition queue.hxx:80
Queue(const Queue &other)
Copy every element into a new Base block.
Queue(const container_type &values)
Copy the underlying sequence.
iterator end() noexcept
Return the mutable end iterator.
bool operator>(const Queue &other) const
Order queues lexicographically.
reference back()
Return the back element.
iterator begin() noexcept
Return a mutable iterator to the front.
reference emplace(Args &&... args)
Construct an element at the back.
const T & const_reference
Read-only element reference.
Definition queue.hxx:84
STORMBYTE_FORCE_INLINE std::strong_ordering operator<=>(const Queue &other) const
Order queues lexicographically.
Queue(Queue &&other) noexcept
Take the block.
reference front()
Return the front element.
void push(const T &value)
Append a copy.
bool operator<(const Queue &other) const
Order queues lexicographically.
void pop()
Remove the front element.
void swap(Queue &other) noexcept
Exchange blocks.
Queue(container_type &&values) noexcept
Take the underlying sequence.
bool empty() const noexcept
Test whether the queue is empty.
STORMBYTE_FORCE_INLINE void push_range(R &&range)
Append a copy of range, in order.
Definition queue.hxx:298
bool operator<=(const Queue &other) const
Order queues lexicographically.
Queue() noexcept
Construct an empty queue.
T & reference
Mutable element reference.
Definition queue.hxx:83
iterator erase(const_iterator first, const_iterator last)
Erase a half-open range.
STORMBYTE_FORCE_INLINE Queue(std::from_range_t, R &&range)
Copy the elements of range, in order.
Definition queue.hxx:139
const_iterator cbegin() const noexcept
Return a read-only iterator to the front.
Contiguous sequence stored on Base's heap.
Definition vector.hxx:81
const T * const_iterator
Read-only contiguous iterator.
Definition vector.hxx:91
T * iterator
Mutable contiguous iterator.
Definition vector.hxx:90
Count of abstract units (uint64_t storage).
Definition size.hxx:107
Type whose == and != both yield something convertible to bool.
Definition comparison.hxx:81
Recognizes a conditional Safe component after stripping cv and references.
Definition safe.hxx:301
FIFO wrappers supported by generic queue operations.
Definition wrappers.hxx:164
Owned values that cross a DLL without the caller's CRT.
void ThrowQueueOutOfBounds()
Throw the queue out-of-bounds error from Base.
void swap(Binary &left, Binary &right) noexcept
Exchange two sequences.
Definition binary.hxx:760
Root namespace of the StormByte suite.
Octet count. Defined in byte_size.hxx. Forwarded so Type::Numeral can name it.
STL namespace.
#define STORMBYTE_FORCE_INLINE
Definition platform.h:134
Host-width-stable count (uint64_t), implicit only to std::size_t.
Recognizes types whose DLL-boundary safety depends on documented provider requirements.
Definition safe.hxx:262
Identifies Safe FIFO wrappers.
Definition safe.hxx:337
Recognizes collection values with Base-controlled lifetime.
Definition safe.hxx:347
Recognizes Base Safe components, not universal ABI compatibility.
Definition safe.hxx:138
#define STORMBYTE_PUBLIC_TYPE
Definition visibility.h:57
#define STORMBYTE_PUBLIC
Definition visibility.h:52