10#ifndef ENDIAN_BUFFER_HPP
11#define ENDIAN_BUFFER_HPP
14#include "../core/bitmath_func.hpp"
15#include "../strings_type.h"
23template <
typename Tcont =
typename std::vector<u
int8_t>,
typename Titer =
typename std::back_insert_iterator<Tcont>>
32 EndianBufferWriter &operator <<(
const std::string &data) {
return *
this << std::string_view{ data }; }
35 EndianBufferWriter &operator <<(
bool data) {
return *this << static_cast<uint8_t>(data ? 1 : 0); }
37 template <
typename... Targs>
40 this->
WriteTuple(data, std::index_sequence_for<Targs...>{});
44 template <
typename... Targs>
47 this->WriteVariant(variant);
58 this->
Write(data.base());
62 template <
class T>
requires (!std::is_class_v<T>)
65 if constexpr (std::is_enum_v<T>) {
73 template <
typename Tvalue,
typename Tbuf = std::vector<u
int8_t>>
74 static Tbuf FromValue(
const Tvalue &data)
84 template <
class Ttuple,
size_t... Tindices>
85 void WriteTuple(
const Ttuple &values, std::index_sequence<Tindices...>)
87 ((*this << std::get<Tindices>(values)), ...);
85 void WriteTuple(
const Ttuple &values, std::index_sequence<Tindices...>) {
…}
90 template <
typename T, std::
size_t I = 0>
91 void WriteVariant(
const T &variant )
93 if constexpr (I < std::variant_size_v<T>) {
94 if (I == variant.index()) {
95 static_assert(std::variant_size_v<T> < std::numeric_limits<uint8_t>::max());
96 this->
Write(
static_cast<uint8_t
>(variant.index()));
97 *this << std::get<I>(variant);
101 WriteVariant<T, I + 1>(variant);
108 for (
auto c : value) {
111 this->buffer++ =
'\0';
118 static_assert(
sizeof(T) <= 8,
"Value can't be larger than 8 bytes");
120 if constexpr (
sizeof(T) > 1) {
121 this->buffer++ =
GB(value, 0, 8);
122 this->buffer++ =
GB(value, 8, 8);
123 if constexpr (
sizeof(T) > 2) {
124 this->buffer++ =
GB(value, 16, 8);
125 this->buffer++ =
GB(value, 24, 8);
127 if constexpr (
sizeof(T) > 4) {
128 this->buffer++ =
GB(value, 32, 8);
129 this->buffer++ =
GB(value, 40, 8);
130 this->buffer++ =
GB(value, 48, 8);
131 this->buffer++ =
GB(value, 56, 8);
134 this->buffer++ = value;
154 void rewind() { this->read_pos = 0; }
158 EndianBufferReader &operator >>(
bool &data) { data = this->Read<uint8_t>() != 0;
return *
this; }
160 template <
typename... Targs>
163 this->
ReadTuple(data, std::index_sequence_for<Targs...>{});
167 template <
typename... Targs>
170 this->ReadVariant(this->Read<uint8_t>(), variant);
179 template <ConvertibleThroughBase T>
182 data = T{this->Read<typename T::BaseType>()};
186 template <
class T>
requires (!std::is_class_v<T>)
189 if constexpr (std::is_enum_v<T>) {
190 data =
static_cast<T
>(this->Read<std::underlying_type_t<T>>());
192 data = this->Read<T>();
197 template <
typename Tvalue>
198 static Tvalue ToValue(std::span<const uint8_t>
buffer)
208 template <
class Ttuple,
size_t... Tindices>
209 void ReadTuple(Ttuple &values, std::index_sequence<Tindices...>)
211 ((*
this >> std::get<Tindices>(values)), ...);
209 void ReadTuple(Ttuple &values, std::index_sequence<Tindices...>) {
…}
214 template <
typename T, std::
size_t I = 0>
215 void ReadVariant(uint8_t index, T &variant)
217 if constexpr (I < std::variant_size_v<T>) {
219 ReadVariant<T, I + 1>(index, variant);
223 std::variant_alternative_t<I, T> data;
233 while (this->read_pos < this->
buffer.size()) {
234 char ch = this->Read<char>();
235 if (ch ==
'\0')
break;
245 static_assert(!std::is_const_v<T>,
"Can't read into const variables");
246 static_assert(
sizeof(T) <= 8,
"Value can't be larger than 8 bytes");
248 if (
read_pos +
sizeof(T) > this->buffer.size())
return {};
250 T value =
static_cast<T
>(this->buffer[this->read_pos++]);
251 if constexpr (
sizeof(T) > 1) {
252 value +=
static_cast<T
>(this->buffer[this->read_pos++]) << 8;
254 if constexpr (
sizeof(T) > 2) {
255 value +=
static_cast<T
>(this->buffer[this->read_pos++]) << 16;
256 value +=
static_cast<T
>(this->buffer[this->read_pos++]) << 24;
258 if constexpr (
sizeof(T) > 4) {
259 value +=
static_cast<T
>(this->buffer[this->read_pos++]) << 32;
260 value +=
static_cast<T
>(this->buffer[this->read_pos++]) << 40;
261 value +=
static_cast<T
>(this->buffer[this->read_pos++]) << 48;
262 value +=
static_cast<T
>(this->buffer[this->read_pos++]) << 56;
debug_inline static constexpr uint GB(const T x, const uint8_t s, const uint8_t n)
Fetch n bits from x, started at bit s.
Container for an encoded string, created by GetEncodedString.
std::string string
The encoded string.
Endian-aware buffer adapter that always reads values in little endian order.
size_t read_pos
Current read position.
std::span< const uint8_t > buffer
Reference to storage buffer.
void ReadTuple(Ttuple &values, std::index_sequence< Tindices... >)
Helper function to read a tuple from the buffer.
T Read()
Fundamental read function.
std::string ReadStr()
Read overload for string data.
Endian-aware buffer adapter that always writes values in little endian order.
void WriteTuple(const Ttuple &values, std::index_sequence< Tindices... >)
Helper function to write a tuple to the buffer.
Titer buffer
Output iterator for the destination buffer.
void Write(T value)
Fundamental write function.
void Write(std::string_view value)
Write overload for string values.
A type is considered 'convertible through base()' when it has a 'base()' function that returns someth...
constexpr std::underlying_type_t< enum_type > to_underlying(enum_type e)
Implementation of std::to_underlying (from C++23)