GREM 3.12.0
Game/Rendering Engine Modules for C++
Loading...
Searching...
No Matches
grem::AnyBase< Base, SmallCapacity, SmallAlignment > Class Template Reference

Generic type-erased semi-regular value wrapper. More...

#include <GREM/core/data/Any.hpp>

Public Member Functions

constexpr AnyBase () noexcept
 Construct an empty wrapper.
template<typename T>
requires (!std::is_same_v<std::decay_t<T>, AnyBase>)
 AnyBase (T &&value)
 Construct a wrapper from a value.
 ~AnyBase ()
 Destructor.
 AnyBase (const AnyBase &other)
 Copy constructor.
 AnyBase (AnyBase &&other) noexcept
 Move constructor.
AnyBase & operator= (const AnyBase &other)
 Copy assignment.
AnyBase & operator= (AnyBase &&other) noexcept
 Move assignment.
void reset () noexcept
 Destroy the wrapped object if one exists, and reset the wrapper to empty.
template<typename T, typename... Args>
std::decay_t< T > & emplace (Args &&... args)
 Destroy the wrapped object if one exists, then replace it with a new object.
Base * base () noexcept
 Get a base pointer to the wrapped value.
const Base * base () const noexcept
 Get a base pointer to the wrapped value.
Base * getUnsafeSmallObjectBasePointer ()
 Get a base pointer to the wrapped value, under the assumption that the value currently held by the wrapper fits into the small object optimization (in both size and alignment), and also that its address can be safely cast to the base pointer type without applying any offset (which can only be true if the concrete value type only has one base class, and the base class is not virtual).
const Base * getUnsafeSmallObjectBasePointer () const
 Get a base pointer to the wrapped value, under the assumption that the value currently held by the wrapper fits into the small object optimization (in both size and alignment), and also that its address can be safely cast to the base pointer type without applying any offset (which can only be true if the concrete value type only has one base class, and the base class is not virtual).
Base * getUnsafeLargeObjectBasePointer ()
 Get a base pointer to the wrapped value, under the assumption that the value currently held by the wrapper does not fit into the small object optimization.
const Base * getUnsafeLargeObjectBasePointer () const
 Get a base pointer to the wrapped value, under the assumption that the value currently held by the wrapper does not fit into the small object optimization.
auto & operator* ()
 Get a base reference to the wrapped value.
const auto & operator* () const
 Get a base reference to the wrapped value.
Base * operator-> ()
 Get a base pointer to the wrapped value.
const Base * operator-> () const
 Get a base pointer to the wrapped value.
template<typename T>
std::decay_t< T > * get_if () noexcept
 Try to get a pointer to the wrapped value.
template<typename T>
const std::decay_t< T > * get_if () const noexcept
 Try to get a pointer to the wrapped value.
template<typename T>
std::decay_t< T > & get ()
 Access the wrapped value.
template<typename T>
const std::decay_t< T > & get () const
 Access the wrapped value.

Static Public Member Functions

template<typename T, typename... Args>
static AnyBase create (Args &&... args)
 Create a wrapper of a new value.

Detailed Description

template<typename Base, std::size_t SmallCapacity = sizeof(void*) * 3, std::size_t SmallAlignment = alignof(void*)>
class grem::AnyBase< Base, SmallCapacity, SmallAlignment >

Generic type-erased semi-regular value wrapper.

Template Parameters
Basegeneric base type, or void.
SmallCapacitymaximum capacity for small object optimization.
SmallAlignmentmaximum alignment for small object optimization.

Constructor & Destructor Documentation

◆ AnyBase() [1/4]

template<typename Base, std::size_t SmallCapacity = sizeof(void*) * 3, std::size_t SmallAlignment = alignof(void*)>
grem::AnyBase< Base, SmallCapacity, SmallAlignment >::AnyBase ( )
inlineconstexprnoexcept

Construct an empty wrapper.

◆ AnyBase() [2/4]

template<typename Base, std::size_t SmallCapacity = sizeof(void*) * 3, std::size_t SmallAlignment = alignof(void*)>
template<typename T>
requires (!std::is_same_v<std::decay_t<T>, AnyBase>)
grem::AnyBase< Base, SmallCapacity, SmallAlignment >::AnyBase ( T && value)
inline

