StormByte-Buffer 4.0.0
C++26 buffer module of the StormByte suite
Loading...
Searching...
No Matches
generic.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
47#include <StormByte/byte_size.hxx>
48#include <StormByte/safe/binary.hxx>
49#include <StormByte/type_traits.hxx>
50
51#include <algorithm>
52#include <cstdint>
53#include <iterator>
54#include <ranges>
55#include <span>
56#include <string_view>
57#include <type_traits>
58#include <utility>
59
64namespace StormByte {
69 namespace Buffer {
82 public:
87
91 Generic() noexcept;
92
97 Generic(const Generic& other) noexcept;
98
103 Generic(Generic&& other) noexcept;
104
108 virtual ~Generic() noexcept = 0;
109
115 Generic& operator=(const Generic& other) noexcept;
116
122 Generic& operator=(Generic&& other) noexcept;
123
127
132
137 virtual StormByte::ByteSize Size() const noexcept = 0;
138
142
143 protected:
148
155 template<Type::ByteInputRange Src>
156 static STORMBYTE_FORCE_INLINE Safe::Binary DataConvert(const Src& src) noexcept {
157 Safe::Binary out;
158 if constexpr (requires { std::ranges::size(src); }) {
159 auto s = std::ranges::size(src);
160 if (s > 0)
161 out.reserve(StormByte::ByteSize{static_cast<std::size_t>(s)});
162 }
163 std::transform(std::ranges::begin(src), std::ranges::end(src), std::back_inserter(out),
164 [](auto&& e) noexcept { return static_cast<std::byte>(e); });
165 return out;
166 }
167
174 template<Type::ByteInputRange Src>
177 return std::move(src);
178 else {
179 Safe::Binary out;
180 if constexpr (requires { std::ranges::size(src); }) {
181 auto s = std::ranges::size(src);
182 if (s > 0)
183 out.reserve(StormByte::ByteSize{static_cast<std::size_t>(s)});
184 }
185 std::transform(std::ranges::begin(src), std::ranges::end(src), std::back_inserter(out),
186 [](auto&& e) noexcept { return static_cast<std::byte>(e); });
187 return out;
188 }
189 }
190
196 static Safe::Binary DataConvert(std::string_view sv) noexcept;
197
203 static Safe::Binary DataConvert(const char* s) noexcept;
204
208 };
209
210 class WriteOnly;
211
221 public:
226
230 ReadOnly() noexcept;
231
236 ReadOnly(const ReadOnly& other) noexcept;
237
242 ReadOnly(ReadOnly&& other) noexcept;
243
247 virtual ~ReadOnly() noexcept;
248
254 ReadOnly& operator=(const ReadOnly& other) noexcept;
255
261 ReadOnly& operator=(ReadOnly&& other) noexcept;
262
266
271
276 virtual StormByte::ByteSize Available() const noexcept = 0;
277
282 virtual const Safe::Binary& Data() const noexcept = 0;
283
289 virtual bool Empty() const noexcept = 0;
290
295 virtual bool EoF() const noexcept = 0;
296
301 virtual bool IsReadable() const noexcept = 0;
302
306
311
316 virtual void Clean() noexcept = 0;
317
323 virtual void Clear() noexcept = 0;
324
331 virtual bool Drop(const StormByte::ByteSize& count) noexcept = 0;
332
340 virtual void Seek(const std::ptrdiff_t& offset, const Position& mode) const noexcept = 0;
341
345
350
357 inline virtual bool Extract(const StormByte::ByteSize& count, StormByte::Safe::Binary& outBuffer) noexcept = 0;
358
364 inline bool Extract(StormByte::Safe::Binary& outBuffer) noexcept {
365 return Extract(StormByte::ByteSize{0}, outBuffer);
366 }
367
374 inline virtual bool Extract(const StormByte::ByteSize& count, WriteOnly& outBuffer) noexcept = 0;
375
381 inline bool Extract(WriteOnly& outBuffer) noexcept {
382 return Extract(StormByte::ByteSize{0}, outBuffer);
383 }
384
390 virtual void ExtractUntilEoF(StormByte::Safe::Binary& outBuffer) noexcept = 0;
391
397 virtual void ExtractUntilEoF(WriteOnly& outBuffer) noexcept = 0;
398
402
407
416 virtual bool Peek(const StormByte::ByteSize& count, StormByte::Safe::Binary& outBuffer) const noexcept = 0;
417
425 virtual bool Peek(const StormByte::ByteSize& count, WriteOnly& outBuffer) const noexcept = 0;
426
430
435
442 virtual bool Read(const StormByte::ByteSize& count, StormByte::Safe::Binary& outBuffer) const noexcept = 0;
443
449 inline bool Read(StormByte::Safe::Binary& outBuffer) const noexcept {
450 return Read(StormByte::ByteSize{0}, outBuffer);
451 }
452
459 virtual bool Read(const StormByte::ByteSize& count, WriteOnly& outBuffer) const noexcept = 0;
460
466 inline bool Read(WriteOnly& outBuffer) const noexcept {
467 return Read(StormByte::ByteSize{0}, outBuffer);
468 }
469
475 virtual void ReadUntilEoF(StormByte::Safe::Binary& outBuffer) const noexcept = 0;
476
482 virtual void ReadUntilEoF(WriteOnly& outBuffer) const noexcept = 0;
483
487 };
488
498 public:
503
507 WriteOnly() noexcept;
508
513 WriteOnly(const WriteOnly& other) noexcept;
514
519 WriteOnly(WriteOnly&& other) noexcept;
520
524 virtual ~WriteOnly() noexcept;
525
531 WriteOnly& operator=(const WriteOnly& other) noexcept;
532
538 WriteOnly& operator=(WriteOnly&& other) noexcept;
539
543
548
553 virtual bool IsWritable() const noexcept = 0;
554
558 virtual void Close() noexcept = 0;
559
563 virtual void SetError() noexcept = 0;
564
568
573
581 virtual bool Write(const StormByte::ByteSize& count, const Safe::Binary& data) noexcept = 0;
582
590 virtual bool Write(const StormByte::ByteSize& count, Safe::Binary&& data) noexcept = 0;
591
599 virtual bool Write(const StormByte::ByteSize& count, const ReadOnly& data) noexcept = 0;
600
608 virtual bool Write(const StormByte::ByteSize& count, ReadOnly&& data) noexcept = 0;
609
613
618
624 inline bool Write(const ReadOnly& data) noexcept {
625 return Write(data.Available(), data);
626 }
627
633 inline bool Write(ReadOnly&& data) noexcept {
634 return Write(data.Available(), std::move(data));
635 }
636
640
645
651 bool Write(std::string_view sv) noexcept;
652
658 bool Write(const char* s) noexcept;
659
666 bool Write(const StormByte::ByteSize& count, std::string_view sv) noexcept;
667
674 bool Write(const StormByte::ByteSize& count, const char* s) noexcept;
675
682 template<std::size_t N>
683 bool Write(const char (&s)[N]) noexcept {
684 if (N == 0)
685 return Write(Safe::Binary{});
686 return Write(std::string_view(s, (N > 0) ? (N - 1) : 0));
687 }
688
692
697
704 template<Type::ByteInputRange R>
705 STORMBYTE_FORCE_INLINE bool Write(const R& r) noexcept {
706 Safe::Binary tmp;
707 if constexpr (requires { std::ranges::size(r); }) {
708 auto dist = std::ranges::size(r);
709 if (dist > 0)
710 tmp.reserve(StormByte::ByteSize{static_cast<std::size_t>(dist)});
711 }
712 std::transform(std::ranges::begin(r), std::ranges::end(r), std::back_inserter(tmp),
713 [](auto&& e) noexcept { return static_cast<std::byte>(e); });
714 return Write(tmp.size(), std::move(tmp));
715 }
716
724 template<Type::ByteInputRange Rw>
725 STORMBYTE_FORCE_INLINE bool Write(const StormByte::ByteSize& count, const Rw& r) noexcept {
726 if (count == StormByte::ByteSize{0})
727 return Write(r);
728 Safe::Binary tmp;
729 tmp.reserve(count);
730 auto it = std::ranges::begin(r);
731 auto end = std::ranges::end(r);
732 StormByte::ByteSize written{0};
733 for (; it != end && written < count; ++it, written = written + StormByte::ByteSize{1})
734 tmp.push_back(static_cast<std::byte>(*it));
735 return Write(written, std::move(tmp));
736 }
737
745 template<Type::ByteInputRange Rrw>
746 STORMBYTE_FORCE_INLINE bool Write(const StormByte::ByteSize& count, Rrw&& r) noexcept {
747 if (count == StormByte::ByteSize{0})
748 return Write(std::forward<Rrw>(r));
749 if constexpr (Type::SameAs<Rrw, Safe::Binary>) {
750 Safe::Binary tmp = std::move(r);
751 if (tmp.size() > count)
752 tmp.resize(count);
753 return Write(tmp.size(), std::move(tmp));
754 } else {
755 Safe::Binary tmp;
756 tmp.reserve(count);
757 auto it = std::ranges::begin(r);
758 auto end = std::ranges::end(r);
759 StormByte::ByteSize written{0};
760 for (; it != end && written < count; ++it, written = written + StormByte::ByteSize{1})
761 tmp.push_back(static_cast<std::byte>(*it));
762 return Write(written, std::move(tmp));
763 }
764 }
765
772 template<Type::ByteInputRange Rr>
773 STORMBYTE_FORCE_INLINE bool Write(Rr&& r) noexcept {
774 if constexpr (Type::SameAs<Rr, Safe::Binary>)
775 return Write(r.size(), std::move(r));
776 else {
777 Safe::Binary tmp;
778 if constexpr (requires { std::ranges::size(r); }) {
779 auto dist = std::ranges::size(r);
780 if (dist > 0)
781 tmp.reserve(StormByte::ByteSize{static_cast<std::size_t>(dist)});
782 }
783 std::transform(std::ranges::begin(r), std::ranges::end(r), std::back_inserter(tmp),
784 [](auto&& e) noexcept { return static_cast<std::byte>(e); });
785 return Write(tmp.size(), std::move(tmp));
786 }
787 }
788
797 template<Type::ByteInputIterator I, typename S>
799 STORMBYTE_FORCE_INLINE bool Write(I first, S last) noexcept {
800 Safe::Binary tmp;
801 std::transform(first, last, std::back_inserter(tmp),
802 [](auto&& e) noexcept { return static_cast<std::byte>(e); });
803 return Write(tmp.size(), std::move(tmp));
804 }
805
815 template<Type::ByteInputIterator I2, typename S2>
817 STORMBYTE_FORCE_INLINE bool Write(const StormByte::ByteSize& count, I2 first, S2 last) noexcept {
818 if (count == StormByte::ByteSize{0})
819 return Write(first, last);
820 Safe::Binary tmp;
821 StormByte::ByteSize written{0};
822 for (; first != last && written < count; ++first, written = written + StormByte::ByteSize{1})
823 tmp.push_back(static_cast<std::byte>(*first));
824 return Write(written, std::move(tmp));
825 }
826
830 };
831
839 public:
844
848 ReadWrite() noexcept;
849
854 ReadWrite(const ReadWrite& other) noexcept;
855
860 ReadWrite(ReadWrite&& other) noexcept;
861
865 virtual ~ReadWrite() noexcept;
866
872 ReadWrite& operator=(const ReadWrite& other) noexcept;
873
879 ReadWrite& operator=(ReadWrite&& other) noexcept;
880
884 };
885
887 extern template Safe::Binary STORMBYTE_BUFFER_PUBLIC Generic::DataConvert<Safe::Binary>(const Safe::Binary&) noexcept;
888 extern template Safe::Binary STORMBYTE_BUFFER_PUBLIC Generic::DataConvert<Safe::Binary>(Safe::Binary&&) noexcept;
889 extern template Safe::Binary STORMBYTE_BUFFER_PUBLIC Generic::DataConvert<std::span<const std::byte>>(const std::span<const std::byte>&) noexcept;
890 extern template Safe::Binary STORMBYTE_BUFFER_PUBLIC Generic::DataConvert<std::span<std::byte>>(const std::span<std::byte>&) noexcept;
891 extern template Safe::Binary STORMBYTE_BUFFER_PUBLIC Generic::DataConvert<std::span<std::byte>>(std::span<std::byte>&&) noexcept;
892
893 extern template bool STORMBYTE_BUFFER_PUBLIC WriteOnly::Write<Safe::Binary>(const Safe::Binary&) noexcept;
894 extern template bool STORMBYTE_BUFFER_PUBLIC WriteOnly::Write<Safe::Binary>(Safe::Binary&&) noexcept;
895 extern template bool STORMBYTE_BUFFER_PUBLIC WriteOnly::Write<Safe::Binary>(const StormByte::ByteSize&, const Safe::Binary&) noexcept;
896 extern template bool STORMBYTE_BUFFER_PUBLIC WriteOnly::Write<Safe::Binary>(const StormByte::ByteSize&, Safe::Binary&&) noexcept;
897 extern template bool STORMBYTE_BUFFER_PUBLIC WriteOnly::Write<std::span<const std::byte>>(const std::span<const std::byte>&) noexcept;
898 extern template bool STORMBYTE_BUFFER_PUBLIC WriteOnly::Write<std::span<const std::byte>>(const StormByte::ByteSize&, const std::span<const std::byte>&) noexcept;
899 extern template bool STORMBYTE_BUFFER_PUBLIC WriteOnly::Write<std::span<std::byte>>(const std::span<std::byte>&) noexcept;
900 extern template bool STORMBYTE_BUFFER_PUBLIC WriteOnly::Write<std::span<std::byte>>(const StormByte::ByteSize&, const std::span<std::byte>&) noexcept;
901 extern template bool STORMBYTE_BUFFER_PUBLIC WriteOnly::Write<Safe::Binary::const_iterator, Safe::Binary::const_iterator>(Safe::Binary::const_iterator, Safe::Binary::const_iterator) noexcept;
902 extern template bool STORMBYTE_BUFFER_PUBLIC WriteOnly::Write<Safe::Binary::iterator, Safe::Binary::iterator>(Safe::Binary::iterator, Safe::Binary::iterator) noexcept;
903 extern template bool STORMBYTE_BUFFER_PUBLIC WriteOnly::Write<Safe::Binary::const_iterator, Safe::Binary::const_iterator>(const StormByte::ByteSize&, Safe::Binary::const_iterator, Safe::Binary::const_iterator) noexcept;
904 extern template bool STORMBYTE_BUFFER_PUBLIC WriteOnly::Write<Safe::Binary::iterator, Safe::Binary::iterator>(const StormByte::ByteSize&, Safe::Binary::iterator, Safe::Binary::iterator) noexcept;
906 }
907}
908
915
922
929
Abstract root of the byte-buffer interfaces.
Definition generic.hxx:81
Generic() noexcept
Default constructor.
static STORMBYTE_FORCE_INLINE Safe::Binary DataConvert(Src &&src) noexcept
Convert an rvalue input range to StormByte::Safe::Binary.
Definition generic.hxx:175
static STORMBYTE_FORCE_INLINE Safe::Binary DataConvert(const Src &src) noexcept
Convert an lvalue input range to StormByte::Safe::Binary.
Definition generic.hxx:156
static Safe::Binary DataConvert(const char *s) noexcept
Convert a C string to StormByte::Safe::Binary.
virtual StormByte::ByteSize Size() const noexcept=0
Total number of bytes stored.
static Safe::Binary DataConvert(std::string_view sv) noexcept
Convert a string view to StormByte::Safe::Binary.
Buffer that can be read and not written.
Definition generic.hxx:220
virtual bool EoF() const noexcept=0
End of stream.
virtual bool IsReadable() const noexcept=0
Whether the buffer can still be read.
virtual void ExtractUntilEoF(WriteOnly &outBuffer) noexcept=0
Extract until EoF into a WriteOnly.
virtual const Safe::Binary & Data() const noexcept=0
View of internal storage.
virtual bool Empty() const noexcept=0
Whether the store holds no bytes.
virtual void Clean() noexcept=0
Drop bytes from the start up to the current read position.
virtual bool Extract(const StormByte::ByteSize &count, WriteOnly &outBuffer) noexcept=0
Extract bytes into a WriteOnly.
virtual bool Drop(const StormByte::ByteSize &count) noexcept=0
Discard count unread bytes.
virtual void ExtractUntilEoF(StormByte::Safe::Binary &outBuffer) noexcept=0
Extract until EoF into a StormByte::Safe::Binary.
virtual StormByte::ByteSize Available() const noexcept=0
Bytes available from the current read position.
virtual bool Peek(const StormByte::ByteSize &count, WriteOnly &outBuffer) const noexcept=0
Peek into a WriteOnly.
bool Read(StormByte::Safe::Binary &outBuffer) const noexcept
Read all available bytes into a StormByte::Safe::Binary.
Definition generic.hxx:449
bool Read(WriteOnly &outBuffer) const noexcept
Read all available bytes into a WriteOnly.
Definition generic.hxx:466
virtual bool Read(const StormByte::ByteSize &count, StormByte::Safe::Binary &outBuffer) const noexcept=0
Read into a StormByte::Safe::Binary.
virtual void Seek(const std::ptrdiff_t &offset, const Position &mode) const noexcept=0
Move the logical read position.
virtual bool Read(const StormByte::ByteSize &count, WriteOnly &outBuffer) const noexcept=0
Read into a WriteOnly.
virtual bool Peek(const StormByte::ByteSize &count, StormByte::Safe::Binary &outBuffer) const noexcept=0
Peek into a StormByte::Safe::Binary.
virtual void ReadUntilEoF(StormByte::Safe::Binary &outBuffer) const noexcept=0
Read until EoF into a StormByte::Safe::Binary.
ReadOnly() noexcept
Default constructor.
bool Extract(WriteOnly &outBuffer) noexcept
Extract all available bytes into a WriteOnly.
Definition generic.hxx:381
virtual void Clear() noexcept=0
Clear stored bytes and reset logical positions.
virtual bool Extract(const StormByte::ByteSize &count, StormByte::Safe::Binary &outBuffer) noexcept=0
Extract bytes into a StormByte::Safe::Binary.
virtual void ReadUntilEoF(WriteOnly &outBuffer) const noexcept=0
Read until EoF into a WriteOnly.
ReadWrite() noexcept
Default constructor.
Buffer that can be written and not read.
Definition generic.hxx:497
virtual void Close() noexcept=0
Stop further writes.
virtual bool IsWritable() const noexcept=0
Whether the buffer accepts writes.
bool Write(const char *s) noexcept
Write a C string.
virtual bool Write(const StormByte::ByteSize &count, const Safe::Binary &data) noexcept=0
Append bytes from a StormByte::Safe::Binary (copy).
STORMBYTE_FORCE_INLINE bool Write(const R &r) noexcept
Write all elements from an input range.
Definition generic.hxx:705
STORMBYTE_FORCE_INLINE bool Write(I first, S last) noexcept
Write all elements from an iterator pair.
Definition generic.hxx:799
bool Write(const char(&s)[N]) noexcept
Write a string literal without the trailing NUL.
Definition generic.hxx:683
bool Write(ReadOnly &&data) noexcept
Append all available bytes from a ReadOnly (extract).
Definition generic.hxx:633
virtual void SetError() noexcept=0
Permanent error.
bool Write(const StormByte::ByteSize &count, std::string_view sv) noexcept
Write up to count characters of a string view.
WriteOnly() noexcept
Default constructor.
STORMBYTE_FORCE_INLINE bool Write(const StormByte::ByteSize &count, Rrw &&r) noexcept
Write up to count elements from an rvalue range.
Definition generic.hxx:746
STORMBYTE_FORCE_INLINE bool Write(Rr &&r) noexcept
Write all elements from an rvalue range.
Definition generic.hxx:773
bool Write(const StormByte::ByteSize &count, const char *s) noexcept
Write up to count characters of a C string.
bool Write(std::string_view sv) noexcept
Write a string view.
STORMBYTE_FORCE_INLINE bool Write(const StormByte::ByteSize &count, I2 first, S2 last) noexcept
Write up to count elements from an iterator pair.
Definition generic.hxx:817
STORMBYTE_FORCE_INLINE bool Write(const StormByte::ByteSize &count, const Rw &r) noexcept
Write up to count elements from an input range.
Definition generic.hxx:725
void resize(const ByteSize &new_size)
void reserve(const ByteSize &new_cap)
void push_back(std::byte value)
ByteSize size() const noexcept
STORMBYTE_DECLARE_MAYBE_SAFE(class StormByte::System::Device::Access)
Buffer module of the StormByte suite.
Position
Forward declaration of the WriteOnly interface.
Definition typedefs.hxx:74
Root namespace of the StormByte C++ suite.
STL namespace.
#define STORMBYTE_FORCE_INLINE
#define STORMBYTE_BUFFER_PUBLIC
Definition visibility.h:54