1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
|
#include "DataStream.hpp"
#include <bit>
#include <limits>
#include <utility>
static_assert(std::numeric_limits<float>::is_iec559, "Non IEE754/IEC559 'float' is not supported.");
static_assert(std::numeric_limits<double>::is_iec559, "Non IEE754/IEC559 'double' is not supported.");
static_assert(std::endian::native == std::endian::little || std::endian::native == std::endian::big, "Mixed endian is not supported.");
// Reading/writing signed integer byte-by-byte is fine, since the representation got fixed to 2's complements in C++20
static uint16_t ByteSwap(uint16_t n)
{
auto bytes = reinterpret_cast<std::byte*>(n);
std::swap(bytes[0], bytes[1]);
return n;
}
static uint32_t ByteSwap(uint32_t n)
{
#ifdef _MSC_VER
// TODO
#else
return __builtin_bswap32(n);
#endif
}
static uint64_t ByteSwap(uint64_t n)
{
#ifdef _MSC_VER
// TODO
#else
return __builtin_bswap64(n);
#endif
}
template <class TSigned>
static TSigned ByteSwap(TSigned n)
{
using Unsigned = std::make_unsigned_t<TSigned>;
auto swapped = ::ByteSwap(std::bit_cast<Unsigned>(n));
return std::bit_cast<TSigned>(swapped);
}
std::endian BaseDataStream::GetEndianness() const
{
return mEndian;
}
void BaseDataStream::SetEndianness(std::endian endianness)
{
mEndian = endianness;
}
InputDataStream::InputDataStream(std::fstream stream)
: mBackend{ std::move(stream) }
{
}
void InputDataStream::ReadBytes(size_t byteCount, std::byte* buffer)
{
mBackend.read(reinterpret_cast<char*>(buffer), static_cast<std::streamsize>(byteCount));
}
void InputDataStream::ReadBytes(size_t byteCount, char* buffer)
{
mBackend.read(reinterpret_cast<char*>(buffer), static_cast<std::streamsize>(byteCount));
}
void InputDataStream::ReadBytes(size_t byteCount, signed char* buffer)
{
mBackend.read(reinterpret_cast<char*>(buffer), static_cast<std::streamsize>(byteCount));
}
void InputDataStream::ReadBytes(size_t byteCount, unsigned char* buffer)
{
mBackend.read(reinterpret_cast<char*>(buffer), static_cast<std::streamsize>(byteCount));
}
void InputDataStream::Read(int8_t& n)
{
// sizeof() of a reference type yields the size of the reference
mBackend.read(reinterpret_cast<char*>(&n), sizeof(n));
}
void InputDataStream::Read(int16_t& n)
{
int16_t tmp;
mBackend.read(reinterpret_cast<char*>(&tmp), sizeof(tmp));
if (GetEndianness() != std::endian::native) {
n = ::ByteSwap(tmp);
} else {
n = tmp;
}
}
void InputDataStream::Read(int32_t& n)
{
int32_t tmp;
mBackend.read(reinterpret_cast<char*>(&tmp), sizeof(tmp));
if (GetEndianness() != std::endian::native) {
n = ::ByteSwap(tmp);
} else {
n = tmp;
}
}
void InputDataStream::Read(int64_t& n)
{
int64_t tmp;
mBackend.read(reinterpret_cast<char*>(&tmp), sizeof(tmp));
if (GetEndianness() != std::endian::native) {
n = ::ByteSwap(tmp);
} else {
n = tmp;
}
}
void InputDataStream::Read(uint8_t& n)
{
mBackend.read(reinterpret_cast<char*>(&n), sizeof(n));
}
void InputDataStream::Read(uint16_t& n)
{
uint16_t tmp;
mBackend.read(reinterpret_cast<char*>(&tmp), sizeof(tmp));
if (GetEndianness() != std::endian::native) {
n = ::ByteSwap(tmp);
} else {
n = tmp;
}
}
void InputDataStream::Read(uint32_t& n)
{
uint32_t tmp;
mBackend.read(reinterpret_cast<char*>(&tmp), sizeof(tmp));
if (GetEndianness() != std::endian::native) {
n = ::ByteSwap(tmp);
} else {
n = tmp;
}
}
void InputDataStream::Read(uint64_t& n)
{
uint64_t tmp;
mBackend.read(reinterpret_cast<char*>(&tmp), sizeof(tmp));
if (GetEndianness() != std::endian::native) {
n = ::ByteSwap(tmp);
} else {
n = tmp;
}
}
void InputDataStream::Read(float& n)
{
uint32_t buffer;
Read(buffer);
if (GetEndianness() != std::endian::native) {
buffer = ::ByteSwap(buffer);
}
n = std::bit_cast<float>(buffer);
}
void InputDataStream::Read(double& n)
{
uint64_t buffer;
Read(buffer);
if (GetEndianness() != std::endian::native) {
buffer = ::ByteSwap(buffer);
}
n = std::bit_cast<double>(buffer);
}
OutputDataStream::OutputDataStream(std::fstream stream)
: mBackend{ std::move(stream) }
{
}
void OutputDataStream::WriteBytes(size_t byteCount, const std::byte* buffer)
{
mBackend.write(reinterpret_cast<const char*>(buffer), static_cast<std::streamsize>(byteCount));
}
void OutputDataStream::WriteBytes(size_t byteCount, const char* buffer)
{
mBackend.write(reinterpret_cast<const char*>(buffer), static_cast<std::streamsize>(byteCount));
}
void OutputDataStream::WriteBytes(size_t byteCount, const signed char* buffer)
{
mBackend.write(reinterpret_cast<const char*>(buffer), static_cast<std::streamsize>(byteCount));
}
void OutputDataStream::WriteBytes(size_t byteCount, const unsigned char* buffer)
{
mBackend.write(reinterpret_cast<const char*>(buffer), static_cast<std::streamsize>(byteCount));
}
void OutputDataStream::Write(int8_t n)
{
mBackend.write(reinterpret_cast<const char*>(&n), sizeof(n));
}
void OutputDataStream::Write(int16_t n)
{
if (GetEndianness() != std::endian::native) {
n = ::ByteSwap(n);
}
mBackend.write(reinterpret_cast<const char*>(&n), sizeof(n));
}
void OutputDataStream::Write(int32_t n)
{
if (GetEndianness() != std::endian::native) {
n = ::ByteSwap(n);
}
mBackend.write(reinterpret_cast<const char*>(&n), sizeof(n));
}
void OutputDataStream::Write(int64_t n)
{
if (GetEndianness() != std::endian::native) {
n = ::ByteSwap(n);
}
mBackend.write(reinterpret_cast<const char*>(&n), sizeof(n));
}
void OutputDataStream::Write(uint8_t n)
{
mBackend.write(reinterpret_cast<const char*>(&n), sizeof(n));
}
void OutputDataStream::Write(uint16_t n)
{
if (GetEndianness() != std::endian::native) {
n = ::ByteSwap(n);
}
mBackend.write(reinterpret_cast<const char*>(&n), sizeof(n));
}
void OutputDataStream::Write(uint32_t n)
{
if (GetEndianness() != std::endian::native) {
n = ::ByteSwap(n);
}
mBackend.write(reinterpret_cast<const char*>(&n), sizeof(n));
}
void OutputDataStream::Write(uint64_t n)
{
if (GetEndianness() != std::endian::native) {
n = ::ByteSwap(n);
}
mBackend.write(reinterpret_cast<const char*>(&n), sizeof(n));
}
void OutputDataStream::Write(float n)
{
auto buffer = std::bit_cast<uint32_t>(n);
if (GetEndianness() != std::endian::native) {
buffer = ::ByteSwap(buffer);
}
mBackend.read(reinterpret_cast<char*>(&buffer), sizeof(buffer));
}
void OutputDataStream::Write(double n)
{
auto buffer = std::bit_cast<uint64_t>(n);
if (GetEndianness() != std::endian::native) {
buffer = ::ByteSwap(buffer);
}
mBackend.read(reinterpret_cast<char*>(&buffer), sizeof(buffer));
}
|