5#include "../StringView.h"
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>
20 COGSFOUNDATION_API std::string MemToHexString(
const void* memory,
size_t size);
36 MemoryBuffer(
size_t size, MemBlockType type) : type(type) { resize(size,
false,
false); }
38 MemoryBuffer(
size_t size,
bool onlyReserve =
false, MemBlockType type = MemBlockType::Block)
45 resize(size,
false,
false);
50 : allocator(allocator), type(type)
54 : allocator(allocator), type(type)
56 resize(size,
false,
false);
63 buffer = other.buffer;
64 currentSize = other.currentSize;
65 storageSize = other.storageSize;
66 currentPos = other.currentPos;
67 allocator = other.allocator;
70 other.buffer =
nullptr;
71 other.currentSize = 0;
72 other.storageSize = 0;
74 other.type = MemBlockType::Block;
81 buffer = other.buffer;
82 currentSize = other.currentSize;
83 storageSize = other.storageSize;
84 currentPos = other.currentPos;
85 allocator = other.allocator;
88 other.buffer =
nullptr;
89 other.currentSize = 0;
90 other.storageSize = 0;
92 other.type = MemBlockType::Block;
104 void changeType(MemBlockType newType);
106 void reset(
size_t size,
Allocator * allocator);
108 bool resize(
size_t size,
bool keep =
true,
bool forceRealloc =
false);
109 bool reserve(
size_t size);
113 void clear() { resize(0,
false,
true); }
115 void * data() {
return buffer; }
116 const void * data()
const {
return buffer; }
118 void * dataFromPos() {
return static_cast<uint8_t*
>(buffer) + currentPos; }
119 const void * dataFromPos()
const {
return static_cast<const uint8_t*
>(buffer) + currentPos; }
121 size_t size()
const {
return currentSize; }
122 size_t capacity()
const {
return storageSize; }
123 bool empty()
const {
return currentSize == 0; }
128 if (storageSize < (currentSize + bytes)) {
129 if (!reserve(std::max<size_t>(128, ((storageSize + bytes) * 3) / 2))) {
133 void* rv =
static_cast<uint8_t*
>(buffer) + currentSize;
134 currentSize += bytes;
140 const Allocator * getAllocator()
const {
return allocator; }
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();
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);
170 void seek(int64_t offset, Anchor from);
172 size_t pos()
const {
return currentPos; }
173 size_t unreadSize()
const {
return size() - pos(); }
175 bool areContentsEqual(
const MemoryBuffer& rhs)
const;
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;
187 template<
typename T> T readImpl() {
190 read(&value,
sizeof(value));
199 typedef T value_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) {}
213 bool resize(
size_t size,
bool keep =
true,
bool forceRealloc =
false) {
return buffer.resize(
sizeof(T) * size, keep, forceRealloc); }
215 bool reserve(
size_t size) {
return buffer.reserve(
sizeof(T) * size); }
217 T & operator[](
size_t i)
220 assert(buffer.data() !=
nullptr);
223 return static_cast<T*
>(buffer.data())[i];
226 const T & operator[](
size_t i)
const
229 assert(buffer.data() !=
nullptr);
232 return static_cast<const T*
>(buffer.data())[i];
237 std::swap(buffer, other.buffer);
242 buffer.copy(other.buffer);
245 void clear() { buffer.clear(); }
247 T * data() {
return static_cast<T*
>(buffer.data()); }
248 const T * data()
const {
return static_cast<const T*
>(buffer.data()); }
250 T * begin() {
return data(); }
251 const T * begin()
const {
return data(); }
253 T * end() {
return begin() + size(); }
254 const T * end()
const {
return begin() + size(); }
256 size_t size()
const {
return buffer.size() /
sizeof(T); }
258 bool empty()
const {
return buffer.empty(); }
260 size_t byteSize()
const {
return buffer.size(); }
264 T&
grow() {
return *
static_cast<T*
>(buffer.grow(
sizeof(T))); }
Base allocator implementation.
void * grow(size_t bytes)
Contains all Cogs related functionality.