StormByte-Buffer 4.0.0
C++26 buffer module of the StormByte suite
Loading...
Searching...
No Matches
bridge.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
44#include <StormByte/type_traits/safe.hxx>
45
52#include <StormByte/safe/pointers.hxx>
53#include <StormByte/type_traits.hxx>
54
55#include <mutex>
56#include <utility>
57
62namespace StormByte {
67 namespace Buffer {
130 public:
135 enum class Operation {
136 Blocking,
137 NonBlocking
138 };
139
144 enum class State {
145 Open,
148 };
149
156
164 template<typename In, typename Out>
173
180 template<typename Out>
184 AttachIoOut(std::forward<Out>(out));
185 AttachNonIoIn(in);
186 }
187
194 template<typename In>
198 AttachIoIn(std::forward<In>(in));
199 AttachNonIoOut(out);
200 }
201
207 template<typename In, typename Out>
208 Bridge(In&&, Out&&)
213
217 Bridge(const Bridge&) = delete;
218
223 Bridge(Bridge&& other) noexcept;
224
228 ~Bridge() noexcept;
229
233 Bridge& operator=(const Bridge&) = delete;
234
240 Bridge& operator=(Bridge&& other) noexcept;
241
251 void Close() noexcept;
252
257 bool EoF() const noexcept;
258
263 bool Failed() const noexcept;
264
269 bool InputIsIO() const noexcept;
270
279 bool InputPullBlocking() const noexcept;
280
285 enum State State() const noexcept;
286
294 Operation operation = Operation::Blocking);
295
300 const StormByte::Safe::Shared<StormByte::Buffer::ReadTelemetry> ReadTelemetry() const noexcept;
301
306 const StormByte::Safe::Shared<StormByte::Buffer::WriteTelemetry> WriteTelemetry() const noexcept;
307
308 private:
312 Bridge() noexcept;
313
318 void AttachNonIoIn(ReadOnly& in);
319
324 void AttachNonIoOut(WriteOnly& out);
325
332 void CacheReadTelemetry() noexcept;
333
340 void CacheWriteTelemetry() noexcept;
341
345 void ReleaseTips() noexcept;
346
351 bool SourceEoF() const noexcept;
352
360 IO::Result Pull(StormByte::ByteSize n, FIFO& dest, Operation operation);
361
367 IO::Result Push(FIFO& src);
368
374 template<typename In>
375 STORMBYTE_FORCE_INLINE void AttachIoIn(In&& in) {
376 m_io_in_blocking = (in.ReadAhead() == StormByte::ByteSize{0});
377 m_io_in = StormByte::Safe::Unique<IO::BufferedReader>::MakePointer<std::remove_cvref_t<In>>(
378 std::forward<In>(in));
379 CacheReadTelemetry();
380 }
381
387 template<typename Out>
388 STORMBYTE_FORCE_INLINE void AttachIoOut(Out&& out) {
389 m_io_out = StormByte::Safe::Unique<IO::BufferedWriter>::MakePointer<std::remove_cvref_t<Out>>(
390 std::forward<Out>(out));
391 CacheWriteTelemetry();
392 }
393
397 ReadOnly* m_ext_in {nullptr};
401 WriteOnly* m_ext_out {nullptr};
405 StormByte::Safe::Unique<IO::BufferedReader> m_io_in;
409 StormByte::Safe::Unique<IO::BufferedWriter> m_io_out;
413 StormByte::Safe::Shared<StormByte::Buffer::ReadTelemetry> m_owned_read;
417 StormByte::Safe::Shared<StormByte::Buffer::WriteTelemetry> m_owned_write;
421 bool m_io_in_blocking {false};
426 using SessionState = enum State;
427
431 SessionState m_session_state {SessionState::Open};
435 mutable std::mutex m_mutex;
436 };
437 }
438}
439
Manual bridge between two StormByte-Buffer ends.
Definition bridge.hxx:129
const StormByte::Safe::Shared< StormByte::Buffer::WriteTelemetry > WriteTelemetry() const noexcept
Write counters.
Bridge(In &&, Out &&)=delete
Reject borrowed or const IO leaves instead of falling back to borrowed tips.
const StormByte::Safe::Shared< StormByte::Buffer::ReadTelemetry > ReadTelemetry() const noexcept
Read counters.
bool Failed() const noexcept
Whether a tip has failed for real.
Bridge(ReadOnly &in, WriteOnly &out)
Two non-IO tips.
bool EoF() const noexcept
Whether the read tip reports end-of-stream.
Operation
Read-side wait policy for Passthrough.
Definition bridge.hxx:135
Bridge(const Bridge &)=delete
Copy construction is disabled for one-shot sessions.
Bridge(Bridge &&other) noexcept
Move constructor.
void Close() noexcept
Release owned tips.
bool InputPullBlocking() const noexcept
Whether Pumper must Blocking-pull this source.
State
Session lifetime.
Definition bridge.hxx:144
STORMBYTE_FORCE_INLINE Bridge(In &&in, Out &&out)
Two IO leaves.
Definition bridge.hxx:165
bool InputIsIO() const noexcept
Whether the read tip is a stolen IO leaf.
~Bridge() noexcept
Destructor.
StormByte::ByteSize Passthrough(StormByte::ByteSize n, Operation operation=Operation::Blocking)
Move bytes from the read tip to the write tip.
STORMBYTE_FORCE_INLINE Bridge(ReadOnly &in, Out &&out)
Non-IO source, IO sink stolen.
Definition bridge.hxx:181
STORMBYTE_FORCE_INLINE Bridge(In &&in, WriteOnly &out)
IO source stolen, non-IO sink.
Definition bridge.hxx:195
Byte-oriented FIFO buffer with grow-on-demand and close/error support.
Definition fifo.hxx:86
Coordinated binary read source with optional prefetch and cache.
Definition buffered_reader.hxx:214
Coordinated binary write sink with optional chunked write-behind and a delayed-seek page map.
Definition buffered_writer.hxx:234
Buffer that can be read and not written.
Definition generic.hxx:220
Buffer that can be written and not read.
Definition generic.hxx:497
static Unique< T > MakePointer(Args &&... args)
STORMBYTE_DECLARE_MAYBE_SAFE(class StormByte::System::Device::Access)
Buffered binary sources and sinks.
State
Session state of a coordinated reader or writer.
Definition typedefs.hxx:105
Buffer module of the StormByte suite.
@ Closed
Queue, Sink or budget closed.
Definition byte_budget.hxx:84
Root namespace of the StormByte C++ suite.
#define STORMBYTE_FORCE_INLINE
#define STORMBYTE_BUFFER_PUBLIC
Definition visibility.h:54