10#ifndef ENDIAN_BUFFER_HPP
11#define ENDIAN_BUFFER_HPP
14#include "../core/bitmath_func.hpp"
22template <
typename Tcont =
typename std::vector<u
int8_t>,
typename Titer =
typename std::back_insert_iterator<Tcont>>
31 EndianBufferWriter &operator <<(
const std::string &data) {
return *
this << std::string_view{ data }; }
32 EndianBufferWriter &operator <<(
const char *data) {
return *
this << std::string_view{ data }; }
34 EndianBufferWriter &operator <<(
bool data) {
return *this << static_cast<uint8_t>(data ? 1 : 0); }
36 template <
typename... Targs>
39 this->
WriteTuple(data, std::index_sequence_for<Targs...>{});
45 this->
Write(data.base());
49 template <
class T>
requires (!std::is_class_v<T>)
52 if constexpr (std::is_enum_v<T>) {
60 template <
typename Tvalue,
typename Tbuf = std::vector<u
int8_t>>
61 static Tbuf FromValue(
const Tvalue &data)
71 template <
class Ttuple,
size_t... Tindices>
72 void WriteTuple(
const Ttuple &values, std::index_sequence<Tindices...>)
74 ((*this << std::get<Tindices>(values)), ...);
78 void Write(std::string_view value)
80 for (
auto c : value) {
83 this->buffer++ =
'\0';
90 static_assert(
sizeof(T) <= 8,
"Value can't be larger than 8 bytes");
92 if constexpr (
sizeof(T) > 1) {
93 this->buffer++ =
GB(value, 0, 8);
94 this->buffer++ =
GB(value, 8, 8);
95 if constexpr (
sizeof(T) > 2) {
96 this->buffer++ =
GB(value, 16, 8);
97 this->buffer++ =
GB(value, 24, 8);
99 if constexpr (
sizeof(T) > 4) {
100 this->buffer++ =
GB(value, 32, 8);
101 this->buffer++ =
GB(value, 40, 8);
102 this->buffer++ =
GB(value, 48, 8);
103 this->buffer++ =
GB(value, 56, 8);
106 this->buffer++ = value;
126 void rewind() { this->read_pos = 0; }
129 EndianBufferReader &operator >>(
bool &data) { data = this->Read<uint8_t>() != 0;
return *
this; }
131 template <
typename... Targs>
134 this->
ReadTuple(data, std::index_sequence_for<Targs...>{});
138 template <ConvertibleThroughBase T>
141 data = T{this->Read<typename T::BaseType>()};
145 template <
class T>
requires (!std::is_class_v<T>)
148 if constexpr (std::is_enum_v<T>) {
149 data =
static_cast<T
>(this->Read<std::underlying_type_t<T>>());
151 data = this->Read<T>();
156 template <
typename Tvalue>
157 static Tvalue ToValue(std::span<const uint8_t>
buffer)
167 template <
class Ttuple,
size_t... Tindices>
168 void ReadTuple(Ttuple &values, std::index_sequence<Tindices...>)
170 ((*
this >> std::get<Tindices>(values)), ...);
177 while (this->read_pos < this->
buffer.size()) {
178 char ch = this->Read<char>();
179 if (ch ==
'\0')
break;
189 static_assert(!std::is_const_v<T>,
"Can't read into const variables");
190 static_assert(
sizeof(T) <= 8,
"Value can't be larger than 8 bytes");
192 if (
read_pos +
sizeof(T) > this->buffer.size())
return {};
194 T value =
static_cast<T
>(this->buffer[this->read_pos++]);
195 if constexpr (
sizeof(T) > 1) {
196 value +=
static_cast<T
>(this->buffer[this->read_pos++]) << 8;
198 if constexpr (
sizeof(T) > 2) {
199 value +=
static_cast<T
>(this->buffer[this->read_pos++]) << 16;
200 value +=
static_cast<T
>(this->buffer[this->read_pos++]) << 24;
202 if constexpr (
sizeof(T) > 4) {
203 value +=
static_cast<T
>(this->buffer[this->read_pos++]) << 32;
204 value +=
static_cast<T
>(this->buffer[this->read_pos++]) << 40;
205 value +=
static_cast<T
>(this->buffer[this->read_pos++]) << 48;
206 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.
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)