Browse Source

meta: meta_factory::setup function

Michele Caini 11 months ago
parent
commit
2a37b972d9
2 changed files with 46 additions and 0 deletions
  1. 13 0
      src/entt/meta/factory.hpp
  2. 33 0
      test/entt/meta/meta_factory.cpp

+ 13 - 0
src/entt/meta/factory.hpp

@@ -176,6 +176,19 @@ public:
         return *this;
     }
 
+    /**
+     * @brief Invokes a meta setup function for the current factory, if any.
+     *
+     * @warning
+     * The factory is provided in its current state and is not reinitialized.
+     *
+     * @return A meta factory for the parent type.
+     */
+    meta_factory setup() noexcept {
+        meta_setup(*this);
+        return *this;
+    }
+
     /**
      * @brief Assigns a meta base to a meta type.
      *

+ 33 - 0
test/entt/meta/meta_factory.cpp

@@ -61,6 +61,22 @@ private:
     bool *cb;
 };
 
+template<typename Type>
+// concept-like approach (waiting for C++20 and concepts)
+auto meta_setup(entt::meta_factory<Type> factory) -> decltype(&Type::value, void()) {
+    using namespace entt::literals;
+    factory.data<&Type::value>("value"_hs).conv<decltype(Type::value)>();
+}
+
+void meta_setup(entt::meta_factory<test::boxed_int> factory) {
+    using namespace entt::literals;
+    factory.data<&test::boxed_int::value>("value"_hs);
+}
+
+void meta_setup(...) {
+    ENTT_FAIL("oops");
+}
+
 struct MetaFactory: ::testing::Test {
     void TearDown() override {
         entt::meta_reset();
@@ -118,6 +134,23 @@ ENTT_DEBUG_TEST_F(MetaFactoryDeathTest, Type) {
     ASSERT_DEATH(other.type("foo"_hs), "");
 }
 
+TEST_F(MetaFactory, Setup) {
+    using namespace entt::literals;
+
+    entt::meta_factory<test::boxed_int>{}.setup();
+    entt::meta_factory<test::boxed_char>{}.setup();
+
+    ASSERT_TRUE(entt::resolve<test::boxed_int>().data("value"_hs));
+    ASSERT_TRUE(entt::resolve<test::boxed_char>().data("value"_hs));
+
+    ASSERT_FALSE(entt::resolve<test::boxed_int>().can_convert(entt::resolve<int>()));
+    ASSERT_TRUE(entt::resolve<test::boxed_char>().can_convert(entt::resolve<char>()));
+}
+
+ENTT_DEBUG_TEST_F(MetaFactoryDeathTest, Setup) {
+    ASSERT_DEATH(entt::meta_factory<int>{}.setup(), "");
+}
+
 TEST_F(MetaFactory, Base) {
     entt::meta_factory<clazz> factory{};
     decltype(std::declval<entt::meta_type>().base()) range{};