alpaqa 1.0.0a16
Nonconvex constrained optimization
Loading...
Searching...
No Matches
type-erasure.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <alpaqa/export.hpp>
7
8#include <algorithm>
9#include <cassert>
10#include <cstddef>
11#include <functional>
12#include <memory>
13#include <new>
14#include <stdexcept>
15#include <type_traits>
16#include <typeinfo>
17#include <utility>
18
19namespace alpaqa::util {
20
21class ALPAQA_EXPORT bad_type_erased_type : public std::logic_error {
22 public:
23 bad_type_erased_type(const std::type_info &actual_type,
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} {}
28
29 [[nodiscard]] const char *what() const noexcept override {
30 message = "";
31 if (const char *w = std::logic_error::what(); w && *w) {
32 message += w;
33 message += ": ";
34 }
35 message = "Type requested: " + demangled_typename(requested_type) +
36 ", type contained: " + demangled_typename(actual_type);
37 return message.c_str();
38 }
39
40 private:
41 const std::type_info &actual_type;
42 const std::type_info &requested_type;
43 mutable std::string message;
44};
45
46class ALPAQA_EXPORT bad_type_erased_constness : public std::logic_error {
47 public:
49 : std::logic_error{"Non-const method called on a TypeErased object "
50 "that references a const object"} {}
51};
52
53/// Struct that stores the size of a polymorphic object, as well as pointers to
54/// functions to copy, move or destroy the object.
55/// Inherit from this struct to add useful functions.
57
58 template <class>
59 struct required_function; // undefined
60 template <class R, class... Args>
61 struct required_function<R(Args...)> {
62 using type = R (*)(void *self, Args...);
63 };
64 template <class R, class... Args>
66 using type = R (*)(const void *self, Args...);
67 };
68 template <class, class VTable = BasicVTable>
69 struct optional_function; // undefined
70 template <class R, class... Args, class VTable>
71 struct optional_function<R(Args...), VTable> {
72 using type = R (*)(void *self, Args..., const VTable &);
73 };
74 template <class R, class... Args, class VTable>
75 struct optional_function<R(Args...) const, VTable> {
76 using type = R (*)(const void *self, Args..., const VTable &);
77 };
78 /// A required function includes a void pointer to self, in addition to the
79 /// arguments of @p F.
80 template <class F>
82 /// An optional function includes a void pointer to self, the arguments of
83 /// @p F, and an additional reference to the VTable, so that it can be
84 /// implemented in terms of other functions.
85 template <class F, class VTable = BasicVTable>
87
88 /// Copy-construct a new instance into storage.
89 required_function_t<void(void *storage) const> copy = nullptr;
90 /// Move-construct a new instance into storage.
91 required_function_t<void(void *storage)> move = nullptr;
92 /// Destruct the given instance.
94 /// The original type of the stored object.
95 const std::type_info *type = &typeid(void);
96
97 BasicVTable() = default;
98
99 template <class T>
100 BasicVTable(std::in_place_t, T &) noexcept {
101 copy = [](const void *self, void *storage) {
102 new (storage) T(*std::launder(reinterpret_cast<const T *>(self)));
103 };
104 // TODO: require that move constructor is noexcept?
105 move = [](void *self, void *storage) noexcept {
106 if constexpr (std::is_const_v<T>)
107 std::terminate();
108 else
109 new (storage)
110 T(std::move(*std::launder(reinterpret_cast<T *>(self))));
111 };
112 destroy = [](void *self) {
113 if constexpr (std::is_const_v<T>)
114 std::terminate();
115 else
116 std::launder(reinterpret_cast<T *>(self))->~T();
117 };
118 type = &typeid(T);
119 }
120};
121
122namespace detail {
123template <class Class, class... ExtraArgs>
124struct Launderer {
125 private:
126 template <auto M, class V, class C, class R, class... Args>
127 [[gnu::always_inline]] static constexpr auto
128 do_invoke(V *self, Args... args, ExtraArgs...) -> R {
129 return std::invoke(M, *std::launder(reinterpret_cast<C *>(self)),
130 std::forward<Args>(args)...);
131 }
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...)
135 const) {
136 return do_invoke<M, const void, const Class, R, Args...>;
137 }
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
141 invoker_ovl(R (T::*)(Args...)) {
142 if constexpr (std::is_const_v<Class>)
143 return +[](void *, Args..., ExtraArgs...) {
145 };
146 else
147 return do_invoke<M, void, Class, R, Args...>;
148 }
149
150 public:
151 /// Returns a function that accepts a void pointer, casts it to the class
152 /// type of the member function @p Method, launders it, and then invokes
153 /// @p Method with it, passing on the arguments to @p Method. The function
154 /// can also accept additional arguments at the end, of type @p ExtraArgs.
155 template <auto Method>
156 [[gnu::always_inline]] static constexpr auto invoker() {
158 }
159};
160} // namespace detail
161
162/// @copydoc detail::Launderer::invoker
163template <class Class, auto Method, class... ExtraArgs>
164[[gnu::always_inline]] constexpr auto type_erased_wrapped() {
166}
167
168template <class VTable, class Allocator>
169inline constexpr size_t default_te_buffer_size() {
170 struct S {
171 [[no_unique_address]] Allocator allocator;
172 void *self = nullptr;
173 VTable vtable;
174 };
175 const size_t max_size = 128;
176 return max_size - std::min(max_size, sizeof(S));
177}
178
179template <class... Types>
180inline constexpr size_t required_te_buffer_size_for() {
181 constexpr size_t sizes[] = {sizeof(Types)...};
182 return *std::max_element(std::begin(sizes), std::end(sizes));
183}
184
185/// Similar to `std::in_place_t`.
186template <class T>
188/// Convenience instance of @ref te_in_place_t.
189template <class T>
191
192/// Class for polymorphism through type erasure. Saves the entire vtable, and
193/// uses small buffer optimization.
194///
195/// @todo Decouple allocation/small buffer optimization.
196template <class VTable = BasicVTable,
197 class Allocator = std::allocator<std::byte>,
200 public:
201 static constexpr size_t small_buffer_size = SmallBufferSize;
203
204 private:
205 using allocator_traits = std::allocator_traits<allocator_type>;
206 using buffer_type = std::array<std::byte, small_buffer_size>;
207 [[no_unique_address]] alignas(std::max_align_t) buffer_type small_buffer;
209
210 private:
211 /// True if @p T is not a child class of @ref TypeErased.
212 template <class T>
213 static constexpr auto no_child_of_ours =
214 !std::is_base_of_v<TypeErased, std::remove_cvref_t<T>>;
215
216 protected:
217 static constexpr size_t invalid_size =
218 static_cast<size_t>(0xDEAD'BEEF'DEAD'BEEF);
219 static constexpr size_t mut_ref_size =
220 static_cast<size_t>(0xFFFF'FFFF'FFFF'FFFF);
221 static constexpr size_t const_ref_size =
222 static_cast<size_t>(0xFFFF'FFFF'FFFF'FFFE);
223 [[nodiscard]] static bool size_indicates_ownership(size_t size) {
225 }
226 [[nodiscard]] static bool size_indicates_const(size_t size) {
230 /// Pointer to the stored object.
231 void *self = nullptr;
232 /// Size required to store the object.
234 VTable vtable;
235
236 public:
237 /// Default constructor.
239 /// Default constructor (allocator aware).
242 /// Copy constructor.
248 /// Copy constructor (allocator aware).
250 : allocator{std::move(alloc)} {
252 }
253 /// Copy assignment.
255 // Check for self-assignment
256 if (&other == this)
257 return *this;
258 // Delete our own storage before assigning a new value
259 cleanup();
261 return *this;
262 }
263
264 /// Move constructor.
266 : allocator{std::move(other.allocator)} {
267 size = other.size;
268 vtable = std::move(other.vtable);
269 // If dynamically allocated, simply steal storage, or if not owned,
270 // simply move the pointer
271 if (size > small_buffer_size || !other.owns_referenced_object()) {
272 // We stole the allocator, so we can steal the storage as well
273 self = std::exchange(other.self, nullptr);
274 }
275 // Otherwise, use the small buffer and do an explicit move
276 else if (other.self) {
277 self = small_buffer.data();
278 vtable.move(other.self, self); // assumed not to throw
279 vtable.destroy(other.self); // nothing to deallocate
280 other.self = nullptr;
281 }
282 }
283 /// Move constructor (allocator aware).
285 : allocator{alloc} {
286 // Only continue if other actually contains a value
287 if (other.self == nullptr)
288 return;
289 size = other.size;
290 vtable = std::move(other.vtable);
291 // If dynamically allocated, simply steal other's storage
292 if (size > small_buffer_size) {
293 // Can we steal the storage because of equal allocators?
294 if (allocator == other.allocator) {
295 self = std::exchange(other.self, nullptr);
296 }
297 // If the allocators are not the same, we cannot steal the
298 // storage, so do an explicit move
299 else {
300 self = allocator.allocate(size);
301 vtable.move(other.self, self);
302 // Cannot call other.cleanup() here because we stole the vtable
303 vtable.destroy(other.self);
304 other.deallocate();
305 }
306 }
307 // If not owned, simply move the pointer
308 else if (!other.owns_referenced_object()) {
309 self = std::exchange(other.self, nullptr);
310 }
311 // Otherwise, use the small buffer and do an explicit move
312 else if (other.self) {
313 self = small_buffer.data();
314 vtable.move(other.self, self);
315 // Cannot call other.cleanup() here because we stole the vtable
316 vtable.destroy(other.self); // nothing to deallocate
317 other.self = nullptr;
318 }
319 other.size = invalid_size;
320 }
321 /// Move assignment.
323 // Check for self-assignment
324 if (&other == this)
325 return *this;
326 // Delete our own storage before assigning a new value
327 cleanup();
328 // Check if we are allowed to steal the allocator
329 static constexpr bool prop_alloc =
330 allocator_traits::propagate_on_container_move_assignment::value;
331 if constexpr (prop_alloc)
332 allocator = std::move(other.allocator);
333 // Only assign if other contains a value
334 if (other.self == nullptr)
335 return *this;
336
337 size = other.size;
338 vtable = std::move(other.vtable);
339 // If dynamically allocated, simply steal other's storage
340 if (size > small_buffer_size) {
341 // Can we steal the storage because of equal allocators?
342 if (prop_alloc || allocator == other.allocator) {
343 self = std::exchange(other.self, nullptr);
344 }
345 // If the allocators are not the same, we cannot steal the
346 // storage, so do an explicit move
347 else {
348 self = allocator.allocate(size);
349 vtable.move(other.self, self); // assumed not to throw
350 vtable.destroy(other.self);
351 // Careful, we might have moved other.allocator!
352 auto &deallocator = prop_alloc ? allocator : other.allocator;
353 using pointer_t = typename allocator_traits::pointer;
354 auto &&other_pointer = static_cast<pointer_t>(other.self);
355 deallocator.deallocate(other_pointer, size);
356 other.self = nullptr;
357 }
358 }
359 // If not owned, simply move the pointer
360 else if (!owns_referenced_object()) {
361 self = std::exchange(other.self, nullptr);
362 }
363 // Otherwise, use the small buffer and do an explicit move
364 else if (other.self) {
365 self = small_buffer.data();
366 vtable.move(other.self, self);
367 vtable.destroy(other.self); // nothing to deallocate
368 other.self = nullptr;
369 }
370 other.size = invalid_size;
371 return *this;
372 }
373
374 /// Destructor.
376
377 /// Main constructor that type-erases the given argument.
378 template <class T, class Alloc>
379 explicit TypeErased(std::allocator_arg_t, const Alloc &alloc, T &&d)
380 : allocator{alloc} {
381 construct_inplace<std::remove_cvref_t<T>>(std::forward<T>(d));
382 }
383 /// Main constructor that type-erases the object constructed from the given
384 /// argument.
385 template <class T, class Alloc, class... Args>
386 explicit TypeErased(std::allocator_arg_t, const Alloc &alloc,
388 : allocator{alloc} {
389 construct_inplace<std::remove_cvref_t<T>>(std::forward<Args>(args)...);
390 }
391 /// @copydoc TypeErased(std::allocator_arg_t, const Alloc &, T &&)
392 /// Requirement prevents this constructor from taking precedence over the
393 /// copy and move constructors.
394 template <class T>
395 requires no_child_of_ours<T>
396 explicit TypeErased(T &&d) {
397 construct_inplace<std::remove_cvref_t<T>>(std::forward<T>(d));
398 }
399 /// Main constructor that type-erases the object constructed from the given
400 /// argument.
401 template <class T, class... Args>
403 construct_inplace<std::remove_cvref_t<T>>(std::forward<Args>(args)...);
404 }
405
406 /// Construct a type-erased wrapper of type Ret for an object of type T,
407 /// initialized in-place with the given arguments.
408 template <class Ret, class T, class Alloc, class... Args>
409 requires std::is_base_of_v<TypeErased, Ret>
410 static Ret make(std::allocator_arg_t tag, const Alloc &alloc,
411 Args &&...args) {
412 Ret r{tag, alloc};
413 r.template construct_inplace<T>(std::forward<Args>(args)...);
414 return r;
415 }
416 /// Construct a type-erased wrapper of type Ret for an object of type T,
417 /// initialized in-place with the given arguments.
418 template <class Ret, class T, class... Args>
419 requires no_leading_allocator<Args...>
420 static Ret make(Args &&...args) {
421 return make<Ret, T>(std::allocator_arg, allocator_type{},
422 std::forward<Args>(args)...);
423 }
424
425 /// Check if this wrapper wraps an object. False for default-constructed
426 /// objects.
427 explicit operator bool() const noexcept { return self != nullptr; }
428
429 /// Check if this wrapper owns the storage of the wrapped object, or
430 /// whether it simply stores a reference to an object that was allocated
431 /// elsewhere.
435
436 /// Check if the wrapped object is const.
440
441 /// Get a copy of the allocator.
443
444 /// Query the contained type.
445 [[nodiscard]] const std::type_info &type() const noexcept {
446 return *vtable.type;
447 }
448
449 /// Convert the type-erased object to the given type.
450 /// @throws alpaqa::util::bad_type_erased_type
451 /// If T does not match the stored type.
452 template <class T>
453 requires(!std::is_const_v<T>)
454 [[nodiscard]] T &as() & {
455 if (typeid(T) != type())
456 throw bad_type_erased_type(type(), typeid(T));
459 return *reinterpret_cast<T *>(self);
460 }
461 /// @copydoc as()
462 template <class T>
463 requires(std::is_const_v<T>)
464 [[nodiscard]] T &as() const & {
465 if (typeid(T) != type())
466 throw bad_type_erased_type(type(), typeid(T));
467 return *reinterpret_cast<T *>(self);
468 }
469 /// @copydoc as()
470 template <class T>
471 [[nodiscard]] T &&as() && {
472 if (typeid(T) != type())
473 throw bad_type_erased_type(type(), typeid(T));
474 if (!std::is_const_v<T> && referenced_object_is_const())
476 return std::move(*reinterpret_cast<T *>(self));
477 }
478
479 /// Get a type-erased pointer to the wrapped object.
480 /// @throws alpaqa::util::bad_type_erased_constness
481 /// If the wrapped object is const.
482 /// @see @ref get_const_pointer()
483 [[nodiscard]] void *get_pointer() const {
486 return self;
487 }
488 /// Get a type-erased pointer to the wrapped object.
489 [[nodiscard]] const void *get_const_pointer() const { return self; }
490
491 /// @see @ref derived_from_TypeErased
492 template <std::derived_from<TypeErased> Child>
493 friend void derived_from_TypeErased_helper(const Child &) noexcept {
494 static constexpr bool False = sizeof(Child) != sizeof(Child);
495 static_assert(False, "not allowed in an evaluated context");
496 }
497
498 private:
499 /// Deallocates the storage when destroyed.
511
512 /// Ensure that storage is available, either by using the small buffer if
513 /// it is large enough, or by calling the allocator.
514 /// Returns a RAII wrapper that deallocates the storage unless released.
516 assert(!self);
518 assert(size > 0);
521 : allocator.allocate(size);
522 this->size = size;
523 return {this};
524 }
525
526 /// Deallocate the memory without invoking the destructor.
527 void deallocate() {
529 assert(size > 0);
531 using pointer_t = typename allocator_traits::pointer;
533 allocator.deallocate(reinterpret_cast<pointer_t>(self), size);
534 self = nullptr;
535 }
536
537 /// Destroy the type-erased object (if not empty), and deallocate the memory
538 /// if necessary.
539 void cleanup() {
540 if (!owns_referenced_object()) {
541 self = nullptr;
542 } else if (self) {
543 vtable.destroy(self);
544 deallocate();
545 }
546 }
547
548 template <bool CopyAllocator>
550 constexpr bool prop_alloc =
551 allocator_traits::propagate_on_container_copy_assignment::value;
552 if constexpr (CopyAllocator && prop_alloc)
553 allocator = other.allocator;
554 if (!other)
555 return;
556 vtable = other.vtable;
557 if (!other.owns_referenced_object()) {
558 // Non-owning: simply copy the pointer.
559 size = other.size;
560 self = other.self;
561 } else {
562 auto storage_guard = allocate(other.size);
563 // If copy constructor throws, storage should be deallocated and
564 // self set to null, otherwise the TypeErased destructor will
565 // attempt to call the contained object's destructor, which is
566 // undefined behavior if construction failed.
567 vtable.copy(other.self, self);
568 storage_guard.release();
569 }
570 }
571
572 protected:
573 /// Ensure storage and construct the type-erased object of type T in-place.
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>) {
578 T ptr{args...};
579 using Tnp = std::remove_pointer_t<T>;
580 size = std::is_const_v<Tnp> ? const_ref_size : mut_ref_size;
581 vtable = VTable{std::in_place, *ptr};
582 self = const_cast<std::remove_const_t<Tnp> *>(ptr);
583 } else {
584 // Allocate memory
585 auto storage_guard = allocate(sizeof(T));
586 // Construct the stored object
587 using destroyer = std::unique_ptr<T, noop_delete<T>>;
588 destroyer obj_guard{new (self) T{std::forward<Args>(args)...}};
589 vtable = VTable{std::in_place, static_cast<T &>(*obj_guard.get())};
590 obj_guard.release();
591 storage_guard.release();
592 }
593 }
594
595 /// Call the vtable function @p f with the given arguments @p args,
596 /// implicitly passing the @ref self pointer and @ref vtable reference if
597 /// necessary.
598 template <class Ret, class... FArgs, class... Args>
599 [[gnu::always_inline]] decltype(auto) call(Ret (*f)(const void *, FArgs...),
600 Args &&...args) const {
601 assert(f);
602 assert(self);
603 using LastArg = util::last_type_t<FArgs...>;
604 if constexpr (std::is_same_v<LastArg, const VTable &>)
605 return f(self, std::forward<Args>(args)..., vtable);
606 else
607 return f(self, std::forward<Args>(args)...);
608 }
609 /// @copydoc call
610 template <class Ret, class... FArgs, class... Args>
611 [[gnu::always_inline]] decltype(auto) call(Ret (*f)(void *, FArgs...),
612 Args &&...args) {
613 assert(f);
614 assert(self);
617 using LastArg = util::last_type_t<FArgs...>;
618 if constexpr (std::is_same_v<LastArg, const VTable &>)
619 return f(self, std::forward<Args>(args)..., vtable);
620 else
621 return f(self, std::forward<Args>(args)...);
622 }
623 /// @copydoc call
624 template <class Ret>
625 [[gnu::always_inline]] decltype(auto) call(Ret (*f)(const void *)) const {
626 assert(f);
627 assert(self);
628 return f(self);
629 }
630 /// @copydoc call
631 template <class Ret>
632 [[gnu::always_inline]] decltype(auto) call(Ret (*f)(void *)) {
633 assert(f);
634 assert(self);
637 return f(self);
638 }
639 /// @copydoc call
640 template <class Ret>
641 [[gnu::always_inline]] decltype(auto) call(Ret (*f)(const void *,
642 const VTable &)) const {
643 assert(f);
644 assert(self);
645 return f(self, vtable);
646 }
647 /// @copydoc call
648 template <class Ret>
649 [[gnu::always_inline]] decltype(auto) call(Ret (*f)(void *,
650 const VTable &)) {
651 assert(f);
652 assert(self);
655 return f(self, vtable);
656 }
657};
658
659template <class Child>
661 requires(Child c) { derived_from_TypeErased_helper(c); };
662
663} // namespace alpaqa::util
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.
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.
constexpr const auto inf
Definition config.hpp:98
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)