mirror of https://git.suyu.dev/suyu/suyu
scratch_buffer: Add member types to ScratchBuffer
Allows for implicit conversion to std::span<T>.
This commit is contained in:
parent
9252ad4e10
commit
b8c906f9d1
|
@ -5,7 +5,6 @@
|
||||||
|
|
||||||
#include <iterator>
|
#include <iterator>
|
||||||
|
|
||||||
#include "common/concepts.h"
|
|
||||||
#include "common/make_unique_for_overwrite.h"
|
#include "common/make_unique_for_overwrite.h"
|
||||||
|
|
||||||
namespace Common {
|
namespace Common {
|
||||||
|
@ -19,15 +18,22 @@ namespace Common {
|
||||||
template <typename T>
|
template <typename T>
|
||||||
class ScratchBuffer {
|
class ScratchBuffer {
|
||||||
public:
|
public:
|
||||||
using iterator = T*;
|
|
||||||
using const_iterator = const T*;
|
|
||||||
using value_type = T;
|
|
||||||
using element_type = T;
|
using element_type = T;
|
||||||
using iterator_category = std::contiguous_iterator_tag;
|
using value_type = T;
|
||||||
|
using size_type = size_t;
|
||||||
|
using difference_type = std::ptrdiff_t;
|
||||||
|
using pointer = T*;
|
||||||
|
using const_pointer = const T*;
|
||||||
|
using reference = T&;
|
||||||
|
using const_reference = const T&;
|
||||||
|
using iterator = pointer;
|
||||||
|
using const_iterator = const_pointer;
|
||||||
|
using iterator_category = std::random_access_iterator_tag;
|
||||||
|
using iterator_concept = std::contiguous_iterator_tag;
|
||||||
|
|
||||||
ScratchBuffer() = default;
|
ScratchBuffer() = default;
|
||||||
|
|
||||||
explicit ScratchBuffer(size_t initial_capacity)
|
explicit ScratchBuffer(size_type initial_capacity)
|
||||||
: last_requested_size{initial_capacity}, buffer_capacity{initial_capacity},
|
: last_requested_size{initial_capacity}, buffer_capacity{initial_capacity},
|
||||||
buffer{Common::make_unique_for_overwrite<T[]>(initial_capacity)} {}
|
buffer{Common::make_unique_for_overwrite<T[]>(initial_capacity)} {}
|
||||||
|
|
||||||
|
@ -39,7 +45,7 @@ public:
|
||||||
|
|
||||||
/// This will only grow the buffer's capacity if size is greater than the current capacity.
|
/// This will only grow the buffer's capacity if size is greater than the current capacity.
|
||||||
/// The previously held data will remain intact.
|
/// The previously held data will remain intact.
|
||||||
void resize(size_t size) {
|
void resize(size_type size) {
|
||||||
if (size > buffer_capacity) {
|
if (size > buffer_capacity) {
|
||||||
auto new_buffer = Common::make_unique_for_overwrite<T[]>(size);
|
auto new_buffer = Common::make_unique_for_overwrite<T[]>(size);
|
||||||
std::move(buffer.get(), buffer.get() + buffer_capacity, new_buffer.get());
|
std::move(buffer.get(), buffer.get() + buffer_capacity, new_buffer.get());
|
||||||
|
@ -51,7 +57,7 @@ public:
|
||||||
|
|
||||||
/// This will only grow the buffer's capacity if size is greater than the current capacity.
|
/// This will only grow the buffer's capacity if size is greater than the current capacity.
|
||||||
/// The previously held data will be destroyed if a reallocation occurs.
|
/// The previously held data will be destroyed if a reallocation occurs.
|
||||||
void resize_destructive(size_t size) {
|
void resize_destructive(size_type size) {
|
||||||
if (size > buffer_capacity) {
|
if (size > buffer_capacity) {
|
||||||
buffer_capacity = size;
|
buffer_capacity = size;
|
||||||
buffer = Common::make_unique_for_overwrite<T[]>(buffer_capacity);
|
buffer = Common::make_unique_for_overwrite<T[]>(buffer_capacity);
|
||||||
|
@ -59,43 +65,43 @@ public:
|
||||||
last_requested_size = size;
|
last_requested_size = size;
|
||||||
}
|
}
|
||||||
|
|
||||||
[[nodiscard]] T* data() noexcept {
|
[[nodiscard]] pointer data() noexcept {
|
||||||
return buffer.get();
|
return buffer.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
[[nodiscard]] const T* data() const noexcept {
|
[[nodiscard]] const_pointer data() const noexcept {
|
||||||
return buffer.get();
|
return buffer.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
[[nodiscard]] T* begin() noexcept {
|
[[nodiscard]] iterator begin() noexcept {
|
||||||
return data();
|
return data();
|
||||||
}
|
}
|
||||||
|
|
||||||
[[nodiscard]] const T* begin() const noexcept {
|
[[nodiscard]] const_iterator begin() const noexcept {
|
||||||
return data();
|
return data();
|
||||||
}
|
}
|
||||||
|
|
||||||
[[nodiscard]] T* end() noexcept {
|
[[nodiscard]] iterator end() noexcept {
|
||||||
return data() + last_requested_size;
|
return data() + last_requested_size;
|
||||||
}
|
}
|
||||||
|
|
||||||
[[nodiscard]] const T* end() const noexcept {
|
[[nodiscard]] const_iterator end() const noexcept {
|
||||||
return data() + last_requested_size;
|
return data() + last_requested_size;
|
||||||
}
|
}
|
||||||
|
|
||||||
[[nodiscard]] T& operator[](size_t i) {
|
[[nodiscard]] reference operator[](size_type i) {
|
||||||
return buffer[i];
|
return buffer[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
[[nodiscard]] const T& operator[](size_t i) const {
|
[[nodiscard]] const_reference operator[](size_type i) const {
|
||||||
return buffer[i];
|
return buffer[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
[[nodiscard]] size_t size() const noexcept {
|
[[nodiscard]] size_type size() const noexcept {
|
||||||
return last_requested_size;
|
return last_requested_size;
|
||||||
}
|
}
|
||||||
|
|
||||||
[[nodiscard]] size_t capacity() const noexcept {
|
[[nodiscard]] size_type capacity() const noexcept {
|
||||||
return buffer_capacity;
|
return buffer_capacity;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -106,8 +112,8 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
size_t last_requested_size{};
|
size_type last_requested_size{};
|
||||||
size_t buffer_capacity{};
|
size_type buffer_capacity{};
|
||||||
std::unique_ptr<T[]> buffer{};
|
std::unique_ptr<T[]> buffer{};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue