Stratax 0.3.1
Loading...
Searching...
No Matches
stratax::core::Buffer< T, Alignment > Class Template Reference

Fixed-size owner of aligned, contiguous element storage. More...

#include <Buffer.hpp>

Public Types

using value_type = T
 Type of each stored element.
 
using size_type = std::size_t
 Unsigned type used for element counts and indices.
 
using difference_type = std::ptrdiff_t
 Signed type used for distances between iterators.
 
using reference = T &
 Mutable element reference type.
 
using const_reference = const T &
 Read-only element reference type.
 
using pointer = T *
 Mutable pointer to an element.
 
using const_pointer = const T *
 Read-only pointer to an element.
 
using iterator = pointer
 Mutable contiguous random-access iterator type.
 
using const_iterator = const_pointer
 Read-only contiguous random-access iterator type.
 
using reverse_iterator = std::reverse_iterator< iterator >
 Mutable iterator that traverses elements in reverse order.
 
using const_reverse_iterator = std::reverse_iterator< const_iterator >
 Read-only iterator that traverses elements in reverse order.
 

Public Member Functions

 Buffer () noexcept
 Constructs an empty buffer that owns no allocation.
 
 Buffer (size_type size)
 Constructs a buffer with value-initialized elements.
 
 Buffer (size_type size, const_reference value)
 Constructs a buffer with all elements initialized to value.
 
 Buffer (std::initializer_list< value_type > list)
 Constructs a buffer from an initializer list.
 
 Buffer (const Buffer &other)
 Copy-constructs a buffer with independent storage.
 
 Buffer (Buffer &&other) noexcept
 Move-constructs a buffer by transferring ownership.
 
Bufferoperator= (const Buffer &other)
 Copy-assigns from another buffer.
 
Bufferoperator= (Buffer &&other) noexcept
 Move-assigns from another buffer.
 
 ~Buffer ()
 Destroys the buffer and releases owned storage.
 
reference operator[] (size_type index) noexcept
 Returns an unchecked reference to an element.
 
const_reference operator[] (size_type index) const noexcept
 Returns an unchecked const reference to an element.
 
reference front ()
 Returns a reference to the first element.
 
const_reference front () const
 Returns a const reference to the first element.
 
reference back ()
 Returns a reference to the last element.
 
const_reference back () const
 Returns a const reference to the last element.
 
pointer data () noexcept
 Returns a pointer to the underlying contiguous storage.
 
const_pointer data () const noexcept
 Returns a const pointer to the underlying contiguous storage.
 
iterator begin () noexcept
 Returns a mutable iterator to the first element.
 
const_iterator begin () const noexcept
 Returns a const iterator to the first element.
 
const_iterator cbegin () const noexcept
 Returns a const iterator to the first element.
 
iterator end () noexcept
 Returns a mutable iterator one past the final element.
 
const_iterator end () const noexcept
 Returns a const iterator one past the final element.
 
const_iterator cend () const noexcept
 Returns a const iterator one past the final element.
 
reverse_iterator rbegin () noexcept
 Returns a mutable reverse iterator to the final element.
 
const_reverse_iterator rbegin () const noexcept
 Returns a const reverse iterator to the final element.
 
const_reverse_iterator crbegin () const noexcept
 Returns a const reverse iterator to the final element.
 
reverse_iterator rend () noexcept
 Returns the past-the-end iterator for mutable reverse traversal.
 
const_reverse_iterator rend () const noexcept
 Returns the past-the-end iterator for const reverse traversal.
 
const_reverse_iterator crend () const noexcept
 Returns the past-the-end iterator for const reverse traversal.
 
size_type size () const noexcept
 Returns the number of stored elements.
 
bool empty () const noexcept
 Returns whether the buffer has no elements.
 
void fill (const_reference value)
 Assigns a value to every element in the buffer.
 
void swap (Buffer &other) noexcept
 Swaps storage and size with another buffer.
 

Static Public Member Functions

static constexpr size_type alignment () noexcept
 Returns the alignment, in bytes, used for buffer allocations.
 
