StormByte C++ Library: Buffer module 1.2.0
StormByte-Buffer is the buffer module of the StormByte C++ 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 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
23#include <StormByte/type_traits.hxx>
24
25#include <condition_variable>
26#include <cstddef>
27#include <functional>
28#include <memory>
29
30namespace StormByte::Buffer {
52 template<Type::MoveConstructible T>
53 class Sink {
54 public:
60 using Select = std::function<std::size_t(std::size_t)>;
61
70 Sink() noexcept;
71
75 Sink(const Sink&) = delete;
76
80 Sink(Sink&&) noexcept = delete;
81
85 ~Sink() noexcept;
86
90 Sink& operator=(const Sink&) = delete;
91
95 Sink& operator=(Sink&&) noexcept = delete;
96
114 void Push(int key, T item) noexcept;
115
125 void Eof() noexcept;
126
140 class Lane {
141 public:
151 Sink& operator>>(Sink& dest) noexcept;
152
153 private:
154 friend class Sink;
155 Lane(Sink& from, int key) noexcept;
156 Sink* m_from;
157 int m_key;
158 };
159
165 Lane To(int key) noexcept;
166
174 Sink& operator>>(Sink& dest) noexcept;
175
181 Sink& operator<<(Sink& src) noexcept;
182
188 Sink& operator<<(Lane lane) noexcept;
189
195 [[deprecated("use producer >> consumer")]]
196 void Bind(Sink& consumer);
197
208 [[deprecated("use producer.To(key) >> consumer")]]
209 void Bind(int key, Sink& consumer);
210
214 void Drain() noexcept;
215
220 bool Draining() const noexcept;
221
226 void Notify(std::condition_variable& consumer) noexcept;
227
234 void Unnotify() noexcept;
235
250 std::size_t Capacity(int key) const noexcept;
251
257 void Capacity(int key, std::size_t capacity) noexcept;
258
264 std::size_t Size(int key) const noexcept;
265
271 bool Full(int key) const noexcept;
272
288 T Pop() noexcept;
289
297 T Pop(const Select& select) noexcept;
298
312 bool EoF() const noexcept;
313
318 bool Ready() const noexcept;
319
324 private:
329 class Implementation;
330
331 std::unique_ptr<Implementation> m_impl;
332 };
333}
334
335#include <StormByte/buffer/sink.txx>
One-key redirect: from.To(key) >> dest.
Definition sink.hxx:140
Sink & operator>>(Sink &dest) noexcept
Wire this key onto dest.
Set of Hopper buckets keyed by an integer.
Definition sink.hxx:53
Sink & operator>>(Sink &dest) noexcept
Share every existing hopper with dest.
Lane To(int key) noexcept
Redirect of one hopper key.
void Unnotify() noexcept
Clears Notify on this Sink and on every hopper.
std::size_t Capacity(int key) const noexcept
Gets capacity ceiling of bucket key.
std::function< std::size_t(std::size_t)> Select
Index chooser for Pop selection across multiple buckets.
Definition sink.hxx:60
void Bind(int key, Sink &consumer)
Creates or retrieves the hopper for key and shares it with consumer.
void Drain() noexcept
Marks Sink as a terminal producer (un-wired Push calls drop instead of waiting).
void Bind(Sink &consumer)
Shares all existing hoppers on this Sink with the consumer Sink.
Sink() noexcept
Constructs an empty Sink with zero buckets.
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.
Sink & operator<<(Sink &src) noexcept
Same as src >> *this (all hoppers).
bool EoF() const noexcept
Checks if the Sink is finished.
std::size_t Size(int key) const noexcept
Gets pending item count in bucket key.
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.