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 size_t capacity() const { return storageSize; }
123 bool empty() const { return currentSize == 0; }
124
127 void* grow(size_t bytes) {
128 if (storageSize < (currentSize + bytes)) {
129 if (!reserve(std::max<size_t>(128, ((storageSize + bytes) * 3) / 2))) {
130 return nullptr;
131 }
132 }
133 void* rv = static_cast<uint8_t*>(buffer) + currentSize;
134 currentSize += bytes;
135 return rv;
136 }
137
138 void swap(MemoryBuffer & other);
139
140 const Allocator * getAllocator() const { return allocator; }
141
142 size_t read(void* buffer, size_t noofbytes);
143 uint8_t read8() { return readImpl<uint8_t>(); }
144 uint16_t read16() { return readImpl<uint16_t>(); }
145 uint32_t read32() { return readImpl<uint32_t>(); }
146 uint64_t read64() { return readImpl<uint64_t>(); }
147 float readFloat() { return readImpl<float>(); }
148 double readDouble() { return readImpl<double>(); }
149 glm::vec2 readFloat2();
150 glm::vec3 readFloat3();
151 glm::vec4 readFloat4();
152 glm::quat readQuaternion();
153 glm::mat4 readMatrix();
154 std::string readString();
155
156 bool write(const void* buffer, size_t noofbytes);
157 bool write8(uint8_t value) { return write(&value, sizeof(value)); }
158 bool write16(uint16_t value) { return write(&value, sizeof(value)); }
159 bool write32(uint32_t value) { return write(&value, sizeof(value)); }
160 bool write64(uint64_t value) { return write(&value, sizeof(value)); }
161 bool writeFloat(float value) { return write(&value, sizeof(value)); }
162 bool writeDouble(double value) { return write(&value, sizeof(value)); }
163 bool writeFloat2(const glm::vec2& value);
164 bool writeFloat3(const glm::vec3& value);
165 bool writeFloat4(const glm::vec4& value);
166 bool writeQuaternion(const glm::quat& value);
167 bool writeMatrix(const glm::mat4& value);
168 bool writeString(const StringView& str);
169
170 void seek(int64_t offset, Anchor from);
171
172 size_t pos() const { return currentPos; }
173 size_t unreadSize() const { return size() - pos(); }
174
175 bool areContentsEqual(const MemoryBuffer& rhs) const;
176
177 private:
178 void * buffer = nullptr;
179 size_t currentSize = 0;
180 size_t storageSize = 0;
181 size_t currentPos = 0;
182 Allocator * allocator = Allocator::defaultAllocator();
183 MemBlockType type = MemBlockType::Block;
184
185 void release();
186
187 template<typename T> T readImpl() {
188 T value = {};
189
190 read(&value, sizeof(value));
191 return value;
192 }
193 };
194
195 template<typename T>
197 {
198 public:
199 typedef T value_type;
200
201 TypedBuffer() {}
202 TypedBuffer(MemBlockType type) : buffer(type) {}
203 TypedBuffer(Memory::Allocator * allocator, MemBlockType type = MemBlockType::Block) : buffer(allocator, type) {}
204 TypedBuffer(size_t size, MemBlockType type = MemBlockType::Block) : buffer(sizeof(T) * size, type) {}
205 TypedBuffer(size_t size, Memory::Allocator * allocator, MemBlockType type = MemBlockType::Block) : buffer(sizeof(T) * size, allocator, type) {}
206
207 TypedBuffer(const TypedBuffer &) = delete;
208 TypedBuffer & operator=(const TypedBuffer &) = delete;
209
210 TypedBuffer(TypedBuffer && other) noexcept = default;
211 TypedBuffer & operator=(TypedBuffer &&) noexcept = default;
212
213 bool resize(size_t size, bool keep = true, bool forceRealloc = false) { return buffer.resize(sizeof(T) * size, keep, forceRealloc); }
214
215 bool reserve(size_t size) { return buffer.reserve(sizeof(T) * size); }
216
217 T & operator[](size_t i)
218 {
219#ifdef _DEBUG
220 assert(buffer.data() != nullptr);
221 assert(i < size());
222#endif
223 return static_cast<T*>(buffer.data())[i];
224 }
225
226 const T & operator[](size_t i) const
227 {
228#ifdef _DEBUG
229 assert(buffer.data() != nullptr);
230 assert(i < size());
231#endif
232 return static_cast<const T*>(buffer.data())[i];
233 }
234
235 void swap(TypedBuffer & other)
236 {
237 std::swap(buffer, other.buffer);
238 }
239
240 void copy(const TypedBuffer & other)
241 {
242 buffer.copy(other.buffer);
243 }
244
245 void clear() { buffer.clear(); }
246
247 T * data() { return static_cast<T*>(buffer.data()); }
248 const T * data() const { return static_cast<const T*>(buffer.data()); }
249
250 T * begin() { return data(); }
251 const T * begin() const { return data(); }
252
253 T * end() { return begin() + size(); }
254 const T * end() const { return begin() + size(); }
255
256 size_t size() const { return buffer.size() / sizeof(T); }
257
258 bool empty() const { return buffer.empty(); }
259
260 size_t byteSize() const { return buffer.size(); }
261
264 T& grow() { return *static_cast<T*>(buffer.grow(sizeof(T))); }
265
266 protected:
267 MemoryBuffer buffer;
268 };
269 }
270}
Base allocator implementation.
Definition: Allocator.h:30
void * grow(size_t bytes)
Definition: MemoryBuffer.h:127
Contains all Cogs related functionality.
Definition: FieldSetter.h:23