StormByte-Buffer 2.0.0
C++26 buffer module of the StormByte suite
 
Loading...
Searching...
No Matches
buffered_reader.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 <optional>
58#include <span>
59#include <utility>
60
65namespace StormByte {
70 namespace Buffer {
75 namespace Backend {
80 namespace IO {
85 class BufferedReader;
86 }
87 }
88
93 namespace IO {
212 friend class StormByte::Buffer::Backend::IO::BufferedReader;
213
214 public:
224 public:
225 using ReaderParameters::ReaderParameters;
226 };
227
237
243
250 virtual ~BufferedReader() noexcept;
251
255 BufferedReader& operator=(const BufferedReader&) = delete;
256
262 BufferedReader& operator=(BufferedReader&& other) noexcept;
263
276 const StormByte::String::String& Path() const noexcept;
277
282 enum Location Location() const noexcept;
283
288 virtual explicit operator bool() const noexcept final;
289
294 virtual enum State State() const noexcept final;
295
309 virtual bool Open() final;
310
317 virtual Result Close() final;
318
324 virtual bool Rewind() final;
325
330 virtual bool IsOpen() const noexcept final;
331
336 virtual bool IsReadable() const noexcept final;
337
343 virtual bool EoF() const noexcept final;
344
353 virtual StormByte::ByteSize Available() const noexcept final;
354
370 virtual Result Read(StormByte::ByteSize n, FIFO& dest) const final;
371
381 virtual Result Read(std::span<std::byte> dest) const final;
382
389 virtual Result Peek(StormByte::ByteSize n, FIFO& dest) const final;
390
400 virtual Result Peek(std::span<std::byte> dest) const final;
401
428 virtual Result Seek(std::ptrdiff_t offset, Position mode) const final;
429
434 virtual StormByte::ByteSize Tell() const noexcept final;
435
440 virtual bool IsSeekable() const noexcept final;
441
455 virtual bool IsSized() const noexcept final;
456
461 virtual std::optional<StormByte::ByteSize> Size() const noexcept final;
462
480 const StormByte::Shared<StormByte::Buffer::ReadTelemetry> Telemetry() const noexcept;
481
495 virtual StormByte::ByteSize ReadAhead() const noexcept;
496
505 virtual void ReadAhead(StormByte::ByteSize bytes);
506
511 virtual StormByte::ByteSize MaxMemory() const noexcept;
512
520 virtual void MaxMemory(StormByte::ByteSize bytes);
521
526 virtual std::chrono::milliseconds MaxWait() const noexcept;
527
535 virtual void MaxWait(std::chrono::milliseconds wait);
536
541 protected:
548 STORMBYTE_FORCE_INLINE BufferedReader(StormByte::String::String path, enum Location location,
549 Parameters parameters = {}):
550 BufferedReader(std::move(path), location,
551 parameters.ReadAhead().value_or(StormByte::ByteSize{0}),
552 parameters.MaxMemory().value_or(StormByte::ByteSize{0}),
553 parameters.MaxWait().value_or(std::chrono::milliseconds{0})) {}
554
566 StormByte::ByteSize read_ahead, StormByte::ByteSize max_memory,
567 std::chrono::milliseconds max_wait);
568
576 void SetState(enum State state) noexcept;
577
584 virtual void Setup();
585
595
605 virtual Result OriginOpen() = 0;
606
611 virtual Result OriginClose() = 0;
612
624
629 virtual bool OriginCanSeek() const noexcept = 0;
630
640 virtual Result OriginSeek(std::ptrdiff_t offset, Position mode) = 0;
641
646 virtual bool OriginHasSize() const noexcept = 0;
647
652 virtual std::optional<StormByte::ByteSize> OriginSize() const noexcept = 0;
653
658 private:
659 std::unique_ptr<StormByte::Buffer::Backend::IO::BufferedReader> m_io;
660 };
661 }
662 }
663}
Private implementation of StormByte::Buffer::IO::BufferedReader.
Byte-oriented FIFO buffer with grow-on-demand and close/error support.
Definition fifo.hxx:78
Reader knobs.
Definition buffered_reader.hxx:223
Coordinated binary read source with optional prefetch and cache.
Definition buffered_reader.hxx:211
void SetState(enum State state) noexcept
Publish session state from a leaf hook.
BufferedReader(const BufferedReader &)=delete
Copy constructor is deleted.
BufferedReader(StormByte::String::String path, enum Location location, StormByte::ByteSize read_ahead, StormByte::ByteSize max_memory, std::chrono::milliseconds max_wait)
Construct an unopened coordinator (State::Unavailable).
virtual Result OriginClose()=0
Release the underlying device and SetState Unavailable.
virtual bool OriginCanSeek() const noexcept=0
Whether the device can seek.
virtual ~BufferedReader() noexcept
Virtual destructor.
virtual Result OriginPull(StormByte::ByteSize n, FIFO &dest)=0
Read up to n bytes from the device into dest.
virtual void Setup()
Leaf policy hook.
BufferedReader(BufferedReader &&other) noexcept
Move constructor.
virtual StormByte::Shared< StormByte::Buffer::ReadTelemetry > CreateTelemetry() const
Allocate the telemetry object this instance will keep.
virtual Result OriginOpen()=0
Arm the underlying device and SetState.
Cache / dirty-page budget knob.
Definition parameters.hxx:102
Origin wait cap.
Definition parameters.hxx:127
Prefetch length knob.
Definition parameters.hxx:77
Reader counters on top of StormByte::Buffer::ReadTelemetry.
Definition telemetry.hxx:87
Optional reader knobs.
Definition parameters.hxx:205
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