Construct a wrapper from a value.

Parameters
valuevalue to forward to the stored value's constructor.
Exceptions
anyexception thrown by the value type's constructor.
std::bad_allocon allocation failure, if the value type does not qualify for small object optimization.
Note
The stored value's concrete type will be std::decay_t<T>.

◆ ~AnyBase()

template<typename Base, std::size_t SmallCapacity = sizeof(void*) * 3, std::size_t SmallAlignment = alignof(void*)>
grem::AnyBase< Base, SmallCapacity, SmallAlignment >::~AnyBase ( )
inline

Destructor.

◆ AnyBase() [3/4]

template<typename Base, std::size_t SmallCapacity = sizeof(void*) * 3, std::size_t SmallAlignment = alignof(void*)>
grem::AnyBase< Base, SmallCapacity, SmallAlignment >::AnyBase ( const AnyBase< Base, SmallCapacity, SmallAlignment > & other)
inline

Copy constructor.

◆ AnyBase() [4/4]

template<typename Base, std::size_t SmallCapacity = sizeof(void*) * 3, std::size_t SmallAlignment = alignof(void*)>
grem::AnyBase< Base, SmallCapacity, SmallAlignment >::AnyBase ( AnyBase< Base, SmallCapacity, SmallAlignment > && other)
inlinenoexcept

Move constructor.

Member Function Documentation

◆ create()

template<typename Base, std::size_t SmallCapacity = sizeof(void*) * 3, std::size_t SmallAlignment = alignof(void*)>
template<typename T, typename... Args>
AnyBase grem::AnyBase< Base, SmallCapacity, SmallAlignment >::create ( Args &&... args)
inlinestaticnodiscard

Create a wrapper of a new value.

Template Parameters
Ttype of the concrete value to create.
Parameters
argsarguments to forward to the constructor of the new value.
Returns
a wrapper around the newly created value.
Exceptions
anyexception thrown by the value type's constructor.
std::bad_allocon allocation failure, if the value type does not qualify for small object optimization.
Note
The stored value's concrete type will be std::decay_t<T>.

◆ operator=() [1/2]

template<typename Base, std::size_t SmallCapacity = sizeof(void*) * 3, std::size_t SmallAlignment = alignof(void*)>
AnyBase & grem::AnyBase< Base, SmallCapacity, SmallAlignment >::operator= ( const AnyBase< Base, SmallCapacity, SmallAlignment > & other)
inline

Copy assignment.

◆ operator=() [2/2]

template<typename Base, std::size_t SmallCapacity = sizeof(void*) * 3, std::size_t SmallAlignment = alignof(void*)>
AnyBase & grem::AnyBase< Base, SmallCapacity, SmallAlignment >::operator= ( AnyBase< Base, SmallCapacity, SmallAlignment > && other)
inlinenoexcept

Move assignment.

◆ reset()

template<typename Base, std::size_t SmallCapacity = sizeof(void*) * 3, std::size_t SmallAlignment = alignof(void*)>
void grem::AnyBase< Base, SmallCapacity, SmallAlignment >::reset ( )
inlinenoexcept

Destroy the wrapped object if one exists, and reset the wrapper to empty.

◆ emplace()

template<typename Base, std::size_t SmallCapacity = sizeof(void*) * 3, std::size_t SmallAlignment = alignof(void*)>
template<typename T, typename... Args>
std::decay_t< T > & grem::AnyBase< Base, SmallCapacity, SmallAlignment >::emplace ( Args &&... args)
inline

Destroy the wrapped object if one exists, then replace it with a new object.

Template Parameters
Ttype of the concrete value to create.
Parameters
argsarguments to forward to the constructor of the new value.
Returns
a reference to the newly created value.
Exceptions
anyexception thrown by the value type's constructor.
std::bad_allocon allocation failure, if the value type does not qualify for small object optimization.
Note
The stored value's concrete type will be std::decay_t<T>.

◆ base() [1/2]