static constexpr size_type max_size () noexcept
 Returns the largest element count that cannot overflow in bytes.
 

Detailed Description

template<typename T, std::size_t Alignment = config::default_alignment>
class stratax::core::Buffer< T, Alignment >

Fixed-size owner of aligned, contiguous element storage.

Buffer allocates storage with the requested alignment and manages the lifetime of every element in that storage. Its size cannot change after construction, although its elements remain mutable. Elements occupy one contiguous allocation, so pointers and random-access iterators can be used with standard-library algorithms.

Copy operations create an independent allocation and copy the elements. Move operations transfer the allocation in constant time and leave the source empty. Destruction releases every element followed by the aligned allocation, giving the class exclusive RAII ownership of its storage.

Template Parameters
TElement type stored in the buffer.
AlignmentAllocation alignment in bytes. It must be a power of two and at least alignof(T).
Example
stratax::core::Buffer<float, 64> samples{1.0F, 2.0F, 3.0F};
samples[1] = 5.0F;
std::fill(samples.begin(), samples.end(), 4.0F);
stratax::core::Buffer<float, 64> copy = samples; // Deep copy.
copy.front() = 9.0F; // samples is unchanged.
Fixed-size owner of aligned, contiguous element storage.
Definition Buffer.hpp:51
reference front()
Returns a reference to the first element.
Definition Buffer.hpp:334
Note
The class does not provide resizing or capacity management. Create or assign another Buffer when a different element count is required.

Definition at line 50 of file Buffer.hpp.

Member Typedef Documentation

◆ const_iterator

template<typename T , std::size_t Alignment = config::default_alignment>
using stratax::core::Buffer< T, Alignment >::const_iterator = const_pointer

Read-only contiguous random-access iterator type.

Definition at line 74 of file Buffer.hpp.

◆ const_pointer

template<typename T , std::size_t Alignment = config::default_alignment>
using stratax::core::Buffer< T, Alignment >::const_pointer = const T*

Read-only pointer to an element.

Definition at line 70 of file Buffer.hpp.

◆ const_reference

template<typename T , std::size_t Alignment = config::default_alignment>
using stratax::core::Buffer< T, Alignment >::const_reference = const T&

Read-only element reference type.

Definition at line 66 of file Buffer.hpp.

◆ const_reverse_iterator

template<typename T , std::size_t Alignment = config::default_alignment>
using stratax::core::Buffer< T, Alignment >::const_reverse_iterator = std::reverse_iterator<const_iterator>

Read-only iterator that traverses elements in reverse order.

Definition at line 78 of file Buffer.hpp.

◆ difference_type

template<typename T , std::size_t Alignment = config::default_alignment>
using stratax::core::Buffer< T, Alignment >::difference_type = std::ptrdiff_t

Signed type used for distances between iterators.

Definition at line 62 of file Buffer.hpp.

◆ iterator

template<typename T , std::size_t Alignment = config::default_alignment>
using stratax::core::Buffer< T, Alignment >::iterator = pointer

Mutable contiguous random-access iterator type.

Definition at line 72 of file Buffer.hpp.

◆ pointer

template<typename T , std::size_t Alignment = config::default_alignment>
using stratax::core::Buffer< T, Alignment >::pointer = T*

Mutable pointer to an element.

Definition at line 68 of file Buffer.hpp.

◆ reference

template<typename T , std::size_t Alignment = config::default_alignment>
using stratax::core::Buffer< T, Alignment >::reference = T&

Mutable element reference type.

Definition at line 64 of file Buffer.hpp.

◆ reverse_iterator

template<typename T , std::size_t Alignment = config::default_alignment>
using stratax::core::Buffer< T, Alignment >::reverse_iterator = std::reverse_iterator<iterator>

Mutable iterator that traverses elements in reverse order.

Definition at line 76 of file Buffer.hpp.

◆ size_type

template<typename T , std::size_t Alignment = config::default_alignment>
using stratax::core::Buffer< T, Alignment >::size_type = std::size_t

Unsigned type used for element counts and indices.

