Buffer.cpp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #include "core/Buffer.hpp"
  2. #include <cstring>
  3. #include "core/Buffer.hpp"
  4. #include "core/Math.hpp"
  5. #include "core/Utility.hpp"
  6. Core::Buffer::Buffer() : length(0), capacity(0), buffer(nullptr) {
  7. }
  8. Core::Buffer::Buffer(const Buffer& other) : Buffer() {
  9. add(other.getData(), other.getLength());
  10. }
  11. Core::Buffer::Buffer(Buffer&& other) noexcept : Buffer() {
  12. swap(other);
  13. }
  14. Core::Buffer::~Buffer() {
  15. deallocateRaw(buffer);
  16. }
  17. Core::Buffer& Core::Buffer::operator=(Buffer&& other) noexcept {
  18. swap(other);
  19. return *this;
  20. }
  21. Core::Buffer& Core::Buffer::operator=(const Buffer& other) {
  22. return add(other.getData(), other.length);
  23. }
  24. Core::Buffer& Core::Buffer::add(const void* data, size_t size) {
  25. while(length + size > capacity) {
  26. capacity += Core::max<size_t>(8, capacity / 4);
  27. buffer = static_cast<char*>(reallocateRaw(buffer, capacity));
  28. }
  29. memcpy(buffer + length, data, size);
  30. length += size;
  31. return *this;
  32. }
  33. size_t Core::Buffer::getLength() const {
  34. return length;
  35. }
  36. const char* Core::Buffer::getData() const {
  37. return buffer;
  38. }
  39. void Core::Buffer::swap(Buffer& other) noexcept {
  40. Core::swap(length, other.length);
  41. Core::swap(capacity, other.capacity);
  42. Core::swap(buffer, other.buffer);
  43. }
  44. void Core::Buffer::clear() {
  45. length = 0;
  46. }