Просмотр исходного кода

meta: properties can be also runtime objects

Michele Caini 6 лет назад
Родитель
Сommit
b0ea150e94
4 измененных файлов с 41 добавлено и 54 удалено
  1. 0 1
      TODO
  2. 0 14
      src/entt/core/type_traits.hpp
  3. 22 20
      src/entt/meta/factory.hpp
  4. 19 19
      test/entt/meta/meta.cpp

+ 0 - 1
TODO

@@ -36,6 +36,5 @@
 
 
 * make meta work across boundaries
 * make meta work across boundaries
   - extend and make factory::prop more flexible
   - extend and make factory::prop more flexible
-  - entt::reflect<T>().type("foo"_hs, entt::as_property); or similar
   - a better context
   - a better context
   - tests, doc
   - tests, doc

+ 0 - 14
src/entt/core/type_traits.hpp

@@ -120,20 +120,6 @@ template<typename Type>
 using type_list_unique_t = typename type_list_unique<Type>::type;
 using type_list_unique_t = typename type_list_unique<Type>::type;
 
 
 
 
-/**
- * @brief A class to use to push around constexpr properties, nothing more.
- * @tparam Key Property key.
- * @tparam Value Property value.
- */
-template<auto Key, auto Value>
-struct property {
-    /*! @brief Property key. */
-    static constexpr auto key = Key;
-    /*! @brief Property value. */
-    static constexpr auto value = Value;
-};
-
-
 /**
 /**
  * @brief Provides the member constant `value` to true if a given type is
  * @brief Provides the member constant `value` to true if a given type is
  * equality comparable, false otherwise.
  * equality comparable, false otherwise.

+ 22 - 20
src/entt/meta/factory.hpp

@@ -696,22 +696,6 @@ class extended_meta_factory: public meta_factory<Type> {
         return node && (node->key() == key || duplicate(key, node->next));
         return node && (node->key() == key || duplicate(key, node->next));
     }
     }
 
 
-    template<auto Key, auto Value>
-    void prop(property<Key, Value>) {
-        static internal::meta_prop_node node{
-            *props,
-            []() -> meta_any {
-                return Key;
-            },
-            []() -> meta_any {
-                return Value;
-            }
-        };
-
-        ENTT_ASSERT(!duplicate(meta_any{Key}, *props));
-        *props = &node;
-    }
-
     extended_meta_factory(entt::internal::meta_prop_node **target)
     extended_meta_factory(entt::internal::meta_prop_node **target)
         : props{target}
         : props{target}
     {}
     {}
@@ -719,13 +703,31 @@ class extended_meta_factory: public meta_factory<Type> {
 public:
 public:
     /**
     /**
      * @brief Assigns properties to the last meta object created.
      * @brief Assigns properties to the last meta object created.
-     * @tparam Property Properties to assign to the meta object.
+     * @tparam Key Type of the property key.
+     * @tparam Value Type of the property value.
+     * @param pkey Property key.
+     * @param pvalue Property value.
      * @return A meta factory for the parent type.
      * @return A meta factory for the parent type.
      */
      */
-    template<typename... Property>
-    auto prop() {
+    template<typename Key, typename Value>
+    auto prop(Key &&pkey, Value &&pvalue) {
         ENTT_ASSERT(props);
         ENTT_ASSERT(props);
-        (prop(Property{}), ...);
+        static auto key{std::forward<Key>(pkey)};
+        static auto value{std::forward<Value>(pvalue)};
+
+        static internal::meta_prop_node node{
+            *props,
+            []() -> meta_any {
+                return key;
+            },
+            []() -> meta_any {
+                return value;
+            }
+        };
+
+        ENTT_ASSERT(!duplicate(key, *props));
+        *props = &node;
+
         return *this;
         return *this;
     }
     }
 
 

+ 19 - 19
test/entt/meta/meta.cpp

