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>
55 using type = R (*)(
void *self, Args...);
59 template <
class R,
class... Args>
61 using type = R (*)(
const void *self, 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>
101 const std::type_info *
type = &
typeid(void);
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();
123template <
class Class,
class... ExtraArgs>
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>
134 [[gnu::always_inline]]
static constexpr auto invoker_ovl(R (T::*)(Args...)
136 return do_invoke<M,
const void,
const Class, R, Args...>;
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
142 return do_invoke<M, void, Class, R, Args...>;
150 template <auto Method>
151 [[gnu::always_inline]]
static constexpr auto invoker() {
152 return invoker_ovl<Method>(Method);
158template <
class Class,
auto Method,
class... ExtraArgs>
163template <
class VTable,
class Allocator>
166 [[no_unique_address]] Allocator allocator;
167 void *self =
nullptr;
170 const size_t max_size = 128;
171 return max_size - std::min(max_size,
sizeof(S));
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>,
193 size_t SmallBufferSize = default_te_buffer_size<VTable, Allocator>()>
209 !std::is_base_of_v<TypeErased, std::remove_cvref_t<T>>;
213 static_cast<size_t>(0xDEADBEEFDEADBEEF);
224 template <class Alloc>
230 do_copy_assign<false>(other);
235 do_copy_assign<false>(other);
244 do_copy_assign<true>(other);
250 :
allocator{std::move(other.allocator)} {
256 self = std::exchange(other.self,
nullptr);
259 else if (other.self) {
262 vtable.destroy(other.self);
263 other.self =
nullptr;
270 if (other.self ==
nullptr)
273 vtable = std::move(other.vtable);
278 self = std::exchange(other.self,
nullptr);
286 vtable.destroy(other.self);
291 else if (other.self) {
295 vtable.destroy(other.self);
296 other.self =
nullptr;
307 static constexpr bool prop_alloc =
308 allocator_traits::propagate_on_container_move_assignment::value;
309 if constexpr (prop_alloc)
312 if (other.self ==
nullptr)
316 vtable = std::move(other.vtable);
320 if (prop_alloc ||
allocator == other.allocator) {
321 self = std::exchange(other.self,
nullptr);
328 vtable.destroy(other.self);
330 auto &deallocator = prop_alloc ?
allocator : other.allocator;
331 using pointer_t =
typename allocator_traits::pointer;
332 auto &&other_pointer =
static_cast<pointer_t
>(other.self);
333 deallocator.deallocate(other_pointer,
size);
334 other.self =
nullptr;
338 else if (other.self) {
341 vtable.destroy(other.self);
342 other.self =
nullptr;
351 template <
class T,
class Alloc>
352 explicit TypeErased(std::allocator_arg_t,
const Alloc &alloc, T &&d)
354 construct_inplace<std::remove_cvref_t<T>>(std::forward<T>(d));
358 template <
class T,
class Alloc,
class... Args>
359 explicit TypeErased(std::allocator_arg_t,
const Alloc &alloc,
362 construct_inplace<std::remove_cvref_t<T>>(std::forward<Args>(args)...);
368 requires no_child_of_ours<T>
370 construct_inplace<std::remove_cvref_t<T>>(std::forward<T>(d));
374 template <
class T,
class... Args>
376 construct_inplace<std::remove_cvref_t<T>>(std::forward<Args>(args)...);
381 template <
class Ret,
class T,
class Alloc,
class... Args>
382 requires std::is_base_of_v<TypeErased, Ret>
383 static Ret
make(std::allocator_arg_t tag,
const Alloc &alloc,
386 r.template construct_inplace<T>(std::forward<Args>(args)...);
391 template <
class Ret,
class T,
class... Args>
393 static Ret
make(Args &&...args) {
395 std::forward<Args>(args)...);
400 explicit operator bool() const noexcept {
return self !=
nullptr; }
406 [[nodiscard]]
const std::type_info &
type() const noexcept {
414 if (
typeid(T) !=
type())
416 return *
reinterpret_cast<T *
>(
self);
420 const T &
as() const & {
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>
479 constexpr bool prop_alloc =
480 allocator_traits::propagate_on_container_copy_assignment::value;
481 if constexpr (CopyAllocator && prop_alloc)
492 storage_guard.release();
497 template <
class T,
class... Args>
499 static_assert(std::is_same_v<T, std::remove_cvref_t<T>>);
501 auto storage_guard =
allocate(
sizeof(T));
503 using destroyer = std::unique_ptr<T, noop_delete<T>>;
504 destroyer object_guard{
new (
self) T{std::forward<Args>(args)...}};
505 vtable = VTable{std::in_place,
static_cast<T &
>(*object_guard.get())};
506 object_guard.release();
507 storage_guard.release();
513 template <
class Ret,
class... FArgs,
class... Args>
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 &>)
520 return f(
self, std::forward<Args>(args)...,
vtable);
522 return f(
self, std::forward<Args>(args)...);
525 template <
class Ret,
class... FArgs,
class... Args>
526 [[gnu::always_inline]]
decltype(
auto)
call(Ret (*f)(
void *, FArgs...),
531 if constexpr (std::is_same_v<LastArg, const VTable &>)
532 return f(
self, std::forward<Args>(args)...,
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 *,
std::allocator< std::byte > allocator_type
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.
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)