From ebb2af1225edd2457055ecf714d653d2b6445370 Mon Sep 17 00:00:00 2001 From: skypjack Date: Fri, 17 Oct 2025 08:13:41 +0200 Subject: [PATCH] any: make ::initialize fully support void --- src/entt/core/any.hpp | 83 ++++++++++++++++++++----------------------- 1 file changed, 39 insertions(+), 44 deletions(-) diff --git a/src/entt/core/any.hpp b/src/entt/core/any.hpp index 0b6fa986c..5d020cf54 100644 --- a/src/entt/core/any.hpp +++ b/src/entt/core/any.hpp @@ -131,48 +131,48 @@ class basic_any: private internal::basic_any_storage { template void initialize([[maybe_unused]] Args &&...args) { - if constexpr(!std::is_void_v) { - using plain_type = std::remove_const_t>; + using plain_type = std::remove_const_t>; - vtable = basic_vtable; - descriptor = &type_id; + vtable = basic_vtable; + descriptor = &type_id; - if constexpr(std::is_lvalue_reference_v) { - static_assert((std::is_lvalue_reference_v && ...) && (sizeof...(Args) == 1u), "Invalid arguments"); - mode = std::is_const_v> ? any_policy::cref : any_policy::ref; - // NOLINTNEXTLINE(bugprone-multi-level-implicit-pointer-conversion) - this->instance = (std::addressof(args), ...); - } else if constexpr(in_situ_v) { - mode = any_policy::embedded; + if constexpr(std::is_void_v) { + vtable = nullptr; + mode = any_policy::empty; + this->instance = nullptr; + } else if constexpr(std::is_lvalue_reference_v) { + static_assert((std::is_lvalue_reference_v && ...) && (sizeof...(Args) == 1u), "Invalid arguments"); + mode = std::is_const_v> ? any_policy::cref : any_policy::ref; + // NOLINTNEXTLINE(bugprone-multi-level-implicit-pointer-conversion) + this->instance = (std::addressof(args), ...); + } else if constexpr(in_situ_v) { + mode = any_policy::embedded; - if constexpr(std::is_aggregate_v && (sizeof...(Args) != 0u || !std::is_default_constructible_v)) { - ::new(&this->buffer) plain_type{std::forward(args)...}; - } else { - // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-array-to-pointer-decay) - ::new(&this->buffer) plain_type(std::forward(args)...); - } + if constexpr(std::is_aggregate_v && (sizeof...(Args) != 0u || !std::is_default_constructible_v)) { + ::new(&this->buffer) plain_type{std::forward(args)...}; } else { - mode = any_policy::dynamic; + // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-array-to-pointer-decay) + ::new(&this->buffer) plain_type(std::forward(args)...); + } + } else { + mode = any_policy::dynamic; - if constexpr(std::is_aggregate_v && (sizeof...(Args) != 0u || !std::is_default_constructible_v)) { - this->instance = new plain_type{std::forward(args)...}; - } else if constexpr(std::is_array_v) { - static_assert(sizeof...(Args) == 0u, "Invalid arguments"); - this->instance = new plain_type[std::extent_v](); - } else { - this->instance = new plain_type(std::forward(args)...); - } + if constexpr(std::is_aggregate_v && (sizeof...(Args) != 0u || !std::is_default_constructible_v)) { + this->instance = new plain_type{std::forward(args)...}; + } else if constexpr(std::is_array_v) { + static_assert(sizeof...(Args) == 0u, "Invalid arguments"); + this->instance = new plain_type[std::extent_v](); + } else { + this->instance = new plain_type(std::forward(args)...); } } } basic_any(const basic_any &other, const any_policy pol) noexcept - : base_type{}, + : base_type{other.data()}, vtable{other.vtable}, descriptor{other.descriptor}, - mode{pol} { - this->instance = other.data(); - } + mode{pol} {} void destroy_if_owner() { if(owner()) { @@ -212,7 +212,9 @@ public: : base_type{} { static_assert(!std::is_const_v && !std::is_void_v, "Non-const non-void pointer required"); - if(value != nullptr) { + if(value == nullptr) { + initialize(); + } else { initialize(*value); mode = any_policy::dynamic; } @@ -266,11 +268,8 @@ public: */ basic_any &operator=(const basic_any &other) { if(this != &other) { - reset(); - - if(other.vtable) { - other.vtable(request::copy, other, this); - } + destroy_if_owner(); + other.vtable ? other.vtable(request::copy, other, this) : initialize(); } return *this; @@ -424,7 +423,7 @@ public: */ template void emplace(Args &&...args) { - reset(); + destroy_if_owner(); initialize(std::forward(args)...); } @@ -460,11 +459,7 @@ public: /*! @brief Destroys contained object */ void reset() { destroy_if_owner(); - - this->instance = nullptr; - vtable = nullptr; - descriptor = &type_id; - mode = any_policy::empty; + initialize(); } /** @@ -528,8 +523,8 @@ public: private: vtable_type *vtable{}; - const type_info &(*descriptor)() noexcept {&type_id}; - any_policy mode{any_policy::empty}; + const type_info &(*descriptor)() noexcept {}; + any_policy mode{}; }; /**