From f457f306ceb039780f30dc2b38677bd0f8d75640 Mon Sep 17 00:00:00 2001 From: Jonathan G Rennison Date: Sat, 30 Dec 2023 19:47:54 +0000 Subject: [PATCH] Add std::unique_ptr wrapper (UniqueBuffer) which also stores the size --- src/core/alloc_type.hpp | 82 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git a/src/core/alloc_type.hpp b/src/core/alloc_type.hpp index 555c8aea7c..b6419fb251 100644 --- a/src/core/alloc_type.hpp +++ b/src/core/alloc_type.hpp @@ -11,6 +11,7 @@ #define ALLOC_TYPE_HPP #include "alloc_func.hpp" +#include /** * A reusable buffer that can be used for places that temporary allocate @@ -125,4 +126,85 @@ struct NoOpDeleter void operator()(const void* ptr) {} }; +/** + * A wrapper around std::unique_ptr which also stores the size. + */ +template +class UniqueBuffer { +private: + std::unique_ptr buffer; ///< The real data buffer + size_t buffer_size = 0; ///< Number of T elements in the buffer + +public: + void swap(UniqueBuffer &other) noexcept + { + std::swap(this->buffer, other.buffer); + std::swap(this->buffer_size, other.buffer_size); + } + + void reset(size_t size = 0) + { + if (size > 0) { + this->buffer.reset(new T[size]); + } else { + this->buffer.reset(); + } + this->buffer_size = size; + } + + T *get() const noexcept + { + return this->buffer.get(); + } + + size_t size() const noexcept + { + return this->buffer_size; + } + + T& operator[](size_t i) const noexcept + { + return this->buffer[i]; + } + + UniqueBuffer() noexcept {} + + UniqueBuffer(const UniqueBuffer &other) = delete; + + UniqueBuffer(UniqueBuffer &&other) noexcept + { + this->swap(other); + } + + UniqueBuffer(size_t size) + { + this->reset(size); + } + + UniqueBuffer(std::unique_ptr buffer, size_t size) : buffer(std::move(buffer)), buffer_size(size) {} + + UniqueBuffer& operator=(const UniqueBuffer &other) = delete; + + UniqueBuffer& operator=(UniqueBuffer &&other) noexcept + { + this->swap(other); + return *this; + } + + bool operator==(std::nullptr_t) const noexcept + { + return this->buffer == nullptr; + } + + bool operator!=(std::nullptr_t) const noexcept + { + return this->buffer != nullptr; + } + + explicit operator bool() const noexcept + { + return (bool)this->buffer; + } +}; + #endif /* ALLOC_TYPE_HPP */