meta: avoid pointless lookups in try_cast (#1201)

This commit is contained in:
Paolo Monteverde
2024-12-13 14:35:05 +01:00
committed by GitHub
parent abdbddf77f
commit 5ef4a2f22a
2 changed files with 5 additions and 5 deletions
+3 -3
View File
@@ -438,7 +438,7 @@ public:
*/
template<typename Type>
[[nodiscard]] const Type *try_cast() const {
const auto other = internal::resolve<std::remove_cv_t<Type>>(internal::meta_context::from(*ctx));
const auto &other = type_id<std::remove_cv_t<Type>>();
return static_cast<const Type *>(internal::try_cast(internal::meta_context::from(*ctx), node, other, storage.data()));
}
@@ -448,7 +448,7 @@ public:
if constexpr(std::is_const_v<Type>) {
return std::as_const(*this).try_cast<std::remove_const_t<Type>>();
} else {
const auto other = internal::resolve<std::remove_cv_t<Type>>(internal::meta_context::from(*ctx));
const auto &other = type_id<std::remove_cv_t<Type>>();
return static_cast<Type *>(const_cast<void *>(internal::try_cast(internal::meta_context::from(*ctx), node, other, storage.data())));
}
}
@@ -1304,7 +1304,7 @@ public:
*/
[[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 (internal::try_cast(internal::meta_context::from(*ctx), node, other.node, this) != nullptr);
return (other.node.info != nullptr) && (internal::try_cast(internal::meta_context::from(*ctx), node, *other.node.info, this) != nullptr);
}
/**
+2 -2
View File
@@ -198,8 +198,8 @@ template<typename... Args>
return value(context);
}
[[nodiscard]] inline const void *try_cast(const meta_context &context, const meta_type_node &from, const meta_type_node &to, const void *instance) noexcept {
if((from.info != nullptr) && (to.info != nullptr) && *from.info == *to.info) {
[[nodiscard]] inline const void *try_cast(const meta_context &context, const meta_type_node &from, const type_info &to, const void *instance) noexcept {
if((from.info != nullptr) && *from.info == to) {
return instance;
}