Browse Source

meta: update meta_handle context propagation (with non-regression tests)

skypjack 5 months ago
parent
commit
a90584f1ce
2 changed files with 53 additions and 8 deletions
  1. 8 8
      src/entt/meta/meta.hpp
  2. 45 0
      test/entt/meta/meta_handle.cpp

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

@@ -650,13 +650,13 @@ template<typename Type>
 
 /*! @brief Opaque pointers to instances of any type. */
 class meta_handle {
-    template<typename Type, typename = std::enable_if_t<std::is_same_v<std::decay_t<Type>, meta_any>>>
-    meta_handle(int, const meta_ctx &ctx, Type &value)
-        : any{ctx, value.as_ref()} {}
+    template<typename Type, typename... Args, typename = std::enable_if_t<std::is_same_v<std::decay_t<Type>, meta_any>>>
+    meta_handle(int, Type &value, Args &&...args)
+        : any{std::forward<Args>(args)..., value.as_ref()} {}
 
-    template<typename Type>
-    meta_handle(char, const meta_ctx &ctx, Type &value)
-        : any{ctx, std::in_place_type<Type &>, value} {}
+    template<typename Type, typename... Args>
+    meta_handle(char, Type &value, Args &&...args)
+        : any{std::forward<Args>(args)..., std::in_place_type<Type &>, value} {}
 
 public:
     /*! Default constructor. */
@@ -670,7 +670,7 @@ public:
      */
     template<typename Type, typename = std::enable_if_t<!std::is_same_v<std::decay_t<Type>, meta_handle>>>
     meta_handle(const meta_ctx &ctx, Type &value)
-        : meta_handle{0, ctx, value} {}
+        : meta_handle{0, value, ctx} {}
 
     /**
      * @brief Creates a handle that points to an unmanaged object.
@@ -679,7 +679,7 @@ public:
      */
     template<typename Type, typename = std::enable_if_t<!std::is_same_v<std::decay_t<Type>, meta_handle>>>
     meta_handle(Type &value)
-        : meta_handle{0, locator<meta_ctx>::value_or(), value} {}
+        : meta_handle{0, value} {}
 
     /**
      * @brief Context aware move constructor.

+ 45 - 0
test/entt/meta/meta_handle.cpp

@@ -1,6 +1,8 @@
 #include <utility>
 #include <gtest/gtest.h>
 #include <entt/core/hashed_string.hpp>
+#include <entt/locator/locator.hpp>
+#include <entt/meta/context.hpp>
 #include <entt/meta/factory.hpp>
 #include <entt/meta/meta.hpp>
 
@@ -61,3 +63,46 @@ TEST_F(MetaHandle, Handle) {
     ASSERT_FALSE(std::as_const(handle)->invoke("decr"_hs));
     ASSERT_EQ(instance.value, 0);
 }
+
+TEST_F(MetaHandle, Value) {
+    int value{2};
+    entt::meta_handle handle{value};
+    entt::meta_handle chandle{std::as_const(value)};
+
+    ASSERT_NE(handle->try_cast<int>(), nullptr);
+    ASSERT_NE(handle->try_cast<const int>(), nullptr);
+    ASSERT_EQ(chandle->try_cast<int>(), nullptr);
+    ASSERT_NE(chandle->try_cast<const int>(), nullptr);
+
+    ASSERT_EQ(&handle->context(), &entt::locator<entt::meta_ctx>::value_or());
+    ASSERT_EQ(&chandle->context(), &entt::locator<entt::meta_ctx>::value_or());
+}
+
+TEST_F(MetaHandle, MetaAny) {
+    entt::meta_any value{2};
+    entt::meta_handle handle{value};
+    entt::meta_handle chandle{std::as_const(value)};
+
+    ASSERT_NE(handle->try_cast<int>(), nullptr);
+    ASSERT_NE(handle->try_cast<const int>(), nullptr);
+    ASSERT_EQ(chandle->try_cast<int>(), nullptr);
+    ASSERT_NE(chandle->try_cast<const int>(), nullptr);
+
+    ASSERT_EQ(&handle->context(), &entt::locator<entt::meta_ctx>::value_or());
+    ASSERT_EQ(&chandle->context(), &entt::locator<entt::meta_ctx>::value_or());
+}
+
+TEST_F(MetaHandle, ScopedMetaAny) {
+    entt::meta_ctx ctx{};
+    entt::meta_any value{ctx, 2};
+    entt::meta_handle handle{value};
+    entt::meta_handle chandle{std::as_const(value)};
+
+    ASSERT_NE(handle->try_cast<int>(), nullptr);
+    ASSERT_NE(handle->try_cast<const int>(), nullptr);
+    ASSERT_EQ(chandle->try_cast<int>(), nullptr);
+    ASSERT_NE(chandle->try_cast<const int>(), nullptr);
+
+    ASSERT_EQ(&handle->context(), &ctx);
+    ASSERT_EQ(&chandle->context(), &ctx);
+}