Переглянути джерело

meta: added size_of to meta type (close #565)

Michele Caini 5 роки тому
батько
коміт
ebc8328712

+ 3 - 1
src/entt/meta/internal.hpp

@@ -37,7 +37,7 @@ class meta_storage {
     using steal_fn_type = void(meta_storage &, meta_storage &);
     using destroy_fn_type = void(meta_storage &);
 
-    template<typename Type, typename = std::void_t<>>
+    template<typename Type, typename = void>
     struct type_traits {
         template<typename... Args>
         static void instance(meta_storage &buffer, Args &&... args) {
@@ -264,6 +264,7 @@ struct meta_type_node {
     id_type id;
     meta_type_node * next;
     meta_prop_node * prop;
+    const size_type size_of;
     const bool is_void;
     const bool is_integral;
     const bool is_floating_point;
@@ -408,6 +409,7 @@ public:
             {},
             nullptr,
             nullptr,
+            size_of_v<Type>,
             std::is_void_v<Type>,
             std::is_integral_v<Type>,
             std::is_floating_point_v<Type>,

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

@@ -1044,6 +1044,14 @@ public:
         return node->id;
     }
 
+    /**
+    * @brief Returns the size of the underlying type if known.
+    * @return The size of the underlying type if known, 0 otherwise.
+    */
+    [[nodiscard]] size_type size_of() const ENTT_NOEXCEPT {
+        return node->size_of;
+    }
+
     /**
      * @brief Checks whether a type refers to void or not.
      * @return True if the underlying type is void, false otherwise.

+ 4 - 4
test/entt/meta/meta_ctor.cpp

@@ -35,10 +35,10 @@ struct MetaCtor: ::testing::Test {
         entt::meta<derived_t>().base<base_t>();
 
         entt::meta<clazz_t>().type("clazz"_hs)
-                .ctor<const base_t &, int>()
-                .ctor<const int &, char>().prop(3, false)
-                .ctor<entt::overload<clazz_t(int)>(&clazz_t::factory)>().prop('c', 42)
-                .ctor<entt::overload<clazz_t(base_t, int, int)>(&clazz_t::factory)>();
+            .ctor<const base_t &, int>()
+            .ctor<const int &, char>().prop(3, false)
+            .ctor<entt::overload<clazz_t(int)>(&clazz_t::factory)>().prop('c', 42)
+            .ctor<entt::overload<clazz_t(base_t, int, int)>(&clazz_t::factory)>();
     }
 };
 

+ 16 - 16
test/entt/meta/meta_data.cpp

@@ -69,26 +69,26 @@ struct MetaData: ::testing::Test {
         entt::meta<derived_t>().base<base_t>().dtor<&derived_t::destroy>();
 
         entt::meta<clazz_t>().type("clazz"_hs)
-                .data<&clazz_t::i, entt::as_ref_t>("i"_hs).prop(3, 0)
-                .data<&clazz_t::j>("j"_hs).prop(true, 1)
-                .data<&clazz_t::h>("h"_hs).prop(property_t::random, 2)
-                .data<&clazz_t::k>("k"_hs).prop(property_t::value, 3)
-                .data<&clazz_t::base>("base"_hs)
-                .data<&clazz_t::i, entt::as_void_t>("void"_hs);
+            .data<&clazz_t::i, entt::as_ref_t>("i"_hs).prop(3, 0)
+            .data<&clazz_t::j>("j"_hs).prop(true, 1)
+            .data<&clazz_t::h>("h"_hs).prop(property_t::random, 2)
+            .data<&clazz_t::k>("k"_hs).prop(property_t::value, 3)
+            .data<&clazz_t::base>("base"_hs)
+            .data<&clazz_t::i, entt::as_void_t>("void"_hs);
 
         entt::meta<setter_getter_t>()
-                .type("setter_getter"_hs)
-                .data<&setter_getter_t::static_setter, &setter_getter_t::static_getter>("x"_hs)
-                .data<&setter_getter_t::setter, &setter_getter_t::getter>("y"_hs)
-                .data<&setter_getter_t::static_setter, &setter_getter_t::getter>("z"_hs)
-                .data<&setter_getter_t::setter_with_ref, &setter_getter_t::getter_with_ref>("w"_hs)
-                .data<nullptr, &setter_getter_t::getter>("z_ro"_hs)
-                .data<nullptr, &setter_getter_t::value>("value"_hs);
+            .type("setter_getter"_hs)
+            .data<&setter_getter_t::static_setter, &setter_getter_t::static_getter>("x"_hs)
+            .data<&setter_getter_t::setter, &setter_getter_t::getter>("y"_hs)
+            .data<&setter_getter_t::static_setter, &setter_getter_t::getter>("z"_hs)
+            .data<&setter_getter_t::setter_with_ref, &setter_getter_t::getter_with_ref>("w"_hs)
+            .data<nullptr, &setter_getter_t::getter>("z_ro"_hs)
+            .data<nullptr, &setter_getter_t::value>("value"_hs);
 
         entt::meta<array_t>()
-                .type("array"_hs)
-                .data<&array_t::global>("global"_hs)
-                .data<&array_t::local>("local"_hs);
+            .type("array"_hs)
+            .data<&array_t::global>("global"_hs)
+            .data<&array_t::local>("local"_hs);
     }
 
     void SetUp() override {

+ 8 - 8
test/entt/meta/meta_func.cpp

@@ -65,14 +65,14 @@ struct MetaFunc: ::testing::Test {
         entt::meta<derived_t>().base<base_t>().dtor<&derived_t::destroy>();
 
         entt::meta<func_t>().type("func"_hs)
-                .func<entt::overload<int(const base_t &, int, int)>(&func_t::f)>("f3"_hs)
-                .func<entt::overload<int(int, int)>(&func_t::f)>("f2"_hs).prop(true, false)
-                .func<entt::overload<int(int) const>(&func_t::f)>("f1"_hs).prop(true, false)
-                .func<&func_t::g>("g"_hs).prop(true, false)
-                .func<&func_t::h>("h"_hs).prop(true, false)
-                .func<&func_t::k>("k"_hs).prop(true, false)
-                .func<&func_t::v, entt::as_void_t>("v"_hs)
-                .func<&func_t::a, entt::as_ref_t>("a"_hs);
+            .func<entt::overload<int(const base_t &, int, int)>(&func_t::f)>("f3"_hs)
+            .func<entt::overload<int(int, int)>(&func_t::f)>("f2"_hs).prop(true, false)
+            .func<entt::overload<int(int) const>(&func_t::f)>("f1"_hs).prop(true, false)
+            .func<&func_t::g>("g"_hs).prop(true, false)
+            .func<&func_t::h>("h"_hs).prop(true, false)
+            .func<&func_t::k>("k"_hs).prop(true, false)
+            .func<&func_t::v, entt::as_void_t>("v"_hs)
+            .func<&func_t::a, entt::as_ref_t>("a"_hs);
     }
 
     void SetUp() override {

+ 24 - 17
test/entt/meta/meta_type.cpp

@@ -70,25 +70,25 @@ struct MetaType: ::testing::Test {
         entt::meta<concrete_t>().base<base_t>().base<abstract_t>();
 
         entt::meta<property_t>()
-                .data<property_t::random>("random"_hs)
-                    .prop(property_t::random, 0)
-                    .prop(property_t::value, 3)
-                .data<property_t::value>("value"_hs)
-                    .prop(std::make_tuple(std::make_pair(property_t::random, true), std::make_pair(property_t::value, 0), property_t::key_only))
-                    .prop(property_t::list)
-                .data<property_t::key_only>("key_only"_hs)
-                    .prop([]() { return property_t::key_only; })
-                .data<property_t::list>("list"_hs)
-                   .props(std::make_pair(property_t::random, false), std::make_pair(property_t::value, 0), property_t::key_only)
-                .data<&set<property_t>, &get<property_t>>("var"_hs);
+            .data<property_t::random>("random"_hs)
+                .prop(property_t::random, 0)
+                .prop(property_t::value, 3)
+            .data<property_t::value>("value"_hs)
+                .prop(std::make_tuple(std::make_pair(property_t::random, true), std::make_pair(property_t::value, 0), property_t::key_only))
+                .prop(property_t::list)
+            .data<property_t::key_only>("key_only"_hs)
+                .prop([]() { return property_t::key_only; })
+            .data<property_t::list>("list"_hs)
+               .props(std::make_pair(property_t::random, false), std::make_pair(property_t::value, 0), property_t::key_only)
+            .data<&set<property_t>, &get<property_t>>("var"_hs);
 
         entt::meta<clazz_t>()
-                .type("clazz"_hs)
-                    .prop(property_t::value, 42)
-                .ctor().ctor<const base_t &, int>()
-                .data<&clazz_t::value>("value"_hs)
-                .func<&clazz_t::member>("member"_hs)
-                .func<&clazz_t::func>("func"_hs);
+            .type("clazz"_hs)
+                .prop(property_t::value, 42)
+            .ctor().ctor<const base_t &, int>()
+            .data<&clazz_t::value>("value"_hs)
+            .func<&clazz_t::member>("member"_hs)
+            .func<&clazz_t::func>("func"_hs);
     }
 };
 
@@ -135,6 +135,13 @@ TEST_F(MetaType, Functionalities) {
     ASSERT_EQ(prop.value(), 42);
 }
 
+TEST_F(MetaType, SizeOf) {
+    ASSERT_EQ(entt::resolve<void>().size_of(), 0u);
+    ASSERT_EQ(entt::resolve<int>().size_of(), sizeof(int));
+    ASSERT_EQ(entt::resolve<int[]>().size_of(), 0u);
+    ASSERT_EQ(entt::resolve<int[3]>().size_of(), sizeof(int[3]));
+}
+
 TEST_F(MetaType, Traits) {
     ASSERT_TRUE(entt::resolve<void>().is_void());
     ASSERT_FALSE(entt::resolve<int>().is_void());