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 bool empty()
const {
return currentSize == 0; }
127 if (storageSize < (currentSize + bytes)) {
128 if (!reserve(std::max<size_t>(128, ((storageSize + bytes) * 3) / 2))) {
132 void* rv =
static_cast<uint8_t*
>(buffer) + currentSize;
133 currentSize += bytes;
139 const Allocator * getAllocator()
const {
return allocator; }
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();
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);
169 void seek(int64_t offset, Anchor from);
171 size_t pos()
const {
return currentPos; }
172 size_t unreadSize()
const {
return size() - pos(); }
174 bool areContentsEqual(
const MemoryBuffer& rhs)
const;
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;
186 template<
typename T> T readImpl() {
189 read(&value,
sizeof(value));
198 typedef T value_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) {}
212 bool resize(
size_t size,
bool keep =
true,
bool forceRealloc =
false) {
return buffer.resize(
sizeof(T) * size, keep, forceRealloc); }
214 bool reserve(
size_t size) {
return buffer.reserve(
sizeof(T) * size); }
216 T & operator[](
size_t i)
219 assert(buffer.data() !=
nullptr);
222 return static_cast<T*
>(buffer.data())[i];
225 const T & operator[](
size_t i)
const
228 assert(buffer.data() !=
nullptr);
231 return static_cast<const T*
>(buffer.data())[i];
236 std::swap(buffer, other.buffer);
241 buffer.copy(other.buffer);
244 void clear() { buffer.clear(); }
246 T * data() {
return static_cast<T*
>(buffer.data()); }
247 const T * data()
const {
return static_cast<const T*
>(buffer.data()); }
249 T * begin() {
return data(); }
250 const T * begin()
const {
return data(); }
252 T * end() {
return begin() + size(); }
253 const T * end()
const {
return begin() + size(); }
255 size_t size()
const {
return buffer.size() /
sizeof(T); }
257 bool empty()
const {
return buffer.empty(); }
259 size_t byteSize()
const {
return buffer.size(); }
263 T&
grow() {
return *
static_cast<T*
>(buffer.grow(
sizeof(T))); }
Base allocator implementation.
void * grow(size_t bytes)
Contains all Cogs related functionality.