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();
49 : std::logic_error{
"Non-const method called on a TypeErased object "
50 "that references a const object"} {}
60 template <
class R,
class...
Args>
64 template <
class R,
class...
Args>
68 template <
class,
class VTable = BasicVTable>
70 template <
class R,
class...
Args,
class VTable>
72 using type = R (*)(
void *self,
Args...,
const VTable &);
74 template <
class R,
class...
Args,
class VTable>
76 using type = R (*)(
const void *self,
Args...,
const VTable &);
85 template <
class F,
class VTable = BasicVTable>
101 copy = [](
const void *self,
void *storage) {
102 new (storage)
T(*std::launder(
reinterpret_cast<const T *
>(self)));
105 move = [](
void *self,
void *storage)
noexcept {
106 if constexpr (std::is_const_v<T>)
110 T(std::move(*std::launder(
reinterpret_cast<T *
>(self))));
113 if constexpr (std::is_const_v<T>)
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
142 if constexpr (std::is_const_v<Class>)
155 template <auto Method>
156 [[gnu::always_inline]]
static constexpr auto invoker() {
168template <
class VTable,
class Allocator>
171 [[no_unique_address]] Allocator allocator;
172 void *self =
nullptr;
179template <
class...
Types>
181 constexpr size_t sizes[] = {
sizeof(
Types)...};
182 return *std::max_element(std::begin(sizes), std::end(sizes));
197 class Allocator = std::allocator<std::byte>,
214 !std::is_base_of_v<TypeErased, std::remove_cvref_t<T>>;
218 static_cast<size_t>(0xDEAD'BEEF'DEAD'BEEF);
220 static_cast<size_t>(0xFFFF'FFFF'FFFF'FFFF);
222 static_cast<size_t>(0xFFFF'FFFF'FFFF'FFFE);
273 self = std::exchange(
other.self,
nullptr);
276 else if (
other.self) {
280 other.self =
nullptr;
287 if (
other.self ==
nullptr)
295 self = std::exchange(
other.self,
nullptr);
308 else if (!
other.owns_referenced_object()) {
309 self = std::exchange(
other.self,
nullptr);
312 else if (
other.self) {
317 other.self =
nullptr;
330 allocator_traits::propagate_on_container_move_assignment::value;
334 if (
other.self ==
nullptr)
343 self = std::exchange(
other.self,
nullptr);
353 using pointer_t =
typename allocator_traits::pointer;
356 other.self =
nullptr;
361 self = std::exchange(
other.self,
nullptr);
364 else if (
other.self) {
368 other.self =
nullptr;
378 template <
class T,
class Alloc>
385 template <
class T,
class Alloc,
class...
Args>
401 template <
class T,
class...
Args>
409 requires std::is_base_of_v<TypeErased, Ret>
418 template <
class Ret,
class T,
class...
Args>
422 std::forward<Args>(
args)...);
453 requires(!std::is_const_v<T>)
455 if (
typeid(
T) !=
type())
459 return *
reinterpret_cast<T *
>(
self);
463 requires(std::is_const_v<T>)
465 if (
typeid(
T) !=
type())
467 return *
reinterpret_cast<T *
>(
self);
472 if (
typeid(
T) !=
type())
476 return std::move(*
reinterpret_cast<T *
>(
self));
492 template <std::derived_from<TypeErased> Child>
494 static constexpr bool False =
sizeof(
Child) !=
sizeof(
Child);
495 static_assert(False,
"not allowed in an evaluated context");
506 :
instance{std::exchange(
o.instance,
nullptr)} {}
531 using pointer_t =
typename allocator_traits::pointer;
548 template <
bool CopyAllocator>
551 allocator_traits::propagate_on_container_copy_assignment::value;
557 if (!
other.owns_referenced_object()) {
574 template <
class T,
class...
Args>
576 static_assert(std::is_same_v<T, std::remove_cvref_t<T>>);
577 if constexpr (std::is_pointer_v<T>) {
579 using Tnp = std::remove_pointer_t<T>;
581 vtable = VTable{std::in_place, *ptr};
582 self =
const_cast<std::remove_const_t<Tnp> *
>(ptr);
587 using destroyer = std::unique_ptr<T, noop_delete<T>>;
599 [[gnu::always_inline]]
decltype(
auto)
call(
Ret (*f)(
const void *,
FArgs...),
600 Args &&...args)
const {
604 if constexpr (std::is_same_v<LastArg, const VTable &>)
607 return f(
self, std::forward<Args>(
args)...);
618 if constexpr (std::is_same_v<LastArg, const VTable &>)
621 return f(
self, std::forward<Args>(
args)...);
625 [[gnu::always_inline]]
decltype(
auto)
call(
Ret (*f)(
const void *))
const {
632 [[gnu::always_inline]]
decltype(
auto)
call(
Ret (*f)(
void *)) {
641 [[gnu::always_inline]]
decltype(
auto)
call(
Ret (*f)(
const void *,
642 const VTable &))
const {
649 [[gnu::always_inline]]
decltype(
auto)
call(
Ret (*f)(
void *,
659template <
class Child>
661 requires(
Child c) { derived_from_TypeErased_helper(c); };
std::allocator< std::byte > allocator_type
Class for polymorphism through type erasure.
T & as() &
Convert the type-erased object to the given type.
void * get_pointer() const
Get a type-erased pointer to the wrapped object.
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.
const void * get_const_pointer() const
Get a type-erased pointer to the wrapped object.
static constexpr size_t mut_ref_size
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
decltype(auto) call(Ret(*f)(void *))
Call the vtable function f with the given arguments args, implicitly passing the self pointer and vta...
friend void derived_from_TypeErased_helper(const Child &) noexcept
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 size_t const_ref_size
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.
bool referenced_object_is_const() const noexcept
Check if the wrapped object is const.
bool owns_referenced_object() const noexcept
Check if this wrapper owns the storage of the wrapped object, or whether it simply stores a reference...
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.
static bool size_indicates_const(size_t size)
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.
T && as() &&
Convert the type-erased object to the given type.
void do_copy_assign(const TypeErased &other)
std::allocator_traits< allocator_type > allocator_traits
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.
static bool size_indicates_ownership(size_t size)
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.
T & as() const &
Convert the type-erased object to the given type.
bad_type_erased_constness()
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(*)(void *self, Args..., const VTable &) type
R(*)(const 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_function_t< void(void *storage) const > 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.
required_function_t< void(void *storage)> move
Move-construct a new instance into storage.
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)