43 struct FunctionTable {
44 void (*destroy)(
AnyBase& self)
noexcept;
45 void (*moveConstructOther)(
AnyBase& self,
AnyBase& other)
noexcept;
48 Base* (*getValuePointer)(
AnyBase& self)
noexcept;
52 static constexpr bool IS_SMALL =
sizeof(T) <= SmallCapacity &&
alignof(T) <= SmallAlignment && std::is_nothrow_move_constructible_v<T>;
55 static const FunctionTable* getFunctionTable()
noexcept requires(IS_SMALL<T>) {
56 static_assert(std::is_void_v<Base> || std::is_base_of_v<Base, T>);
57 static constexpr FunctionTable FUNCTION_TABLE{
58 .destroy = [](
AnyBase& self)
noexcept ->
void { std::destroy_at(
reinterpret_cast<T*
>(self.smallObject)); },
59 .moveConstructOther = [](
AnyBase& self,
AnyBase& other)
noexcept ->
void {
60 std::construct_at(
reinterpret_cast<T*
>(other.smallObject), std::move(*std::launder(
reinterpret_cast<T*
>(self.
smallObject))));
61 other.functionTable = self.functionTable;
63 .copyConstructOther = [](
const AnyBase& self,
AnyBase& other) ->
void {
64 std::construct_at(
reinterpret_cast<T*
>(other.smallObject), *std::launder(
reinterpret_cast<const T*
>(self.
smallObject)));
65 other.functionTable = self.functionTable;
67 .copyAssignOther = [](
const AnyBase& self,
AnyBase& other) ->
void {
68 if (other.functionTable == &FUNCTION_TABLE) {
69 *std::launder(
reinterpret_cast<T*
>(other.smallObject)) = *std::launder(
reinterpret_cast<const T*
>(self.
smallObject));
71 if constexpr (std::is_nothrow_copy_constructible_v<T>) {
72 other.functionTable->destroy(other);
73 std::construct_at(
reinterpret_cast<T*
>(other.smallObject), *std::launder(
reinterpret_cast<const T*
>(self.
smallObject)));
74 other.functionTable = self.functionTable;
76 T temporary = *std::launder(
reinterpret_cast<const T*
>(self.
smallObject));
77 other.functionTable->destroy(other);
78 std::construct_at(
reinterpret_cast<T*
>(other.smallObject), std::move(temporary));
79 other.functionTable = self.functionTable;
83 .getValuePointer = [](
AnyBase& self)
noexcept -> Base* {
return std::launder(
reinterpret_cast<T*
>(self.
smallObject)); },
85 return &FUNCTION_TABLE;
89 static const FunctionTable* getFunctionTable()
noexcept requires(!IS_SMALL<T>) {
90 static_assert(std::is_void_v<Base> || std::is_base_of_v<Base, T>);
91 static constexpr FunctionTable FUNCTION_TABLE{
92 .destroy = [](
AnyBase& self)
noexcept ->
void {
93 delete static_cast<T*
>(self.largeObject);
95 .moveConstructOther = [](
AnyBase& self,
AnyBase& other)
noexcept ->
void {
97 other.functionTable = self.functionTable;
98 self.functionTable = &NULL_FUNCTION_TABLE;
100 .copyConstructOther = [](
const AnyBase& self,
AnyBase& other) ->
void {
101 other.largeObject =
new T{*
static_cast<const T*
>(self.
largeObject)};
102 other.functionTable = self.functionTable;
104 .copyAssignOther = [](
const AnyBase& self,
AnyBase& other) ->
void {
105 if (other.functionTable == &FUNCTION_TABLE) {
106 *
static_cast<T*
>(other.largeObject) = *
static_cast<const T*
>(self.
largeObject);
108 T*
const objectCopy =
new T{*
static_cast<const T*
>(self.
largeObject)};
109 other.functionTable->destroy(other);
110 other.largeObject = objectCopy;
111 other.functionTable = self.functionTable;
116 return &FUNCTION_TABLE;
135 template <
typename T,
typename... Args>
137 return AnyBase{InplaceTag{}, std::in_place_type<T>, std::forward<Args>(args)...};
144 : functionTable(&NULL_FUNCTION_TABLE) {}
157 template <
typename T>
159 :
AnyBase(InplaceTag{}, std::in_place_type<T>, std::forward<T>(value)) {}
163 functionTable->destroy(*
this);
168 other.functionTable->copyConstructOther(other, *
this);
173 other.functionTable->moveConstructOther(other, *
this);
178 if (
this == &other) {
181 other.functionTable->copyAssignOther(other, *
this);
187 if (
this == &other) {
190 functionTable->destroy(*
this);
191 other.functionTable->moveConstructOther(other, *
this);
199 functionTable->destroy(*
this);
200 functionTable = &NULL_FUNCTION_TABLE;
219 template <
typename T,
typename... Args>
222 using X = std::decay_t<T>;
224 if constexpr (IS_SMALL<X>) {
225 result = std::construct_at(
reinterpret_cast<X*
>(
smallObject), std::forward<Args>(args)...);
227 result =
new X(std::forward<Args>(args)...);
230 functionTable = getFunctionTable<X>();
242 [[nodiscard]] Base*
base() noexcept {
243 return functionTable->getValuePointer(*
this);
254 [[nodiscard]]
const Base*
base() const noexcept {
255 return functionTable->getValuePointer(*
const_cast<AnyBase*
>(
this));
274#if defined(_WIN32) || defined(__linux__)
275 Base*
const result = std::launder(
reinterpret_cast<Base*
>(
smallObject));
299#if defined(_WIN32) || defined(__linux__)
300 const Base*
const result = std::launder(
reinterpret_cast<const Base*
>(
smallObject));
349 [[nodiscard]]
auto&
operator*()
requires(!std::is_void_v<Base>) {
350 Base*
const result =
base();
363 [[nodiscard]]
const auto&
operator*() const requires(!std::is_void_v<Base>) {
364 const Base*
const result =
base();
376 [[nodiscard]] Base*
operator->()
requires(!std::is_void_v<Base>) {
377 Base*
const result =
base();
389 [[nodiscard]]
const Base*
operator->() const requires(!std::is_void_v<Base>) {
390 const Base*
const result =
base();
405 template <
typename T>
406 [[nodiscard]] std::decay_t<T>*
get_if() noexcept {
407 using X = std::decay_t<T>;
408 if (functionTable != getFunctionTable<X>()) {
411 if constexpr (IS_SMALL<X>) {
412 return std::launder(
reinterpret_cast<X*
>(
smallObject));
429 template <
typename T>
430 [[nodiscard]]
const std::decay_t<T>*
get_if() const noexcept {
431 using X = std::decay_t<T>;
432 if (functionTable != getFunctionTable<X>()) {
435 if constexpr (IS_SMALL<X>) {
436 return std::launder(
reinterpret_cast<const X*
>(
smallObject));
454 template <
typename T>
455 [[nodiscard]] std::decay_t<T>&
get() {
456 std::decay_t<T>*
const result =
get_if<T>();
475 template <
typename T>
476 [[nodiscard]]
const std::decay_t<T>&
get()
const {
477 const std::decay_t<T>*
const result =
get_if<T>();
485 struct InplaceTag {};
487 template <
typename T,
typename... Args>
488 AnyBase(InplaceTag, std::in_place_type_t<T>, Args&&... args) {
489 using X = std::decay_t<T>;
490 if constexpr (IS_SMALL<X>) {
491 std::construct_at(
reinterpret_cast<X*
>(
smallObject), std::forward<Args>(args)...);
495 functionTable = getFunctionTable<X>();
498 static void nullDestroy(
AnyBase&)
noexcept {}
499 static void nullMoveConstructOther(
AnyBase&,
AnyBase& other)
noexcept {
500 other.functionTable = &NULL_FUNCTION_TABLE;
502 static void nullCopyConstructOther(
const AnyBase&,
AnyBase& other) {
503 other.functionTable = &NULL_FUNCTION_TABLE;
506 other.functionTable->destroy(other);
507 other.functionTable = &NULL_FUNCTION_TABLE;
509 static Base* nullGetValuePointer(
AnyBase&)
noexcept {
513 static constexpr FunctionTable NULL_FUNCTION_TABLE{
514 .destroy = nullDestroy,
515 .moveConstructOther = nullMoveConstructOther,
516 .copyConstructOther = nullCopyConstructOther,
517 .copyAssignOther = nullCopyAssignOther,
518 .getValuePointer = nullGetValuePointer,
521 const FunctionTable* functionTable = &NULL_FUNCTION_TABLE;