meta: meta_type::can_cast (see #1028)

This commit is contained in:
skypjack
2023-07-24 17:32:51 +02:00
committed by Michele Caini
parent 4517ae22e5
commit eaa93d287e
2 changed files with 17 additions and 0 deletions

View File

@@ -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.

View File

@@ -303,6 +303,14 @@ TEST_F(MetaType, TemplateInfo) {
ASSERT_EQ(entt::resolve<std::shared_ptr<int>>().template_arg(1u), entt::meta_type{});
}
TEST_F(MetaType, CanCast) {
auto type = entt::resolve<derived_t>();
ASSERT_FALSE(type.can_cast(entt::resolve<void>()));
ASSERT_TRUE(type.can_cast(entt::resolve<base_t>()));
ASSERT_TRUE(type.can_cast(entt::resolve<derived_t>()));
}
TEST_F(MetaType, Base) {
auto type = entt::resolve<derived_t>();