GREM 3.12.0
Game/Rendering Engine Modules for C++
Loading...
Searching...
No Matches
Reader.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Ivar Härnqvist
2// SPDX-License-Identifier: MIT
3
4#ifndef GREM_CORE_DATA_READER_HPP
5#define GREM_CORE_DATA_READER_HPP
6
8
18
19#include <istream> // std::istream, std::streamsize
20#include <stdexcept> // std::length_error
21#include <streambuf> // std::streambuf
22#include <utility> // std::exchange
23
24namespace grem {
25
26namespace detail {
27
28template <typename Self>
29class ReaderBase {
30public:
31 [[nodiscard]] GREM_ALWAYS_INLINE Allocation<byte> readBytesIntoAllocation(size_t maxLength = Limits<size_t>::MAX) {
32 return readBytesInto<Allocation<byte>>(maxLength);
33 }
34
35 [[nodiscard]] GREM_ALWAYS_INLINE ArrayList<byte> readBytesIntoArrayList(size_t maxLength = Limits<size_t>::MAX) {
36 return readBytesInto<ArrayList<byte>>(maxLength);
37 }
38
39 [[nodiscard]] GREM_ALWAYS_INLINE Buffer<byte> readBytesIntoBuffer(size_t maxLength = Limits<size_t>::MAX) {
40 return readBytesInto<Buffer<byte>>(maxLength);
41 }
42
43 [[nodiscard]] GREM_ALWAYS_INLINE String readBytesIntoString(size_t maxLength = Limits<size_t>::MAX) {
44 return readBytesInto<String>(maxLength);
45 }
46
47 [[nodiscard]] GREM_ALWAYS_INLINE Optional<char> peekChar() {
48 if (const Optional<byte> result = peekByte()) {
49 return bit_cast<char>(*result);
50 }
51 return {};
52 }
53
54 [[nodiscard]] GREM_ALWAYS_INLINE char readChar() {
55 return bit_cast<char>(readByte());
56 }
57
58 [[nodiscard]] GREM_ALWAYS_INLINE Optional<char> tryReadChar() {
59 if (const Optional<byte> result = tryReadByte()) {
60 return bit_cast<char>(*result);
61 }
62 return {};
63 }
64
65 template <size_t N>
66 [[nodiscard]] GREM_ALWAYS_INLINE Array<byte, N> readBytes() {
67 if constexpr (N == 1) {
68 return {readByte()};
69 } else {
70 Array<byte, N> data{};
71 read(data);
72 return data;
73 }
74 }
75
76 template <size_t N>
77 [[nodiscard]] GREM_ALWAYS_INLINE Optional<Array<byte, N>> tryReadBytes() {
78 if constexpr (N == 1) {
79 if (const Optional<byte> result = tryReadByte()) {
80 return Array<byte, 1>{*result};
81 }
82 return {};
83 } else {
84 Array<byte, N> data{};
85 if (!tryRead(data)) {
86 return {};
87 }
88 return data;
89 }
90 }
91
92 template <size_t N>
93 [[nodiscard]] GREM_ALWAYS_INLINE Array<char, N> readChars() {
94 Array<byte, N> data{};
95 read(data);
96 return bit_cast<Array<char, N>>(data);
97 }
98
99 template <size_t N>
100 [[nodiscard]] GREM_ALWAYS_INLINE Optional<Array<char, N>> tryReadChars() {
101 Array<byte, N> data{};
102 if (!tryRead(data)) {
103 return {};
104 }
105 return bit_cast<Array<char, N>>(data);
106 }
107
108 [[nodiscard]] String readLine(size_t maxLength = Limits<size_t>::MAX) {
109 String result{};
110 while (result.size() < maxLength) {
111 const Optional<char> ch = tryReadChar();
112 if (!ch || *ch == '\n') {
113 break;
114 }
115 if (*ch == '\r') {
116 if (peekChar() == '\n') {
117 skipByte();
118 }
119 break;
120 }
121 result.push_back(*ch);
122 }
123 return result;
124 }
125
126 [[nodiscard]] String readNullTerminatedString(size_t maxLength = Limits<size_t>::MAX) {
127 String result{};
128 while (result.size() < maxLength) {
129 const Optional<char> ch = tryReadChar();
130 if (!ch || *ch == '\0') {
131 break;
132 }
133 result.push_back(*ch);
134 }
135 return result;
136 }
137
138 [[nodiscard]] GREM_ALWAYS_INLINE int8_t peekInt8() {
139 if (const Optional<byte> result = peekByte()) {
140 return bit_cast<int8_t>(*result);
141 }
142 return {};
143 }
144
145 [[nodiscard]] GREM_ALWAYS_INLINE int8_t readInt8() {
146 return bit_cast<int8_t>(readByte());
147 }
148
149 [[nodiscard]] GREM_ALWAYS_INLINE Optional<int8_t> tryReadInt8() {
150 if (const Optional<byte> result = tryReadByte()) {
151 return bit_cast<int8_t>(*result);
152 }
153 return {};
154 }
155
156 [[nodiscard]] GREM_ALWAYS_INLINE uint8_t peekUInt8() {
157 if (const Optional<byte> result = peekByte()) {
158 return bit_cast<uint8_t>(*result);
159 }
160 return {};
161 }
162
163 [[nodiscard]] GREM_ALWAYS_INLINE uint8_t readUInt8() {
164 return bit_cast<uint8_t>(readByte());
165 }
166
167 [[nodiscard]] GREM_ALWAYS_INLINE Optional<uint8_t> tryReadUInt8() {
168 if (const Optional<byte> result = tryReadByte()) {
169 return bit_cast<uint8_t>(*result);
170 }
171 return {};
172 }
173
174 [[nodiscard]] GREM_ALWAYS_INLINE int16_t readInt16HE() {
175 Array<byte, 2> data{};
176 read(data);
177 return bit_cast<int16_t>(data);
178 }
179
180 [[nodiscard]] GREM_ALWAYS_INLINE Optional<int16_t> tryReadInt16HE() {
181 Array<byte, 2> data{};
182 if (!tryRead(data)) {
183 return {};
184 }
185 return bit_cast<int16_t>(data);
186 }
187
188 [[nodiscard]] GREM_ALWAYS_INLINE int16_t readInt16LE() {
189 Array<byte, 2> data{};
190 read(data);
191 return convertLittleEndianToHostEndian(bit_cast<int16_t>(data));
192 }
193
194 [[nodiscard]] GREM_ALWAYS_INLINE Optional<int16_t> tryReadInt16LE() {
195 Array<byte, 2> data{};
196 if (!tryRead(data)) {
197 return {};
198 }
199 return convertLittleEndianToHostEndian(bit_cast<int16_t>(data));
200 }
201
202 [[nodiscard]] GREM_ALWAYS_INLINE int16_t readInt16BE() {
203 Array<byte, 2> data{};
204 read(data);
205 return convertBigEndianToHostEndian(bit_cast<int16_t>(data));
206 }
207
208 [[nodiscard]] GREM_ALWAYS_INLINE Optional<int16_t> tryReadInt16BE() {
209 Array<byte, 2> data{};
210 if (!tryRead(data)) {
211 return {};
212 }
213 return convertBigEndianToHostEndian(bit_cast<int16_t>(data));
214 }
215
216 [[nodiscard]] GREM_ALWAYS_INLINE uint16_t readUInt16HE() {
217 Array<byte, 2> data{};
218 read(data);
219 return bit_cast<uint16_t>(data);
220 }
221
222 [[nodiscard]] GREM_ALWAYS_INLINE Optional<uint16_t> tryReadUInt16HE() {
223 Array<byte, 2> data{};
224 if (!tryRead(data)) {
225 return {};
226 }
227 return bit_cast<uint16_t>(data);
228 }
229
230 [[nodiscard]] GREM_ALWAYS_INLINE uint16_t readUInt16LE() {
231 Array<byte, 2> data{};
232 read(data);
233 return convertLittleEndianToHostEndian(bit_cast<uint16_t>(data));
234 }
235
236 [[nodiscard]] GREM_ALWAYS_INLINE Optional<uint16_t> tryReadUInt16LE() {
237 Array<byte, 2> data{};
238 if (!tryRead(data)) {
239 return {};
240 }
241 return convertLittleEndianToHostEndian(bit_cast<uint16_t>(data));
242 }
243
244 [[nodiscard]] GREM_ALWAYS_INLINE uint16_t readUInt16BE() {
245 Array<byte, 2> data{};
246 read(data);
247 return convertBigEndianToHostEndian(bit_cast<uint16_t>(data));
248 }
249
250 [[nodiscard]] GREM_ALWAYS_INLINE Optional<uint16_t> tryReadUInt16BE() {
251 Array<byte, 2> data{};
252 if (!tryRead(data)) {
253 return {};
254 }
255 return convertBigEndianToHostEndian(bit_cast<uint16_t>(data));
256 }
257
258 [[nodiscard]] GREM_ALWAYS_INLINE int32_t readInt32HE() {
259 Array<byte, 4> data{};
260 read(data);
261 return bit_cast<int32_t>(data);
262 }
263
264 [[nodiscard]] GREM_ALWAYS_INLINE Optional<int32_t> tryReadInt32HE() {
265 Array<byte, 4> data{};
266 if (!tryRead(data)) {
267 return {};
268 }
269 return bit_cast<int32_t>(data);
270 }
271
272 [[nodiscard]] GREM_ALWAYS_INLINE int32_t readInt32LE() {
273 Array<byte, 4> data{};
274 read(data);
275 return convertLittleEndianToHostEndian(bit_cast<int32_t>(data));
276 }
277
278 [[nodiscard]] GREM_ALWAYS_INLINE Optional<int32_t> tryReadInt32LE() {
279 Array<byte, 4> data{};
280 if (!tryRead(data)) {
281 return {};
282 }
283 return convertLittleEndianToHostEndian(bit_cast<int32_t>(data));
284 }
285
286 [[nodiscard]] GREM_ALWAYS_INLINE int32_t readInt32BE() {
287 Array<byte, 4> data{};
288 read(data);
289 return convertBigEndianToHostEndian(bit_cast<int32_t>(data));
290 }
291
292 [[nodiscard]] GREM_ALWAYS_INLINE Optional<int32_t> tryReadInt32BE() {
293 Array<byte, 4> data{};
294 if (!tryRead(data)) {
295 return {};
296 }
297 return convertBigEndianToHostEndian(bit_cast<int32_t>(data));
298 }
299
300 [[nodiscard]] GREM_ALWAYS_INLINE uint32_t readUInt32HE() {
301 Array<byte, 4> data{};
302 read(data);
303 return bit_cast<uint32_t>(data);
304 }
305
306 [[nodiscard]] GREM_ALWAYS_INLINE Optional<uint32_t> tryReadUInt32HE() {
307 Array<byte, 4> data{};
308 if (!tryRead(data)) {
309 return {};
310 }
311 return bit_cast<uint32_t>(data);
312 }
313
314 [[nodiscard]] GREM_ALWAYS_INLINE uint32_t readUInt32LE() {
315 Array<byte, 4> data{};
316 read(data);
317 return convertLittleEndianToHostEndian(bit_cast<uint32_t>(data));
318 }
319
320 [[nodiscard]] GREM_ALWAYS_INLINE Optional<uint32_t> tryReadUInt32LE() {
321 Array<byte, 4> data{};
322 if (!tryRead(data)) {
323 return {};
324 }
325 return convertLittleEndianToHostEndian(bit_cast<uint32_t>(data));
326 }
327
328 [[nodiscard]] GREM_ALWAYS_INLINE uint32_t readUInt32BE() {
329 Array<byte, 4> data{};
330 read(data);
331 return convertBigEndianToHostEndian(bit_cast<uint32_t>(data));
332 }
333
334 [[nodiscard]] GREM_ALWAYS_INLINE Optional<uint32_t> tryReadUInt32BE() {
335 Array<byte, 4> data{};
336 if (!tryRead(data)) {
337 return {};
338 }
339 return convertBigEndianToHostEndian(bit_cast<uint32_t>(data));
340 }
341
342 [[nodiscard]] GREM_ALWAYS_INLINE int64_t readInt64HE() {
343 Array<byte, 8> data{};
344 read(data);
345 return bit_cast<int64_t>(data);
346 }
347
348 [[nodiscard]] GREM_ALWAYS_INLINE Optional<int64_t> tryReadInt64HE() {
349 Array<byte, 8> data{};
350 if (!tryRead(data)) {
351 return {};
352 }
353 return bit_cast<int64_t>(data);
354 }
355
356 [[nodiscard]] GREM_ALWAYS_INLINE int64_t readInt64LE() {
357 Array<byte, 8> data{};
358 read(data);
359 return convertLittleEndianToHostEndian(bit_cast<int64_t>(data));
360 }
361
362 [[nodiscard]] GREM_ALWAYS_INLINE Optional<int64_t> tryReadInt64LE() {
363 Array<byte, 8> data{};
364 if (!tryRead(data)) {
365 return {};
366 }
367 return convertLittleEndianToHostEndian(bit_cast<int64_t>(data));
368 }
369
370 [[nodiscard]] GREM_ALWAYS_INLINE int64_t readInt64BE() {
371 Array<byte, 8> data{};
372 read(data);
373 return convertBigEndianToHostEndian(bit_cast<int64_t>(data));
374 }
375
376 [[nodiscard]] GREM_ALWAYS_INLINE Optional<int64_t> tryReadInt64BE() {
377 Array<byte, 8> data{};
378 if (!tryRead(data)) {
379 return {};
380 }
381 return convertBigEndianToHostEndian(bit_cast<int64_t>(data));
382 }
383
384 [[nodiscard]] GREM_ALWAYS_INLINE uint64_t readUInt64HE() {
385 Array<byte, 8> data{};
386 read(data);
387 return bit_cast<uint64_t>(data);
388 }
389
390 [[nodiscard]] GREM_ALWAYS_INLINE Optional<uint64_t> tryReadUInt64HE() {
391 Array<byte, 8> data{};
392 if (!tryRead(data)) {
393 return {};
394 }
395 return bit_cast<uint64_t>(data);
396 }
397
398 [[nodiscard]] GREM_ALWAYS_INLINE uint64_t readUInt64LE() {
399 Array<byte, 8> data{};
400 read(data);
401 return convertLittleEndianToHostEndian(bit_cast<uint64_t>(data));
402 }
403
404 [[nodiscard]] GREM_ALWAYS_INLINE Optional<uint64_t> tryReadUInt64LE() {
405 Array<byte, 8> data{};
406 if (!tryRead(data)) {
407 return {};
408 }
409 return convertLittleEndianToHostEndian(bit_cast<uint64_t>(data));
410 }
411
412 [[nodiscard]] GREM_ALWAYS_INLINE uint64_t readUInt64BE() {
413 Array<byte, 8> data{};
414 read(data);
415 return convertBigEndianToHostEndian(bit_cast<uint64_t>(data));
416 }
417
418 [[nodiscard]] GREM_ALWAYS_INLINE Optional<uint64_t> tryReadUInt64BE() {
419 Array<byte, 8> data{};
420 if (!tryRead(data)) {
421 return {};
422 }
423 return convertBigEndianToHostEndian(bit_cast<uint64_t>(data));
424 }
425
426 [[nodiscard]] int64_t readIntLEB128() {
427 uint64_t result = 0;
428 int shift = 0;
429 uint8_t byte{};
430 do {
431 byte = readUInt8();
432 result |= static_cast<uint64_t>(byte & 0b01111111) << shift;
433 shift += 7;
434 } while (shift < 64 && (byte & 0b10000000) != 0);
435 if (shift < 64 && (byte & 0b01000000) != 0) {
436 result |= ~uint64_t{0} << shift;
437 }
438 return bit_cast<int64_t>(result);
439 }
440
441 [[nodiscard]] Optional<int64_t> tryReadIntLEB128() {
442 uint64_t result = 0;
443 int shift = 0;
444 uint8_t byte{};
445 do {
446 const Optional<uint8_t> nextByte = tryReadUInt8();
447 if (!nextByte) {
448 return {};
449 }
450 byte = *nextByte;
451 result |= static_cast<uint64_t>(byte & 0b01111111) << shift;
452 shift += 7;
453 } while (shift < 64 && (byte & 0b10000000) != 0);
454 if (shift < 64 && (byte & 0b01000000) != 0) {
455 result |= ~uint64_t{0} << shift;
456 }
457 return bit_cast<int64_t>(result);
458 }
459
460 [[nodiscard]] uint64_t readUIntLEB128() {
461 uint64_t result = 0;
462 int shift = 0;
463 uint8_t byte{};
464 do {
465 byte = readUInt8();
466 result |= static_cast<uint64_t>(byte & 0b01111111) << shift;
467 shift += 7;
468 } while (shift < 64 && (byte & 0b10000000) != 0);
469 return result;
470 }
471
472 [[nodiscard]] Optional<uint64_t> tryReadUIntLEB128() {
473 uint64_t result = 0;
474 int shift = 0;
475 uint8_t byte{};
476 do {
477 const Optional<uint8_t> nextByte = tryReadUInt8();
478 if (!nextByte) {
479 return {};
480 }
481 byte = *nextByte;
482 result |= static_cast<uint64_t>(byte & 0b01111111) << shift;
483 shift += 7;
484 } while (shift < 64 && (byte & 0b10000000) != 0);
485 return result;
486 }
487
488 [[nodiscard]] GREM_ALWAYS_INLINE float16_t readFloat16HE() {
489 static_assert(Limits<float16_t>::IS_IEC60559);
490 Array<byte, 2> data{};
491 read(data);
492 return bit_cast<float16_t>(data);
493 }
494
495 [[nodiscard]] GREM_ALWAYS_INLINE Optional<float16_t> tryReadFloat16HE() {
496 static_assert(Limits<float16_t>::IS_IEC60559);
497 Array<byte, 2> data{};
498 if (!tryRead(data)) {
499 return {};
500 }
501 return bit_cast<float16_t>(data);
502 }
503
504 [[nodiscard]] GREM_ALWAYS_INLINE float16_t readFloat16LE() {
505 static_assert(Limits<float16_t>::IS_IEC60559);
506 Array<byte, 2> data{};
507 read(data);
508 return convertLittleEndianToHostEndian(bit_cast<float16_t>(data));
509 }
510
511 [[nodiscard]] GREM_ALWAYS_INLINE Optional<float16_t> tryReadFloat16LE() {
512 static_assert(Limits<float16_t>::IS_IEC60559);
513 Array<byte, 2> data{};
514 if (!tryRead(data)) {
515 return {};
516 }
517 return convertLittleEndianToHostEndian(bit_cast<float16_t>(data));
518 }
519
520 [[nodiscard]] GREM_ALWAYS_INLINE float16_t readFloat16BE() {
521 static_assert(Limits<float16_t>::IS_IEC60559);
522 Array<byte, 2> data{};
523 read(data);
524 return convertBigEndianToHostEndian(bit_cast<float16_t>(data));
525 }
526
527 [[nodiscard]] GREM_ALWAYS_INLINE Optional<float16_t> tryReadFloat16BE() {
528 static_assert(Limits<float16_t>::IS_IEC60559);
529 Array<byte, 2> data{};
530 if (!tryRead(data)) {
531 return {};
532 }
533 return convertBigEndianToHostEndian(bit_cast<float16_t>(data));
534 }
535
536 [[nodiscard]] GREM_ALWAYS_INLINE float32_t readFloat32HE() {
537 static_assert(Limits<float32_t>::IS_IEC60559);
538 Array<byte, 4> data{};
539 read(data);
540 return bit_cast<float32_t>(data);
541 }
542
543 [[nodiscard]] GREM_ALWAYS_INLINE Optional<float32_t> tryReadFloat32HE() {
544 static_assert(Limits<float32_t>::IS_IEC60559);
545 Array<byte, 4> data{};
546 if (!tryRead(data)) {
547 return {};
548 }
549 return bit_cast<float32_t>(data);
550 }
551
552 [[nodiscard]] GREM_ALWAYS_INLINE float32_t readFloat32LE() {
553 static_assert(Limits<float32_t>::IS_IEC60559);
554 Array<byte, 4> data{};
555 read(data);
556 return convertLittleEndianToHostEndian(bit_cast<float32_t>(data));
557 }
558
559 [[nodiscard]] GREM_ALWAYS_INLINE Optional<float32_t> tryReadFloat32LE() {
560 static_assert(Limits<float32_t>::IS_IEC60559);
561 Array<byte, 4> data{};
562 if (!tryRead(data)) {
563 return {};
564 }
565 return convertLittleEndianToHostEndian(bit_cast<float32_t>(data));
566 }
567
568 [[nodiscard]] GREM_ALWAYS_INLINE float32_t readFloat32BE() {
569 static_assert(Limits<float32_t>::IS_IEC60559);
570 Array<byte, 4> data{};
571 read(data);
572 return convertBigEndianToHostEndian(bit_cast<float32_t>(data));
573 }
574
575 [[nodiscard]] GREM_ALWAYS_INLINE Optional<float32_t> tryReadFloat32BE() {
576 static_assert(Limits<float32_t>::IS_IEC60559);
577 Array<byte, 4> data{};
578 if (!tryRead(data)) {
579 return {};
580 }
581 return convertBigEndianToHostEndian(bit_cast<float32_t>(data));
582 }
583
584 [[nodiscard]] GREM_ALWAYS_INLINE Optional<float64_t> tryReadFloat64HE() {
585 static_assert(Limits<float64_t>::IS_IEC60559);
586 Array<byte, 8> data{};
587 if (!tryRead(data)) {
588 return {};
589 }
590 return bit_cast<float64_t>(data);
591 }
592
593 [[nodiscard]] GREM_ALWAYS_INLINE float64_t readFloat64LE() {
594 static_assert(Limits<float64_t>::IS_IEC60559);
595 Array<byte, 8> data{};
596 read(data);
597 return convertLittleEndianToHostEndian(bit_cast<float64_t>(data));
598 }
599
600 [[nodiscard]] GREM_ALWAYS_INLINE Optional<float64_t> tryReadFloat64LE() {
601 static_assert(Limits<float64_t>::IS_IEC60559);
602 Array<byte, 8> data{};
603 if (!tryRead(data)) {
604 return {};
605 }
606 return convertLittleEndianToHostEndian(bit_cast<float64_t>(data));
607 }
608
609 [[nodiscard]] GREM_ALWAYS_INLINE float64_t readFloat64BE() {
610 static_assert(Limits<float64_t>::IS_IEC60559);
611 Array<byte, 8> data{};
612 read(data);
613 return convertBigEndianToHostEndian(bit_cast<float64_t>(data));
614 }
615
616 [[nodiscard]] GREM_ALWAYS_INLINE Optional<float64_t> tryReadFloat64BE() {
617 static_assert(Limits<float64_t>::IS_IEC60559);
618 Array<byte, 8> data{};
619 if (!tryRead(data)) {
620 return {};
621 }
622 return convertBigEndianToHostEndian(bit_cast<float64_t>(data));
623 }
624
625private:
626 template <typename Container>
627 [[nodiscard]] GREM_ALWAYS_INLINE Container readBytesInto(size_t maxLength) {
628 return static_cast<Self*>(this)->template readBytesInto<Container>(maxLength);
629 }
630
631 GREM_ALWAYS_INLINE void read(Span<byte> data) {
632 static_cast<Self*>(this)->read(data);
633 }
634
635 [[nodiscard]] GREM_ALWAYS_INLINE bool tryRead(Span<byte> data) {
636 return static_cast<Self*>(this)->tryRead(data);
637 }
638
639 [[nodiscard]] GREM_ALWAYS_INLINE Optional<byte> peekByte() {
640 return static_cast<Self*>(this)->peekByte();
641 }
642
643 [[nodiscard]] GREM_ALWAYS_INLINE byte readByte() {
644 return static_cast<Self*>(this)->readByte();
645 }
646
647 GREM_ALWAYS_INLINE void skipByte() {
648 static_cast<Self*>(this)->skipByte();
649 }
650
651 [[nodiscard]] GREM_ALWAYS_INLINE Optional<byte> tryReadByte() {
652 return static_cast<Self*>(this)->tryReadByte();
653 }
654};
655
656} // namespace detail
657
658class Reader : private detail::ReaderBase<Reader> {
659public:
660 GREM_ALWAYS_INLINE Reader(void* context, size_t (*readSomeImplementation)(void* context, Span<byte> data)) noexcept
661 : context(context)
662 , readSomeImplementation(readSomeImplementation) {}
663
665 : Reader(&input, [](void* context, Span<byte> data) -> size_t {
666 Span<const byte>& input = *static_cast<Span<const byte>*>(context);
667 const size_t bytesRead = min(input.size(), data.size());
668 if (bytesRead > 0) {
669 memcpy(data.data(), input.data(), bytesRead);
670 input = input.subspan(bytesRead);
671 }
672 return bytesRead;
673 }) {}
674
675 GREM_ALWAYS_INLINE Reader(std::streambuf& input) noexcept
676 : Reader(&input, [](void* context, Span<byte> data) -> size_t {
677 std::streambuf& input = *static_cast<std::streambuf*>(context);
678 size_t totalBytesRead = 0;
679 while (!data.empty()) {
680 const size_t size = min(data.size(), size_t{Limits<std::streamsize>::MAX});
681 const std::streamsize bytesRead = input.sgetn(reinterpret_cast<char*>(data.data()), static_cast<std::streamsize>(size));
682 if (bytesRead <= 0) {
683 break;
684 }
685 totalBytesRead += static_cast<size_t>(bytesRead);
686 data = data.subspan(static_cast<size_t>(bytesRead));
687 }
688 return totalBytesRead;
689 }) {}
690
691 GREM_ALWAYS_INLINE Reader(std::istream& input)
692 : Reader(*input.rdbuf()) {}
693
694 ~Reader() = default;
695
696 Reader(const Reader&) = delete;
697 Reader& operator=(const Reader&) = delete;
698
700 : context(std::exchange(other.context, nullptr))
701 , readSomeImplementation(std::exchange(other.readSomeImplementation, nullptr))
702 , writeOffset(std::exchange(other.writeOffset, size_t{0}))
703 , readOffset(std::exchange(other.readOffset, size_t{0})) {
704 if (readOffset < writeOffset) {
705 memcpy(buffer.data(), other.buffer.data() + readOffset, writeOffset - readOffset);
706 }
707 }
708
710 if (this == &other) {
711 return *this;
712 }
713 context = std::exchange(other.context, nullptr);
714 readSomeImplementation = std::exchange(other.readSomeImplementation, nullptr);
715 writeOffset = std::exchange(other.writeOffset, size_t{0});
716 readOffset = std::exchange(other.readOffset, size_t{0});
717 if (readOffset < writeOffset) {
718 memcpy(buffer.data(), other.buffer.data() + readOffset, writeOffset - readOffset);
719 }
720 return *this;
721 }
722
723 [[nodiscard]] bool eof() {
724 if (readOffset < writeOffset) {
725 return false;
726 }
727 writeOffset = readSomeImplementation(context, buffer);
728 readOffset = 0;
729 return writeOffset != 0;
730 }
731
732 [[nodiscard]] size_t readSome(Span<byte> data) {
733 if (readOffset == writeOffset) {
734 writeOffset = readSomeImplementation(context, buffer);
735 readOffset = 0;
736 }
737 const size_t bytesRead = min(writeOffset - readOffset, data.size());
738 if (bytesRead > 0) {
739 memcpy(data.data(), buffer.data() + readOffset, bytesRead);
740 readOffset += bytesRead;
741 }
742 return bytesRead;
743 }
744
745 [[nodiscard]] size_t readUntilEOF(Span<byte> data) {
746 size_t result = 0;
747 while (!data.empty()) {
748 const size_t bytesRead = readSome(data);
749 if (bytesRead == 0) {
750 break;
751 }
752 data = data.subspan(bytesRead);
753 result += bytesRead;
754 }
755 return result;
756 }
757
758 [[nodiscard]] size_t skipSome(size_t maxLength) {
759 if (readOffset == writeOffset) {
760 writeOffset = readSomeImplementation(context, buffer);
761 readOffset = 0;
762 }
763 const size_t bytesSkipped = min(writeOffset - readOffset, maxLength);
764 readOffset += bytesSkipped;
765 return bytesSkipped;
766 }
767
768 size_t skipUntilEOF(size_t maxLength) {
769 size_t result = 0;
770 while (maxLength > 0) {
771 const size_t bytesRead = skipSome(maxLength);
772 if (bytesRead == 0) {
773 break;
774 }
775 maxLength -= bytesRead;
776 result += bytesRead;
777 }
778 return result;
779 }
780
781 void read(Span<byte> data) {
782 if (!tryRead(data)) {
783 throw std::length_error{"Unexpected end of data."};
784 }
785 }
786
787 void skip(size_t n) {
788 if (!trySkip(n)) {
789 throw std::length_error{"Unexpected end of data."};
790 }
791 }
792
793 [[nodiscard]] bool tryRead(Span<byte> data) {
794 return readUntilEOF(data) == data.size();
795 }
796
797 [[nodiscard]] bool trySkip(size_t n) {
798 while (n > 0) {
799 const size_t bytesSkipped = skipSome(n);
800 if (bytesSkipped == 0) {
801 return false;
802 }
803 n -= bytesSkipped;
804 }
805 return true;
806 }
807
808 [[nodiscard]] Optional<byte> peekByte() {
809 if (readOffset < writeOffset) {
810 return buffer[readOffset];
811 }
812 writeOffset = readSomeImplementation(context, buffer);
813 readOffset = 0;
814 if (writeOffset == 0) {
815 return {};
816 }
817 return buffer[0];
818 }
819
820 [[nodiscard]] byte readByte() {
821 if (const Optional<byte> result = tryReadByte()) {
822 return *result;
823 }
824 throw std::length_error{"Unexpected end of input."};
825 }
826
827 void skipByte() {
828 if (!trySkipByte()) {
829 throw std::length_error{"Unexpected end of input."};
830 }
831 }
832
833 [[nodiscard]] Optional<byte> tryReadByte() {
834 if (const Optional<byte> result = peekByte()) {
835 ++readOffset;
836 return result;
837 }
838 return {};
839 }
840
841 [[nodiscard]] bool trySkipByte() {
842 return tryReadByte().has_value();
843 }
844
845 using ReaderBase::peekChar;
846 using ReaderBase::peekInt8;
847 using ReaderBase::peekUInt8;
848 using ReaderBase::readBytes;
849 using ReaderBase::readBytesIntoAllocation;
850 using ReaderBase::readBytesIntoArrayList;
851 using ReaderBase::readBytesIntoBuffer;
852 using ReaderBase::readBytesIntoString;
853 using ReaderBase::readChar;
854 using ReaderBase::readChars;
855 using ReaderBase::readFloat16BE;
856 using ReaderBase::readFloat16HE;
857 using ReaderBase::readFloat16LE;
858 using ReaderBase::readFloat32BE;
859 using ReaderBase::readFloat32HE;
860 using ReaderBase::readFloat32LE;
861 using ReaderBase::readFloat64BE;
862 using ReaderBase::readFloat64LE;
863 using ReaderBase::readInt16BE;
864 using ReaderBase::readInt16HE;
865 using ReaderBase::readInt16LE;
866 using ReaderBase::readInt32BE;
867 using ReaderBase::readInt32HE;
868 using ReaderBase::readInt32LE;
869 using ReaderBase::readInt64BE;
870 using ReaderBase::readInt64HE;
871 using ReaderBase::readInt64LE;
872 using ReaderBase::readInt8;
873 using ReaderBase::readIntLEB128;
874 using ReaderBase::readLine;
875 using ReaderBase::readNullTerminatedString;
876 using ReaderBase::readUInt16BE;
877 using ReaderBase::readUInt16HE;
878 using ReaderBase::readUInt16LE;
879 using ReaderBase::readUInt32BE;
880 using ReaderBase::readUInt32HE;
881 using ReaderBase::readUInt32LE;
882 using ReaderBase::readUInt64BE;
883 using ReaderBase::readUInt64HE;
884 using ReaderBase::readUInt64LE;
885 using ReaderBase::readUInt8;
886 using ReaderBase::readUIntLEB128;
887 using ReaderBase::tryReadBytes;
888 using ReaderBase::tryReadChar;
889 using ReaderBase::tryReadChars;
890 using ReaderBase::tryReadFloat16BE;
891 using ReaderBase::tryReadFloat16HE;
892 using ReaderBase::tryReadFloat16LE;
893 using ReaderBase::tryReadFloat32BE;
894 using ReaderBase::tryReadFloat32HE;
895 using ReaderBase::tryReadFloat32LE;
896 using ReaderBase::tryReadFloat64BE;
897 using ReaderBase::tryReadFloat64HE;
898 using ReaderBase::tryReadFloat64LE;
899 using ReaderBase::tryReadInt16BE;
900 using ReaderBase::tryReadInt16HE;
901 using ReaderBase::tryReadInt16LE;
902 using ReaderBase::tryReadInt32BE;
903 using ReaderBase::tryReadInt32HE;
904 using ReaderBase::tryReadInt32LE;
905 using ReaderBase::tryReadInt64BE;
906 using ReaderBase::tryReadInt64HE;
907 using ReaderBase::tryReadInt64LE;
908 using ReaderBase::tryReadInt8;
909 using ReaderBase::tryReadIntLEB128;
910 using ReaderBase::tryReadUInt16BE;
911 using ReaderBase::tryReadUInt16HE;
912 using ReaderBase::tryReadUInt16LE;
913 using ReaderBase::tryReadUInt32BE;
914 using ReaderBase::tryReadUInt32HE;
915 using ReaderBase::tryReadUInt32LE;
916 using ReaderBase::tryReadUInt64BE;
917 using ReaderBase::tryReadUInt64HE;
918 using ReaderBase::tryReadUInt64LE;
919 using ReaderBase::tryReadUInt8;
920 using ReaderBase::tryReadUIntLEB128;
921
922private:
923 friend ReaderBase;
924
925 template <typename Container>
926 [[nodiscard]] Container readBytesInto(size_t maxLength) {
927 static_assert(sizeof(typename Container::value_type) == sizeof(byte));
928
929 Container result{};
930 result.resize(min(buffer.size(), maxLength));
931 size_t length = readSome(asWritableBytes(Span{result}));
932 if (length > 0) {
933 while (length < maxLength) {
934 result.resize(min(length + buffer.size(), maxLength));
935 const size_t bytesRead = readSome(asWritableBytes(Span{result}).subspan(length));
936 if (bytesRead == 0) {
937 break;
938 }
939 length += bytesRead;
940 }
941 }
942 result.resize(length);
943 return result;
944 }
945
946 void* context;
947 size_t (*readSomeImplementation)(void* context, Span<byte> data);
948 size_t writeOffset = 0;
949 size_t readOffset = 0;
950 Array<byte, 1024> buffer;
951};
952
953class SpanReader : private detail::ReaderBase<SpanReader> {
954public:
956 : input(&input) {}
957
958 [[nodiscard]] GREM_ALWAYS_INLINE operator Reader() const {
959 return Reader{*input};
960 }
961
962 [[nodiscard]] GREM_ALWAYS_INLINE bool eof() {
963 return input->empty();
964 }
965
966 [[nodiscard]] size_t readSome(Span<byte> data) {
967 return readUntilEOF(data);
968 }
969
970 [[nodiscard]] size_t readUntilEOF(Span<byte> data) {
971 const size_t bytesRead = min(data.size(), input->size());
972 if (bytesRead > 0) {
973 memcpy(data.data(), input->data(), bytesRead);
974 *input = input->subspan(bytesRead);
975 }
976 return bytesRead;
977 }
978
979 [[nodiscard]] size_t skipSome(size_t maxLength) {
980 return skipUntilEOF(maxLength);
981 }
982
983 [[nodiscard]] size_t skipUntilEOF(size_t maxLength) {
984 const size_t bytesSkipped = min(maxLength, input->size());
985 *input = input->subspan(bytesSkipped);
986 return bytesSkipped;
987 }
988
989 [[nodiscard]] Span<const byte> peek(size_t n) const noexcept {
990 return input->first(min(n, input->size()));
991 }
992
993 void read(Span<byte> data) {
994 if (!tryRead(data)) {
995 throw std::length_error{"Unexpected end of data."};
996 }
997 }
998
999 void skip(size_t n) {
1000 if (!trySkip(n)) {
1001 throw std::length_error{"Unexpected end of data."};
1002 }
1003 }
1004
1005 [[nodiscard]] bool tryRead(Span<byte> data) {
1006 return readUntilEOF(data) == data.size();
1007 }
1008
1009 [[nodiscard]] bool trySkip(size_t n) {
1010 if (n > 0) {
1011 if (n > input->size()) {
1012 return false;
1013 }
1014 *input = input->subspan(n);
1015 }
1016 return true;
1017 }
1018
1019 [[nodiscard]] Optional<byte> peekByte() {
1020 if (input->empty()) {
1021 return {};
1022 }
1023 return input->front();
1024 }
1025
1026 [[nodiscard]] byte readByte() {
1027 if (const Optional<byte> result = tryReadByte()) {
1028 return *result;
1029 }
1030 throw std::length_error{"Unexpected end of input."};
1031 }
1032
1033 void skipByte() {
1034 if (!trySkipByte()) {
1035 throw std::length_error{"Unexpected end of data."};
1036 }
1037 }
1038
1039 [[nodiscard]] Optional<byte> tryReadByte() {
1040 if (const Optional<byte> result = peekByte()) {
1041 *input = input->subspan(1);
1042 return result;
1043 }
1044 return {};
1045 }
1046
1047 [[nodiscard]] bool trySkipByte() {
1048 if (input->empty()) {
1049 return false;
1050 }
1051 *input = input->subspan(1);
1052 return true;
1053 }
1054
1055 using ReaderBase::peekChar;
1056 using ReaderBase::peekInt8;
1057 using ReaderBase::peekUInt8;
1058 using ReaderBase::readBytes;
1059 using ReaderBase::readBytesIntoAllocation;
1060 using ReaderBase::readBytesIntoArrayList;
1061 using ReaderBase::readBytesIntoBuffer;
1062 using ReaderBase::readBytesIntoString;
1063 using ReaderBase::readChar;
1064 using ReaderBase::readChars;
1065 using ReaderBase::readFloat16BE;
1066 using ReaderBase::readFloat16HE;
1067 using ReaderBase::readFloat16LE;
1068 using ReaderBase::readFloat32BE;
1069 using ReaderBase::readFloat32HE;
1070 using ReaderBase::readFloat32LE;
1071 using ReaderBase::readFloat64BE;
1072 using ReaderBase::readFloat64LE;
1073 using ReaderBase::readInt16BE;
1074 using ReaderBase::readInt16HE;
1075 using ReaderBase::readInt16LE;
1076 using ReaderBase::readInt32BE;
1077 using ReaderBase::readInt32HE;
1078 using ReaderBase::readInt32LE;
1079 using ReaderBase::readInt64BE;
1080 using ReaderBase::readInt64HE;
1081 using ReaderBase::readInt64LE;
1082 using ReaderBase::readInt8;
1083 using ReaderBase::readIntLEB128;
1084 using ReaderBase::readLine;
1085 using ReaderBase::readNullTerminatedString;
1086 using ReaderBase::readUInt16BE;
1087 using ReaderBase::readUInt16HE;
1088 using ReaderBase::readUInt16LE;
1089 using ReaderBase::readUInt32BE;
1090 using ReaderBase::readUInt32HE;
1091 using ReaderBase::readUInt32LE;
1092 using ReaderBase::readUInt64BE;
1093 using ReaderBase::readUInt64HE;
1094 using ReaderBase::readUInt64LE;
1095 using ReaderBase::readUInt8;
1096 using ReaderBase::readUIntLEB128;
1097 using ReaderBase::tryReadBytes;
1098 using ReaderBase::tryReadChar;
1099 using ReaderBase::tryReadChars;
1100 using ReaderBase::tryReadFloat16BE;
1101 using ReaderBase::tryReadFloat16HE;
1102 using ReaderBase::tryReadFloat16LE;
1103 using ReaderBase::tryReadFloat32BE;
1104 using ReaderBase::tryReadFloat32HE;
1105 using ReaderBase::tryReadFloat32LE;
1106 using ReaderBase::tryReadFloat64BE;
1107 using ReaderBase::tryReadFloat64HE;
1108 using ReaderBase::tryReadFloat64LE;
1109 using ReaderBase::tryReadInt16BE;
1110 using ReaderBase::tryReadInt16HE;
1111 using ReaderBase::tryReadInt16LE;
1112 using ReaderBase::tryReadInt32BE;
1113 using ReaderBase::tryReadInt32HE;
1114 using ReaderBase::tryReadInt32LE;
1115 using ReaderBase::tryReadInt64BE;
1116 using ReaderBase::tryReadInt64HE;
1117 using ReaderBase::tryReadInt64LE;
1118 using ReaderBase::tryReadInt8;
1119 using ReaderBase::tryReadIntLEB128;
1120 using ReaderBase::tryReadUInt16BE;
1121 using ReaderBase::tryReadUInt16HE;
1122 using ReaderBase::tryReadUInt16LE;
1123 using ReaderBase::tryReadUInt32BE;
1124 using ReaderBase::tryReadUInt32HE;
1125 using ReaderBase::tryReadUInt32LE;
1126 using ReaderBase::tryReadUInt64BE;
1127 using ReaderBase::tryReadUInt64HE;
1128 using ReaderBase::tryReadUInt64LE;
1129 using ReaderBase::tryReadUInt8;
1130 using ReaderBase::tryReadUIntLEB128;
1131
1132private:
1133 friend ReaderBase;
1134
1135 template <typename Container>
1136 [[nodiscard]] Container readBytesInto(size_t maxLength) {
1137 static_assert(sizeof(typename Container::value_type) == sizeof(byte));
1138
1139 Container result{};
1140 if (!input->empty()) {
1141 result.resize(min(input->size(), maxLength));
1142 memcpy(result.data(), input->data(), result.size());
1143 *input = {};
1144 }
1145 return result;
1146 }
1147
1148 Span<const byte>* input;
1149};
1150
1151} // namespace grem
1152
1153#endif
#define GREM_ALWAYS_INLINE
Definition attributes.hpp:10
Optional value type that holds either a value or no value.
Definition Optional.hpp:58
Definition Reader.hpp:658
void skipByte()
Definition Reader.hpp:827
Reader & operator=(const Reader &)=delete
bool trySkipByte()
Definition Reader.hpp:841
GREM_ALWAYS_INLINE Reader(std::istream &input)
Definition Reader.hpp:691
GREM_ALWAYS_INLINE Reader(std::streambuf &input) noexcept
Definition Reader.hpp:675
byte readByte()
Definition Reader.hpp:820
size_t skipUntilEOF(size_t maxLength)
Definition Reader.hpp:768
GREM_ALWAYS_INLINE Reader(void *context, size_t(*readSomeImplementation)(void *context, Span< byte > data)) noexcept
Definition Reader.hpp:660
size_t skipSome(size_t maxLength)
Definition Reader.hpp:758
size_t readUntilEOF(Span< byte > data)
Definition Reader.hpp:745
bool eof()
Definition Reader.hpp:723
bool tryRead(Span< byte > data)
Definition Reader.hpp:793
~Reader()=default
Reader(const Reader &)=delete
void skip(size_t n)
Definition Reader.hpp:787
Optional< byte > peekByte()
Definition Reader.hpp:808
Optional< byte > tryReadByte()
Definition Reader.hpp:833
GREM_ALWAYS_INLINE Reader & operator=(Reader &&other) noexcept
Definition Reader.hpp:709
size_t readSome(Span< byte > data)
Definition Reader.hpp:732
GREM_ALWAYS_INLINE Reader(Reader &&other) noexcept
Definition Reader.hpp:699
bool trySkip(size_t n)
Definition Reader.hpp:797
GREM_ALWAYS_INLINE Reader(Span< const byte > &input) noexcept
Definition Reader.hpp:664
void read(Span< byte > data)
Definition Reader.hpp:781
size_t skipSome(size_t maxLength)
Definition Reader.hpp:979
void skip(size_t n)
Definition Reader.hpp:999
size_t readSome(Span< byte > data)
Definition Reader.hpp:966
Span< const byte > peek(size_t n) const noexcept
Definition Reader.hpp:989
bool trySkipByte()
Definition Reader.hpp:1047
void skipByte()
Definition Reader.hpp:1033
bool tryRead(Span< byte > data)
Definition Reader.hpp:1005
GREM_ALWAYS_INLINE bool eof()
Definition Reader.hpp:962
Optional< byte > tryReadByte()
Definition Reader.hpp:1039
GREM_ALWAYS_INLINE SpanReader(Span< const byte > &input) noexcept
Definition Reader.hpp:955
void read(Span< byte > data)
Definition Reader.hpp:993
size_t readUntilEOF(Span< byte > data)
Definition Reader.hpp:970
Optional< byte > peekByte()
Definition Reader.hpp:1019
bool trySkip(size_t n)
Definition Reader.hpp:1009
byte readByte()
Definition Reader.hpp:1026
size_t skipUntilEOF(size_t maxLength)
Definition Reader.hpp:983
Definition Span.hpp:146
constexpr bool empty() const noexcept
Definition Span.hpp:257
constexpr detail::subspan_type_t< T, EXTENT, Offset, N > subspan() const
Definition Span.hpp:284
constexpr size_type size() const noexcept
Definition Span.hpp:207
constexpr pointer data() const noexcept
Definition Span.hpp:203
@ N
'n'
Definition keyboard.hpp:84
Definition aliases.hpp:9
auto asWritableBytes(Span< T, N > s) noexcept
Definition Span.hpp:368
Array(T, Us...) -> Array< T, 1+sizeof...(Us)>
float float32_t
Definition fundamentals.hpp:155
GREM_ALWAYS_INLINE constexpr T convertBigEndianToHostEndian(T value)
Convert a value in big-endian byte order to the corresponding value in host-endian byte order.
Definition fundamentals.hpp:523
double float64_t
Definition fundamentals.hpp:157
Span(R &&range) -> Span< std::remove_reference_t< decltype(*std::data(range))> >
StringBase< char, std::char_traits< char >, std::allocator< char > > String
Definition String.hpp:17
GREM_ALWAYS_INLINE constexpr T convertLittleEndianToHostEndian(T value)
Convert a value in little-endian byte order to the corresponding value in host-endian byte order.
Definition fundamentals.hpp:542
GREM_ALWAYS_INLINE constexpr decltype(auto) min(A &&a, B &&b)
Get the smaller of two values, or the first value if they are equivalent.
Definition fundamentals.hpp:585
GREM_ALWAYS_INLINE constexpr T GREM_VECTORCALL length(T a)
Definition math.hpp:2001
constexpr size_type size() const noexcept
Definition Array.hpp:139
static constexpr bool IS_IEC60559
Definition fundamentals.hpp:307
static constexpr T MAX
Definition fundamentals.hpp:29