OpenTTD Source 20250521-master-g82876c25e0
endian_buffer.hpp
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6 */
7
10#ifndef ENDIAN_BUFFER_HPP
11#define ENDIAN_BUFFER_HPP
12
13#include <string_view>
14#include "../core/bitmath_func.hpp"
15#include "../strings_type.h"
16
23template <typename Tcont = typename std::vector<uint8_t>, typename Titer = typename std::back_insert_iterator<Tcont>>
26 Titer buffer;
27
28public:
30 EndianBufferWriter(typename Titer::container_type &container) : buffer(std::back_inserter(container)) {}
31
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); }
36
37 template <typename... Targs>
38 EndianBufferWriter &operator <<(const std::tuple<Targs...> &data)
39 {
40 this->WriteTuple(data, std::index_sequence_for<Targs...>{});
41 return *this;
42 }
43
44 template <typename... Targs>
45 EndianBufferWriter &operator <<(const std::variant<Targs...> &variant)
46 {
47 this->WriteVariant(variant);
48 return *this;
49 }
50
51 EndianBufferWriter &operator <<(const std::monostate &)
52 {
53 return *this;
54 }
55
56 EndianBufferWriter &operator <<(const ConvertibleThroughBase auto data)
57 {
58 this->Write(data.base());
59 return *this;
60 }
61
62 template <class T> requires (!std::is_class_v<T>)
63 EndianBufferWriter &operator <<(const T data)
64 {
65 if constexpr (std::is_enum_v<T>) {
66 this->Write(to_underlying(data));
67 } else {
68 this->Write(data);
69 }
70 return *this;
71 }
72
73 template <typename Tvalue, typename Tbuf = std::vector<uint8_t>>
74 static Tbuf FromValue(const Tvalue &data)
75 {
76 Tbuf buffer;
77 EndianBufferWriter writer{ buffer };
78 writer << data;
79 return buffer;
80 }
81
82private:
84 template <class Ttuple, size_t... Tindices>
85 void WriteTuple(const Ttuple &values, std::index_sequence<Tindices...>)
86 {
87 ((*this << std::get<Tindices>(values)), ...);
88 }
89
90 template <typename T, std::size_t I = 0>
91 void WriteVariant(const T &variant )
92 {
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);
98 return;
99 }
100
101 WriteVariant<T, I + 1>(variant);
102 }
103 }
104
106 void Write(std::string_view value)
107 {
108 for (auto c : value) {
109 this->buffer++ = c;
110 }
111 this->buffer++ = '\0';
112 }
113
115 template <class T>
116 void Write(T value)
117 {
118 static_assert(sizeof(T) <= 8, "Value can't be larger than 8 bytes");
119
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);
126 }
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);
132 }
133 } else {
134 this->buffer++ = value;
135 }
136 }
137};
138
147 std::span<const uint8_t> buffer;
149 size_t read_pos = 0;
150
151public:
152 EndianBufferReader(std::span<const uint8_t> buffer) : buffer(buffer) {}
153
154 void rewind() { this->read_pos = 0; }
155
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; }
159
160 template <typename... Targs>
161 EndianBufferReader &operator >>(std::tuple<Targs...> &data)
162 {
163 this->ReadTuple(data, std::index_sequence_for<Targs...>{});
164 return *this;
165 }
166
167 template <typename... Targs>
168 EndianBufferReader &operator >>(std::variant<Targs...> &variant)
169 {
170 this->ReadVariant(this->Read<uint8_t>(), variant);
171 return *this;
172 }
173
174 EndianBufferReader &operator >>(const std::monostate &)
175 {
176 return *this;
177 }
178
179 template <ConvertibleThroughBase T>
180 EndianBufferReader &operator >>(T &data)
181 {
182 data = T{this->Read<typename T::BaseType>()};
183 return *this;
184 }
185
186 template <class T> requires (!std::is_class_v<T>)
187 EndianBufferReader &operator >>(T &data)
188 {
189 if constexpr (std::is_enum_v<T>) {
190 data = static_cast<T>(this->Read<std::underlying_type_t<T>>());
191 } else {
192 data = this->Read<T>();
193 }
194 return *this;
195 }
196
197 template <typename Tvalue>
198 static Tvalue ToValue(std::span<const uint8_t> buffer)
199 {
200 Tvalue result{};
201 EndianBufferReader reader{ buffer };
202 reader >> result;
203 return result;
204 }
205
206private:
208 template <class Ttuple, size_t... Tindices>
209 void ReadTuple(Ttuple &values, std::index_sequence<Tindices...>)
210 {
211 ((*this >> std::get<Tindices>(values)), ...);
212 }
213
214 template <typename T, std::size_t I = 0>
215 void ReadVariant(uint8_t index, T &variant)
216 {
217 if constexpr (I < std::variant_size_v<T>) {
218 if (I != index) {
219 ReadVariant<T, I + 1>(index, variant);
220 return;
221 }
222
223 std::variant_alternative_t<I, T> data;
224 *this >> data;
225 variant = data;
226 }
227 }
228
230 std::string ReadStr()
231 {
232 std::string str;
233 while (this->read_pos < this->buffer.size()) {
234 char ch = this->Read<char>();
235 if (ch == '\0') break;
236 str.push_back(ch);
237 }
238 return str;
239 }
240
242 template <class T>
244 {
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");
247
248 if (read_pos + sizeof(T) > this->buffer.size()) return {};
249
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;
253 }
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;
257 }
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;
263 }
264
265 return value;
266 }
267};
268
269#endif /* ENDIAN_BUFFER_HPP */
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)
Definition enum_type.hpp:17