CeresEngine 0.2.0
A game development framework
Loading...
Searching...
No Matches
Buffer.hpp
Go to the documentation of this file.
1//
2// CeresEngine - A game development framework
3//
4// Created by Rogiel Sulzbach.
5// Copyright (c) 2018-2022 Rogiel Sulzbach. All rights reserved.
6//
7
8#pragma once
9
10#include "Hash.hpp"
11
13
14#include <algorithm>
15#include <cstddef>
16#include <cstring>
17#include <initializer_list>
18#include <string>
19#include <tuple>
20#include <vector>
21
22namespace CeresEngine {
23
28 class Buffer {
29 public:
31 using Iterator = Byte*;
32
34 using ConstIterator = const Byte*;
35
36 private:
38 Byte* mBuffer = nullptr;
39
41 size_t mSize = 0;
42
44 size_t mCapacity = 0;
45
47 bool mOwns = true;
48
49 public:
51 Buffer() noexcept;
52
54 Buffer(std::nullptr_t) noexcept; // NOLINT
55
58 explicit Buffer(size_t size) noexcept;
59
65 explicit Buffer(Byte* buffer, size_t size, bool copy = false) noexcept;
66
70 explicit Buffer(const Byte* buffer, size_t size) noexcept;
71
74 Buffer(std::initializer_list<unsigned char> bytes) noexcept;
75
76 public:
79 Buffer(const Buffer& other) noexcept = delete;
80
84 Buffer& operator=(const Buffer& other) noexcept = delete;
85
88 Buffer(Buffer&& other) noexcept;
89
93 Buffer& operator=(Buffer&& other) noexcept;
94
96 ~Buffer() noexcept;
97
98 public:
102 [[nodiscard]] Buffer copy() const noexcept;
103
104 public:
107 Byte* data() noexcept { return mBuffer; }
108
111 [[nodiscard]] const Byte* data() const noexcept { return mBuffer; }
112
115 Byte* buffer() noexcept { return mBuffer; }
116
119 [[nodiscard]] const Byte* buffer() const noexcept { return mBuffer; }
120
123 [[nodiscard]] size_t size() const noexcept { return mSize; }
124
127 [[nodiscard]] size_t capacity() const noexcept { return mCapacity; }
128
131 [[nodiscard]] bool empty() const noexcept { return mSize == 0; }
132
135 [[nodiscard]] bool owns() const noexcept { return mOwns; }
136
139 [[nodiscard]] bool valid() const noexcept { return mBuffer != nullptr; }
140
141 public:
144 [[nodiscard]] Iterator begin() noexcept { return mBuffer; }
145
148 [[nodiscard]] Iterator end() noexcept { return mBuffer + mSize; }
149
152 [[nodiscard]] ConstIterator begin() const noexcept { return mBuffer; }
153
156 [[nodiscard]] ConstIterator end() const noexcept { return mBuffer + mSize; }
157
160 [[nodiscard]] ConstIterator cbegin() const noexcept { return mBuffer; }
161
164 [[nodiscard]] ConstIterator cend() const noexcept { return mBuffer + mSize; }
165
166 public:
172 bool operator==(const Buffer& other) const noexcept;
173
179 bool operator!=(const Buffer& other) const noexcept;
180
184 Byte& operator[](size_t offset) noexcept;
185
189 Byte operator[](size_t offset) const noexcept;
190
193 explicit operator bool() const noexcept;
194
195 operator const unsigned char*() const noexcept; // NOLINT
196 operator const char*() const noexcept; // NOLINT
197 operator unsigned char*() noexcept; // NOLINT
198 operator char*() noexcept; // NOLINT
199
200 // operator std::vector<UInt8>() const noexcept { //
201 // NOLINT(google-explicit-constructor,hicpp-explicit-conversions)
202 // return std::vector<UInt8>(begin(), end());
203 // }
204
205 public:
208 void resize(size_t newSize) noexcept;
209
212 void reserve(size_t newCapacity) noexcept;
213
216 void own() noexcept;
217
220 void append(const Buffer& buffer) noexcept;
221
225 void write(const char* buf, size_t length) noexcept;
226
231 [[nodiscard]] auto reference(size_t start = 0, size_t length = std::numeric_limits<size_t>::max()) const noexcept -> Buffer;
232
234 [[nodiscard]] std::string hexdump() const noexcept;
235
237 [[nodiscard]] std::string hexstring() const noexcept;
238
239 friend std::ostream& operator<<(std::ostream& os, const Buffer& buffer);
240 };
241
242 // ---------------------------------------------------------------------------------------------
243
244 inline bool Buffer::operator==(const Buffer& other) const noexcept {
245 if(mBuffer == other.mBuffer && mSize == other.mSize) {
246 return true;
247 }
248 if(mSize != other.mSize) {
249 return false;
250 }
251
252 return memcmp(mBuffer, other.mBuffer, mSize) == 0;
253 }
254
255 inline bool Buffer::operator!=(const Buffer& other) const noexcept { return !(*this == other); }
256 inline Byte& Buffer::operator[](const size_t offset) noexcept { return mBuffer[offset]; }
257 inline Byte Buffer::operator[](const size_t offset) const noexcept { return mBuffer[offset]; }
258
259 inline Buffer::operator bool() const noexcept { return valid(); }
260
261 inline Buffer::operator const unsigned char*() const noexcept { return reinterpret_cast<const unsigned char*>(data()); }
262 inline Buffer::operator const char*() const noexcept { return reinterpret_cast<const char*>(data()); }
263 inline Buffer::operator unsigned char*() noexcept { return reinterpret_cast<unsigned char*>(data()); }
264 inline Buffer::operator char*() noexcept { return reinterpret_cast<char*>(data()); }
265
266 // ---------------------------------------------------------------------------------------------
267
268 class CopyableBuffer : public Buffer {
269 public:
270 using Buffer::Buffer;
271
272 CopyableBuffer() noexcept;
273
274 CopyableBuffer(const CopyableBuffer& other) noexcept;
275 CopyableBuffer& operator=(const CopyableBuffer& other) noexcept;
276
278 CopyableBuffer& operator=(CopyableBuffer&& other) noexcept;
279
280 CopyableBuffer(const Buffer& other) noexcept; // NOLINT
281 CopyableBuffer(Buffer&& other) noexcept; // NOLINT
282 };
283
284} // namespace CeresEngine
285
286namespace CeresEngine {
288}
289
290template<> struct std::hash<CeresEngine::Buffer> {
291 size_t operator()(const CeresEngine::Buffer& buffer) {
292 using CeresEngine::hash;
293 return hash(buffer.begin(), buffer.end());
294 }
295};
296
297template<> struct std::hash<CeresEngine::CopyableBuffer> : std::hash<CeresEngine::Buffer> {};
Represents a secure buffer i.e.
Definition Buffer.hpp:28
bool valid() const noexcept
Checks if the buffer is valid.
Definition Buffer.hpp:139
ConstIterator cend() const noexcept
Gets a iterator that points to the end of the buffer.
Definition Buffer.hpp:164
const Byte * data() const noexcept
Gets a pointer to the raws buffer data.
Definition Buffer.hpp:111
Iterator end() noexcept
Gets a iterator that points to the end of the buffer.
Definition Buffer.hpp:148
size_t capacity() const noexcept
Gets the buffer capacity.
Definition Buffer.hpp:127
Buffer copy() const noexcept
Creates a explicit buffer copy.
Iterator begin() noexcept
Gets a iterator that points to the begining of the buffer.
Definition Buffer.hpp:144
std::string hexdump() const noexcept
Creates a hexdump representation.
Byte * data() noexcept
Gets a pointer to the raws buffer data.
Definition Buffer.hpp:107
auto reference(size_t start=0, size_t length=std::numeric_limits< size_t >::max()) const noexcept -> Buffer
Creates a buffer reference.
bool mOwns
A flag that indicates whether the buffer is owned or not.
Definition Buffer.hpp:47
void write(const char *buf, size_t length) noexcept
Writes buf to the end of the buffer.
bool operator!=(const Buffer &other) const noexcept
Compares a buffer for inequality If either buffer contain the same address and size,...
Definition Buffer.hpp:255
size_t mCapacity
The buffer capacity.
Definition Buffer.hpp:44
ConstIterator begin() const noexcept
Gets a iterator that points to the beginning of the buffer.
Definition Buffer.hpp:152
bool operator==(const Buffer &other) const noexcept
Compares a buffer for equality If either buffer contain the same address and size,...
Definition Buffer.hpp:244
void reserve(size_t newCapacity) noexcept
Reservers at least newCapacity bytes in the buffer.
const Byte * buffer() const noexcept
Gets a pointer to the raws buffer data.
Definition Buffer.hpp:119
Byte & operator[](size_t offset) noexcept
Accesses a buffer element by its offset.
Definition Buffer.hpp:256
Byte * mBuffer
The buffer pointer.
Definition Buffer.hpp:38
void append(const Buffer &buffer) noexcept
Appends a buffer to the end of this buffer.
size_t size() const noexcept
Gets the number of bytes currently allocated in the buffer.
Definition Buffer.hpp:123
void resize(size_t newSize) noexcept
Resizes the buffer to be able to store up-to "newSize" bytes.
bool owns() const noexcept
Checks if the buffer is currently owned.
Definition Buffer.hpp:135
std::string hexstring() const noexcept
Creates a hexdump representation.
ConstIterator end() const noexcept
Gets a iterator that points to the end of the buffer.
Definition Buffer.hpp:156
Buffer() noexcept
Creates a new empty buffer.
ConstIterator cbegin() const noexcept
Gets a iterator that points to the beginning of the buffer.
Definition Buffer.hpp:160
size_t mSize
The buffer size.
Definition Buffer.hpp:41
bool empty() const noexcept
Checks if the buffer is currently empty (i.e.
Definition Buffer.hpp:131
Byte * buffer() noexcept
Gets a pointer to the raws buffer data.
Definition Buffer.hpp:115
void own() noexcept
Transforms a unowned buffer into a owned buffer.
Definition Buffer.hpp:268
Definition Application.hpp:19
Byte
Definition DataTypes.hpp:40
constexpr size_t hash(const T &v)
Generates a hash for the provided type.
Definition Hash.hpp:25
Definition Span.hpp:668