StormByte-Buffer 2.0.0
C++26 buffer module of the StormByte suite
 
Loading...
Searching...
No Matches
sink.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 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-Buffer source in this
20 * repository. They do not cover other StormByte modules or any third-party
21 * material shipped with this repository (including everything under
22 * thirdparty/, and in particular the bundled StormByte-Logger tree and
23 * the rest of the StormByte suite it vendors), which remains under its own
24 * license.
25 *
26 * Neither license grants any patent rights. Any patent licenses required
27 * to use this software or third-party components must be obtained separately
28 * from the patent holders.
29 *
30 * StormByte-Buffer is distributed in the hope that it will be useful,
31 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 * GNU Lesser General Public License for more details.
34 *
35 * You should have received a copy of the GNU Lesser General Public License
36 * version 3 along with StormByte-Buffer. If not, see
37 * <https://www.gnu.org/licenses/lgpl-3.0.html>.
38 *
39 * SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-StormByte-Commercial
40 */
41
42#pragma once
43
45#include <StormByte/size.hxx>
47
48#include <condition_variable>
49#include <cstddef>
50#include <functional>
51#include <memory>
52#include <vector>
53
54namespace StormByte::Buffer {
78 template<Type::MoveConstructible T>
79 class Sink {
80 public:
86 using Select = std::function<StormByte::Size(StormByte::Size)>;
87
96 Sink() noexcept;
97
101 Sink(const Sink&) = delete;
102
106 Sink(Sink&&) noexcept = delete;
107
111 ~Sink() noexcept;
112
116 Sink& operator=(const Sink&) = delete;
117
121 Sink& operator=(Sink&&) noexcept = delete;
122
140 void Push(int key, T item) noexcept;
141
151 void Eof() noexcept;
152
166 class Lane {
167 public:
177 Sink& operator>>(Sink& dest) noexcept;
178
179 private:
180 friend class Sink;
181 Lane(Sink& from, int key) noexcept;
182 Sink* m_from;
183 int m_key;
184 };
185
191 Lane To(int key) noexcept;
192
200 Sink& operator>>(Sink& dest) noexcept;
201
207 Sink& operator<<(Sink& src) noexcept;
208
214 Sink& operator<<(Lane lane) noexcept;
215
219 void Drain() noexcept;
220
225 bool Draining() const noexcept;
226
231 void Notify(std::condition_variable& consumer) noexcept;
232
239 void Unnotify() noexcept;
240
256 std::vector<int> Keys() const noexcept;
257
262 StormByte::Size Buckets() const noexcept;
263
269 bool Contains(int key) const noexcept;
270
276 StormByte::Size Capacity(int key) const noexcept;
277
283 void Capacity(int key, StormByte::Size capacity) noexcept;
284
290 StormByte::Size Size(int key) const noexcept;
291
297 bool Full(int key) const noexcept;
298
304 bool Empty(int key) const noexcept;
305
311 bool EoF(int key) const noexcept;
312
318 bool Ready(int key) const noexcept;
319
329 T Front(int key) const noexcept requires Type::CopyConstructible<T>;
330
346 T Pop() noexcept;
347
355 T Pop(const Select& select) noexcept;
356
368 T Pop(int key) noexcept;
369
383 bool EoF() const noexcept;
384
389 bool Ready() const noexcept;
390
395 private:
400 class Implementation;
401
402 std::unique_ptr<Implementation> m_io;
403 };
404}
405
406#include <StormByte/buffer/sink.txx>
One-key redirect: from.To(key) >> dest.
Definition sink.hxx:166
Sink & operator>>(Sink &dest) noexcept
Wire this key onto dest.
Set of Hopper buckets keyed by an integer.
Definition sink.hxx:79
Sink & operator>>(Sink &dest) noexcept
Share every existing hopper with dest.
StormByte::Size Buckets() const noexcept
Number of hoppers wired on this Sink.
Lane To(int key) noexcept
Redirect of one hopper key.
bool Contains(int key) const noexcept
Whether hopper key exists on this Sink.
void Unnotify() noexcept
Clears Notify on this Sink and on every hopper.
void Drain() noexcept
Marks Sink as a terminal producer (un-wired Push calls drop instead of waiting).
std::vector< int > Keys() const noexcept
Keys of hoppers currently wired on this Sink.
StormByte::Size Capacity(int key) const noexcept
Gets capacity ceiling of bucket key.
Sink() noexcept
Constructs an empty Sink with zero buckets.
T Front(int key) const noexcept
Copy of the next item in bucket key without dequeuing.
bool Full(int key) const noexcept
Checks if bucket key is full.
bool Draining() const noexcept
Checks whether Drain was called.
void Notify(std::condition_variable &consumer) noexcept
Registers consumer condition variable to notify on all current and future hoppers.
void Push(int key, T item) noexcept
Pushes an item into the bucket specified by key.
bool Empty(int key) const noexcept
Whether bucket key has no pending items.
Sink & operator<<(Sink &src) noexcept
Same as src >> *this (all hoppers).
bool EoF() const noexcept
Checks if the Sink is finished.
std::function< StormByte::Size(StormByte::Size)> Select
Index chooser for Pop selection across multiple buckets.
Definition sink.hxx:86
bool Ready() const noexcept
Checks whether a Pop call can return immediately (item available or EoF).
Sink & operator<<(Lane lane) noexcept
Same as lane >> *this.
void Eof() noexcept
Closes the Sink and releases writer holds on its hoppers.
T Pop() noexcept
Pops one item from the Sink using default round-robin order.
Buffer module of the StormByte suite.
Root namespace of the StormByte C++ suite.
STL namespace.