Helios Engine
A modular ECS based data-oriented C++23 game engine framework
Loading...
Searching...
No Matches
helios::container::TypedBuffer< Allocator > Class Template Reference

Type-erased single-instance byte storage. More...

#include <typed_buffer.hpp>

Public Types

using allocator_type = Allocator
using size_type = size_t
using TypeIndex = utils::TypeIndex
using StorageType = std::vector<std::byte, allocator_type>

Public Member Functions

constexpr TypedBuffer () noexcept(std::is_nothrow_default_constructible_v< StorageType >)=default
 Default constructor. Creates an empty buffer with no associated type.
constexpr TypedBuffer (const allocator_type &alloc) noexcept(std::is_nothrow_constructible_v< StorageType, const allocator_type & >)
 Constructs an empty buffer with a custom allocator.
constexpr TypedBuffer (std::pmr::memory_resource *resource) noexcept(std::is_nothrow_constructible_v< allocator_type, std::pmr::memory_resource * >)
 Constructs an empty buffer from a PMR memory resource.
 TypedBuffer (std::nullptr_t)=delete
template<TypedBufferStorable T, typename... Args>
requires std::constructible_from<T, Args...>
constexpr TypedBuffer (std::in_place_type_t< T > tag, Args &&... args)
 Constructs a buffer holding a value of type T.
template<TypedBufferStorable T, typename... Args>
requires std::constructible_from<T, Args...>
constexpr TypedBuffer (std::in_place_type_t< T > tag, const allocator_type &alloc, Args &&... args)
 Constructs a buffer holding a value of type T with a custom allocator.
constexpr TypedBuffer (const TypedBuffer &other)
constexpr TypedBuffer (TypedBuffer &&other) noexcept(std::is_nothrow_move_constructible_v< StorageType >)
constexpr ~TypedBuffer () noexcept
constexpr TypedBufferoperator= (const TypedBuffer &other)
constexpr TypedBufferoperator= (TypedBuffer &&other) noexcept(std::is_nothrow_move_assignable_v< StorageType >)
template<TypedBufferStorable T>
constexpr void ChangeType () noexcept
 Changes the stored type, destroying the current value if any.
constexpr void Reset () noexcept
 Destroys the stored value and resets type information.
template<TypedBufferStorable T, typename... Args>
requires std::constructible_from<T, Args...>
T & Set (Args &&... args)
 Constructs (or replaces) the stored value in-place.
constexpr void Swap (TypedBuffer &other) noexcept(std::is_nothrow_swappable_v< StorageType >)
 Swaps contents with another TypedBuffer.
constexpr bool Empty () const noexcept
 Returns true if no value is currently stored.
constexpr bool HasType () const noexcept
 Returns true if a type has been associated with this buffer.
template<TypedBufferStorable T>
constexpr bool IsType () const noexcept
 Checks if the stored type matches T.
constexpr TypeIndex StoredTypeId () const noexcept
 Gets the current stored type index.
constexpr size_type ElementSize () const noexcept
 Returns the size in bytes of the stored element type, or 0 if no type is set.
template<TypedBufferStorable T>
T & Value () noexcept
 Accesses the stored value.
template<TypedBufferStorable T>
const T & Value () const noexcept
 Accesses the stored value (const).
constexpr auto Bytes () const noexcept -> std::span< const std::byte >
 Returns a const byte span of the stored value (empty if no value is stored).

Static Public Member Functions

template<TypedBufferStorable T>
static consteval TypeIndex TypeIndexOf () noexcept
 Gets the compile-time type index for T.

Friends

constexpr void swap (TypedBuffer &lhs, TypedBuffer &rhs) noexcept(std::is_nothrow_swappable_v< StorageType >)

Detailed Description

template<typename Allocator = std::allocator<std::byte>>
class helios::container::TypedBuffer< Allocator >

Type-erased single-instance byte storage.

Stores exactly one instance of any TypedBufferStorable type in a std::vector<std::byte> backing buffer. The stored type is not fixed at class instantiation; it is set at runtime and verified on every access. Object lifetime is properly managed. Trivially-copyable types are fast-pathed.

Template Parameters
AllocatorThe allocator type for the byte storage (default: std::allocator<std::byte>)

Definition at line 30 of file typed_buffer.hpp.

Member Typedef Documentation

◆ allocator_type

template<typename Allocator = std::allocator<std::byte>>
using helios::container::TypedBuffer< Allocator >::allocator_type = Allocator

Definition at line 32 of file typed_buffer.hpp.

◆ size_type

template<typename Allocator = std::allocator<std::byte>>
using helios::container::TypedBuffer< Allocator >::size_type = size_t

Definition at line 33 of file typed_buffer.hpp.

◆ StorageType

template<typename Allocator = std::allocator<std::byte>>
using helios::container::TypedBuffer< Allocator >::StorageType = std::vector<std::byte, allocator_type>

