From b0ea150e94b2da6edffda53467a2f56c4107df73 Mon Sep 17 00:00:00 2001 From: Michele Caini Date: Mon, 14 Oct 2019 15:51:14 +0200 Subject: [PATCH] meta: properties can be also runtime objects --- TODO | 1 - src/entt/core/type_traits.hpp | 14 ------------ src/entt/meta/factory.hpp | 42 ++++++++++++++++++----------------- test/entt/meta/meta.cpp | 38 +++++++++++++++---------------- 4 files changed, 41 insertions(+), 54 deletions(-) diff --git a/TODO b/TODO index 48ca05d55..591c582e5 100644 --- a/TODO +++ b/TODO @@ -36,6 +36,5 @@ * make meta work across boundaries - extend and make factory::prop more flexible - - entt::reflect().type("foo"_hs, entt::as_property); or similar - a better context - tests, doc diff --git a/src/entt/core/type_traits.hpp b/src/entt/core/type_traits.hpp index c6e36769d..d9d1457db 100644 --- a/src/entt/core/type_traits.hpp +++ b/src/entt/core/type_traits.hpp @@ -120,20 +120,6 @@ template using type_list_unique_t = typename type_list_unique::type; -/** - * @brief A class to use to push around constexpr properties, nothing more. - * @tparam Key Property key. - * @tparam Value Property value. - */ -template -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 * equality comparable, false otherwise. diff --git a/src/entt/meta/factory.hpp b/src/entt/meta/factory.hpp index 6b46a88b6..bf98ec68a 100644 --- a/src/entt/meta/factory.hpp +++ b/src/entt/meta/factory.hpp @@ -696,22 +696,6 @@ class extended_meta_factory: public meta_factory { return node && (node->key() == key || duplicate(key, node->next)); } - template - void prop(property) { - 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) : props{target} {} @@ -719,13 +703,31 @@ class extended_meta_factory: public meta_factory { public: /** * @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. */ - template - auto prop() { + template + auto prop(Key &&pkey, Value &&pvalue) { ENTT_ASSERT(props); - (prop(Property{}), ...); + static auto key{std::forward(pkey)}; + static auto value{std::forward(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; } diff --git a/test/entt/meta/meta.cpp b/test/entt/meta/meta.cpp index dd531d805..0a96d3046 100644 --- a/test/entt/meta/meta.cpp +++ b/test/entt/meta/meta.cpp @@ -147,15 +147,15 @@ struct Meta: ::testing::Test { entt::meta() .type("char"_hs) - .prop>() + .prop(properties::prop_int, 42) .data<&set, &get>("value"_hs); entt::meta() .data("prop_bool"_hs) - .prop>() + .prop(properties::prop_int, 0) .data("prop_int"_hs) - .prop>() - .prop>() + .prop(properties::prop_bool, true) + .prop(properties::prop_int, 0) .data<&set, &get>("value"_hs); entt::meta().data<0u>("min"_hs).data<100u>("max"_hs); @@ -165,12 +165,12 @@ struct Meta: ::testing::Test { entt::meta() .type("derived"_hs) - .prop>() + .prop(properties::prop_int, 99) .base() .ctor() - .prop>() + .prop(properties::prop_bool, false) .ctor<&derived_factory>() - .prop>() + .prop(properties::prop_int, 42) .conv<&derived_type::f>() .conv<&derived_type::g>(); @@ -186,13 +186,13 @@ struct Meta: ::testing::Test { entt::meta() .type("data"_hs) .data<&data_type::i, entt::as_alias_t>("i"_hs) - .prop>() + .prop(properties::prop_int, 0) .data<&data_type::j>("j"_hs) - .prop>() + .prop(properties::prop_int, 1) .data<&data_type::h>("h"_hs) - .prop>() + .prop(properties::prop_int, 2) .data<&data_type::k>("k"_hs) - .prop>() + .prop(properties::prop_int, 3) .data<&data_type::empty>("empty"_hs) .data<&data_type::v, entt::as_void_t>("v"_hs); @@ -205,15 +205,15 @@ struct Meta: ::testing::Test { .type("func"_hs) .func(&func_type::f)>("f3"_hs) .func(&func_type::f)>("f2"_hs) - .prop>() + .prop(properties::prop_bool, false) .func(&func_type::f)>("f1"_hs) - .prop>() + .prop(properties::prop_bool, false) .func<&func_type::g>("g"_hs) - .prop>() + .prop(properties::prop_bool, false) .func<&func_type::h>("h"_hs) - .prop>() + .prop(properties::prop_bool, false) .func<&func_type::k>("k"_hs) - .prop>() + .prop(properties::prop_bool, false) .func<&func_type::v, entt::as_void_t>("v"_hs) .func<&func_type::a, entt::as_alias_t>("a"_hs); @@ -226,14 +226,14 @@ struct Meta: ::testing::Test { entt::meta() .type("an_abstract_type"_hs) - .prop>() + .prop(properties::prop_bool, false) .data<&an_abstract_type::i>("i"_hs) .func<&an_abstract_type::f>("f"_hs) .func<&an_abstract_type::g>("g"_hs); entt::meta() .type("another_abstract_type"_hs) - .prop>() + .prop(properties::prop_int, 42) .data<&another_abstract_type::j>("j"_hs) .func<&another_abstract_type::h>("h"_hs); @@ -249,7 +249,7 @@ struct Meta: ::testing::Test { entt::meta() .type("my_type"_hs) - .prop>() + .prop(properties::prop_bool, false) .ctor<>(); entt::meta()