Melon
A C++ library designed for RadishOS.
Loading...
Searching...
No Matches
Buffer.hpp
1#pragma once
2
3#include "../../Typing.hpp"
4#include "../Exceptions/BufferOverflow.hpp"
5#include "../Exceptions/OutOfRange.hpp"
6
7#include <string.h>
8
9namespace Melon::Memory
10{
16 template<typename T>
17 class Buffer
18 {
19 T *data;
20 Typing::USize __size;
21
22 public:
23 Buffer()
24 {
25 this->data = new T[1];
26 this->__size = 0;
27 }
28
32 Buffer(const T *data, Typing::USize size)
33 : __size(size)
34 {
35 this->data = new T[size];
36 memcpy(this->data, data, size);
37 }
38
41 Buffer(const Buffer<T> &other)
42 {
43 this->__size = other.__size;
44 this->data = new T[this->__size ? this->__size : 1];
45 memcpy(this->data, other.data, this->__size);
46 }
47
50 Buffer(Buffer<T> &&other)
51 {
52 this->__size = other.__size;
53 this->data = new T[this->__size ? this->__size : 1];
54 memcpy(this->data, other.data, other.__size);
55
56 memset(other.data, 0, other.__size);
57 other.__size = 0;
58 }
59
62 {
63 if (this->data)
64 delete[] this->data;
65 }
66
71 Buffer<T> &copy(this const Buffer<T> &self, Buffer<T> &dest, Typing::USize size)
72 {
73 if (size > self.__size)
74 throw Exceptions::BufferOverflow(size, self.__size);
75 if (size > dest.__size)
76 throw Exceptions::BufferOverflow(size, dest.__size);
77
78 memcpy(dest.data, self.data, size);
79
80 return dest;
81 }
82
87 Buffer<T> &set(this Buffer<T> &self, T c, Typing::USize size)
88 {
89 if (size > self.__size)
90 throw Exceptions::BufferOverflow(size, self.__size);
91
92 memset(self.data, c, size);
93
94 return self;
95 }
96
104 Buffer<T> &move(this const Buffer<T> &self, Buffer<T> &dest, Typing::USize size)
105 {
106 if (size > self.__size)
107 throw Exceptions::BufferOverflow(size, self.__size);
108 if (size > dest.__size)
109 throw Exceptions::BufferOverflow(size, dest.__size);
110
111 memmove(dest.data, self.data, size);
112
113 return dest;
114 }
115
120 int compare(this const Buffer<T> &self, const Buffer<T> &other, Typing::USize size)
121 {
122 if (size > self.__size)
123 throw Exceptions::BufferOverflow(size, self.__size);
124 if (size > other.__size)
125 throw Exceptions::BufferOverflow(size, other.__size);
126
127 int result = memcmp(self.data, other.data, size);
128
129 return result;
130 }
131
134 const T *get(this const Buffer<T> &self)
135 {
136 return self.data;
137 }
138
141 Typing::USize size(this const Buffer<T> &self)
142 {
143 return self.__size;
144 }
145
148 const T *begin(this const Buffer<T> &self)
149 {
150 return self.data;
151 }
152
155 const T *end(this const Buffer<T> &self)
156 {
157 return self.data + self.__size;
158 }
159
165 void resize(this Buffer<T> &self, Typing::USize new_size)
166 {
167 T *new_data = new T[new_size];
168
169 for (Typing::USize i = 0; i < self.__size; ++i)
170 new_data[i] = self.data[i];
171
172 delete[] self.data;
173 self.data = new_data;
174 self.__size = new_size;
175 }
176
180 bool operator ==(this const Buffer<T> &self, const Buffer<T> &other)
181 {
182 return self.__size == other.__size and self.compare(other, self.__size) == 0;
183 }
184
188 bool operator !=(this const Buffer<T> &self, const Buffer<T> &other)
189 {
190 return not (self == other);
191 }
192
196 bool operator <(this const Buffer<T> &self, const Buffer<T> &other)
197 {
198 Typing::USize size = self.__size < other.__size ? self.__size : other.__size;
199 int result = self.compare(other, size);
200 if (result != 0)
201 return result < 0;
202 return self.__size < other.__size;
203 }
204
208 bool operator >(this const Buffer<T> &self, const Buffer<T> &other)
209 {
210 Typing::USize size = self.__size < other.__size ? self.__size : other.__size;
211 int result = self.compare(other, size);
212 if (result != 0)
213 return result > 0;
214 return self.__size > other.__size;
215 }
216
220 bool operator <=(this const Buffer<T> &self, const Buffer<T> &other)
221 {
222 return self < other or self == other;
223 }
224
228 bool operator >=(this const Buffer<T> &self, const Buffer<T> &other)
229 {
230 return self > other or self == other;
231 }
232
238 T &operator [](this Buffer<T> &self, Typing::USize index)
239 {
240 if (index >= self.__size)
241 throw Exceptions::OutOfRange(index, self.__size);
242
243 return self.data[index];
244 }
245
251 const T &operator [](this const Buffer<T> &self, Typing::USize index)
252 {
253 if (index >= self.__size)
254 throw Exceptions::OutOfRange(index, self.__size);
255
256 return self.data[index];
257 }
258
261 Buffer<T> &operator =(this Buffer<T> &self, const Buffer<T> &other)
262 {
263 if (self != other) {
264 delete[] self.data;
265 self.data = new T[other.__size ? other.__size : 1];
266 self.__size = other.__size;
267 memcpy(self.data, other.data, other.__size);
268 }
269
270 return self;
271 }
272
275 Buffer<T> &operator =(this Buffer<T> &self, Buffer<T> &&other)
276 {
277 if (self != other) {
278 delete[] self.data;
279 self.data = new T[other.__size ? other.__size : 1];
280 memcpy(self.data, other.data, other.__size);
281 self.__size = other.__size;
282
283 memset(other.data, 0, other.__size);
284 other.__size = 0;
285 }
286
287 return self;
288 }
289 };
290} // namespace Melon::Memory
Thrown when a buffer overflows.
Definition BufferOverflow.hpp:11
Thrown when an index is out of range.
Definition OutOfRange.hpp:11
Buffer< T > & operator=(this Buffer< T > &self, const Buffer< T > &other)
Copy assignment operator.
Definition Buffer.hpp:261
Buffer(Buffer< T > &&other)
Move constructor.
Definition Buffer.hpp:50
const T * get(this const Buffer< T > &self)
Gets a constant pointer to the raw memory.
Definition Buffer.hpp:134
Buffer(const Buffer< T > &other)
Copy constructor.
Definition Buffer.hpp:41
Buffer< T > & copy(this const Buffer< T > &self, Buffer< T > &dest, Typing::USize size)
Copies some objects from here to another buffer.
Definition Buffer.hpp:71
bool operator==(this const Buffer< T > &self, const Buffer< T > &other)
Checks if a buffer has same size and same content.
Definition Buffer.hpp:180
Buffer< T > & move(this const Buffer< T > &self, Buffer< T > &dest, Typing::USize size)
Moves some objects from here to another buffer.
Definition Buffer.hpp:104
void resize(this Buffer< T > &self, Typing::USize new_size)
Resizes the buffer.
Definition Buffer.hpp:165
Typing::USize size(this const Buffer< T > &self)
Gets the size of the buffer.
Definition Buffer.hpp:141
bool operator<(this const Buffer< T > &self, const Buffer< T > &other)
Checks if this buffer is smaller than another.
Definition Buffer.hpp:196
~Buffer()
Destructor.
Definition Buffer.hpp:61
Buffer< T > & set(this Buffer< T > &self, T c, Typing::USize size)
Sets some objects from this buffer to a given value.
Definition Buffer.hpp:87
bool operator>(this const Buffer< T > &self, const Buffer< T > &other)
Checks if this buffer is bigger than another.
Definition Buffer.hpp:208
bool operator<=(this const Buffer< T > &self, const Buffer< T > &other)
Checks if this buffer is smaller than or equals another.
Definition Buffer.hpp:220
Buffer(const T *data, Typing::USize size)
Constructs the buffer from existing raw memory.
Definition Buffer.hpp:32
bool operator>=(this const Buffer< T > &self, const Buffer< T > &other)
Checks if this buffer is bigger than or equals another.
Definition Buffer.hpp:228
const T * end(this const Buffer< T > &self)
Gets a constant pointer to the end of the raw buffer.
Definition Buffer.hpp:155
T & operator[](this Buffer< T > &self, Typing::USize index)
Gets an object from the buffer.
Definition Buffer.hpp:238
bool operator!=(this const Buffer< T > &self, const Buffer< T > &other)
Checks if a buffer does not have same size or same content.
Definition Buffer.hpp:188
int compare(this const Buffer< T > &self, const Buffer< T > &other, Typing::USize size)
Compares some objects between this buffer and another one.
Definition Buffer.hpp:120
const T * begin(this const Buffer< T > &self)
Gets a constant pointer to the base of the raw buffer.
Definition Buffer.hpp:148
Contains various classes and functions for memory management.
Definition Buffer.hpp:10
size_t USize
Biggest unsigned type.
Definition Typing.hpp:38