3#include "../../Typing.hpp"
4#include "../Exceptions/BufferOverflow.hpp"
5#include "../Exceptions/OutOfRange.hpp"
25 this->data =
new T[1];
35 this->data =
new T[
size];
36 memcpy(this->data, data,
size);
43 this->__size = other.__size;
44 this->data =
new T[this->__size ? this->__size : 1];
45 memcpy(this->data, other.data, this->__size);
52 this->__size = other.__size;
53 this->data =
new T[this->__size ? this->__size : 1];
54 memcpy(this->data, other.data, other.__size);
56 memset(other.data, 0, other.__size);
73 if (
size > self.__size)
75 if (
size > dest.__size)
78 memcpy(dest.data, self.data,
size);
89 if (
size > self.__size)
92 memset(self.data, c,
size);
106 if (
size > self.__size)
108 if (
size > dest.__size)
111 memmove(dest.data, self.data,
size);
122 if (
size > self.__size)
124 if (
size > other.__size)
127 int result = memcmp(self.data, other.data,
size);
134 const T *
get(
this const Buffer<T> &self)
148 const T *
begin(
this const Buffer<T> &self)
155 const T *
end(
this const Buffer<T> &self)
157 return self.data + self.__size;
167 T *new_data =
new T[new_size];
170 new_data[i] = self.data[i];
173 self.data = new_data;
174 self.__size = new_size;
180 bool operator ==(
this const Buffer<T> &self,
const Buffer<T> &other)
182 return self.__size == other.__size and self.
compare(other, self.__size) == 0;
188 bool operator !=(
this const Buffer<T> &self,
const Buffer<T> &other)
190 return not (self == other);
196 bool operator <(
this const Buffer<T> &self,
const Buffer<T> &other)
202 return self.__size < other.__size;
208 bool operator >(
this const Buffer<T> &self,
const Buffer<T> &other)
214 return self.__size > other.__size;
220 bool operator <=(
this const Buffer<T> &self,
const Buffer<T> &other)
222 return self < other or self == other;
228 bool operator >=(
this const Buffer<T> &self,
const Buffer<T> &other)
230 return self > other or self == other;
240 if (index >= self.__size)
243 return self.data[index];
253 if (index >= self.__size)
256 return self.data[index];
261 Buffer<T> &
operator =(
this Buffer<T> &self,
const Buffer<T> &other)
265 self.data =
new T[other.__size ? other.__size : 1];
266 self.__size = other.__size;
267 memcpy(self.data, other.data, other.__size);
275 Buffer<T> &
operator =(
this Buffer<T> &self, Buffer<T> &&other)
279 self.data =
new T[other.__size ? other.__size : 1];
280 memcpy(self.data, other.data, other.__size);
281 self.__size = other.__size;
283 memset(other.data, 0, other.__size);
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