Definition at line 36 of file typed_buffer.hpp.

◆ TypeIndex

template<typename Allocator = std::allocator<std::byte>>
using helios::container::TypedBuffer< Allocator >::TypeIndex = utils::TypeIndex

Definition at line 34 of file typed_buffer.hpp.

Constructor & Destructor Documentation

◆ TypedBuffer() [1/8]

template<typename Allocator = std::allocator<std::byte>>
helios::container::TypedBuffer< Allocator >::TypedBuffer ( ) const
constexprdefaultnoexcept

Default constructor. Creates an empty buffer with no associated type.

◆ TypedBuffer() [2/8]

template<typename Allocator = std::allocator<std::byte>>
helios::container::TypedBuffer< Allocator >::TypedBuffer ( const allocator_type & alloc)
inlineexplicitconstexprnoexcept

Constructs an empty buffer with a custom allocator.

Parameters
allocAllocator instance to use

Definition at line 47 of file typed_buffer.hpp.

◆ TypedBuffer() [3/8]

template<typename Allocator = std::allocator<std::byte>>
helios::container::TypedBuffer< Allocator >::TypedBuffer ( std::pmr::memory_resource * resource)
inlineexplicitconstexprnoexcept

Constructs an empty buffer from a PMR memory resource.

Enabled only when allocator_type is constructible from std::pmr::memory_resource*.

Parameters
resourceMemory resource used to construct allocator

Definition at line 57 of file typed_buffer.hpp.

◆ TypedBuffer() [4/8]

template<typename Allocator = std::allocator<std::byte>>
helios::container::TypedBuffer< Allocator >::TypedBuffer ( std::nullptr_t )
delete

◆ TypedBuffer() [5/8]

template<typename Allocator>
requires std::constructible_from<T, Args...>
template<TypedBufferStorable T, typename... Args>
requires std::constructible_from<T, Args...>
helios::container::TypedBuffer< Allocator >::TypedBuffer ( std::in_place_type_t< T > tag,
Args &&... args )
explicitconstexpr

Constructs a buffer holding a value of type T.

Template Parameters
TThe type to store
ArgsConstructor argument types
Parameters
tagIn-place type construction tag (use std::in_place_type<T>)
argsArguments forwarded to T's constructor
TypedBuffer buf(std::in_place_type<int>, 42);
constexpr TypedBuffer() noexcept(std::is_nothrow_default_constructible_v< StorageType >)=default
Default constructor. Creates an empty buffer with no associated type.

Definition at line 251 of file typed_buffer.hpp.

◆ TypedBuffer() [6/8]

template<typename Allocator>
requires std::constructible_from<T, Args...>
template<TypedBufferStorable T, typename... Args>
requires std::constructible_from<T, Args...>
helios::container::TypedBuffer< Allocator >::TypedBuffer ( std::in_place_type_t< T > tag,
const allocator_type & alloc,
Args &&... args )
constexpr

Constructs a buffer holding a value of type T with a custom allocator.

Template Parameters
TThe type to store
ArgsConstructor argument types
Parameters
tagIn-place type construction tag (use std::in_place_type<T>)
allocAllocator instance to use
argsArguments forwarded to T's constructor

Definition at line 262 of file typed_buffer.hpp.

◆ TypedBuffer() [7/8]

template<typename Allocator>
helios::container::TypedBuffer< Allocator >::TypedBuffer ( const TypedBuffer< Allocator > & other)
constexpr

Definition at line 274 of file typed_buffer.hpp.

◆ TypedBuffer() [8/8]

template<typename Allocator>
helios::container::TypedBuffer< Allocator >::TypedBuffer ( TypedBuffer< Allocator > && other)
constexprnoexcept

Definition at line 299 of file typed_buffer.hpp.

◆ ~TypedBuffer()

template<typename Allocator = std::allocator<std::byte>>
helios::container::TypedBuffer< Allocator >::~TypedBuffer ( )
inlineconstexprnoexcept

Definition at line 96 of file typed_buffer.hpp.

Member Function Documentation

◆ Bytes()

template<typename Allocator>
auto helios::container::TypedBuffer< Allocator >::Bytes ( ) const -> std::span< const std::byte >
nodiscardconstexprnoexcept

Returns a const byte span of the stored value (empty if no value is stored).

Returns
Byte span of the stored value

Definition at line 418 of file typed_buffer.hpp.

◆ ChangeType()

template<typename Allocator>
template<TypedBufferStorable T>
void helios::container::TypedBuffer< Allocator >::ChangeType ( )
constexprnoexcept

Changes the stored type, destroying the current value if any.

After this call Empty() is true and IsType<T>() is true. The underlying memory is retained for reuse.

Template Parameters
TThe new type

Definition at line 362 of file typed_buffer.hpp.

◆ ElementSize()

template<typename Allocator = std::allocator<std::byte>>
size_type helios::container::TypedBuffer< Allocator >::ElementSize ( ) const
inlinenodiscardconstexprnoexcept

