StormByte C++ Library: Buffer module 1.2.0
StormByte-Buffer is the buffer module of the StormByte C++ suite.
Loading...
Searching...
No Matches
hopper.hxx
Go to the documentation of this file.
1/*
2 * Copyright (C) 2024-2026 David C. Manuelda (StormBytePP)
3 *
4 * This file is part of StormByte-Buffer.
5 *
6 * StormByte-Buffer is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License version 3
8 * or later, as published by the Free Software Foundation.
9 *
10 * StormByte-Buffer is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with StormByte-Buffer. If not, see
17 * <https://www.gnu.org/licenses/lgpl-3.0.html>.
18 */
19
20#pragma once
21
22#include <StormByte/type_traits.hxx>
23
24#include <condition_variable>
25#include <cstddef>
26#include <memory>
27#include <utility>
28
29namespace StormByte::Buffer {
30 template<Type::MoveConstructible T> class Sink;
31
50 template<Type::MoveConstructible T>
51 class Hopper {
52 public:
61 Hopper() noexcept;
62
67 explicit Hopper(std::size_t capacity) noexcept;
68
72 Hopper(const Hopper&) = delete;
73
77 Hopper(Hopper&&) noexcept = delete;
78
82 ~Hopper() noexcept;
83
87 Hopper& operator=(const Hopper&) = delete;
88
92 Hopper& operator=(Hopper&&) noexcept = delete;
93
107 std::size_t Capacity() const noexcept;
108
116 void Capacity(std::size_t capacity) noexcept;
117
122 std::size_t Size() const noexcept;
123
128 bool Full() const noexcept;
129
146 void Push(T item) noexcept;
147
153 Hopper& operator<<(T item) noexcept;
154
160 void Eof() noexcept;
161
178 T Pop() noexcept;
179
185 Hopper& operator>>(T& item) noexcept;
186
191 bool EoF() const noexcept;
192
197 bool Empty() const noexcept;
198
216 void Notify(std::condition_variable& wake) noexcept;
217
225 void Unnotify() noexcept;
226
240 friend Hopper& operator>>(T& item, Hopper& hopper) noexcept {
241 hopper << std::move(item);
242 return hopper;
243 }
244
251 friend Hopper& operator>>(T&& item, Hopper& hopper) noexcept {
252 hopper << std::move(item);
253 return hopper;
254 }
255
256 private:
257 friend class Sink<T>;
258
259 void AddWriter() noexcept;
260 void CloseWriter() noexcept;
261
266 class Implementation;
267
268 std::unique_ptr<Implementation> m_impl;
269 };
270}
271
272#include <StormByte/buffer/hopper.txx>
Single-producer single-consumer (SPSC) typed item queue.
Definition hopper.hxx:51
void Notify(std::condition_variable &wake) noexcept
Registers the consumer condition variable to notify on Push or Eof.
bool Empty() const noexcept
Checks whether the queue has no pending items.
void Eof() noexcept
Marks end of production and wakes waiters.
T Pop() noexcept
Pops one item from the bucket, or returns default-constructed T if dry.
std::size_t Size() const noexcept
Gets the number of items currently waiting in the bucket.
std::size_t Capacity() const noexcept
Gets the current capacity ceiling.
void Unnotify() noexcept
Drops the pointer set by Notify.
Hopper() noexcept
Constructs an empty unbounded Hopper.
bool EoF() const noexcept
Checks whether Eof was called by a producer.
friend Hopper & operator>>(T &&item, Hopper &hopper) noexcept
Write: rvalue item flows into hopper.
Definition hopper.hxx:251
bool Full() const noexcept
Checks whether a bounded bucket cannot accept another Push without waiting.
void Push(T item) noexcept
Enqueues one item and signals the consumer if Notify was configured.
Set of Hopper buckets keyed by an integer.
Definition sink.hxx:53
Buffer module of the StormByte suite.