Browse Source

meta: move context to basic meta factory

skypjack 1 year ago
parent
commit
0a996f5682
1 changed files with 32 additions and 29 deletions
  1. 32 29
      src/entt/meta/factory.hpp

+ 32 - 29
src/entt/meta/factory.hpp

@@ -50,11 +50,13 @@ class basic_meta_factory {
     using bucket_type = dense_map<id_type, internal::meta_prop_node, identity>;
 
 protected:
-    basic_meta_factory(const type_info &type)
-        : info{&type} {}
+    basic_meta_factory(const type_info &type, meta_ctx &area)
+        : info{&type},
+          properties{},
+          ctx{&area} {}
 
-    [[nodiscard]] inline decltype(auto) owner(meta_ctx &ctx) {
-        auto &&context = internal::meta_context::from(ctx);
+    [[nodiscard]] inline decltype(auto) owner() {
+        auto &&context = internal::meta_context::from(*ctx);
         ENTT_ASSERT(context.value.contains(info->hash()), "Type not available");
         return context.value[info->hash()];
     }
@@ -63,14 +65,22 @@ protected:
         properties = curr;
     }
 
+    void track(const id_type id) noexcept {
+        auto &&elem = this->owner();
+        ENTT_ASSERT(elem.id == id || !resolve(*ctx, id), "Duplicate identifier");
+        properties = &elem.details->prop;
+        elem.id = id;
+    }
+
     void property(const id_type key, internal::meta_prop_node value) {
         ENTT_ASSERT(properties != nullptr, "Meta object does not support properties");
         (*properties)[key] = std::move(value);
     }
 
 private:
-    const type_info *info{};
-    bucket_type *properties{};
+    const type_info *info;
+    bucket_type *properties;
+    meta_ctx *ctx;
 };
 
 } // namespace internal
