1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- #include "core/Buffer.hpp"
- #include <cstring>
- #include "core/Buffer.hpp"
- #include "core/Math.hpp"
- #include "core/Utility.hpp"
- Core::Buffer::Buffer() : length(0), capacity(0), buffer(nullptr) {
- }
- Core::Buffer::Buffer(const Buffer& other) : Buffer() {
- add(other.getData(), other.getLength());
- }
- Core::Buffer::Buffer(Buffer&& other) noexcept : Buffer() {
- swap(other);
- }
- Core::Buffer::~Buffer() {
- deallocateRaw(buffer);
- }
- Core::Buffer& Core::Buffer::operator=(Buffer&& other) noexcept {
- swap(other);
- return *this;
- }
- Core::Buffer& Core::Buffer::operator=(const Buffer& other) {
- return add(other.getData(), other.length);
- }
- Core::Buffer& Core::Buffer::add(const void* data, size_t size) {
- while(length + size > capacity) {
- capacity += Core::max<size_t>(8, capacity / 4);
- buffer = static_cast<char*>(reallocateRaw(buffer, capacity));
- }
- memcpy(buffer + length, data, size);
- length += size;
- return *this;
- }
- size_t Core::Buffer::getLength() const {
- return length;
- }
- const char* Core::Buffer::getData() const {
- return buffer;
- }
- void Core::Buffer::swap(Buffer& other) noexcept {
- Core::swap(length, other.length);
- Core::swap(capacity, other.capacity);
- Core::swap(buffer, other.buffer);
- }
- void Core::Buffer::clear() {
- length = 0;
- }
|