123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- #include "utils/Buffer.hpp"
- #include "math/Math.hpp"
- #include "utils/Utility.hpp"
- Core::Buffer::Buffer(int initialSize)
- : length(0), capacity(initialSize <= 0 ? 1 : initialSize), buffer(nullptr) {
- }
- Core::Buffer::Buffer(Buffer&& other) : Buffer(1) {
- swap(other);
- }
- Core::Buffer::~Buffer() {
- free(buffer);
- }
- Core::Buffer& Core::Buffer::operator=(Buffer&& other) {
- swap(other);
- return *this;
- }
- Core::Error Core::Buffer::copyFrom(const Buffer& other) {
- clear();
- return add(other.getData(), other.length);
- }
- Core::Error Core::Buffer::add(const void* data, int size) {
- if(length + size > capacity || buffer == nullptr) {
- while(length + size > capacity) {
- capacity += Math::max(4, capacity / 4);
- }
- CORE_RETURN_ERROR(reallocate(buffer, capacity));
- }
- memoryCopy(buffer + length, data, size);
- length += size;
- return Error::NONE;
- }
- int Core::Buffer::getLength() const {
- return length;
- }
- Core::Buffer::operator const char*() const {
- return buffer;
- }
- const char* Core::Buffer::getData() const {
- return buffer;
- }
- void Core::Buffer::swap(Buffer& other) {
- Core::swap(length, other.length);
- Core::swap(capacity, other.capacity);
- Core::swap(buffer, other.buffer);
- }
- void Core::Buffer::clear() {
- length = 0;
- }
|