Definition at line 60 of file Buffer.hpp.

◆ value_type

template<typename T , std::size_t Alignment = config::default_alignment>
using stratax::core::Buffer< T, Alignment >::value_type = T

Type of each stored element.

Definition at line 58 of file Buffer.hpp.

Constructor & Destructor Documentation

◆ Buffer() [1/6]

template<typename T , std::size_t Alignment = config::default_alignment>
stratax::core::Buffer< T, Alignment >::Buffer ( )
inlinenoexcept

Constructs an empty buffer that owns no allocation.

size() is zero, empty() is true, and data() is nullptr.

Complexity
O(1).

Definition at line 114 of file Buffer.hpp.

◆ Buffer() [2/6]

template<typename T , std::size_t Alignment = config::default_alignment>
stratax::core::Buffer< T, Alignment >::Buffer ( size_type  size)
inlineexplicit

Constructs a buffer with value-initialized elements.

Each element is initialized from value_type{}. If element construction fails, already-constructed elements and the allocation are cleaned up.

Parameters
sizeNumber of elements to construct.
Exceptions
std::bad_array_new_lengthIf size exceeds max_size().
std::bad_allocIf storage allocation fails.
Anyexception thrown while constructing an element.
Complexity
O(size).

Definition at line 129 of file Buffer.hpp.

◆ Buffer() [3/6]

template<typename T , std::size_t Alignment = config::default_alignment>
stratax::core::Buffer< T, Alignment >::Buffer ( size_type  size,
const_reference  value 
)
inline

Constructs a buffer with all elements initialized to value.

If an element copy fails, already-constructed elements and the allocation are cleaned up.

Parameters
sizeNumber of elements to construct.
valueValue copied into each element.
Exceptions
std::bad_array_new_lengthIf size exceeds max_size().
std::bad_allocIf storage allocation fails.
Anyexception thrown by value_type's copy constructor.
Complexity
O(size).

Definition at line 148 of file Buffer.hpp.

◆ Buffer() [4/6]

template<typename T , std::size_t Alignment = config::default_alignment>
stratax::core::Buffer< T, Alignment >::Buffer ( std::initializer_list< value_type list)
inline

Constructs a buffer from an initializer list.

Elements are copy-constructed in list order. If an element copy fails, already-constructed elements and the allocation are cleaned up.

Parameters
listSource elements.
Exceptions
std::bad_array_new_lengthIf list size exceeds max_size().
std::bad_allocIf storage allocation fails.
Anyexception thrown by value_type's copy constructor.
Complexity
O(list.size()).

Definition at line 167 of file Buffer.hpp.

◆ Buffer() [5/6]

template<typename T , std::size_t Alignment = config::default_alignment>
stratax::core::Buffer< T, Alignment >::Buffer ( const Buffer< T, Alignment > &  other)
inline

Copy-constructs a buffer with independent storage.

If copying an element fails, all elements already constructed in the new allocation are destroyed and the exception is propagated.

Parameters
otherBuffer to copy from.
Exceptions
std::bad_array_new_lengthIf other.size() exceeds max_size().
std::bad_allocIf storage allocation fails.
Anyexception thrown by value_type's copy constructor.
Complexity
O(other.size()).

Definition at line 196 of file Buffer.hpp.

◆ Buffer() [6/6]

template<typename T , std::size_t Alignment = config::default_alignment>
stratax::core::Buffer< T, Alignment >::Buffer ( Buffer< T, Alignment > &&  other)
inlinenoexcept

Move-constructs a buffer by transferring ownership.

After construction, other is empty and owns no allocation. Existing pointers and iterators into other remain valid and now refer to elements owned by the newly constructed buffer. Element move constructors are not invoked.

Parameters
otherBuffer to move from.
Complexity
O(1).

Definition at line 224 of file Buffer.hpp.

◆ ~Buffer()

template<typename T , std::size_t Alignment = config::default_alignment>
stratax::core::Buffer< T, Alignment >::~Buffer ( )
inline

Destroys the buffer and releases owned storage.

Each live element is destroyed before the allocation is released.

