24 const std::type_info &requested_type,
25 const std::string &message =
"")
26 : std::logic_error{message}, actual_type{actual_type},
27 requested_type{requested_type} {}
29 [[nodiscard]]
const char *
what() const noexcept
override {
31 if (
const char *w = std::logic_error::what(); w && *w) {
37 return message.c_str();
53 template <
class R,
class...
Args>
59 template <
class R,
class...
Args>
63 template <
class,
class VTable = BasicVTable>
65 template <
class R,
class...
Args,
class VTable>
67 using type = R (*)(
void *self,
Args...,
const VTable &);
69 template <
class,
class VTable = BasicVTable>
71 template <
class R,
class...
Args,
class VTable>
73 using type = R (*)(
const void *self,
Args...,
const VTable &);
86 template <
class F,
class VTable = BasicVTable>
90 template <
class F,
class VTable = BasicVTable>
107 copy = [](
const void *self,
void *storage) {
108 new (storage)
T(*std::launder(
reinterpret_cast<const T *
>(self)));
111 move = [](
void *self,
void *storage)
noexcept {
113 T(std::move(*std::launder(
reinterpret_cast<T *
>(self))));
116 std::launder(
reinterpret_cast<T *
>(self))->~T();
126 template <
auto M,
class V,
class C,
class R,
class...
Args>
127 [[gnu::always_inline]]
static constexpr auto
129 return std::invoke(
M, *std::launder(
reinterpret_cast<C *
>(self)),
130 std::forward<Args>(
args)...);
132 template <
auto M,
class T,
class R,
class...
Args>
133 requires std::is_base_of_v<T, Class>
138 template <
auto M,
class T,
class R,
class...
Args>
139 requires std::is_base_of_v<T, Class>
140 [[gnu::always_inline]]
static constexpr auto
150 template <auto Method>
151 [[gnu::always_inline]]
static constexpr auto invoker() {
163template <
class VTable,
class Allocator>
166 [[no_unique_address]] Allocator allocator;
167 void *self =
nullptr;
174template <
class...
Types>
176 constexpr size_t sizes[] = {
sizeof(
Types)...};
177 return *std::max_element(std::begin(sizes), std::end(sizes));
192 class Allocator = std::allocator<std::byte>,
209 !std::is_base_of_v<TypeErased, std::remove_cvref_t<T>>;
213 static_cast<size_t>(0xDEADBEEFDEADBEEF);
256 self = std::exchange(
other.self,
nullptr);
259 else if (
other.self) {
263 other.self =
nullptr;
270 if (
other.self ==
nullptr)
278 self = std::exchange(
other.self,
nullptr);
291 else if (
other.self) {
296 other.self =
nullptr;
308 allocator_traits::propagate_on_container_move_assignment::value;
312 if (
other.self ==
nullptr)
321 self = std::exchange(
other.self,
nullptr);
331 using pointer_t =
typename allocator_traits::pointer;
334 other.self =
nullptr;
338 else if (
other.self) {
342 other.self =
nullptr;
351 template <
class T,
class Alloc>
358 template <
class T,
class Alloc,
class...
Args>
374 template <
class T,
class...
Args>
382 requires std::is_base_of_v<TypeErased, Ret>
391 template <
class Ret,
class T,
class...
Args>
395 std::forward<Args>(
args)...);
414 if (
typeid(
T) !=
type())
416 return *
reinterpret_cast<T *
>(
self);
421 if (
typeid(
T) !=
type())
423 return *
reinterpret_cast<const T *
>(
self);
428 if (
typeid(
T) !=
type())
430 return std::move(*
reinterpret_cast<T *
>(
self));
441 :
instance{std::exchange(
o.instance,
nullptr)} {}
462 using pointer_t =
typename allocator_traits::pointer;
477 template <
bool CopyAllocator>
480 allocator_traits::propagate_on_container_copy_assignment::value;
497 template <
class T,
class...
Args>
499 static_assert(std::is_same_v<T, std::remove_cvref_t<T>>);
503 using destroyer = std::unique_ptr<T, noop_delete<T>>;
514 [[gnu::always_inline]]
decltype(
auto)
call(
Ret (*f)(
const void *,
FArgs...),
515 Args &&...args)
const {
519 if constexpr (std::is_same_v<LastArg, const VTable &>)
522 return f(
self, std::forward<Args>(
args)...);
531 if constexpr (std::is_same_v<LastArg, const VTable &>)
534 return f(
self, std::forward<Args>(
args)...);
538 [[gnu::always_inline]]
decltype(
auto)
call(
Ret (*f)(
const void *))
const {
545 [[gnu::always_inline]]
decltype(
auto)
call(
Ret (*f)(
void *)) {
552 [[gnu::always_inline]]
decltype(
auto)
call(
Ret (*f)(
const void *,
553 const VTable &))
const {
560 [[gnu::always_inline]]
decltype(
auto)
call(
Ret (*f)(
void *,
Class for polymorphism through type erasure.
decltype(auto) call(Ret(*f)(const void *)) const
Call the vtable function f with the given arguments args, implicitly passing the self pointer and vta...
Deallocator allocate(size_t size)
Ensure that storage is available, either by using the small buffer if it is large enough,...
TypeErased & operator=(const TypeErased &other)
Copy assignment.
void construct_inplace(Args &&...args)
Ensure storage and construct the type-erased object of type T in-place.
void deallocate()
Deallocate the memory without invoking the destructor.
static constexpr size_t small_buffer_size
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 vta...
void cleanup()
Destroy the type-erased object (if not empty), and deallocate the memory if necessary.
static constexpr size_t invalid_size
const T && as() &&
Convert the type-erased object to the given type.
TypeErased(const TypeErased &other, allocator_type alloc)
Copy constructor (allocator aware).
decltype(auto) call(Ret(*f)(void *))
Call the vtable function f with the given arguments args, implicitly passing the self pointer and vta...
static Ret make(Args &&...args)
Construct a type-erased wrapper of type Ret for an object of type T, initialized in-place with the gi...
static constexpr auto no_child_of_ours
True if T is not a child class of TypeErased.
TypeErased(const TypeErased &other)
Copy constructor.
TypeErased(TypeErased &&other) noexcept
Move constructor.
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 vta...
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 vta...
size_t size
Size required to store the object.
const T & as() const &
Convert the type-erased object to the given type.
TypeErased(te_in_place_t< T >, Args &&...args)
Main constructor that type-erases the object constructed from the given argument.
decltype(auto) call(Ret(*f)(void *, const VTable &))
Call the vtable function f with the given arguments args, implicitly passing the self pointer and vta...
TypeErased() noexcept(noexcept(allocator_type()))=default
Default constructor.
void do_copy_assign(const TypeErased &other)
std::allocator_traits< allocator_type > allocator_traits
T & as() &
Convert the type-erased object to the given type.
std::array< std::byte, small_buffer_size > buffer_type
TypeErased & operator=(TypeErased &&other) noexcept
Move assignment.
TypeErased(std::allocator_arg_t, const Alloc &alloc, T &&d)
Main constructor that type-erases the given argument.
allocator_type get_allocator() const noexcept
Get a copy of the allocator.
void * self
Pointer to the stored object.
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 gi...
TypeErased(T &&d)
Main constructor that type-erases the given argument.
TypeErased(TypeErased &&other, const allocator_type &alloc) noexcept
Move constructor (allocator aware).
const std::type_info & type() const noexcept
Query the contained type.
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.
const std::type_info & actual_type
const std::type_info & requested_type
bad_type_erased_type(const std::type_info &actual_type, const std::type_info &requested_type, const std::string &message="")
const char * what() const noexcept override
std::string demangled_typename(const std::type_info &t)
Get the pretty name of the given type as a string.
constexpr size_t required_te_buffer_size_for()
constexpr auto type_erased_wrapped()
Returns a function that accepts a void pointer, casts it to the class type of the member function Met...
typename last_type< Pack... >::type last_type_t
constexpr te_in_place_t< T > te_in_place
Convenience instance of te_in_place_t.
constexpr size_t default_te_buffer_size()
Similar to std::in_place_t.
R(*)(const void *self, Args..., const VTable &) type
R(*)(void *self, Args..., const VTable &) type
R(*)(const void *self, Args...) type
R(*)(void *self, Args...) type
Struct that stores the size of a polymorphic object, as well as pointers to functions to copy,...
BasicVTable(std::in_place_t, T &) noexcept
required_const_function_t< void(void *storage)> copy
Copy-construct a new instance into storage.
typename optional_function< F, VTable >::type optional_function_t
An optional function includes a void pointer to self, the arguments of F, and an additional reference...
const std::type_info * type
The original type of the stored object.
typename required_function< F >::type required_function_t
A required function includes a void pointer to self, in addition to the arguments of F.
required_function_t< void()> destroy
Destruct the given instance.
typename required_const_function< F >::type required_const_function_t
A required function includes a void pointer to self, in addition to the arguments of F.
required_function_t< void(void *storage)> move
Move-construct a new instance into storage.
typename optional_const_function< F, VTable >::type optional_const_function_t
An optional function includes a void pointer to self, the arguments of F, and an additional reference...
Deallocates the storage when destroyed.
Deallocator & operator=(const Deallocator &)=delete
Deallocator(const Deallocator &)=delete
Deallocator & operator=(Deallocator &&) noexcept=delete
Deallocator(Deallocator &&o) noexcept
Deallocator(TypeErased *instance) noexcept
static constexpr auto do_invoke(V *self, Args... args, ExtraArgs...) -> R
static constexpr auto invoker()
Returns a function that accepts a void pointer, casts it to the class type of the member function Met...
static constexpr auto invoker_ovl(R(T::*)(Args...))
static constexpr auto invoker_ovl(R(T::*)(Args...) const)