StormByte-Buffer 4.0.0
C++26 buffer module of the StormByte suite
Loading...
Searching...
No Matches
pipe.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
48#include <StormByte/logger/log.hxx>
49#include <StormByte/safe/function.hxx>
50#include <StormByte/safe/pointers.hxx>
51#include <StormByte/type_traits.hxx>
52
53#include <string_view>
54
59namespace StormByte {
64 namespace Buffer {
72 public:
77 explicit PipeInput(ReadOnly& input) noexcept;
78
85 bool Read(const StormByte::ByteSize& count, StormByte::Safe::Binary& data) const noexcept;
86
91 bool EoF() const noexcept;
92
97 StormByte::ByteSize Available() const noexcept;
98
103 bool IsReadable() const noexcept;
104
105 private:
106 ReadOnly* m_input;
107 };
108
116 public:
121 explicit PipeOutput(WriteOnly& output) noexcept;
122
128 bool Write(const StormByte::Safe::Binary& data) const noexcept;
129
135 bool Write(StormByte::Safe::Binary&& data) const noexcept;
136
142 bool Write(std::string_view text) const noexcept;
143
148 bool IsWritable() const noexcept;
149
153 void Close() const noexcept;
154
158 void SetError() const noexcept;
159
160 private:
161 WriteOnly* m_output;
162 };
163 }
164}
165
171
177
182namespace StormByte {
187 namespace Buffer {
200 public:
206
211 explicit Pipe(Callback callback) noexcept;
212
221 template<typename Callable>
223 Type::CopyConstructible<std::remove_cvref_t<Callable>> &&
224 requires(std::remove_cvref_t<Callable>& callable, const PipeInput& input,
226 { callable(input, output, log) } -> Type::SameAs<void>;
227 }
228 STORMBYTE_FORCE_INLINE explicit Pipe(Callable&& callable):
229 Pipe(MakeCallback(std::forward<Callable>(callable))) {}
230
235 Pipe(const Pipe& other);
236
241 Pipe(Pipe&& other) noexcept;
242
246 ~Pipe() noexcept;
247
253 Pipe& operator=(const Pipe& other);
254
260 Pipe& operator=(Pipe&& other) noexcept;
261
271 StormByte::Safe::Status Run(const PipeInput& in, const PipeOutput& out,
272 const StormByte::Safe::Shared<StormByte::Logger::Log>& log) const;
273
274 private:
282 template<typename Callable>
283 static STORMBYTE_FORCE_INLINE Callback MakeCallback(Callable&& callable) {
284 using Context = std::remove_cvref_t<Callable>;
285 void* block = StormByte::Safe::Heap::Allocate(sizeof(Context));
286 Context* state = nullptr;
287 try {
288 state = new (block) Context(std::forward<Callable>(callable));
289 }
290 catch (...) {
292 throw;
293 }
294 return Callback(state,
295 [](void* current, const PipeInput& input, const PipeOutput& output,
297 (*static_cast<Context*>(current))(input, output, log);
298 return StormByte::Safe::Status::Success;
299 },
300 [](const void* current) noexcept -> void* {
301 void* cloned = nullptr;
302 try {
303 cloned = StormByte::Safe::Heap::Allocate(sizeof(Context));
304 new (cloned) Context(*static_cast<const Context*>(current));
305 return cloned;
306 }
307 catch (...) {
308 if (cloned != nullptr)
310 return nullptr;
311 }
312 },
313 [](void* current) noexcept {
314 static_cast<Context*>(current)->~Context();
316 });
317 }
318
319 Callback m_callback;
320 };
321 }
322}
323
Synchronous, non-owning stage input borrow.
Definition pipe.hxx:71
bool Read(const StormByte::ByteSize &count, StormByte::Safe::Binary &data) const noexcept
Read bytes and advance the borrowed cursor.
PipeInput(ReadOnly &input) noexcept
Borrow a live stream input.
StormByte::ByteSize Available() const noexcept
Query readable bytes.
bool IsReadable() const noexcept
Query the borrowed stream error state.
bool EoF() const noexcept
Query the end of the borrowed stream.
Synchronous, non-owning stage output borrow with const mutation forwarding.
Definition pipe.hxx:115
PipeOutput(WriteOnly &output) noexcept
Borrow a live stream output.
void SetError() const noexcept
Fail the borrowed output and wake waiters.
bool Write(std::string_view text) const noexcept
Copy length-aware text without a trailing NUL.
bool Write(const StormByte::Safe::Binary &data) const noexcept
Copy bytes to the borrowed writer.
bool IsWritable() const noexcept
Query whether the writer accepts more data.
void Close() const noexcept
Close the borrowed output and wake readers.
bool Write(StormByte::Safe::Binary &&data) const noexcept
Move bytes to the borrowed writer.
One transformation in a Pipeline.
Definition pipe.hxx:199
StormByte::Safe::Function< void(const PipeInput &, const PipeOutput &, const StormByte::Safe::Shared< StormByte::Logger::Log > &)> Callback
Owned callback with borrowed stream endpoints and logger.
Definition pipe.hxx:204
Pipe(Callback callback) noexcept
Adopt an explicitly provided creator-context callback.
Pipe(const Pipe &other)
Copy a Pipe.
~Pipe() noexcept
Release context through its creator callback.
Pipe(Pipe &&other) noexcept
Take a Pipe.
StormByte::Safe::Status Run(const PipeInput &in, const PipeOutput &out, const StormByte::Safe::Shared< StormByte::Logger::Log > &log) const
Run this pipe.
STORMBYTE_FORCE_INLINE Pipe(Callable &&callable)
Own a copyable callable in its creator module.
Definition pipe.hxx:228
Buffer that can be read and not written.
Definition generic.hxx:220
Buffer that can be written and not read.
Definition generic.hxx:497
STORMBYTE_DECLARE_MAYBE_SAFE(class StormByte::System::Device::Access)
Buffer module of the StormByte suite.
void * Allocate(std::size_t bytes)
void Free(void *pointer) noexcept
Root namespace of the StormByte C++ suite.
STL namespace.
#define STORMBYTE_FORCE_INLINE
#define STORMBYTE_BUFFER_PUBLIC
Definition visibility.h:54