Complexity
O(size()).

Definition at line 298 of file Buffer.hpp.

Member Function Documentation

◆ alignment()

template<typename T , std::size_t Alignment = config::default_alignment>
static constexpr size_type stratax::core::Buffer< T, Alignment >::alignment ( )
inlinestaticconstexprnoexcept

Returns the alignment, in bytes, used for buffer allocations.

This is a compile-time property of the Buffer specialization.

Returns
Requested alignment of the underlying allocation, in bytes.
Complexity
O(1).

Definition at line 88 of file Buffer.hpp.

◆ back() [1/2]

template<typename T , std::size_t Alignment = config::default_alignment>
reference stratax::core::Buffer< T, Alignment >::back ( )
inline

Returns a reference to the last element.

Returns
Reference to the last element.
Exceptions
Exceptions::IndexErrorIf the buffer is empty.
Complexity
O(1).

Definition at line 358 of file Buffer.hpp.

◆ back() [2/2]

template<typename T , std::size_t Alignment = config::default_alignment>
const_reference stratax::core::Buffer< T, Alignment >::back ( ) const
inline

Returns a const reference to the last element.

Returns
Const reference to the last element.
Exceptions
Exceptions::IndexErrorIf the buffer is empty.
Complexity
O(1).

Definition at line 370 of file Buffer.hpp.

◆ begin() [1/2]

template<typename T , std::size_t Alignment = config::default_alignment>
const_iterator stratax::core::Buffer< T, Alignment >::begin ( ) const
inlinenoexcept

Returns a const iterator to the first element.

Returns
data(), which equals end() when the buffer is empty.
Complexity
O(1).

Definition at line 415 of file Buffer.hpp.

◆ begin() [2/2]

template<typename T , std::size_t Alignment = config::default_alignment>
iterator stratax::core::Buffer< T, Alignment >::begin ( )
inlinenoexcept

Returns a mutable iterator to the first element.

Returns
data(), which equals end() when the buffer is empty.
Complexity
O(1).

Definition at line 409 of file Buffer.hpp.

◆ cbegin()

template<typename T , std::size_t Alignment = config::default_alignment>
const_iterator stratax::core::Buffer< T, Alignment >::cbegin ( ) const
inlinenoexcept

Returns a const iterator to the first element.

Returns
data(), which equals cend() when the buffer is empty.
Complexity
O(1).

Definition at line 421 of file Buffer.hpp.

◆ cend()

template<typename T , std::size_t Alignment = config::default_alignment>
const_iterator stratax::core::Buffer< T, Alignment >::cend ( ) const
inlinenoexcept

Returns a const iterator one past the final element.

Returns
Past-the-end iterator for the const element range.
Complexity
O(1).

Definition at line 439 of file Buffer.hpp.

◆ crbegin()

template<typename T , std::size_t Alignment = config::default_alignment>
const_reverse_iterator stratax::core::Buffer< T, Alignment >::crbegin ( ) const
inlinenoexcept

Returns a const reverse iterator to the final element.

Returns
Const reverse iterator constructed from cend().
Complexity
O(1).

Definition at line 457 of file Buffer.hpp.

◆ crend()

template<typename T , std::size_t Alignment = config::default_alignment>
const_reverse_iterator stratax::core::Buffer< T, Alignment >::crend ( ) const
inlinenoexcept

Returns the past-the-end iterator for const reverse traversal.

Returns
Const reverse iterator constructed from cbegin().
Complexity
O(1).

Definition at line 475 of file Buffer.hpp.

◆ data() [1/2]

template<typename T , std::size_t Alignment = config::default_alignment>
const_pointer stratax::core::Buffer< T, Alignment >::data ( ) const
inlinenoexcept

Returns a const pointer to the underlying contiguous storage.

Returns
Const pointer to storage, or nullptr when empty.

Elements occupy the range [data(), data() + size()) when non-empty. The pointer remains valid until this buffer is assigned to, moved from, swapped, or destroyed.

Complexity
O(1).

Definition at line 399 of file Buffer.hpp.

