Parcourir la source

meta: non-const access to meta custom

Michele Caini il y a 1 an
Parent
commit
11e6b65108
3 fichiers modifiés avec 13 ajouts et 3 suppressions
  1. 0 1
      TODO
  2. 2 2
      src/entt/meta/meta.hpp
  3. 11 0
      test/entt/meta/meta_custom.cpp

+ 0 - 1
TODO

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

+ 2 - 2
src/entt/meta/meta.hpp

@@ -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);
     }

+ 11 - 0
test/entt/meta/meta_custom.cpp

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