comparison include/core/byte_stream.h @ 369:47c9f8502269

*: clang-format all the things I've edited the formatting a bit. Now pointer asterisks (and reference ampersands) are on the variable instead of the type, as well as having newlines for function braces (but nothing else)
author Paper <paper@tflc.us>
date Fri, 25 Jul 2025 10:16:02 -0400
parents 99c961c91809
children
comparison
equal deleted inserted replaced
368:6d37a998cf91 369:47c9f8502269
1 #ifndef MINORI_CORE_BYTE_STREAM_H_ 1 #ifndef MINORI_CORE_BYTE_STREAM_H_
2 #define MINORI_CORE_BYTE_STREAM_H_ 2 #define MINORI_CORE_BYTE_STREAM_H_
3 3
4 #include "core/endian.h" 4 #include "core/endian.h"
5 5
6 #include <cstdint>
7 #include <cstdlib>
6 #include <string> 8 #include <string>
9 #include <type_traits>
7 #include <vector> 10 #include <vector>
8 #include <cstdint>
9 #include <type_traits>
10 11
11 struct ByteStream { 12 struct ByteStream {
12 public: 13 public:
13 enum class ByteOrder { 14 enum class ByteOrder {
14 Little, 15 Little,
19 20
20 void ResetOffset(); 21 void ResetOffset();
21 void SetEndianness(ByteOrder endian); 22 void SetEndianness(ByteOrder endian);
22 23
23 template<typename T> 24 template<typename T>
24 bool ReadBinary(T& ret) { 25 bool ReadBinary(T &ret)
26 {
25 if (offset_ + sizeof(T) >= size_) 27 if (offset_ + sizeof(T) >= size_)
26 return false; 28 return false;
27 29
28 ret = *reinterpret_cast<T*>(bytes_ + offset_); 30 std::memcpy(&ret, bytes_ + offset_, sizeof(ret));
29 Advance(sizeof(T)); 31 Advance(sizeof(T));
30 return true; 32 return true;
31 } 33 }
32 34
33 template<typename T> 35 template<typename T>
34 bool ReadInt(T& ret) { 36 bool ReadInt(T &ret)
37 {
35 static_assert(std::is_integral<T>::value); 38 static_assert(std::is_integral<T>::value);
36 39
37 if (!ReadBinary<T>(ret)) 40 if (!ReadBinary<T>(ret))
38 return false; 41 return false;
39 42
58 } 61 }
59 62
60 return true; 63 return true;
61 } 64 }
62 65
63 bool ReadString(std::string& str, std::size_t size); 66 bool ReadString(std::string &str, std::size_t size);
64 bool Advance(std::size_t amount); 67 bool Advance(std::size_t amount);
65 68
66 private: 69 private:
67 /* raw data */ 70 /* raw data */
68 std::uint8_t *bytes_ = nullptr; 71 std::uint8_t *bytes_ = nullptr;