Skip to content

Commit 1650d05

Browse files
committed
meta: early exit for typed allow_cast
1 parent 4208eaa commit 1650d05

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

TODO

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,5 @@ TODO:
3131
* allow passing arguments to meta setter/getter (we can fallback on meta invoke probably)
3232
* FetchContent_Populate -> FetchContent_MakeAvailable warnings
3333
* doc: IMPLICIT_DIR_DOCS for dir docs or \dir
34+
* meta non-const allow_cast overloads: (const int &) to (int &) is not allowed, but (const int &) to (double &) is allowed
35+
* improve non-const allow cast with in-place switch

src/entt/meta/meta.hpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -514,8 +514,8 @@ class meta_any {
514514
if constexpr(std::is_reference_v<Type> && !std::is_const_v<std::remove_reference_t<Type>>) {
515515
return meta_any{meta_ctx_arg, *ctx};
516516
} else {
517-
auto other = internal::resolve<std::remove_cv_t<std::remove_reference_t<Type>>>(internal::meta_context::from(*ctx));
518-
return allow_cast(meta_type{*ctx, other});
517+
// also support early return for performance reasons
518+
return ((node.info != nullptr) && (*node.info == entt::type_id<Type>())) ? as_ref() : allow_cast(meta_type{*ctx, internal::resolve<std::remove_cv_t<std::remove_reference_t<Type>>>(internal::meta_context::from(*ctx))});
519519
}
520520
}
521521

@@ -526,8 +526,9 @@ class meta_any {
526526
*/
527527
template<typename Type>
528528
[[nodiscard]] bool allow_cast() {
529-
auto other = internal::resolve<std::remove_cv_t<std::remove_reference_t<Type>>>(internal::meta_context::from(*ctx));
530-
return allow_cast(meta_type{*ctx, other}) && (!(std::is_reference_v<Type> && !std::is_const_v<std::remove_reference_t<Type>>) || storage.data() != nullptr);
529+
// also support early return for performance reasons
530+
return (((node.info != nullptr) && (*node.info == entt::type_id<Type>())) || allow_cast(meta_type{*ctx, internal::resolve<std::remove_cv_t<std::remove_reference_t<Type>>>(internal::meta_context::from(*ctx))}))
531+
&& (!(std::is_reference_v<Type> && !std::is_const_v<std::remove_reference_t<Type>>) || storage.data() != nullptr);
531532
}
532533

533534
/*! @copydoc any::emplace */

0 commit comments

Comments
 (0)