24class EndianBufferWriter {
32 EndianBufferWriter &operator <<(
const std::string &data) {
return *
this << std::string_view{ data }; }
33 EndianBufferWriter &operator <<(
const EncodedString &data) {
return *
this << data.
string; }
34 EndianBufferWriter &operator <<(std::string_view data) { this->
Write(data);
return *
this; }
35 EndianBufferWriter &operator <<(
bool data) {
return *this << static_cast<uint8_t>(data ? 1 : 0); }
37 template <
typename... Targs>
38 EndianBufferWriter &operator <<(
const std::tuple<Targs...> &data)
40 this->
WriteTuple(data, std::index_sequence_for<Targs...>{});
44 template <
typename... Targs>
45 EndianBufferWriter &operator <<(
const std::variant<Targs...> &variant)
47 this->WriteVariant(variant);
51 EndianBufferWriter &operator <<(
const std::monostate &)
56 EndianBufferWriter &operator <<(
const ConvertibleThroughBase
auto data)
58 this->
Write(data.base());
62 template <
class T>
requires (!std::is_class_v<T>)
63 EndianBufferWriter &
operator <<(
const T data)
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)
77 EndianBufferWriter writer{
buffer };
84 template <
class Ttuple,
size_t... Tindices>
85 void WriteTuple(
const Ttuple &values, std::index_sequence<Tindices...>)
87 ((*this << std::get<Tindices>(values)), ...);
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;
145class EndianBufferReader {
154 void rewind() { this->read_pos = 0; }
156 EndianBufferReader &operator >>(std::string &data) { data = this->
ReadStr();
return *
this; }
157 EndianBufferReader &operator >>(EncodedString &data) { data = EncodedString{this->
ReadStr()};
return *
this; }
158 EndianBufferReader &operator >>(
bool &data) { data = this->
Read<uint8_t>() != 0;
return *
this; }
160 template <
typename... Targs>
161 EndianBufferReader &operator >>(std::tuple<Targs...> &data)
163 this->
ReadTuple(data, std::index_sequence_for<Targs...>{});
167 template <
typename... Targs>
168 EndianBufferReader &operator >>(std::variant<Targs...> &variant)
174 EndianBufferReader &operator >>(
const std::monostate &)
179 template <ConvertibleThroughBase T>
180 EndianBufferReader &operator >>(T &data)
186 template <
class T>
requires (!std::is_class_v<T>)
187 EndianBufferReader &
operator >>(T &data)
189 if constexpr (std::is_enum_v<T>) {
197 template <
typename Tvalue>
198 static Tvalue ToValue(std::span<const uint8_t>
buffer)
201 EndianBufferReader reader{
buffer };
208 template <
class Ttuple,
size_t... Tindices>
209 void ReadTuple(Ttuple &values, std::index_sequence<Tindices...>)
211 ((*
this >> std::get<Tindices>(values)), ...);
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;
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;
static constexpr uint GB(const T x, const uint8_t s, const uint8_t n)
Fetch n bits from x, started at bit s.