◆ data() [2/2]

template<typename T , std::size_t Alignment = config::default_alignment>
pointer stratax::core::Buffer< T, Alignment >::data ( )
inlinenoexcept

Returns a pointer to the underlying contiguous storage.

Returns
Pointer to storage, or nullptr when empty.

Elements occupy the range [data(), data() + size()) when non-empty. The pointer remains valid until this buffer is assigned to, moved from, swapped, or destroyed.

Complexity
O(1).

Definition at line 386 of file Buffer.hpp.

◆ empty()

template<typename T , std::size_t Alignment = config::default_alignment>
bool stratax::core::Buffer< T, Alignment >::empty ( ) const
inlinenoexcept

Returns whether the buffer has no elements.

Returns
true if size() is zero; otherwise false.
Complexity
O(1).

Definition at line 494 of file Buffer.hpp.

◆ end() [1/2]

template<typename T , std::size_t Alignment = config::default_alignment>
const_iterator stratax::core::Buffer< T, Alignment >::end ( ) const
inlinenoexcept

Returns a const iterator one past the final element.

Returns
Past-the-end iterator for the const element range.
Complexity
O(1).

Definition at line 433 of file Buffer.hpp.

◆ end() [2/2]

template<typename T , std::size_t Alignment = config::default_alignment>
iterator stratax::core::Buffer< T, Alignment >::end ( )
inlinenoexcept

Returns a mutable iterator one past the final element.

Returns
Past-the-end iterator for the mutable element range.
Complexity
O(1).

Definition at line 427 of file Buffer.hpp.

◆ fill()

template<typename T , std::size_t Alignment = config::default_alignment>
void stratax::core::Buffer< T, Alignment >::fill ( const_reference  value)
inline

Assigns a value to every element in the buffer.

Assignment proceeds from the first element to the last. If an assignment throws, earlier elements retain their new values while the remaining elements keep their previous values.

Parameters
valueValue copied into each element.
Exceptions
Anyexception thrown by value_type's copy-assignment operator.
Complexity
O(size()).

Definition at line 507 of file Buffer.hpp.

◆ front() [1/2]

template<typename T , std::size_t Alignment = config::default_alignment>
reference stratax::core::Buffer< T, Alignment >::front ( )
inline

Returns a reference to the first element.

Returns
Reference to the first element.
Exceptions
Exceptions::IndexErrorIf the buffer is empty.
Complexity
O(1).

Definition at line 334 of file Buffer.hpp.

◆ front() [2/2]

template<typename T , std::size_t Alignment = config::default_alignment>
const_reference stratax::core::Buffer< T, Alignment >::front ( ) const
inline

Returns a const reference to the first element.

Returns
Const reference to the first element.
Exceptions
Exceptions::IndexErrorIf the buffer is empty.
Complexity
O(1).

Definition at line 346 of file Buffer.hpp.

◆ max_size()

template<typename T , std::size_t Alignment = config::default_alignment>
static constexpr size_type stratax::core::Buffer< T, Alignment >::max_size ( )
inlinestaticconstexprnoexcept

Returns the largest element count that cannot overflow in bytes.

This is an arithmetic limit rather than a promise that an allocation of that size will succeed.

Returns
std::numeric_limits<size_type>::max() / sizeof(value_type).
Complexity
O(1).

Definition at line 102 of file Buffer.hpp.

◆ operator=() [1/2]

template<typename T , std::size_t Alignment = config::default_alignment>
Buffer & stratax::core::Buffer< T, Alignment >::operator= ( Buffer< T, Alignment > &&  other)
inlinenoexcept

Move-assigns from another buffer.

Existing elements are destroyed before ownership is transferred. After assignment, other is empty and owns no allocation. Pointers and iterators into other remain valid and now refer to elements owned by this buffer; those into this buffer's former storage are invalidated.

Parameters
otherBuffer to move from.
Returns
Reference to this buffer.
Complexity
O(size()) to destroy the previous elements; ownership transfer is O(1).

Definition at line 270 of file Buffer.hpp.

◆ operator=() [2/2]

