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