template<typename Base, std::size_t SmallCapacity = sizeof(void*) * 3, std::size_t SmallAlignment = alignof(void*)>
Base * grem::AnyBase< Base, SmallCapacity, SmallAlignment >::base ( )
inlinenodiscardnoexcept

Get a base pointer to the wrapped value.

Returns
a non-owning pointer to the value held by the wrapper, or nullptr if the wrapper does not currently hold a value.
See also
get()

◆ base() [2/2]

template<typename Base, std::size_t SmallCapacity = sizeof(void*) * 3, std::size_t SmallAlignment = alignof(void*)>
const Base * grem::AnyBase< Base, SmallCapacity, SmallAlignment >::base ( ) const
inlinenodiscardnoexcept

Get a base pointer to the wrapped value.

Returns
a non-owning read-only pointer to the value held by the wrapper, or nullptr if the wrapper does not currently hold a value.
See also
get()

◆ getUnsafeSmallObjectBasePointer() [1/2]

template<typename Base, std::size_t SmallCapacity = sizeof(void*) * 3, std::size_t SmallAlignment = alignof(void*)>
Base * grem::AnyBase< Base, SmallCapacity, SmallAlignment >::getUnsafeSmallObjectBasePointer ( )
inlinenodiscard

Get a base pointer to the wrapped value, under the assumption that the value currently held by the wrapper fits into the small object optimization (in both size and alignment), and also that its address can be safely cast to the base pointer type without applying any offset (which can only be true if the concrete value type only has one base class, and the base class is not virtual).

Returns
a non-owning pointer to the value held by the wrapper.
Warning
Failure to meet the requirements described above results in undefined behavior.
See also
getUnsafeLargeObjectBasePointer()

◆ getUnsafeSmallObjectBasePointer() [2/2]

template<typename Base, std::size_t SmallCapacity = sizeof(void*) * 3, std::size_t SmallAlignment = alignof(void*)>
const Base * grem::AnyBase< Base, SmallCapacity, SmallAlignment >::getUnsafeSmallObjectBasePointer ( ) const
inlinenodiscard

Get a base pointer to the wrapped value, under the assumption that the value currently held by the wrapper fits into the small object optimization (in both size and alignment), and also that its address can be safely cast to the base pointer type without applying any offset (which can only be true if the concrete value type only has one base class, and the base class is not virtual).

Returns
a non-owning read-only pointer to the value held by the wrapper.
Warning
Failure to meet the requirements described above results in undefined behavior.
See also
getUnsafeLargeObjectBasePointer()

◆ getUnsafeLargeObjectBasePointer() [1/2]

template<typename Base, std::size_t SmallCapacity = sizeof(void*) * 3, std::size_t SmallAlignment = alignof(void*)>
Base * grem::AnyBase< Base, SmallCapacity, SmallAlignment >::getUnsafeLargeObjectBasePointer ( )
inlinenodiscard

Get a base pointer to the wrapped value, under the assumption that the value currently held by the wrapper does not fit into the small object optimization.

Returns
a non-owning pointer to the value held by the wrapper.
Warning
Failure to meet the requirements described above results in undefined behavior.
See also
getUnsafeSmallObjectBasePointer()

◆ getUnsafeLargeObjectBasePointer() [2/2]

template<typename Base, std::size_t SmallCapacity = sizeof(void*) * 3, std::size_t SmallAlignment = alignof(void*)>
const Base * grem::AnyBase< Base, SmallCapacity, SmallAlignment >::getUnsafeLargeObjectBasePointer ( ) const
inlinenodiscard

Get a base pointer to the wrapped value, under the assumption that the value currently held by the wrapper does not fit into the small object optimization.

Returns
a non-owning read-only pointer to the value held by the wrapper.
Warning
Failure to meet the requirements described above results in undefined behavior.
See also
getUnsafeSmallObjectBasePointer()

◆ operator*() [1/2]

template<typename Base, std::size_t SmallCapacity = sizeof(void*) * 3, std::size_t SmallAlignment = alignof(void*)>
auto & grem::AnyBase< Base, SmallCapacity, SmallAlignment >::operator* ( )
inlinenodiscard

Get a base reference to the wrapped value.

