meta: non-const access to meta custom

This commit is contained in:
Michele Caini
2024-07-30 08:49:30 +02:00
parent 1beac44dda
commit 11e6b65108
3 changed files with 13 additions and 3 deletions

1
TODO
View File

@@ -45,4 +45,3 @@ TODO:
* natvis for meta_custom
* meta: vectors for details and props
* suppress -Wself-move on CI with g++13
* meta_custom operator bool

View File

@@ -843,7 +843,7 @@ struct meta_custom {
* @tparam Type Type to which conversion is requested.
*/
template<typename Type>
[[nodiscard]] operator const Type *() const noexcept {
[[nodiscard]] operator Type *() const noexcept {
return (type_id<Type>().hash() == node.type) ? std::static_pointer_cast<Type>(node.value).get() : nullptr;
}
@@ -852,7 +852,7 @@ struct meta_custom {
* @tparam Type Type to which conversion is requested.
*/
template<typename Type>
[[nodiscard]] operator const Type &() const noexcept {
[[nodiscard]] operator Type &() const noexcept {
ENTT_ASSERT(type_id<Type>().hash() == node.type, "Invalid type");
return *std::static_pointer_cast<Type>(node.value);
}

View File

@@ -113,6 +113,17 @@ TEST_F(MetaCustom, Func) {
ASSERT_EQ(static_cast<const int *>(entt::resolve<clazz>().func("g"_hs).custom()), nullptr);
}
TEST_F(MetaCustom, ConstNonConstAndAllInBetween) {
testing::StaticAssertTypeEq<decltype(static_cast<int *>(entt::meta_custom{})), int *>();
testing::StaticAssertTypeEq<decltype(static_cast<int &>(entt::meta_custom{})), int &>();
testing::StaticAssertTypeEq<decltype(static_cast<const int *>(entt::meta_custom{})), const int *>();
testing::StaticAssertTypeEq<decltype(static_cast<const int &>(entt::meta_custom{})), const int &>();
static_cast<char &>(entt::resolve<clazz>().custom()) = '\n';
ASSERT_EQ(*static_cast<const char *>(entt::resolve<clazz>().custom()), '\n');
}
TEST_F(MetaCustom, ReRegistration) {
using namespace entt::literals;