StormByte-Buffer 2.0.0
C++26 buffer module of the StormByte suite
 
Loading...
Searching...
No Matches
buffered_writer.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
53
54#include <chrono>
55#include <cstddef>
56#include <memory>
57#include <span>
58#include <utility>
59
64namespace StormByte {
69 namespace Buffer {
74 namespace Backend {
79 class Bridge;
80
85 namespace IO {
90 class BufferedWriter;
91 }
92 }
93
98 namespace IO {
232 friend class StormByte::Buffer::Backend::IO::BufferedWriter;
233 friend class StormByte::Buffer::Backend::Bridge;
234
235 public:
245 public:
246 using WriterParameters::WriterParameters;
247 };
248
258
264
270 virtual ~BufferedWriter() noexcept;
271
276 BufferedWriter& operator=(const BufferedWriter&) = delete;
277
283 BufferedWriter& operator=(BufferedWriter&& other) noexcept;
284
297 const StormByte::String::String& Path() const noexcept;
298
303 enum Location Location() const noexcept;
304
309 virtual explicit operator bool() const noexcept final;
310
315 virtual enum State State() const noexcept final;
316
329 virtual bool Open() final;
330
339 virtual bool Close() final;
340
345 virtual bool Rewind() final;
346
351 virtual bool IsOpen() const noexcept final;
352
361 virtual Result Flush() final;
362
369 virtual Result Truncate() final;
370
385 virtual Result Write(const FIFO& src) final;
386
392 virtual Result Write(FIFO& src) final;
393
399 virtual Result Write(std::span<const std::byte> src) final;
400
414 virtual StormByte::ByteSize Tell() const noexcept;
415
420 virtual StormByte::ByteSize Dirty() const noexcept;
421
430 virtual StormByte::ByteSize Size() const noexcept;
431
444 virtual Result Seek(std::ptrdiff_t offset, Position mode);
445
462 const StormByte::Shared<StormByte::Buffer::WriteTelemetry> Telemetry() const noexcept;
463
477 virtual StormByte::ByteSize WriteChunk() const noexcept;
478
487 virtual void WriteChunk(StormByte::ByteSize bytes);
488
493 virtual std::size_t BackPressure() const noexcept;
494
502 virtual void BackPressure(std::size_t chunks);
503
510 virtual StormByte::ByteSize MaxMemory() const noexcept;
511
519 virtual void MaxMemory(StormByte::ByteSize bytes);
520
525 virtual std::chrono::milliseconds MaxWait() const noexcept;
526
533 virtual void MaxWait(std::chrono::milliseconds wait);
534
539 protected:
546 STORMBYTE_FORCE_INLINE BufferedWriter(StormByte::String::String path, enum Location location,
547 Parameters parameters = {}):
548 BufferedWriter(std::move(path), location,
549 parameters.WriteChunk().value_or(StormByte::ByteSize{0}),
550 parameters.BackPressure().value_or(0),
551 parameters.MaxWait().value_or(std::chrono::milliseconds{0}),
552 parameters.MaxMemory().value_or(StormByte::ByteSize{0})) {}
553
566 StormByte::ByteSize write_chunk, std::size_t back_pressure,
567 std::chrono::milliseconds max_wait, StormByte::ByteSize max_memory);
568
573 void SetState(enum State state) noexcept;
574
579 void SetTell(StormByte::ByteSize offset) noexcept;
580
587 virtual void Setup();
588
597
607 virtual bool WillWrite(StormByte::ByteSize n) const;
608
618 virtual Result OriginOpen() = 0;
619
624 virtual Result OriginClose() = 0;
625
632 virtual Result OriginPush(std::span<const std::byte> data) = 0;
633
641 virtual Result OriginFlush() = 0;
642
647 virtual Result OriginTruncate() = 0;
648
658
663 private:
664 std::unique_ptr<StormByte::Buffer::Backend::IO::BufferedWriter> m_io;
665 };
666 }
667 }
668}
Private pump for StormByte::Buffer::Bridge.
Private implementation of StormByte::Buffer::IO::BufferedWriter.
Byte-oriented FIFO buffer with grow-on-demand and close/error support.
Definition fifo.hxx:78
Writer ring cap in WriteChunk units.
Definition parameters.hxx:177
Writer knobs.
Definition buffered_writer.hxx:244
Coordinated binary write sink with optional chunked write-behind and a delayed-seek page map.
Definition buffered_writer.hxx:231
void SetTell(StormByte::ByteSize offset) noexcept
Publish the logical write offset from a leaf Seek.
virtual Result OriginFlush()=0
Make accepted bytes visible on the device.
virtual Result OriginPush(std::span< const std::byte > data)=0
Write data to the device.
virtual Result OriginClose()=0
Release the device and SetState Unavailable.
virtual void Setup()
Leaf policy hook.
virtual Result OriginTruncate()=0
Discard origin contents.
BufferedWriter(BufferedWriter &&other) noexcept
Move constructor.
virtual Result OriginSeek(StormByte::ByteSize absolute)
Seek the origin to absolute.
void SetState(enum State state) noexcept
Publish session state from a leaf hook.
virtual ~BufferedWriter() noexcept
Virtual destructor.
virtual StormByte::Shared< StormByte::Buffer::WriteTelemetry > CreateTelemetry() const
Allocate the telemetry object this instance will keep.
BufferedWriter(const BufferedWriter &)=delete
Copy constructor is deleted.
virtual Result OriginOpen()=0
Arm the device and SetState.
virtual bool WillWrite(StormByte::ByteSize n) const
Whether n more bytes can be accepted now.
BufferedWriter(StormByte::String::String path, enum Location location, StormByte::ByteSize write_chunk, std::size_t back_pressure, std::chrono::milliseconds max_wait, StormByte::ByteSize max_memory)
Construct an unopened coordinator (State::Unavailable).
Cache / dirty-page budget knob.
Definition parameters.hxx:102
Origin wait cap.
Definition parameters.hxx:127
Origin push unit.
Definition parameters.hxx:152
Writer counters on top of StormByte::Buffer::WriteTelemetry.
Definition telemetry.hxx:266
Optional writer knobs.
Definition parameters.hxx:274
Base session counters.
Definition telemetry.hxx:82
Location
Where the bytes of a file-like source or sink live.
Definition typedefs.hxx:140
State
Session state of a coordinated reader or writer.
Definition typedefs.hxx:86
Position
Forward declaration of the WriteOnly interface.
Definition typedefs.hxx:73
Root namespace of the StormByte C++ suite.
STL namespace.
Per-call status plus how many bytes this call transferred.
Definition typedefs.hxx:168
#define STORMBYTE_BUFFER_PUBLIC
Definition visibility.h:54