LkEngine 0.1.2
 
Loading...
Searching...
No Matches
StreamReader.h
1#pragma once
2
4#include "LkEngine/Core/Memory/Buffer.h"
5
6//#include <filesystem> /// REMOVE
7
8
9namespace LkEngine {
10
12 {
13 public:
14 virtual ~StreamReader() = default;
15
16 virtual bool IsStreamGood() const = 0;
17 virtual uint64_t GetStreamPosition() = 0;
18 virtual void SetStreamPosition(uint64_t position) = 0;
19 virtual bool ReadData(char* destination, size_t size) = 0;
20
21 void ReadBuffer(FBuffer& Buffer, uint32_t size = 0);
22 void ReadString(std::string& string);
23
24 operator bool() const { return IsStreamGood(); }
25
26 template<typename T>
27 void ReadRaw(T& type)
28 {
29 bool success = ReadData((char*)&type, sizeof(T));
30 LK_ASSERT(success);
31 }
32
33 template<typename T>
34 void ReadObject(T& obj)
35 {
36 T::Deserialize(this, obj);
37 }
38
39 template<typename Key, typename Value>
40 void ReadMap(std::map<Key, Value>& map, uint32_t size = 0)
41 {
42 if (size == 0)
43 {
44 ReadRaw<uint32_t>(size);
45 }
46
47 for (uint32_t i = 0; i < size; i++)
48 {
49 Key key;
50 if constexpr (std::is_trivial<Key>())
51 {
52 ReadRaw<Key>(key);
53 }
54 else
55 {
56 ReadObject<Key>(key);
57 }
58
59 if constexpr (std::is_trivial<Value>())
60 {
61 ReadRaw<Value>(map[key]);
62 }
63 else
64 {
65 ReadObject<Value>(map[key]);
66 }
67 }
68 }
69
70 template<typename Key, typename Value>
71 void ReadMap(std::unordered_map<Key, Value>& map, uint32_t size = 0)
72 {
73 if (size == 0)
74 ReadRaw<uint32_t>(size);
75
76 for (uint32_t i = 0; i < size; i++)
77 {
78 Key key;
79 if constexpr (std::is_trivial<Key>())
80 ReadRaw<Key>(key);
81 else
82 ReadObject<Key>(key);
83
84 if constexpr (std::is_trivial<Value>())
85 ReadRaw<Value>(map[key]);
86 else
87 ReadObject<Value>(map[key]);
88 }
89 }
90
91 template<typename Value>
92 void ReadMap(std::unordered_map<std::string, Value>& map, uint32_t size = 0)
93 {
94 if (size == 0)
95 ReadRaw<uint32_t>(size);
96
97 for (uint32_t i = 0; i < size; i++)
98 {
99 std::string key;
100 ReadString(key);
101
102 if constexpr (std::is_trivial<Value>())
103 ReadRaw<Value>(map[key]);
104 else
105 ReadObject<Value>(map[key]);
106 }
107 }
108
109 template<typename T>
110 void ReadArray(std::vector<T>& array, uint32_t size = 0)
111 {
112 if (size == 0)
113 ReadRaw<uint32_t>(size);
114
115 array.resize(size);
116
117 for (uint32_t i = 0; i < size; i++)
118 {
119 if constexpr (std::is_trivial<T>())
120 ReadRaw<T>(array[i]);
121 else
122 ReadObject<T>(array[i]);
123 }
124 }
125
126 };
127
128 template<>
129 inline void StreamReader::ReadArray(std::vector<std::string>& array, uint32_t size)
130 {
131 if (size == 0)
132 ReadRaw<uint32_t>(size);
133
134 array.resize(size);
135
136 for (uint32_t i = 0; i < size; i++)
137 ReadString(array[i]);
138 }
139
140}
Core header.
Definition StreamReader.h:12
Definition Asset.h:11
Definition Buffer.h:9