alpaqa sparse
Nonconvex constrained optimization
Loading...
Searching...
No Matches
Classes | Public Types | Public Member Functions | Static Public Member Functions | Static Public Attributes | Protected Member Functions | Protected Attributes | Static Protected Attributes | Private Types | Private Member Functions | Private Attributes | Static Private Attributes | List of all members
TypeErased< VTable, Allocator, SmallBufferSize > Class Template Reference

#include <alpaqa/util/type-erasure.hpp>

Detailed Description

template<class VTable = BasicVTable, class Allocator = std::allocator<std::byte>, size_t SmallBufferSize = default_te_buffer_size<VTable, Allocator>()>
class alpaqa::util::TypeErased< VTable, Allocator, SmallBufferSize >

Class for polymorphism through type erasure.

Saves the entire vtable, and uses small buffer optimization.

Definition at line 194 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()))=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, allocator_type alloc)
 Copy constructor (allocator aware).
 
TypeErasedoperator= (const TypeErased &other)
 Copy assignment.
 
 TypeErased (TypeErased &&other) noexcept
 Move constructor.
 
 TypeErased (TypeErased &&other, const allocator_type &alloc) noexcept
 Move constructor (allocator aware).
 
TypeErasedoperator= (TypeErased &&other) noexcept
 Move assignment.
 
 ~TypeErased ()
 Destructor.
 
