#include "utils/Buffer.h" #include #include #include "utils/Utility.h" Buffer::Buffer(int initialSize) : length(0), capacity(initialSize <= 0 ? 1 : initialSize), buffer(static_cast(malloc(static_cast(capacity)))) { } Buffer::Buffer(const Buffer& other) : length(other.length), capacity(other.capacity), buffer(static_cast(malloc(static_cast(capacity)))) { memcpy(buffer, other.buffer, static_cast(length)); } Buffer::Buffer(Buffer&& other) : length(0), capacity(0), buffer(nullptr) { swap(other); } Buffer::~Buffer() { free(buffer); } Buffer& Buffer::operator=(Buffer other) { swap(other); return *this; } Buffer& Buffer::add(const void* data, int size) { while(length + size > capacity) { capacity *= 2; buffer = static_cast( realloc(buffer, static_cast(capacity))); } memcpy(buffer + length, data, static_cast(size)); length += size; return *this; } int Buffer::getLength() const { return length; } Buffer::operator const char*() const { return buffer; } void Buffer::swap(Buffer& other) { Core::swap(length, other.length); Core::swap(capacity, other.capacity); Core::swap(buffer, other.buffer); } void Buffer::clear() { length = 0; }