template<typename T , std::size_t Alignment = config::default_alignment>
Buffer & stratax::core::Buffer< T, Alignment >::operator= ( const Buffer< T, Alignment > &  other)
inline

Copy-assigns from another buffer.

This operation provides the strong exception guarantee: if allocation or element copying fails, this buffer is unchanged. On success, previous pointers, references, and iterators into this buffer are invalidated.

Parameters
otherBuffer to copy from.
Returns
Reference to this buffer.
Exceptions
std::bad_array_new_lengthIf other.size() exceeds max_size().
std::bad_allocIf storage allocation fails.
Anyexception thrown by value_type's copy constructor.
Complexity
O(size() + other.size()).

Definition at line 245 of file Buffer.hpp.

◆ operator[]() [1/2]

template<typename T , std::size_t Alignment = config::default_alignment>
const_reference stratax::core::Buffer< T, Alignment >::operator[] ( size_type  index) const
inlinenoexcept

Returns an unchecked const reference to an element.

Parameters
indexElement position.
Returns
Const reference to the element at index.
Precondition
index < size(); otherwise behavior is undefined.
Complexity
O(1).

Definition at line 325 of file Buffer.hpp.

◆ operator[]() [2/2]

template<typename T , std::size_t Alignment = config::default_alignment>
reference stratax::core::Buffer< T, Alignment >::operator[] ( size_type  index)
inlinenoexcept

Returns an unchecked reference to an element.

Parameters
indexElement position.
Returns
Reference to the element at index.
Precondition
index < size(); otherwise behavior is undefined.
Complexity
O(1).

Definition at line 315 of file Buffer.hpp.

◆ rbegin() [1/2]

template<typename T , std::size_t Alignment = config::default_alignment>
const_reverse_iterator stratax::core::Buffer< T, Alignment >::rbegin ( ) const
inlinenoexcept

Returns a const reverse iterator to the final element.

Returns
Const reverse iterator constructed from end().
Complexity
O(1).

Definition at line 451 of file Buffer.hpp.

◆ rbegin() [2/2]

template<typename T , std::size_t Alignment = config::default_alignment>
reverse_iterator stratax::core::Buffer< T, Alignment >::rbegin ( )
inlinenoexcept

Returns a mutable reverse iterator to the final element.

Returns
Reverse iterator constructed from end().
Complexity
O(1).

Definition at line 445 of file Buffer.hpp.

◆ rend() [1/2]

template<typename T , std::size_t Alignment = config::default_alignment>
const_reverse_iterator stratax::core::Buffer< T, Alignment >::rend ( ) const
inlinenoexcept

Returns the past-the-end iterator for const reverse traversal.

Returns
Const reverse iterator constructed from begin().
Complexity
O(1).

Definition at line 469 of file Buffer.hpp.

◆ rend() [2/2]

template<typename T , std::size_t Alignment = config::default_alignment>
reverse_iterator stratax::core::Buffer< T, Alignment >::rend ( )
inlinenoexcept

Returns the past-the-end iterator for mutable reverse traversal.

Returns
Reverse iterator constructed from begin().
Complexity
O(1).

Definition at line 463 of file Buffer.hpp.

◆ size()

template<typename T , std::size_t Alignment = config::default_alignment>
size_type stratax::core::Buffer< T, Alignment >::size ( ) const
inlinenoexcept

Returns the number of stored elements.

Returns
Element count.

The count is fixed between construction and assignment.

Complexity
O(1).

Definition at line 486 of file Buffer.hpp.

◆ swap()

template<typename T , std::size_t Alignment = config::default_alignment>
void stratax::core::Buffer< T, Alignment >::swap ( Buffer< T, Alignment > &  other)
inlinenoexcept

Swaps storage and size with another buffer.

Parameters
otherBuffer to swap with.

This operation does not move, copy, or swap individual elements. Iterators and pointers continue to refer to the same elements, now owned by the other buffer. References are likewise unaffected.

Complexity
O(1).

Definition at line 523 of file Buffer.hpp.


The documentation for this class was generated from the following file: