guanaqo 1.0.0-alpha.27
Utilities for scientific software
Loading...
Searching...
No Matches
Type Erasure

Detailed Description

Small-buffer-optimized type erasure with configurable vtables.

Files

file  any-ptr.hpp
 Non-owning and shared pointer wrappers with type tagging.
file  required-method.hpp
 Macros to populate type-erased vtables from member functions in guanaqo::TypeErased.
file  type-erasure.hpp
 Flexible type erasure utilities.

Concepts

concept  guanaqo::derived_from_TypeErased

Classes

class  guanaqo::any_ptr
 Like std::any, but storing just the pointer, without any dynamic allocation. More...
class  guanaqo::shared_any_ptr
 Like std::any, but storing a shared_ptr, to allow for move-only types. More...
class  guanaqo::bad_type_erased_type
class  guanaqo::bad_type_erased_constness
struct  guanaqo::CopyMoveDestroyVTable
struct  guanaqo::BasicVTable
 Struct that stores pointers to functions to copy, move or destroy a polymorphic object. More...
class  guanaqo::TypeErased< VTable, Allocator, SmallBufferSize >
 Class for polymorphism through type erasure. More...

Macros

#define GUANAQO_TE_REQUIRED_METHOD(vtable, type, member)
#define GUANAQO_TE_OPTIONAL_METHOD(vtable, type, member, instance)

Typedefs

template<class F>
using guanaqo::required_function_t = typename detail::required_function<F>::type
 A required function includes a void pointer to self, in addition to the arguments of F.
template<class F, class VTable>
using guanaqo::optional_function_t = typename detail::optional_function<F, VTable>::type
 An optional function includes a void pointer to self, the arguments of F, and an additional reference to the VTable, so that it can be implemented in terms of other functions.

Functions

template<class Class, auto Method, class... ExtraArgs>
constexpr auto guanaqo::type_erased_wrapped ()
 Returns a function that accepts a void pointer, casts it to the class type of the member function Method, launders it, and then invokes Method with it, passing on the arguments to Method.
template<class VTable, class Allocator>
constexpr size_t guanaqo::default_te_buffer_size ()
template<class... Types>
constexpr size_t guanaqo::required_te_buffer_size_for ()

Variables

template<class T>
constexpr CopyMoveDestroyVTable guanaqo::copy_move_destroy_vtable

Class Documentation

◆ guanaqo::CopyMoveDestroyVTable

struct guanaqo::CopyMoveDestroyVTable
Collaboration diagram for guanaqo::CopyMoveDestroyVTable:
Class Members
required_function_t< void(void *storage) const > copy Copy-construct a new instance into storage.
required_function_t< void(void *storage)> move Move-construct a new instance into storage.
required_function_t< void()> destroy Destruct the given instance.

Macro Definition Documentation

◆ GUANAQO_TE_REQUIRED_METHOD

#define GUANAQO_TE_REQUIRED_METHOD ( vtable,
type,
member )

#include <guanaqo/required-method.hpp>

Value:
do { \
static_assert( \
requires { &type::member; }, \
"Missing required method '" #type "::" #member "'"); \
(vtable).member = \
} while (0)
constexpr auto type_erased_wrapped()
Returns a function that accepts a void pointer, casts it to the class type of the member function Met...

Definition at line 11 of file required-method.hpp.

◆ GUANAQO_TE_OPTIONAL_METHOD

#define GUANAQO_TE_OPTIONAL_METHOD ( vtable,
type,
member,
instance )

#include <guanaqo/required-method.hpp>

Value:
do { \
if constexpr (requires { &type::member; }) { \
using vtable_t = ::std::remove_cvref_t<decltype(vtable)>; \
auto assign_vtable = [&] { \
(vtable).member = \
::guanaqo::type_erased_wrapped<type, &type::member, \
const vtable_t &>(); \
}; \
if constexpr (requires { &type::provides_##member; }) { \
if (::std::invoke(&type::provides_##member, instance)) \
assign_vtable(); \
} else { \
assign_vtable(); \
} \
} \
} while (0)

Definition at line 20 of file required-method.hpp.

Typedef Documentation

◆ required_function_t

template<class F>
using guanaqo::required_function_t = typename detail::required_function<F>::type

#include <guanaqo/type-erasure.hpp>

A required function includes a void pointer to self, in addition to the arguments of F.

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

◆ optional_function_t

template<class F, class VTable>
using guanaqo::optional_function_t = typename detail::optional_function<F, VTable>::type

#include <guanaqo/type-erasure.hpp>

An optional function includes a void pointer to self, the arguments of F, and an additional reference to the VTable, so that it can be implemented in terms of other functions.

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

Function Documentation

◆ type_erased_wrapped()

template<class Class, auto Method, class... ExtraArgs>
auto guanaqo::type_erased_wrapped ( )
constexpr

#include <guanaqo/type-erasure.hpp>

Returns a function that accepts a void pointer, casts it to the class type of the member function Method, launders it, and then invokes Method with it, passing on the arguments to Method.

The function can also accept additional arguments at the end, of type ExtraArgs.

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

◆ default_te_buffer_size()

template<class VTable, class Allocator>
size_t guanaqo::default_te_buffer_size ( )
constexpr

#include <guanaqo/type-erasure.hpp>

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

◆ required_te_buffer_size_for()

template<class... Types>
size_t guanaqo::required_te_buffer_size_for ( )
constexpr

#include <guanaqo/type-erasure.hpp>

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

Variable Documentation

◆ copy_move_destroy_vtable

template<class T>
CopyMoveDestroyVTable guanaqo::copy_move_destroy_vtable
constexpr

#include <guanaqo/type-erasure.hpp>

Initial value:
= {
.copy =
[](const void *self, void *storage) {
new (storage) T(*std::launder(reinterpret_cast<const T *>(self)));
},
.move =
[](void *self, void *storage) noexcept {
if constexpr (std::is_const_v<T>)
std::terminate();
else
new (storage)
T(std::move(*std::launder(reinterpret_cast<T *>(self))));
},
.destroy =
[](void *self) {
if constexpr (std::is_const_v<T>)
std::terminate();
else
std::destroy_at(std::launder(reinterpret_cast<T *>(self)));
},
}

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