6#include <initializer_list>
13#include <stratax/exceptions/Exceptions.hpp>
15namespace stratax::core {
49template<
typename T, std::
size_t Alignment = config::default_alignment>
53 static_assert(Alignment >=
alignof(T));
54 static_assert((Alignment & (Alignment - 1)) == 0);
104 return std::numeric_limits<size_type>::max() /
sizeof(
value_type);
114 Buffer() noexcept: data_(
nullptr), size_(0) {}
130 : data_(allocate(
size)), size_(
size)
149 : data_(allocate(
size)), size_(
size)
151 construct_fill(value);
167 Buffer(std::initializer_list<value_type> list)
168 : data_(allocate(list.
size())), size_(list.
size())
174 std::construct_at(data_ + i, value);
178 std::destroy_n(data_, i);
197 : data_(allocate(other.size_)),
201 std::uninitialized_copy_n(
225 : data_(other.data_), size_(other.size_)
227 other.data_ =
nullptr;
247 if (
this == &other) {
272 if (
this == &other) {
277 std::destroy_n(data_, size_);
285 other.data_ =
nullptr;
301 std::destroy_n(data_, size_);
360 return data_[size_ - 1];
372 return data_[size_ - 1];
494 [[nodiscard]]
bool empty() const noexcept {
return size_ == 0;}
509 std::fill_n(data_, size_, value);
525 std::swap(data_, other.data_);
526 std::swap(size_, other.size_);
550 throw std::bad_array_new_length();
556 std::align_val_t{Alignment}
567 static void deallocate(
pointer ptr)
noexcept
569 if (ptr ==
nullptr) {
575 std::align_val_t{Alignment}
593 std::uninitialized_fill_n(data_, size_, value);
Fixed-size owner of aligned, contiguous element storage.
const_pointer const_iterator
Read-only contiguous random-access iterator type.
std::reverse_iterator< iterator > reverse_iterator
Mutable iterator that traverses elements in reverse order.
Buffer(size_type size, const_reference value)
Constructs a buffer with all elements initialized to value.
Buffer & operator=(Buffer &&other) noexcept
Move-assigns from another buffer.
const_pointer data() const noexcept
Returns a const pointer to the underlying contiguous storage.
reverse_iterator rend() noexcept
Returns the past-the-end iterator for mutable reverse traversal.
void swap(Buffer &other) noexcept
Swaps storage and size with another buffer.
Buffer(const Buffer &other)
Copy-constructs a buffer with independent storage.
Buffer(size_type size)
Constructs a buffer with value-initialized elements.
T & reference
Mutable element reference type.
size_type size() const noexcept
Returns the number of stored elements.
~Buffer()
Destroys the buffer and releases owned storage.
static constexpr size_type alignment() noexcept
Returns the alignment, in bytes, used for buffer allocations.
const_reverse_iterator rbegin() const noexcept
Returns a const reverse iterator to the final element.
T * pointer
Mutable pointer to an element.
static constexpr size_type max_size() noexcept
Returns the largest element count that cannot overflow in bytes.
const_reverse_iterator crend() const noexcept
Returns the past-the-end iterator for const reverse traversal.
std::ptrdiff_t difference_type
Signed type used for distances between iterators.
reference front()
Returns a reference to the first element.
const_iterator cend() const noexcept
Returns a const iterator one past the final element.
pointer iterator
Mutable contiguous random-access iterator type.
bool empty() const noexcept
Returns whether the buffer has no elements.
reference operator[](size_type index) noexcept
Returns an unchecked reference to an element.
Buffer(std::initializer_list< value_type > list)
Constructs a buffer from an initializer list.
const_reverse_iterator crbegin() const noexcept
Returns a const reverse iterator to the final element.
std::size_t size_type
Unsigned type used for element counts and indices.
reverse_iterator rbegin() noexcept
Returns a mutable reverse iterator to the final element.
void fill(const_reference value)
Assigns a value to every element in the buffer.
pointer data() noexcept
Returns a pointer to the underlying contiguous storage.
const_reference front() const
Returns a const reference to the first element.
iterator begin() noexcept
Returns a mutable iterator to the first element.
const_iterator cbegin() const noexcept
Returns a const iterator to the first element.
const_iterator end() const noexcept
Returns a const iterator one past the final element.
const_reference back() const
Returns a const reference to the last element.
Buffer(Buffer &&other) noexcept
Move-constructs a buffer by transferring ownership.
const T * const_pointer
Read-only pointer to an element.
reference back()
Returns a reference to the last element.
const_iterator begin() const noexcept
Returns a const iterator to the first element.
Buffer() noexcept
Constructs an empty buffer that owns no allocation.
std::reverse_iterator< const_iterator > const_reverse_iterator
Read-only iterator that traverses elements in reverse order.
Buffer & operator=(const Buffer &other)
Copy-assigns from another buffer.
const T & const_reference
Read-only element reference type.
iterator end() noexcept
Returns a mutable iterator one past the final element.
const_reverse_iterator rend() const noexcept
Returns the past-the-end iterator for const reverse traversal.
const_reference operator[](size_type index) const noexcept
Returns an unchecked const reference to an element.
T value_type
Type of each stored element.