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>
66 template <
class R,
class...
Args>
70 template <
class,
class VTable = BasicVTable>
72 template <
class R,
class...
Args,
class VTable>
74 using type = R (*)(
void *self,
Args...,
const VTable &);
76 template <
class,
class VTable = BasicVTable>
78 template <
class R,
class...
Args,
class VTable>
80 using type = R (*)(
const void *self,
Args...,
const VTable &);
93 template <
class F,
class VTable = BasicVTable>
97 template <
class F,
class VTable = BasicVTable>
114 copy = [](
const void *self,
void *storage) {
115 new (storage)
T(*std::launder(
reinterpret_cast<const T *
>(self)));
118 move = [](
void *self,
void *storage)
noexcept {
120 T(std::move(*std::launder(
reinterpret_cast<T *
>(self))));
123 std::launder(
reinterpret_cast<T *
>(self))->~T();
133 template <
auto M,
class V,
class C,
class R,
class...
Args>
134 [[gnu::always_inline]]
static constexpr auto
136 return std::invoke(
M, *std::launder(
reinterpret_cast<C *
>(self)),
137 std::forward<Args>(
args)...);
139 template <
auto M,
class T,
class R,
class...
Args>
140 requires std::is_base_of_v<T, Class>
145 template <
auto M,
class T,
class R,
class...
Args>
146 requires std::is_base_of_v<T, Class>
147 [[gnu::always_inline]]
static constexpr auto
149 if constexpr (std::is_const_v<Class>)
162 template <auto Method>
163 [[gnu::always_inline]]
static constexpr auto invoker() {
175template <
class VTable,
class Allocator>
178 [[no_unique_address]] Allocator allocator;
179 void *self =
nullptr;
186template <
class...
Types>
188 constexpr size_t sizes[] = {
sizeof(
Types)...};
189 return *std::max_element(std::begin(sizes), std::end(sizes));
204 class Allocator = std::allocator<std::byte>,
221 !std::is_base_of_v<TypeErased, std::remove_cvref_t<T>>;
225 static_cast<size_t>(0xDEAD'BEEF'DEAD'BEEF);
227 static_cast<size_t>(0xFFFF'FFFF'FFFF'FFFF);
229 static_cast<size_t>(0xFFFF'FFFF'FFFF'FFFE);
280 self = std::exchange(
other.self,
nullptr);
283 else if (
other.self) {
287 other.self =
nullptr;
294 if (
other.self ==
nullptr)
302 self = std::exchange(
other.self,
nullptr);
315 else if (!
other.owns_referenced_object()) {
316 self = std::exchange(
other.self,
nullptr);
319 else if (
other.self) {
324 other.self =
nullptr;
337 allocator_traits::propagate_on_container_move_assignment::value;
341 if (
other.self ==
nullptr)
350 self = std::exchange(
other.self,
nullptr);
360 using pointer_t =
typename allocator_traits::pointer;
363 other.self =
nullptr;
368 self = std::exchange(
other.self,
nullptr);
371 else if (
other.self) {
375 other.self =
nullptr;
385 template <
class T,
class Alloc>
392 template <
class T,
class Alloc,
class...
Args>
408 template <
class T,
class...
Args>
416 requires std::is_base_of_v<TypeErased, Ret>
425 template <
class Ret,
class T,
class...
Args>
429 std::forward<Args>(
args)...);
460 if (
typeid(
T) !=
type())
462 return *
reinterpret_cast<T *
>(
self);
467 if (
typeid(
T) !=
type())
469 return *
reinterpret_cast<const T *
>(
self);
474 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); };
Class for polymorphism through type erasure.
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
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...
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.
const T & as() const &
Convert the type-erased object to the given type.
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.
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.
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.
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(*)(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)