annotate include/core/byte_stream.h @ 367:8d45d892be88 default tip

*: instead of pugixml, use Qt XML features this means we have one extra Qt dependency though...
author Paper <paper@tflc.us>
date Sun, 17 Nov 2024 22:55:47 -0500
parents 99c961c91809
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
364
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1 #ifndef MINORI_CORE_BYTE_STREAM_H_
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2 #define MINORI_CORE_BYTE_STREAM_H_
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
3
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
4 #include "core/endian.h"
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
5
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
6 #include <string>
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
7 #include <vector>
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
8 #include <cstdint>
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
9 #include <type_traits>
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
10
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
11 struct ByteStream {
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
12 public:
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
13 enum class ByteOrder {
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
14 Little,
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
15 Big,
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
16 };
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
17
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
18 ByteStream(std::uint8_t *bytes, std::size_t size);
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
19
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
20 void ResetOffset();
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
21 void SetEndianness(ByteOrder endian);
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
22
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
23 template<typename T>
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
24 bool ReadBinary(T& ret) {
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
25 if (offset_ + sizeof(T) >= size_)
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
26 return false;
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
27
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
28 ret = *reinterpret_cast<T*>(bytes_ + offset_);
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
29 Advance(sizeof(T));
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
30 return true;
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
31 }
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
32
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
33 template<typename T>
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
34 bool ReadInt(T& ret) {
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
35 static_assert(std::is_integral<T>::value);
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
36
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
37 if (!ReadBinary<T>(ret))
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
38 return false;
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
39
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
40 switch (endian_) {
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
41 case ByteOrder::Little:
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
42 if constexpr (std::is_unsigned<T>::value) {
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
43 ret = Endian::byteswap_little_to_host(ret);
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
44 } else {
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
45 ret = Endian::signed_byteswap_little_to_host(ret);
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
46 }
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
47 break;
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
48 case ByteOrder::Big:
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
49 if constexpr (std::is_unsigned<T>::value) {
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
50 ret = Endian::byteswap_big_to_host(ret);
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
51 } else {
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
52 ret = Endian::signed_byteswap_big_to_host(ret);
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
53 }
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
54 break;
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
55 default:
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
56 /* can't know for sure. punt */
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
57 return false;
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
58 }
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
59
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
60 return true;
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
61 }
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
62
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
63 bool ReadString(std::string& str, std::size_t size);
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
64 bool Advance(std::size_t amount);
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
65
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
66 private:
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
67 /* raw data */
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
68 std::uint8_t *bytes_ = nullptr;
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
69 std::size_t offset_ = 0;
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
70 std::size_t size_ = 0;
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
71
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
72 ByteOrder endian_ = ByteOrder::Little;
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
73 };
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
74
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
75 #endif /* MINORI_CORE_BYTE_STREAM_H_ */