Returns
a non-owning reference to the value held by the wrapper.
See also
base()

◆ operator*() [2/2]

template<typename Base, std::size_t SmallCapacity = sizeof(void*) * 3, std::size_t SmallAlignment = alignof(void*)>
const auto & grem::AnyBase< Base, SmallCapacity, SmallAlignment >::operator* ( ) const
inlinenodiscard

Get a base reference to the wrapped value.

Returns
a non-owning read-only reference to the value held by the wrapper.
See also
base()

◆ operator->() [1/2]

template<typename Base, std::size_t SmallCapacity = sizeof(void*) * 3, std::size_t SmallAlignment = alignof(void*)>
Base * grem::AnyBase< Base, SmallCapacity, SmallAlignment >::operator-> ( )
inlinenodiscard

Get a base pointer to the wrapped value.

Returns
a non-owning pointer to the value held by the wrapper.
See also
base()

◆ operator->() [2/2]

template<typename Base, std::size_t SmallCapacity = sizeof(void*) * 3, std::size_t SmallAlignment = alignof(void*)>
const Base * grem::AnyBase< Base, SmallCapacity, SmallAlignment >::operator-> ( ) const
inlinenodiscard

Get a base pointer to the wrapped value.

Returns
a non-owning read-only pointer to the value held by the wrapper.
See also
base()

◆ get_if() [1/2]

template<typename Base, std::size_t SmallCapacity = sizeof(void*) * 3, std::size_t SmallAlignment = alignof(void*)>
template<typename T>
std::decay_t< T > * grem::AnyBase< Base, SmallCapacity, SmallAlignment >::get_if ( )
inlinenodiscardnoexcept

Try to get a pointer to the wrapped value.

Template Parameters
Ttype of the wrapped value to get.
Returns
a non-owning pointer to the value held by the wrapper, or nullptr if the wrapper does not currently hold a value of the given type.
See also
get()

◆ get_if() [2/2]

template<typename Base, std::size_t SmallCapacity = sizeof(void*) * 3, std::size_t SmallAlignment = alignof(void*)>
template<typename T>
const std::decay_t< T > * grem::AnyBase< Base, SmallCapacity, SmallAlignment >::get_if ( ) const
inlinenodiscardnoexcept

Try to get a pointer to the wrapped value.

Template Parameters
Ttype of the wrapped value to get.
Returns
a non-owning read-only pointer to the value held by the wrapper, or nullptr if the wrapper does not currently hold a value of the given type.
See also
get()

◆ get() [1/2]

template<typename Base, std::size_t SmallCapacity = sizeof(void*) * 3, std::size_t SmallAlignment = alignof(void*)>
template<typename T>
std::decay_t< T > & grem::AnyBase< Base, SmallCapacity, SmallAlignment >::get ( )
inlinenodiscard

Access the wrapped value.

Template Parameters
Ttype of the wrapped value to get.
Returns
a reference to the value held by the wrapper.
Exceptions
BadAnyAccessif the wrapper does not currently hold a value of the given type.
See also
get_if()

◆ get() [2/2]

template<typename Base, std::size_t SmallCapacity = sizeof(void*) * 3, std::size_t SmallAlignment = alignof(void*)>
template<typename T>
const std::decay_t< T > & grem::AnyBase< Base, SmallCapacity, SmallAlignment >::get ( ) const
inlinenodiscard

Access the wrapped value.

Template Parameters
Ttype of the wrapped value to get.
Returns
a read-only reference to the value held by the wrapper.
Exceptions
BadAnyAccessif the wrapper does not currently hold a value of the given type.
See also
get_if()

Member Data Documentation

◆ largeObject

template<typename Base, std::size_t SmallCapacity = sizeof(void*) * 3, std::size_t SmallAlignment = alignof(void*)>
Base* grem::AnyBase< Base, SmallCapacity, SmallAlignment >::largeObject

◆ smallObject

template<typename Base, std::size_t SmallCapacity = sizeof(void*) * 3, std::size_t SmallAlignment = alignof(void*)>
std::byte grem::AnyBase< Base, SmallCapacity, SmallAlignment >::smallObject[SmallCapacity]

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