diff --git a/src/util/Buffer.hpp b/src/util/Buffer.hpp new file mode 100644 index 00000000..4787d2ed --- /dev/null +++ b/src/util/Buffer.hpp @@ -0,0 +1,52 @@ +#pragma once + +#include +#include + +namespace util { + template + class Buffer { + std::unique_ptr ptr; + size_t length; + public: + Buffer(size_t length) + : ptr(std::make_unique(length)), length(length) { + } + + Buffer(std::unique_ptr ptr, size_t length) + : ptr(std::move(ptr)), length(length) {} + + Buffer(const T* src, size_t length) + : ptr(std::make_unique(length)), length(length) { + std::memcpy(ptr.get(), src, length); + } + + T& operator[](long long index) { + return ptr[index]; + } + + const T& operator[](long long index) const { + return ptr[index]; + } + + T* data() { + return ptr.get(); + } + + const T* data() const { + return ptr.get(); + } + + size_t size() const { + return length; + } + + std::unique_ptr release() { + return std::move(ptr); + } + + Buffer clone() const { + return Buffer(ptr.get(), length); + } + }; +}