Cogs.Core
MemoryBuffer.h
1#pragma once
2
3#include "Allocator.h"
4
5#include "../StringView.h"
6
7#include <glm/ext/quaternion_float.hpp>
8#include <glm/ext/matrix_float4x4.hpp>
9#include <glm/ext/vector_float3.hpp>
10#include <glm/ext/vector_float4.hpp>
11
12#include <string>
13#include <utility>
14
15namespace Cogs
16{
17 namespace Memory
18 {
20 COGSFOUNDATION_API std::string MemToHexString(const void* memory, size_t size);
21
22 class COGSFOUNDATION_API MemoryBuffer
23 {
24 public:
25 enum class Anchor
26 {
27 Start, // Seek position is relative to the start of the data.
28 Current, // Seek position is relative to the current read/write position.
29 End // Seek position is relative to the end of the data.
30 };
31
32 MemoryBuffer() = default;
33
34 MemoryBuffer(MemBlockType type) : type(type) {}
35
36 MemoryBuffer(size_t size, MemBlockType type) : type(type) { resize(size, false, false); }
37
38 MemoryBuffer(size_t size, bool onlyReserve = false, MemBlockType type = MemBlockType::Block)
39 : type(type)
40 {
41 if (onlyReserve) {
42 reserve(size);
43 }
44 else {
45 resize(size, false, false);
46 }
47 }
48
49 MemoryBuffer(Allocator * allocator, MemBlockType type = MemBlockType::Block)
50 : allocator(allocator), type(type)
51 {}
52
53 MemoryBuffer(size_t size, Allocator * allocator, MemBlockType type = MemBlockType::Block)
54 : allocator(allocator), type(type)
55 {
56 resize(size, false, false);
57 }
58
59 MemoryBuffer(const void* memory, size_t size);
60
61 MemoryBuffer(MemoryBuffer && other) noexcept
62 {
63 buffer = other.buffer;
64 currentSize = other.currentSize;
65 storageSize = other.storageSize;
66 currentPos = other.currentPos;
67 allocator = other.allocator;
68 type = other.type;
69
70 other.buffer = nullptr;
71 other.currentSize = 0;
72 other.storageSize = 0;
73 other.currentPos = 0;
74 other.type = MemBlockType::Block;
75 }
76
77 MemoryBuffer & operator=(MemoryBuffer && other) noexcept
78 {
79 release();
80
81 buffer = other.buffer;
82 currentSize = other.currentSize;
83 storageSize = other.storageSize;
84 currentPos = other.currentPos;
85 allocator = other.allocator;
86 type = other.type;
87
88 other.buffer = nullptr;
89 other.currentSize = 0;
90 other.storageSize = 0;
91 other.currentPos = 0;
92 other.type = MemBlockType::Block;
93 return *this;
94 }
95
96 MemoryBuffer(const MemoryBuffer &) = delete;
97 MemoryBuffer & operator=(const MemoryBuffer &) = delete;
98
100 {
101 release();
102 }
103
104 void changeType(MemBlockType newType);
105
106 void reset(size_t size, Allocator * allocator);
107
108 bool resize(size_t size, bool keep = true, bool forceRealloc = false);
109 bool reserve(size_t size);
110
111 void copy(const MemoryBuffer & other);
112
113 void clear() { resize(0, false, true); }
114
115 void * data() { return buffer; }
116 const void * data() const { return buffer; }
117
118 void * dataFromPos() { return static_cast<uint8_t*>(buffer) + currentPos; }
119 const void * dataFromPos() const { return static_cast<const uint8_t*>(buffer) + currentPos; }
120
121 size_t size() const { return currentSize; }
122 bool empty() const { return currentSize == 0; }
123
126 void* grow(size_t bytes) {
127 if (storageSize < (currentSize + bytes)) {
128 if (!reserve(std::max<size_t>(128, ((storageSize + bytes) * 3) / 2))) {
129 return nullptr;
130 }
131 }
132 void* rv = static_cast<uint8_t*>(buffer) + currentSize;
133 currentSize += bytes;
134 return rv;
135 }
136
137 void swap(MemoryBuffer & other);
138
139 const Allocator * getAllocator() const { return allocator; }
140
141 size_t read(void* buffer, size_t noofbytes);
142 uint8_t read8() { return readImpl<uint8_t>(); }
143 uint16_t read16() { return readImpl<uint16_t>(); }
144 uint32_t read32() { return readImpl<uint32_t>(); }
145 uint64_t read64() { return readImpl<uint64_t>(); }
146 float readFloat() { return readImpl<float>(); }
147 double readDouble() { return readImpl<double>(); }
148 glm::vec2 readFloat2();
149 glm::vec3 readFloat3();
150 glm::vec4 readFloat4();
151 glm::quat readQuaternion();
152 glm::mat4 readMatrix();
153 std::string readString();
154
155 bool write(const void* buffer, size_t noofbytes);
156 bool write8(uint8_t value) { return write(&value, sizeof(value)); }
157 bool write16(uint16_t value) { return write(&value, sizeof(value)); }
158 bool write32(uint32_t value) { return write(&value, sizeof(value)); }
159 bool write64(uint64_t value) { return write(&value, sizeof(value)); }
160 bool writeFloat(float value) { return write(&value, sizeof(value)); }
161 bool writeDouble(double value) { return write(&value, sizeof(value)); }
162 bool writeFloat2(const glm::vec2& value);
163 bool writeFloat3(const glm::vec3& value);
164 bool writeFloat4(const glm::vec4& value);
165 bool writeQuaternion(const glm::quat& value);
166 bool writeMatrix(const glm::mat4& value);
167 bool writeString(const StringView& str);
168
169 void seek(int64_t offset, Anchor from);
170
171 size_t pos() const { return currentPos; }
172 size_t unreadSize() const { return size() - pos(); }
173
174 bool areContentsEqual(const MemoryBuffer& rhs) const;
175
176 private:
177 void * buffer = nullptr;
178 size_t currentSize = 0;
179 size_t storageSize = 0;
180 size_t currentPos = 0;
181 Allocator * allocator = Allocator::defaultAllocator();
182 MemBlockType type = MemBlockType::Block;
183
184 void release();
185
186 template<typename T> T readImpl() {
187 T value = {};
188
189 read(&value, sizeof(value));
190 return value;
191 }
192 };
193
194 template<typename T>
196 {
197 public:
198 typedef T value_type;
199
200 TypedBuffer() {}
201 TypedBuffer(MemBlockType type) : buffer(type) {}
202 TypedBuffer(Memory::Allocator * allocator, MemBlockType type = MemBlockType::Block) : buffer(allocator, type) {}
203 TypedBuffer(size_t size, MemBlockType type = MemBlockType::Block) : buffer(sizeof(T) * size, type) {}
204 TypedBuffer(size_t size, Memory::Allocator * allocator, MemBlockType type = MemBlockType::Block) : buffer(sizeof(T) * size, allocator, type) {}
205
206 TypedBuffer(const TypedBuffer &) = delete;
207 TypedBuffer & operator=(const TypedBuffer &) = delete;
208
209 TypedBuffer(TypedBuffer && other) noexcept = default;
210 TypedBuffer & operator=(TypedBuffer &&) noexcept = default;
211
212 bool resize(size_t size, bool keep = true, bool forceRealloc = false) { return buffer.resize(sizeof(T) * size, keep, forceRealloc); }
213
214 bool reserve(size_t size) { return buffer.reserve(sizeof(T) * size); }
215
216 T & operator[](size_t i)
217 {
218#ifdef _DEBUG
219 assert(buffer.data() != nullptr);
220 assert(i < size());
221#endif
222 return static_cast<T*>(buffer.data())[i];
223 }
224
225 const T & operator[](size_t i) const
226 {
227#ifdef _DEBUG
228 assert(buffer.data() != nullptr);
229 assert(i < size());
230#endif
231 return static_cast<const T*>(buffer.data())[i];
232 }
233
234 void swap(TypedBuffer & other)
235 {
236 std::swap(buffer, other.buffer);
237 }
238
239 void copy(const TypedBuffer & other)
240 {
241 buffer.copy(other.buffer);
242 }
243
244 void clear() { buffer.clear(); }
245
246 T * data() { return static_cast<T*>(buffer.data()); }
247 const T * data() const { return static_cast<const T*>(buffer.data()); }
248
249 T * begin() { return data(); }
250 const T * begin() const { return data(); }
251
252 T * end() { return begin() + size(); }
253 const T * end() const { return begin() + size(); }
254
255 size_t size() const { return buffer.size() / sizeof(T); }
256
257 bool empty() const { return buffer.empty(); }
258
259 size_t byteSize() const { return buffer.size(); }
260
263 T& grow() { return *static_cast<T*>(buffer.grow(sizeof(T))); }
264
265 protected:
266 MemoryBuffer buffer;
267 };
268 }
269}
Base allocator implementation.
Definition: Allocator.h:30
void * grow(size_t bytes)
Definition: MemoryBuffer.h:126
Contains all Cogs related functionality.
Definition: FieldSetter.h:23