Browse Source

meta_any: decays Type on storage construction to handle correctly const T[N] types (close #687)

Michele Caini 4 years ago
parent
commit
60393fbc5f
2 changed files with 18 additions and 5 deletions
  1. 4 4
      src/entt/meta/meta.hpp
  2. 14 1
      test/entt/meta/meta_prop.cpp

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

@@ -251,9 +251,9 @@ public:
      * @tparam Type Type of object to use to initialize the wrapper.
      * @param value An instance of an object to use to initialize the wrapper.
      */
-    template<typename Type, typename = std::enable_if_t<!std::is_same_v<std::remove_cv_t<std::remove_reference_t<Type>>, meta_any>>>
+    template<typename Type, typename = std::enable_if_t<!std::is_same_v<std::decay_t<Type>, meta_any>>>
     meta_any(Type &&value)
-        : meta_any{std::in_place_type<std::remove_cv_t<std::remove_reference_t<Type>>>, std::forward<Type>(value)}
+        : meta_any{std::in_place_type<std::decay_t<Type>>, std::forward<Type>(value)}
     {}
 
     /**
@@ -642,11 +642,11 @@ struct meta_handle {
      * @tparam Type Type of object to use to initialize the handle.
      * @param value An instance of an object to use to initialize the handle.
      */
-    template<typename Type, typename = std::enable_if_t<!std::is_same_v<std::remove_cv_t<std::remove_reference_t<Type>>, meta_handle>>>
+    template<typename Type, typename = std::enable_if_t<!std::is_same_v<std::decay_t<Type>, meta_handle>>>
     meta_handle(Type &value) ENTT_NOEXCEPT
         : meta_handle{}
     {
-        if constexpr(std::is_same_v<std::remove_cv_t<std::remove_reference_t<Type>>, meta_any>) {
+        if constexpr(std::is_same_v<std::decay_t<Type>, meta_any>) {
             any = value.as_ref();
         } else {
             any.emplace<Type &>(value);

+ 14 - 1
test/entt/meta/meta_prop.cpp

@@ -1,3 +1,4 @@
+#include <string.h>
 #include <gtest/gtest.h>
 #include <entt/core/hashed_string.hpp>
 #include <entt/meta/factory.hpp>
@@ -18,7 +19,8 @@ struct MetaProp: ::testing::Test {
 
         entt::meta<base_2_t>()
             .type("base_2"_hs)
-            .prop("bool"_hs, false);
+            .prop("bool"_hs, false)
+            .prop("char[]"_hs, "char[]");
 
         entt::meta<derived_t>()
             .type("derived"_hs)
@@ -57,6 +59,17 @@ TEST_F(MetaProp, FromBase) {
     ASSERT_EQ(prop_int.value().cast<int>(), 42);
 }
 
+TEST_F(MetaProp, DeducedArrayType) {
+    using namespace entt::literals;
+
+    auto prop = entt::resolve<base_2_t>().prop("char[]"_hs);
+
+    ASSERT_TRUE(prop);
+    ASSERT_EQ(prop.key(), "char[]"_hs);
+    ASSERT_EQ(prop.value().type(), entt::resolve<const char *>());
+    ASSERT_EQ(std::strcmp(prop.value().cast<const char *>(), "char[]"), 0);
+}
+
 TEST_F(MetaProp, ReRegistration) {
     using namespace entt::literals;