From c158c2d839d2ff329a30e658555ea32d39fd706e Mon Sep 17 00:00:00 2001 From: Michele Caini Date: Thu, 9 Jan 2025 17:37:29 +0100 Subject: [PATCH] meta: get rid of unnecessary checks --- src/entt/meta/meta.hpp | 10 +++++----- src/entt/meta/node.hpp | 10 ++++++---- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/entt/meta/meta.hpp b/src/entt/meta/meta.hpp index a0420cfb9..aa53e28c6 100644 --- a/src/entt/meta/meta.hpp +++ b/src/entt/meta/meta.hpp @@ -439,7 +439,7 @@ class meta_any { template [[nodiscard]] const Type *try_cast() const { const auto &other = type_id>(); - return static_cast(internal::try_cast(internal::meta_context::from(*ctx), node, other, storage.data())); + return static_cast(internal::try_cast(ctx, node, other, storage.data())); } /*! @copydoc try_cast */ @@ -449,7 +449,7 @@ class meta_any { return std::as_const(*this).try_cast>(); } else { const auto &other = type_id(); - return static_cast(const_cast(internal::try_cast(internal::meta_context::from(*ctx), node, other, storage.data()))); + return static_cast(const_cast(internal::try_cast(ctx, node, other, storage.data()))); } } @@ -1303,7 +1303,7 @@ class meta_type { */ [[nodiscard]] bool can_cast(const meta_type &other) const noexcept { // casting this is UB in all cases but we aren't going to use the resulting pointer, so... - return (ctx != nullptr) && other && (internal::try_cast(internal::meta_context::from(*ctx), node, *other.node.info, this) != nullptr); + return other && (internal::try_cast(ctx, node, *other.node.info, this) != nullptr); } /** @@ -1312,7 +1312,7 @@ class meta_type { * @return True if the conversion is allowed, false otherwise. */ [[nodiscard]] bool can_convert(const meta_type &other) const noexcept { - return (ctx != nullptr) && (internal::try_convert(internal::meta_context::from(*ctx), node, other.info(), other.is_arithmetic() || other.is_enum(), nullptr, [](const void *, auto &&...args) { return ((static_cast(args), 1) + ... + 0u); }) != 0u); + return (internal::try_convert(ctx, node, other.info(), other.is_arithmetic() || other.is_enum(), nullptr, [](const void *, auto &&...args) { return ((static_cast(args), 1) + ... + 0u); }) != 0u); } /** @@ -1547,7 +1547,7 @@ bool meta_any::set(const id_type id, Type &&value) { } [[nodiscard]] inline meta_any meta_any::allow_cast(const meta_type &type) const { - return internal::try_convert(internal::meta_context::from(*ctx), node, type.info(), type.is_arithmetic() || type.is_enum(), storage.data(), [this, &type]([[maybe_unused]] const void *instance, [[maybe_unused]] auto &&...args) { + return internal::try_convert(ctx, node, type.info(), type.is_arithmetic() || type.is_enum(), storage.data(), [this, &type]([[maybe_unused]] const void *instance, [[maybe_unused]] auto &&...args) { if constexpr((std::is_same_v>, internal::meta_type_node> || ...)) { return (args.from_void(*ctx, nullptr, instance), ...); } else if constexpr((std::is_same_v>, internal::meta_conv_node> || ...)) { diff --git a/src/entt/meta/node.hpp b/src/entt/meta/node.hpp index 1719d15f1..e73aae701 100644 --- a/src/entt/meta/node.hpp +++ b/src/entt/meta/node.hpp @@ -198,14 +198,15 @@ template return value(context); } -[[nodiscard]] inline const void *try_cast(const meta_context &context, const meta_type_node &from, const type_info &to, const void *instance) noexcept { +[[nodiscard]] inline const void *try_cast(const meta_ctx *context, const meta_type_node &from, const type_info &to, const void *instance) noexcept { if((from.info != nullptr) && *from.info == to) { return instance; } if(from.details) { for(auto &&curr: from.details->base) { - if(const void *elem = try_cast(context, curr.resolve(context), to, curr.cast(instance)); elem) { + ENTT_ASSERT(context != nullptr, "Null context not allowed"); + if(const void *elem = try_cast(context, curr.resolve(meta_context::from(*context)), to, curr.cast(instance)); elem) { return elem; } } @@ -215,7 +216,7 @@ template } template -[[nodiscard]] inline auto try_convert(const meta_context &context, const meta_type_node &from, const type_info &to, const bool arithmetic_or_enum, const void *instance, Func func) { +[[nodiscard]] inline auto try_convert(const meta_ctx *context, const meta_type_node &from, const type_info &to, const bool arithmetic_or_enum, const void *instance, Func func) { if(from.info && *from.info == to) { return func(instance, from); } @@ -228,7 +229,8 @@ template } for(auto &&curr: from.details->base) { - if(auto other = try_convert(context, curr.resolve(context), to, arithmetic_or_enum, curr.cast(instance), func); other) { + ENTT_ASSERT(context != nullptr, "Null context not allowed"); + if(auto other = try_convert(context, curr.resolve(meta_context::from(*context)), to, arithmetic_or_enum, curr.cast(instance), func); other) { return other; } }