From eaa93d287e5086574e9958da1cf6297cbddeaf90 Mon Sep 17 00:00:00 2001 From: skypjack Date: Mon, 24 Jul 2023 17:32:51 +0200 Subject: [PATCH] meta: meta_type::can_cast (see #1028) --- src/entt/meta/meta.hpp | 9 +++++++++ test/entt/meta/meta_type.cpp | 8 ++++++++ 2 files changed, 17 insertions(+) diff --git a/src/entt/meta/meta.hpp b/src/entt/meta/meta.hpp index 443d2c330..792c89ebf 100644 --- a/src/entt/meta/meta.hpp +++ b/src/entt/meta/meta.hpp @@ -1324,6 +1324,15 @@ public: return index < template_arity() ? meta_type{*ctx, node.templ.arg(internal::meta_context::from(*ctx), index)} : meta_type{}; } + /** + * @brief Checks if a type supports direct casting to another type. + * @return True if direct casting is allowed, false otherwise. + */ + [[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); + } + /** * @brief Returns a range to visit registered top-level base meta types. * @return An iterable range to visit registered top-level base meta types. diff --git a/test/entt/meta/meta_type.cpp b/test/entt/meta/meta_type.cpp index 56012deff..ea8630590 100644 --- a/test/entt/meta/meta_type.cpp +++ b/test/entt/meta/meta_type.cpp @@ -303,6 +303,14 @@ TEST_F(MetaType, TemplateInfo) { ASSERT_EQ(entt::resolve>().template_arg(1u), entt::meta_type{}); } +TEST_F(MetaType, CanCast) { + auto type = entt::resolve(); + + ASSERT_FALSE(type.can_cast(entt::resolve())); + ASSERT_TRUE(type.can_cast(entt::resolve())); + ASSERT_TRUE(type.can_cast(entt::resolve())); +} + TEST_F(MetaType, Base) { auto type = entt::resolve();