@@ -147,15 +147,15 @@ struct Meta: ::testing::Test {
 
 
         entt::meta<char>()
         entt::meta<char>()
                 .type("char"_hs)
                 .type("char"_hs)
-                    .prop<entt::property<properties::prop_int, 42>>()
+                    .prop(properties::prop_int, 42)
                 .data<&set<char>, &get<char>>("value"_hs);
                 .data<&set<char>, &get<char>>("value"_hs);
 
 
         entt::meta<properties>()
         entt::meta<properties>()
                 .data<properties::prop_bool>("prop_bool"_hs)
                 .data<properties::prop_bool>("prop_bool"_hs)
-                    .prop<entt::property<properties::prop_int, 0>>()
+                    .prop(properties::prop_int, 0)
                 .data<properties::prop_int>("prop_int"_hs)
                 .data<properties::prop_int>("prop_int"_hs)
-                    .prop<entt::property<properties::prop_bool, true>>()
-                    .prop<entt::property<properties::prop_int, 0>>()
+                    .prop(properties::prop_bool, true)
+                    .prop(properties::prop_int, 0)
                 .data<&set<properties>, &get<properties>>("value"_hs);
                 .data<&set<properties>, &get<properties>>("value"_hs);
 
 
         entt::meta<unsigned int>().data<0u>("min"_hs).data<100u>("max"_hs);
         entt::meta<unsigned int>().data<0u>("min"_hs).data<100u>("max"_hs);
@@ -165,12 +165,12 @@ struct Meta: ::testing::Test {
 
 
         entt::meta<derived_type>()
         entt::meta<derived_type>()
                 .type("derived"_hs)
                 .type("derived"_hs)
-                    .prop<entt::property<properties::prop_int, 99>>()
+                    .prop(properties::prop_int, 99)
                 .base<base_type>()
                 .base<base_type>()
                 .ctor<const base_type &, int, char>()
                 .ctor<const base_type &, int, char>()
-                    .prop<entt::property<properties::prop_bool, false>>()
+                    .prop(properties::prop_bool, false)
                 .ctor<&derived_factory>()
                 .ctor<&derived_factory>()
-                    .prop<entt::property<properties::prop_int, 42>>()
+                    .prop(properties::prop_int, 42)
                 .conv<&derived_type::f>()
                 .conv<&derived_type::f>()
                 .conv<&derived_type::g>();
                 .conv<&derived_type::g>();
 
 
@@ -186,13 +186,13 @@ struct Meta: ::testing::Test {
         entt::meta<data_type>()
         entt::meta<data_type>()
                 .type("data"_hs)
                 .type("data"_hs)
                 .data<&data_type::i, entt::as_alias_t>("i"_hs)
                 .data<&data_type::i, entt::as_alias_t>("i"_hs)
-                    .prop<entt::property<properties::prop_int, 0>>()
+                    .prop(properties::prop_int, 0)
                 .data<&data_type::j>("j"_hs)
                 .data<&data_type::j>("j"_hs)
-                    .prop<entt::property<properties::prop_int, 1>>()
+                    .prop(properties::prop_int, 1)
                 .data<&data_type::h>("h"_hs)
                 .data<&data_type::h>("h"_hs)
-                    .prop<entt::property<properties::prop_int, 2>>()
+                    .prop(properties::prop_int, 2)
                 .data<&data_type::k>("k"_hs)
                 .data<&data_type::k>("k"_hs)
-                    .prop<entt::property<properties::prop_int, 3>>()
+                    .prop(properties::prop_int, 3)
                 .data<&data_type::empty>("empty"_hs)
                 .data<&data_type::empty>("empty"_hs)
                 .data<&data_type::v, entt::as_void_t>("v"_hs);
                 .data<&data_type::v, entt::as_void_t>("v"_hs);
 
 
@@ -205,15 +205,15 @@ struct Meta: ::testing::Test {
                 .type("func"_hs)
                 .type("func"_hs)
                 .func<entt::overload<int(const base_type &, int, int)>(&func_type::f)>("f3"_hs)
                 .func<entt::overload<int(const base_type &, int, int)>(&func_type::f)>("f3"_hs)
                 .func<entt::overload<int(int, int)>(&func_type::f)>("f2"_hs)
                 .func<entt::overload<int(int, int)>(&func_type::f)>("f2"_hs)
-                    .prop<entt::property<properties::prop_bool, false>>()
+                    .prop(properties::prop_bool, false)
                 .func<entt::overload<int(int) const>(&func_type::f)>("f1"_hs)
                 .func<entt::overload<int(int) const>(&func_type::f)>("f1"_hs)
-                    .prop<entt::property<properties::prop_bool, false>>()
+                    .prop(properties::prop_bool, false)
                 .func<&func_type::g>("g"_hs)
                 .func<&func_type::g>("g"_hs)
-                    .prop<entt::property<properties::prop_bool, false>>()
+                    .prop(properties::prop_bool, false)
                 .func<&func_type::h>("h"_hs)
                 .func<&func_type::h>("h"_hs)
-                    .prop<entt::property<properties::prop_bool, false>>()
+                    .prop(properties::prop_bool, false)
                 .func<&func_type::k>("k"_hs)
                 .func<&func_type::k>("k"_hs)
-                    .prop<entt::property<properties::prop_bool, false>>()
+                    .prop(properties::prop_bool, false)
                 .func<&func_type::v, entt::as_void_t>("v"_hs)
                 .func<&func_type::v, entt::as_void_t>("v"_hs)
                 .func<&func_type::a, entt::as_alias_t>("a"_hs);
                 .func<&func_type::a, entt::as_alias_t>("a"_hs);
 
 
@@ -226,14 +226,14 @@ struct Meta: ::testing::Test {
 
 
         entt::meta<an_abstract_type>()
         entt::meta<an_abstract_type>()
                 .type("an_abstract_type"_hs)
                 .type("an_abstract_type"_hs)
-                    .prop<entt::property<properties::prop_bool, false>>()
+                    .prop(properties::prop_bool, false)
                 .data<&an_abstract_type::i>("i"_hs)
                 .data<&an_abstract_type::i>("i"_hs)
                 .func<&an_abstract_type::f>("f"_hs)
                 .func<&an_abstract_type::f>("f"_hs)
                 .func<&an_abstract_type::g>("g"_hs);
                 .func<&an_abstract_type::g>("g"_hs);
 
 
         entt::meta<another_abstract_type>()
         entt::meta<another_abstract_type>()
                 .type("another_abstract_type"_hs)
                 .type("another_abstract_type"_hs)
-                    .prop<entt::property<properties::prop_int, 42>>()
+                    .prop(properties::prop_int, 42)
                 .data<&another_abstract_type::j>("j"_hs)
                 .data<&another_abstract_type::j>("j"_hs)
                 .func<&another_abstract_type::h>("h"_hs);
                 .func<&another_abstract_type::h>("h"_hs);
 
 
@@ -249,7 +249,7 @@ struct Meta: ::testing::Test {
 
 
         entt::meta<derived_type>()
         entt::meta<derived_type>()
                 .type("my_type"_hs)
                 .type("my_type"_hs)
-                    .prop<entt::property<properties::prop_bool, false>>()
+                    .prop(properties::prop_bool, false)
                 .ctor<>();
                 .ctor<>();
 
 
         entt::meta<another_abstract_type>()
         entt::meta<another_abstract_type>()