#include <alpaqa/util/type-erasure.hpp>
Class for polymorphism through type erasure.
Saves the entire vtable, and uses small buffer optimization.
Definition at line 193 of file type-erasure.hpp.
Inheritance diagram for TypeErased< VTable, Allocator, SmallBufferSize >:
Collaboration diagram for TypeErased< VTable, Allocator, SmallBufferSize >:Classes | |
| struct | Deallocator |
| Deallocates the storage when destroyed. More... | |
Public Types | |
| using | allocator_type = Allocator |
Public Member Functions | |
| TypeErased () noexcept(noexcept(allocator_type()) &&noexcept(VTable()))=default | |
| Default constructor. | |
| template<class Alloc > | |
| TypeErased (std::allocator_arg_t, const Alloc &alloc) | |
| Default constructor (allocator aware). | |
| TypeErased (const TypeErased &other) | |
| Copy constructor. | |
| TypeErased (const TypeErased &other, const allocator_type &alloc) | |
| Copy constructor (allocator aware). | |
| TypeErased (std::allocator_arg_t, const allocator_type &alloc, const TypeErased &other) | |
| Copy constructor (allocator aware). | |
| TypeErased & | operator= (const TypeErased &other) |
| Copy assignment. | |
| TypeErased (TypeErased &&other) noexcept | |
| Move constructor. | |
| TypeErased (TypeErased &&other, const allocator_type &alloc) noexcept | |
| Move constructor (allocator aware). | |
| TypeErased (std::allocator_arg_t, const allocator_type &alloc, TypeErased &&other) noexcept | |
| Move constructor (allocator aware). | |
| TypeErased & | operator= (TypeErased &&other) noexcept |
| Move assignment. | |
| ~TypeErased () | |
| Destructor. | |
| template<class T , class Alloc > requires no_child_of_ours<T> | |
| TypeErased (std::allocator_arg_t, const Alloc &alloc, T &&d) | |
| Main constructor that type-erases the given argument. | |
| template<class T , class Alloc , class... Args> | |
| TypeErased (std::allocator_arg_t, const Alloc &alloc, std::in_place_type_t< T >, Args &&...args) | |
| Main constructor that type-erases the object constructed from the given argument. | |
| template<class T > requires no_child_of_ours<T> | |
| TypeErased (T &&d) | |
| Main constructor that type-erases the given argument. | |
| template<class T , class... Args> | |
| TypeErased (std::in_place_type_t< T >, Args &&...args) | |
| Main constructor that type-erases the object constructed from the given argument. | |
| operator bool () const noexcept | |
| Check if this wrapper wraps an object. | |
| bool | owns_referenced_object () const noexcept |
| Check if this wrapper owns the storage of the wrapped object, or whether it simply stores a reference to an object that was allocated elsewhere. | |
| bool | referenced_object_is_const () const noexcept |
| Check if the wrapped object is const. | |
| allocator_type | get_allocator () const noexcept |
| Get a copy of the allocator. | |
| const std::type_info & | type () const noexcept |
| Query the contained type. | |
| template<class T > requires (!std::is_const_v<T>) | |
| T & | as () & |
| Convert the type-erased object to the given type. | |
| template<class T > requires (std::is_const_v<T>) | |
| T & | as () const & |
| Convert the type-erased object to the given type. | |
| template<class T > | |
| T && | as () && |
| Convert the type-erased object to the given type. | |
| void * | get_pointer () const |
| Get a type-erased pointer to the wrapped object. | |
| const void * | get_const_pointer () const |
| Get a type-erased pointer to the wrapped object. | |
Static Public Member Functions | |
| template<class Ret , class T , class Alloc , class... Args> requires std::is_base_of_v<TypeErased, Ret> | |
| static Ret | make (std::allocator_arg_t tag, const Alloc &alloc, Args &&...args) |
| Construct a type-erased wrapper of type Ret for an object of type T, initialized in-place with the given arguments. | |
| template<class Ret , class T , class... Args> requires no_leading_allocator<Args...> | |
| static Ret | make (Args &&...args) |
| Construct a type-erased wrapper of type Ret for an object of type T, initialized in-place with the given arguments. | |
Static Public Attributes | |
| static constexpr size_t | small_buffer_size = SmallBufferSize |
Protected Member Functions | |
| template<class T , class... Args> | |
| void | construct_inplace (Args &&...args) |
| Ensure storage and construct the type-erased object of type T in-place. | |
| template<class Ret , class... FArgs, class... Args> | |
| decltype(auto) | call (Ret(*f)(const void *, FArgs...), Args &&...args) const |
Call the vtable function f with the given arguments args, implicitly passing the self pointer and vtable reference if necessary. | |
| template<class Ret , class... FArgs, class... Args> | |
| decltype(auto) | call (Ret(*f)(void *, FArgs...), Args &&...args) |
Call the vtable function f with the given arguments args, implicitly passing the self pointer and vtable reference if necessary. | |
| template<class Ret > | |
| decltype(auto) | call (Ret(*f)(const void *)) const |
Call the vtable function f with the given arguments args, implicitly passing the self pointer and vtable reference if necessary. | |
| template<class Ret > | |
| decltype(auto) | call (Ret(*f)(void *)) |
Call the vtable function f with the given arguments args, implicitly passing the self pointer and vtable reference if necessary. | |
| template<class Ret > | |
| decltype(auto) | call (Ret(*f)(const void *, const VTable &)) const |
Call the vtable function f with the given arguments args, implicitly passing the self pointer and vtable reference if necessary. | |
| template<class Ret > | |
| decltype(auto) | call (Ret(*f)(void *, const VTable &)) |
Call the vtable function f with the given arguments args, implicitly passing the self pointer and vtable reference if necessary. | |
Static Protected Member Functions | |
| static bool | size_indicates_ownership (size_t size) |
| static bool | size_indicates_const (size_t size) |
Protected Attributes | |
| void * | self = nullptr |
| Pointer to the stored object. | |
| size_t | size = invalid_size |
| Size required to store the object. | |
| VTable | vtable |
Static Protected Attributes | |
| static constexpr size_t | invalid_size |
| static constexpr size_t | mut_ref_size |
| static constexpr size_t | const_ref_size |
Private Types | |
| using | allocator_traits = std::allocator_traits< allocator_type > |
| using | buffer_type = std::array< std::byte, small_buffer_size > |
Private Member Functions | |
| Deallocator | allocate (size_t size) |
| Ensure that storage is available, either by using the small buffer if it is large enough, or by calling the allocator. | |
| void | deallocate () |
| Deallocate the memory without invoking the destructor. | |
| void | cleanup () |
| Destroy the type-erased object (if not empty), and deallocate the memory if necessary. | |
| template<bool CopyAllocator> | |
| void | do_copy_assign (const TypeErased &other) |
Private Attributes | |
| buffer_type | small_buffer |
| allocator_type | allocator |
Static Private Attributes | |
| template<class T > | |
| static constexpr auto | no_child_of_ours |
True if T is not a child class of TypeErased. | |
| using allocator_type = Allocator |
Definition at line 196 of file type-erasure.hpp.
|
private |
Definition at line 199 of file type-erasure.hpp.
|
private |
Definition at line 200 of file type-erasure.hpp.
|
defaultnoexcept |
Default constructor.
|
inline |
Default constructor (allocator aware).
Definition at line 236 of file type-erasure.hpp.
|
inline |
Copy constructor.
Definition at line 239 of file type-erasure.hpp.
|
inline |
Copy constructor (allocator aware).
Definition at line 246 of file type-erasure.hpp.
|
inline |
Copy constructor (allocator aware).
Definition at line 251 of file type-erasure.hpp.
|
inlinenoexcept |
Move constructor.
Definition at line 268 of file type-erasure.hpp.
|
inlinenoexcept |
Move constructor (allocator aware).
Definition at line 291 of file type-erasure.hpp.
|
inlinenoexcept |
Move constructor (allocator aware).
Definition at line 328 of file type-erasure.hpp.
|
inline |
Destructor.
Definition at line 389 of file type-erasure.hpp.
Here is the call graph for this function:
|
inlineexplicit |
Main constructor that type-erases the given argument.
Definition at line 394 of file type-erasure.hpp.
|
inlineexplicit |
Main constructor that type-erases the object constructed from the given argument.
Definition at line 401 of file type-erasure.hpp.
|
inlineexplicit |
Main constructor that type-erases the given argument.
Requirement prevents this constructor from taking precedence over the copy and move constructors.
Definition at line 411 of file type-erasure.hpp.
|
inlineexplicit |
Main constructor that type-erases the object constructed from the given argument.
Definition at line 417 of file type-erasure.hpp.
|
inline |
Copy assignment.
Definition at line 256 of file type-erasure.hpp.
Here is the call graph for this function:
|
inlinenoexcept |
Move assignment.
Definition at line 333 of file type-erasure.hpp.
Here is the call graph for this function:
|
inlinestatic |
Construct a type-erased wrapper of type Ret for an object of type T, initialized in-place with the given arguments.
Definition at line 425 of file type-erasure.hpp.
|
inlinestatic |
Construct a type-erased wrapper of type Ret for an object of type T, initialized in-place with the given arguments.
Definition at line 435 of file type-erasure.hpp.
|
inlineexplicitnoexcept |
Check if this wrapper wraps an object.
False for default-constructed objects.
Definition at line 442 of file type-erasure.hpp.
|
inlinenoexcept |
Check if this wrapper owns the storage of the wrapped object, or whether it simply stores a reference to an object that was allocated elsewhere.
Definition at line 447 of file type-erasure.hpp.
Here is the call graph for this function:
Here is the caller graph for this function:
|
inlinenoexcept |
Check if the wrapped object is const.
Definition at line 452 of file type-erasure.hpp.
Here is the call graph for this function:
Here is the caller graph for this function:
|
inlinenoexcept |
Get a copy of the allocator.
Definition at line 457 of file type-erasure.hpp.
|
inlinenoexcept |
Query the contained type.
Definition at line 460 of file type-erasure.hpp.
Here is the caller graph for this function:
|
inline |
Convert the type-erased object to the given type.
| alpaqa::util::bad_type_erased_type | If T does not match the stored type. |
Definition at line 469 of file type-erasure.hpp.
Here is the call graph for this function:
|
inline |
Convert the type-erased object to the given type.
| alpaqa::util::bad_type_erased_type | If T does not match the stored type. |
Definition at line 479 of file type-erasure.hpp.
Here is the call graph for this function:
|
inline |
Convert the type-erased object to the given type.
| alpaqa::util::bad_type_erased_type | If T does not match the stored type. |
Definition at line 486 of file type-erasure.hpp.
Here is the call graph for this function:
|
inline |
Get a type-erased pointer to the wrapped object.
| alpaqa::util::bad_type_erased_constness | If the wrapped object is const. |
Definition at line 498 of file type-erasure.hpp.
Here is the call graph for this function:
|
inline |
Get a type-erased pointer to the wrapped object.
Definition at line 504 of file type-erasure.hpp.
|
inlineprivate |
Ensure that storage is available, either by using the small buffer if it is large enough, or by calling the allocator.
Returns a RAII wrapper that deallocates the storage unless released.
Definition at line 530 of file type-erasure.hpp.
Here is the call graph for this function:
Here is the caller graph for this function:
|
inlineprivate |
Deallocate the memory without invoking the destructor.
Definition at line 542 of file type-erasure.hpp.
Here is the call graph for this function:
Here is the caller graph for this function:
|
inlineprivate |
Destroy the type-erased object (if not empty), and deallocate the memory if necessary.
Definition at line 554 of file type-erasure.hpp.
Here is the call graph for this function:
Here is the caller graph for this function:
|
inlineprivate |
|
inlineprotected |
Ensure storage and construct the type-erased object of type T in-place.
Definition at line 589 of file type-erasure.hpp.
Here is the call graph for this function:
|
inlineprotected |
Call the vtable function f with the given arguments args, implicitly passing the self pointer and vtable reference if necessary.
Definition at line 620 of file type-erasure.hpp.
|
inlineprotected |
Call the vtable function f with the given arguments args, implicitly passing the self pointer and vtable reference if necessary.
Definition at line 632 of file type-erasure.hpp.
Here is the call graph for this function:
|
inlineprotected |
Call the vtable function f with the given arguments args, implicitly passing the self pointer and vtable reference if necessary.
Definition at line 646 of file type-erasure.hpp.
|
inlineprotected |
Call the vtable function f with the given arguments args, implicitly passing the self pointer and vtable reference if necessary.
Definition at line 653 of file type-erasure.hpp.
Here is the call graph for this function:
|
inlineprotected |
Call the vtable function f with the given arguments args, implicitly passing the self pointer and vtable reference if necessary.
Definition at line 662 of file type-erasure.hpp.
|
inlineprotected |
Call the vtable function f with the given arguments args, implicitly passing the self pointer and vtable reference if necessary.
Definition at line 670 of file type-erasure.hpp.
Here is the call graph for this function:
|
staticconstexpr |
Definition at line 195 of file type-erasure.hpp.
|
private |
Definition at line 201 of file type-erasure.hpp.
|
private |
Definition at line 202 of file type-erasure.hpp.
|
staticconstexprprivate |
True if T is not a child class of TypeErased.
Definition at line 207 of file type-erasure.hpp.
|
staticconstexprprotected |
Definition at line 211 of file type-erasure.hpp.
|
staticconstexprprotected |
Definition at line 213 of file type-erasure.hpp.
|
staticconstexprprotected |
Definition at line 215 of file type-erasure.hpp.
|
protected |
Pointer to the stored object.
Definition at line 225 of file type-erasure.hpp.
|
protected |
Size required to store the object.
Definition at line 227 of file type-erasure.hpp.
|
protected |
Definition at line 228 of file type-erasure.hpp.