| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- module Core.Buffer;
- import Core.Math;
- import Core.Utility;
- import Core.Meta;
- import Core.Std;
- 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;
- }
|