StormByte-Buffer 2.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
52
53#include <memory>
54#include <mutex>
55#include <utility>
56
61namespace StormByte {
66 namespace Buffer {
67 class ExternalBufferReader;
68 class ExternalBufferWriter;
69
79 void operator()(ExternalBufferReader* ptr) const noexcept;
80 };
81
91 void operator()(ExternalBufferWriter* ptr) const noexcept;
92 };
93
157 public:
162 enum class Operation {
163 Blocking,
164 NonBlocking
165 };
166
171 enum class State {
172 Open,
173 Closed,
174 Failed
175 };
176
182 Bridge(ReadOnly& in, WriteOnly& out) noexcept;
183
191 template<typename In, typename Out>
192 STORMBYTE_FORCE_INLINE Bridge(In&& in, Out&& out) noexcept
195 AttachIoIn(std::forward<In>(in));
196 AttachIoOut(std::forward<Out>(out));
197 }
198
205 template<typename Out>
206 STORMBYTE_FORCE_INLINE Bridge(ReadOnly& in, Out&& out) noexcept
208 AttachIoOut(std::forward<Out>(out));
209 AttachNonIoIn(in);
210 }
211
218 template<typename In>
221 AttachIoIn(std::forward<In>(in));
222 AttachNonIoOut(out);
223 }
224
225 Bridge(const Bridge&) = delete;
226
231 Bridge(Bridge&& other) noexcept;
232
236 ~Bridge() noexcept;
237
238 Bridge& operator=(const Bridge&) = delete;
239
245 Bridge& operator=(Bridge&& other) noexcept;
246
256 void Close() noexcept;
257
262 bool EoF() const noexcept;
263
268 bool Failed() const noexcept;
269
274 bool InputIsIO() const noexcept;
275
284 bool InputPullBlocking() const noexcept;
285
290 enum State State() const noexcept;
291
299 Operation operation = Operation::Blocking);
300
305 const StormByte::Shared<StormByte::Buffer::ReadTelemetry> ReadTelemetry() const noexcept;
306
311 const StormByte::Shared<StormByte::Buffer::WriteTelemetry> WriteTelemetry() const noexcept;
312
313 private:
318 void AttachNonIoIn(ReadOnly& in) noexcept;
319
324 void AttachNonIoOut(WriteOnly& out) noexcept;
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) noexcept {
376 m_io_in_blocking = (in.ReadAhead() == StormByte::ByteSize{0});
377 m_io_in = StormByte::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) noexcept {
389 m_io_out = StormByte::Unique<IO::BufferedWriter>::MakePointer<std::remove_cvref_t<Out>>(
390 std::forward<Out>(out));
391 CacheWriteTelemetry();
392 }
393
394 std::unique_ptr<ExternalBufferReader, ExternalReaderDeleter> m_ext_in;
395 std::unique_ptr<ExternalBufferWriter, ExternalWriterDeleter> m_ext_out;
400 bool m_io_in_blocking {false};
401 enum State m_state {State::Open};
402 mutable std::mutex m_mutex;
403 };
404 }
405}
Manual bridge between two StormByte-Buffer ends.
Definition bridge.hxx:156
STORMBYTE_FORCE_INLINE Bridge(ReadOnly &in, Out &&out) noexcept
Non-IO source, IO sink stolen.
Definition bridge.hxx:206
STORMBYTE_FORCE_INLINE Bridge(In &&in, WriteOnly &out) noexcept
IO source stolen, non-IO sink.
Definition bridge.hxx:219
STORMBYTE_FORCE_INLINE Bridge(In &&in, Out &&out) noexcept
Two IO leaves.
Definition bridge.hxx:192
Operation
Read-side wait policy for Passthrough.
Definition bridge.hxx:162
Bridge(const Bridge &)=delete
Bridge(Bridge &&other) noexcept
Move constructor.
State
Session lifetime.
Definition bridge.hxx:171
~Bridge() noexcept
Destructor.
Bridge(ReadOnly &in, WriteOnly &out) noexcept
Two non-IO tips.
Byte-oriented FIFO buffer with grow-on-demand and close/error support.
Definition fifo.hxx:78
Coordinated binary read source with optional prefetch and cache.
Definition buffered_reader.hxx:211
Coordinated binary write sink with optional chunked write-behind and a delayed-seek page map.
Definition buffered_writer.hxx:231
Buffer that can be read and not written.
Definition generic.hxx:228
Basic read counters.
Definition telemetry.hxx:141
Buffer that can be written and not read.
Definition generic.hxx:505
Basic write counters.
Definition telemetry.hxx:181
Root namespace of the StormByte C++ suite.
#define STORMBYTE_FORCE_INLINE
Destroys a non-IO read adapter in this module.
Definition bridge.hxx:74
void operator()(ExternalBufferReader *ptr) const noexcept
Destroy ptr.
Destroys a non-IO write adapter in this module.
Definition bridge.hxx:86
void operator()(ExternalBufferWriter *ptr) const noexcept
Destroy ptr.
#define STORMBYTE_BUFFER_PUBLIC
Definition visibility.h:54