@@ -89,7 +99,7 @@ class meta_factory: private internal::basic_meta_factory {
         static_assert(Policy::template value<data_type>, "Invalid return type for the given policy");
 
         auto &&elem = internal::meta_extend(
-            this->owner(*ctx),
+            this->owner(),
             id,
             internal::meta_data_node{
                 /* this is never static */
@@ -106,16 +116,15 @@ class meta_factory: private internal::basic_meta_factory {
 public:
     /*! @brief Default constructor. */
     meta_factory() noexcept
-        : internal::basic_meta_factory{type_id<Type>()} {}
+        : internal::basic_meta_factory{type_id<Type>(), locator<meta_ctx>::value_or()} {}
 
     /**
      * @brief Context aware constructor.
      * @param area The context into which to construct meta types.
      */
     meta_factory(meta_ctx &area) noexcept
-        : internal::basic_meta_factory{type_id<Type>()},
-          ctx{&area} {
-        auto &&elem = this->owner(*ctx);
+        : internal::basic_meta_factory{type_id<Type>(), area} {
+        auto &&elem = this->owner();
 
         if(!elem.details) {
             elem.details = std::make_shared<internal::meta_type_descriptor>();
@@ -130,10 +139,7 @@ public:
      * @return A meta factory for the given type.
      */
     auto type(const id_type id) noexcept {
-        auto &&elem = this->owner(*ctx);
-        ENTT_ASSERT(elem.id == id || !resolve(*ctx, id), "Duplicate identifier");
-        this->bucket(&elem.details->prop);
-        elem.id = id;
+        this->track(id);
         return *this;
     }
 
@@ -149,7 +155,7 @@ public:
     auto base() noexcept {
         static_assert(!std::is_same_v<Type, Base> && std::is_base_of_v<Base, Type>, "Invalid base type");
         auto *const op = +[](const void *instance) noexcept { return static_cast<const void *>(static_cast<const Base *>(static_cast<const Type *>(instance))); };
-        this->owner(*ctx).details->base.insert_or_assign(type_id<Base>().hash(), internal::meta_base_node{&internal::resolve<Base>, op});
+        this->owner().details->base.insert_or_assign(type_id<Base>().hash(), internal::meta_base_node{&internal::resolve<Base>, op});
         this->bucket(nullptr);
         return *this;
     }
@@ -170,7 +176,7 @@ public:
     auto conv() noexcept {
         using conv_type = std::remove_cv_t<std::remove_reference_t<std::invoke_result_t<decltype(Candidate), Type &>>>;
         auto *const op = +[](const meta_ctx &area, const void *instance) { return forward_as_meta(area, std::invoke(Candidate, *static_cast<const Type *>(instance))); };
-        this->owner(*ctx).details->conv.insert_or_assign(type_id<conv_type>().hash(), internal::meta_conv_node{op});
+        this->owner().details->conv.insert_or_assign(type_id<conv_type>().hash(), internal::meta_conv_node{op});
         this->bucket(nullptr);
         return *this;
     }
@@ -188,7 +194,7 @@ public:
     auto conv() noexcept {
         using conv_type = std::remove_cv_t<std::remove_reference_t<To>>;
         auto *const op = +[](const meta_ctx &area, const void *instance) { return forward_as_meta(area, static_cast<To>(*static_cast<const Type *>(instance))); };
-        this->owner(*ctx).details->conv.insert_or_assign(type_id<conv_type>().hash(), internal::meta_conv_node{op});
+        this->owner().details->conv.insert_or_assign(type_id<conv_type>().hash(), internal::meta_conv_node{op});
         this->bucket(nullptr);
         return *this;
     }
@@ -211,7 +217,7 @@ public:
         using descriptor = meta_function_helper_t<Type, decltype(Candidate)>;
         static_assert(Policy::template value<typename descriptor::return_type>, "Invalid return type for the given policy");
         static_assert(std::is_same_v<std::remove_cv_t<std::remove_reference_t<typename descriptor::return_type>>, Type>, "The function doesn't return an object of the required type");
-        this->owner(*ctx).details->ctor.insert_or_assign(type_id<typename descriptor::args_type>().hash(), internal::meta_ctor_node{descriptor::args_type::size, &meta_arg<typename descriptor::args_type>, &meta_construct<Type, Candidate, Policy>});
+        this->owner().details->ctor.insert_or_assign(type_id<typename descriptor::args_type>().hash(), internal::meta_ctor_node{descriptor::args_type::size, &meta_arg<typename descriptor::args_type>, &meta_construct<Type, Candidate, Policy>});
         this->bucket(nullptr);
         return *this;
     }
@@ -231,7 +237,7 @@ public:
         // default constructor is already implicitly generated, no need for redundancy
         if constexpr(sizeof...(Args) != 0u) {
             using descriptor = meta_function_helper_t<Type, Type (*)(Args...)>;
-            this->owner(*ctx).details->ctor.insert_or_assign(type_id<typename descriptor::args_type>().hash(), internal::meta_ctor_node{descriptor::args_type::size, &meta_arg<typename descriptor::args_type>, &meta_construct<Type, Args...>});
+            this->owner().details->ctor.insert_or_assign(type_id<typename descriptor::args_type>().hash(), internal::meta_ctor_node{descriptor::args_type::size, &meta_arg<typename descriptor::args_type>, &meta_construct<Type, Args...>});
         }
 
         this->bucket(nullptr);
@@ -260,7 +266,7 @@ public:
     auto dtor() noexcept {
         static_assert(std::is_invocable_v<decltype(Func), Type &>, "The function doesn't accept an object of the type provided");
         auto *const op = +[](void *instance) { std::invoke(Func, *static_cast<Type *>(instance)); };
-        this->owner(*ctx).dtor = internal::meta_dtor_node{op};
+        this->owner().dtor = internal::meta_dtor_node{op};
         this->bucket(nullptr);
         return *this;
     }
@@ -285,7 +291,7 @@ public:
             static_assert(Policy::template value<data_type>, "Invalid return type for the given policy");
 
             auto &&elem = internal::meta_extend(
-                this->owner(*ctx),
+                this->owner(),
                 id,
                 internal::meta_data_node{
                     /* this is never static */
@@ -307,7 +313,7 @@ public:
             }
 
             auto &&elem = internal::meta_extend(
-                this->owner(*ctx),
+                this->owner(),
                 id,
                 internal::meta_data_node{
                     ((std::is_same_v<Type, std::remove_cv_t<std::remove_reference_t<data_type>>> || std::is_const_v<std::remove_reference_t<data_type>>) ? internal::meta_traits::is_const : internal::meta_traits::is_none) | internal::meta_traits::is_static,
@@ -350,7 +356,7 @@ public:
 
         if constexpr(std::is_same_v<decltype(Setter), std::nullptr_t>) {
             auto &&elem = internal::meta_extend(
-                this->owner(*ctx),
+                this->owner(),
                 id,
                 internal::meta_data_node{
                     /* this is never static */
@@ -366,7 +372,7 @@ public:
             using args_type = typename meta_function_helper_t<Type, decltype(Setter)>::args_type;
 
             auto &&elem = internal::meta_extend(
-                this->owner(*ctx),
+                this->owner(),
                 id,
                 internal::meta_data_node{
                     /* this is never static nor const */
@@ -425,7 +431,7 @@ public:
         static_assert(Policy::template value<typename descriptor::return_type>, "Invalid return type for the given policy");
 
         auto &&elem = internal::meta_extend(
-            this->owner(*ctx),
+            this->owner(),
             id,
             internal::meta_func_node{
                 (descriptor::is_const ? internal::meta_traits::is_const : internal::meta_traits::is_none) | (descriptor::is_static ? internal::meta_traits::is_static : internal::meta_traits::is_none),
@@ -460,9 +466,6 @@ public:
 
         return *this;
     }
-
-private:
-    meta_ctx *ctx{&locator<meta_ctx>::value_or()};
 };
 
 /**