template<class T , class Alloc >
 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, te_in_place_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 (te_in_place_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.
 
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 >
Tas () &
 Convert the type-erased object to the given type.
 
template<class T >
const Tas () const &
 Convert the type-erased object to the given type.
 
template<class T >
const T && as () &&
 Convert the type-erased object to the given type.
 

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(autocall (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(autocall (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(autocall (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(autocall (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(autocall (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(autocall (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.
 

Protected Attributes

voidself = 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
 

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.
 

Member Typedef Documentation

◆ allocator_type

template<class VTable = BasicVTable, class Allocator = std::allocator<std::byte>, size_t SmallBufferSize = default_te_buffer_size<VTable, Allocator>()>
using allocator_type = Allocator

Definition at line 197 of file type-erasure.hpp.

◆ allocator_traits

template<class VTable = BasicVTable, class Allocator = std::allocator<std::byte>, size_t SmallBufferSize = default_te_buffer_size<VTable, Allocator>()>
using allocator_traits = std::allocator_traits<allocator_type>
private

Definition at line 200 of file type-erasure.hpp.

◆ buffer_type

template<class VTable = BasicVTable, class Allocator = std::allocator<std::byte>, size_t SmallBufferSize = default_te_buffer_size<VTable, Allocator>()>
using buffer_type = std::array<std::byte, small_buffer_size>
private

Definition at line 201 of file type-erasure.hpp.

Constructor & Destructor Documentation

◆ TypeErased() [1/10]

template<class VTable = BasicVTable, class Allocator = std::allocator<std::byte>, size_t SmallBufferSize = default_te_buffer_size<VTable, Allocator>()>
TypeErased ( )
defaultnoexcept

Default constructor.

◆ TypeErased() [2/10]

template<class VTable = BasicVTable, class Allocator = std::allocator<std::byte>, size_t SmallBufferSize = default_te_buffer_size<VTable, Allocator>()>
template<class Alloc >
TypeErased ( std::allocator_arg_t  ,
const Alloc alloc 
)
inline

Default constructor (allocator aware).

Definition at line 225 of file type-erasure.hpp.

◆ TypeErased() [3/10]

template<class VTable = BasicVTable, class Allocator = std::allocator<std::byte>, size_t SmallBufferSize = default_te_buffer_size<VTable, Allocator>()>
TypeErased ( const TypeErased< VTable, Allocator, SmallBufferSize > &  other)
inline

Copy constructor.

Definition at line 227 of file type-erasure.hpp.

◆ TypeErased() [4/10]

template<class VTable = BasicVTable, class Allocator = std::allocator<std::byte>, size_t SmallBufferSize = default_te_buffer_size<VTable, Allocator>()>
TypeErased ( const TypeErased< VTable, Allocator, SmallBufferSize > &  other,
allocator_type  alloc 
)
inline

Copy constructor (allocator aware).

Definition at line 233 of file type-erasure.hpp.

◆ TypeErased() [5/10]

template<class VTable = BasicVTable, class Allocator = std::allocator<std::byte>, size_t SmallBufferSize = default_te_buffer_size<VTable, Allocator>()>
TypeErased ( TypeErased< VTable, Allocator, SmallBufferSize > &&  other)
inlinenoexcept

Move constructor.

Definition at line 249 of file type-erasure.hpp.

◆ TypeErased() [6/10]

template<class VTable = BasicVTable, class Allocator = std::allocator<std::byte>, size_t SmallBufferSize = default_te_buffer_size<VTable, Allocator>()>
TypeErased ( TypeErased< VTable, Allocator, SmallBufferSize > &&  other,
const allocator_type alloc 
)
inlinenoexcept

Move constructor (allocator aware).

Definition at line 267 of file type-erasure.hpp.

◆ ~TypeErased()

template<class VTable = BasicVTable, class Allocator = std::allocator<std::byte>, size_t SmallBufferSize = default_te_buffer_size<VTable, Allocator>()>
~TypeErased ( )
inline

Destructor.

Definition at line 348 of file type-erasure.hpp.

+ Here is the call graph for this function:

◆ TypeErased() [7/10]

template<class VTable = BasicVTable, class Allocator = std::allocator<std::byte>, size_t SmallBufferSize = default_te_buffer_size<VTable, Allocator>()>
template<class T , class Alloc >
TypeErased ( std::allocator_arg_t  ,
const Alloc alloc,
T &&  d 
)
inlineexplicit

Main constructor that type-erases the given argument.

Definition at line 352 of file type-erasure.hpp.

◆ TypeErased() [8/10]

template<class VTable = BasicVTable, class Allocator = std::allocator<std::byte>, size_t SmallBufferSize = default_te_buffer_size<VTable, Allocator>()>
template<class T , class Alloc , class... Args>
TypeErased ( std::allocator_arg_t  ,
const Alloc alloc,
te_in_place_t< T ,
Args &&...  args 
)
inlineexplicit

Main constructor that type-erases the object constructed from the given argument.

Definition at line 359 of file type-erasure.hpp.

◆ TypeErased() [9/10]

template<class VTable = BasicVTable, class Allocator = std::allocator<std::byte>, size_t SmallBufferSize = default_te_buffer_size<VTable, Allocator>()>
template<class T >
requires no_child_of_ours<T>
TypeErased ( T &&  d)
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 369 of file type-erasure.hpp.

◆ TypeErased() [10/10]

template<class VTable = BasicVTable, class Allocator = std::allocator<std::byte>, size_t SmallBufferSize = default_te_buffer_size<VTable, Allocator>()>
template<class T , class... Args>
TypeErased ( te_in_place_t< T ,
Args &&...  args 
)
inlineexplicit

Main constructor that type-erases the object constructed from the given argument.

Definition at line 375 of file type-erasure.hpp.

Member Function Documentation

◆ operator=() [1/2]

template<class VTable = BasicVTable, class Allocator = std::allocator<std::byte>, size_t SmallBufferSize = default_te_buffer_size<VTable, Allocator>()>
TypeErased & operator= ( const TypeErased< VTable, Allocator, SmallBufferSize > &  other)
inline

Copy assignment.

Definition at line 238 of file type-erasure.hpp.

+ Here is the call graph for this function:

◆ operator=() [2/2]

template<class VTable = BasicVTable, class Allocator = std::allocator<std::byte>, size_t SmallBufferSize = default_te_buffer_size<VTable, Allocator>()>
TypeErased & operator= ( TypeErased< VTable, Allocator, SmallBufferSize > &&  other)
inlinenoexcept

Move assignment.

Definition at line 300 of file type-erasure.hpp.

+ Here is the call graph for this function:

◆ make() [1/2]

template<class VTable = BasicVTable, class Allocator = std::allocator<std::byte>, size_t SmallBufferSize = default_te_buffer_size<VTable, Allocator>()>
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 
)
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 383 of file type-erasure.hpp.

◆ make() [2/2]

template<class VTable = BasicVTable, class Allocator = std::allocator<std::byte>, size_t SmallBufferSize = default_te_buffer_size<VTable, Allocator>()>
template<class Ret , class T , class... Args>
requires no_leading_allocator<Args...>
static Ret make ( Args &&...  args)
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 393 of file type-erasure.hpp.

◆ operator bool()

template<class VTable = BasicVTable, class Allocator = std::allocator<std::byte>, size_t SmallBufferSize = default_te_buffer_size<VTable, Allocator>()>
operator bool ( ) const
inlineexplicitnoexcept

Check if this wrapper wraps an object.

False for default-constructed objects.

Definition at line 400 of file type-erasure.hpp.

◆ get_allocator()

template<class VTable = BasicVTable, class Allocator = std::allocator<std::byte>, size_t SmallBufferSize = default_te_buffer_size<VTable, Allocator>()>
allocator_type get_allocator ( ) const
inlinenoexcept

Get a copy of the allocator.

Definition at line 403 of file type-erasure.hpp.

◆ type()

template<class VTable = BasicVTable, class Allocator = std::allocator<std::byte>, size_t SmallBufferSize = default_te_buffer_size<VTable, Allocator>()>
const std::type_info & type ( ) const
inlinenoexcept

Query the contained type.

Definition at line 406 of file type-erasure.hpp.

+ Here is the caller graph for this function:

◆ as() [1/3]

template<class VTable = BasicVTable, class Allocator = std::allocator<std::byte>, size_t SmallBufferSize = default_te_buffer_size<VTable, Allocator>()>
template<class T >
T & as ( ) &
inline

Convert the type-erased object to the given type.

The type is checked in debug builds only, use with caution.

Definition at line 413 of file type-erasure.hpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ as() [2/3]

template<class VTable = BasicVTable, class Allocator = std::allocator<std::byte>, size_t SmallBufferSize = default_te_buffer_size<VTable, Allocator>()>
template<class T >
const T & as ( ) const &
inline

Convert the type-erased object to the given type.

The type is checked in debug builds only, use with caution.

Definition at line 420 of file type-erasure.hpp.

+ Here is the call graph for this function:

◆ as() [3/3]

template<class VTable = BasicVTable, class Allocator = std::allocator<std::byte>, size_t SmallBufferSize = default_te_buffer_size<VTable, Allocator>()>
template<class T >
const T && as ( ) &&
inline

Convert the type-erased object to the given type.

The type is checked in debug builds only, use with caution.

Definition at line 427 of file type-erasure.hpp.

+ Here is the call graph for this function:

◆ allocate()

template<class VTable = BasicVTable, class Allocator = std::allocator<std::byte>, size_t SmallBufferSize = default_te_buffer_size<VTable, Allocator>()>
Deallocator allocate ( size_t  size)
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 450 of file type-erasure.hpp.

+ Here is the caller graph for this function:

◆ deallocate()

template<class VTable = BasicVTable, class Allocator = std::allocator<std::byte>, size_t SmallBufferSize = default_te_buffer_size<VTable, Allocator>()>
void deallocate ( )
inlineprivate

Deallocate the memory without invoking the destructor.

Definition at line 460 of file type-erasure.hpp.

+ Here is the caller graph for this function:

◆ cleanup()

template<class VTable = BasicVTable, class Allocator = std::allocator<std::byte>, size_t SmallBufferSize = default_te_buffer_size<VTable, Allocator>()>
void cleanup ( )
inlineprivate

Destroy the type-erased object (if not empty), and deallocate the memory if necessary.

Definition at line 470 of file type-erasure.hpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ do_copy_assign()

template<class VTable = BasicVTable, class Allocator = std::allocator<std::byte>, size_t SmallBufferSize = default_te_buffer_size<VTable, Allocator>()>
template<bool CopyAllocator>
void do_copy_assign ( const TypeErased< VTable, Allocator, SmallBufferSize > &  other)
inlineprivate

Definition at line 478 of file type-erasure.hpp.

+ Here is the call graph for this function:

◆ construct_inplace()

template<class VTable = BasicVTable, class Allocator = std::allocator<std::byte>, size_t SmallBufferSize = default_te_buffer_size<VTable, Allocator>()>
template<class T , class... Args>
void construct_inplace ( Args &&...  args)
inlineprotected

Ensure storage and construct the type-erased object of type T in-place.

Definition at line 498 of file type-erasure.hpp.

+ Here is the call graph for this function:

◆ call() [1/6]

template<class VTable = BasicVTable, class Allocator = std::allocator<std::byte>, size_t SmallBufferSize = default_te_buffer_size<VTable, Allocator>()>
template<class Ret , class... FArgs, class... Args>
decltype(auto) call ( Ret(*)(const void *, FArgs...)  f,
Args &&...  args 
) const
inlineprotected

Call the vtable function f with the given arguments args, implicitly passing the self pointer and vtable reference if necessary.

Definition at line 514 of file type-erasure.hpp.

◆ call() [2/6]

template<class VTable = BasicVTable, class Allocator = std::allocator<std::byte>, size_t SmallBufferSize = default_te_buffer_size<VTable, Allocator>()>
template<class Ret , class... FArgs, class... Args>
decltype(auto) call ( Ret(*)(void *, FArgs...)  f,
Args &&...  args 
)
inlineprotected

Call the vtable function f with the given arguments args, implicitly passing the self pointer and vtable reference if necessary.

Definition at line 526 of file type-erasure.hpp.

◆ call() [3/6]

template<class VTable = BasicVTable, class Allocator = std::allocator<std::byte>, size_t SmallBufferSize = default_te_buffer_size<VTable, Allocator>()>
template<class Ret >
decltype(auto) call ( Ret(*)(const void *)  f) const
inlineprotected

Call the vtable function f with the given arguments args, implicitly passing the self pointer and vtable reference if necessary.

Definition at line 538 of file type-erasure.hpp.

◆ call() [4/6]

template<class VTable = BasicVTable, class Allocator = std::allocator<std::byte>, size_t SmallBufferSize = default_te_buffer_size<VTable, Allocator>()>
template<class Ret >
decltype(auto) call ( Ret(*)(void *)  f)
inlineprotected

Call the vtable function f with the given arguments args, implicitly passing the self pointer and vtable reference if necessary.

Definition at line 545 of file type-erasure.hpp.

◆ call() [5/6]

template<class VTable = BasicVTable, class Allocator = std::allocator<std::byte>, size_t SmallBufferSize = default_te_buffer_size<VTable, Allocator>()>
template<class Ret >
decltype(auto) call ( Ret(*)(const void *, const VTable &)  f) const
inlineprotected

Call the vtable function f with the given arguments args, implicitly passing the self pointer and vtable reference if necessary.

Definition at line 552 of file type-erasure.hpp.

◆ call() [6/6]

template<class VTable = BasicVTable, class Allocator = std::allocator<std::byte>, size_t SmallBufferSize = default_te_buffer_size<VTable, Allocator>()>
template<class Ret >
decltype(auto) call ( Ret(*)(void *, const VTable &)  f)
inlineprotected

Call the vtable function f with the given arguments args, implicitly passing the self pointer and vtable reference if necessary.

Definition at line 560 of file type-erasure.hpp.

Member Data Documentation

◆ small_buffer_size

template<class VTable = BasicVTable, class Allocator = std::allocator<std::byte>, size_t SmallBufferSize = default_te_buffer_size<VTable, Allocator>()>
constexpr size_t small_buffer_size = SmallBufferSize
staticconstexpr

Definition at line 196 of file type-erasure.hpp.

◆ small_buffer

template<class VTable = BasicVTable, class Allocator = std::allocator<std::byte>, size_t SmallBufferSize = default_te_buffer_size<VTable, Allocator>()>
buffer_type small_buffer
private

Definition at line 202 of file type-erasure.hpp.

◆ allocator

template<class VTable = BasicVTable, class Allocator = std::allocator<std::byte>, size_t SmallBufferSize = default_te_buffer_size<VTable, Allocator>()>
allocator_type allocator
private

Definition at line 203 of file type-erasure.hpp.

◆ no_child_of_ours

template<class VTable = BasicVTable, class Allocator = std::allocator<std::byte>, size_t SmallBufferSize = default_te_buffer_size<VTable, Allocator>()>
template<class T >
constexpr auto no_child_of_ours
staticconstexprprivate
Initial value:
=
!std::is_base_of_v<TypeErased, std::remove_cvref_t<T>>

True if T is not a child class of TypeErased.

Definition at line 208 of file type-erasure.hpp.

◆ invalid_size

template<class VTable = BasicVTable, class Allocator = std::allocator<std::byte>, size_t SmallBufferSize = default_te_buffer_size<VTable, Allocator>()>
constexpr size_t invalid_size
staticconstexprprotected
Initial value:
=
static_cast<size_t>(0xDEADBEEFDEADBEEF)

Definition at line 212 of file type-erasure.hpp.

◆ self

template<class VTable = BasicVTable, class Allocator = std::allocator<std::byte>, size_t SmallBufferSize = default_te_buffer_size<VTable, Allocator>()>
void* self = nullptr
protected

Pointer to the stored object.

Definition at line 215 of file type-erasure.hpp.

◆ size

template<class VTable = BasicVTable, class Allocator = std::allocator<std::byte>, size_t SmallBufferSize = default_te_buffer_size<VTable, Allocator>()>
size_t size = invalid_size
protected

Size required to store the object.

Definition at line 217 of file type-erasure.hpp.

◆ vtable

template<class VTable = BasicVTable, class Allocator = std::allocator<std::byte>, size_t SmallBufferSize = default_te_buffer_size<VTable, Allocator>()>
VTable vtable
protected

Definition at line 218 of file type-erasure.hpp.


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