Returns the size in bytes of the stored element type, or 0 if no type is set.

Returns
Size of the stored type in bytes

Definition at line 190 of file typed_buffer.hpp.

◆ Empty()

template<typename Allocator = std::allocator<std::byte>>
bool helios::container::TypedBuffer< Allocator >::Empty ( ) const
inlinenodiscardconstexprnoexcept

Returns true if no value is currently stored.

Returns
True if empty, false if a value is stored

Definition at line 146 of file typed_buffer.hpp.

◆ HasType()

template<typename Allocator = std::allocator<std::byte>>
bool helios::container::TypedBuffer< Allocator >::HasType ( ) const
inlinenodiscardconstexprnoexcept

Returns true if a type has been associated with this buffer.

Returns
True if a type is set, false if no type information is present

Definition at line 152 of file typed_buffer.hpp.

◆ IsType()

template<typename Allocator = std::allocator<std::byte>>
template<TypedBufferStorable T>
bool helios::container::TypedBuffer< Allocator >::IsType ( ) const
inlinenodiscardconstexprnoexcept

Checks if the stored type matches T.

Template Parameters
TThe type to check against
Returns
true if types match or no type is set

Definition at line 162 of file typed_buffer.hpp.

◆ operator=() [1/2]

template<typename Allocator>
auto helios::container::TypedBuffer< Allocator >::operator= ( const TypedBuffer< Allocator > & other)
constexpr

Definition at line 309 of file typed_buffer.hpp.

◆ operator=() [2/2]

template<typename Allocator>
auto helios::container::TypedBuffer< Allocator >::operator= ( TypedBuffer< Allocator > && other)
constexprnoexcept

Definition at line 344 of file typed_buffer.hpp.

◆ Reset()

template<typename Allocator>
void helios::container::TypedBuffer< Allocator >::Reset ( )
constexprnoexcept

Destroys the stored value and resets type information.

After this call both Empty() and !HasType() are true.

Definition at line 368 of file typed_buffer.hpp.

◆ Set()

template<typename Allocator>
requires std::constructible_from<T, Args...>
template<TypedBufferStorable T, typename... Args>
requires std::constructible_from<T, Args...>
T & helios::container::TypedBuffer< Allocator >::Set ( Args &&... args)
inline

Constructs (or replaces) the stored value in-place.

If a value of a different type is already stored it is destroyed first. The type is updated to T before construction.

Template Parameters
TThe type to store
ArgsConstructor argument types
Parameters
argsArguments forwarded to T's constructor
Returns
Reference to the newly constructed value

Definition at line 377 of file typed_buffer.hpp.

◆ StoredTypeId()

template<typename Allocator = std::allocator<std::byte>>
TypeIndex helios::container::TypedBuffer< Allocator >::StoredTypeId ( ) const
inlinenodiscardconstexprnoexcept

Gets the current stored type index.

Returns
Type index of the stored type, or an invalid index if no type is set

Definition at line 181 of file typed_buffer.hpp.

◆ Swap()

template<typename Allocator>
void helios::container::TypedBuffer< Allocator >::Swap ( TypedBuffer< Allocator > & other)
constexprnoexcept

Swaps contents with another TypedBuffer.

Parameters
otherBuffer to swap with

Definition at line 392 of file typed_buffer.hpp.

◆ TypeIndexOf()

template<typename Allocator = std::allocator<std::byte>>
template<TypedBufferStorable T>
consteval TypeIndex helios::container::TypedBuffer< Allocator >::TypeIndexOf ( )
inlinestaticnodiscardconstevalnoexcept

Gets the compile-time type index for T.

Template Parameters
TThe type to get index for
Returns
Type index

Definition at line 172 of file typed_buffer.hpp.

◆ Value() [1/2]

template<typename Allocator>
template<TypedBufferStorable T>
const T & helios::container::TypedBuffer< Allocator >::Value ( ) const
inlinenodiscardnoexcept

Accesses the stored value (const).

Warning
Triggers assertion if the buffer is empty or the stored type doesn't match T.
Template Parameters
TThe expected type
Returns
Const reference to the stored value

Definition at line 410 of file typed_buffer.hpp.

◆ Value() [2/2]

template<typename Allocator>
template<TypedBufferStorable T>
T & helios::container::TypedBuffer< Allocator >::Value ( )
inlinenodiscardnoexcept

Accesses the stored value.

Warning
Triggers assertion if the buffer is empty or the stored type doesn't match T.
Template Parameters
TThe expected type
Returns
Reference to the stored value

Definition at line 401 of file typed_buffer.hpp.

◆ swap

template<typename Allocator = std::allocator<std::byte>>
void swap ( TypedBuffer< Allocator > & lhs,
TypedBuffer< Allocator > & rhs )
friend

Definition at line 137 of file typed_buffer.hpp.