소스 검색

meta: make meta_func return the underlying context

Michele Caini 1 년 전
부모
커밋
041b5e9882
2개의 변경된 파일24개의 추가작업 그리고 10개의 파일을 삭제
  1. 9 10
      src/entt/meta/meta.hpp
  2. 15 0
      test/entt/meta/meta_func.cpp

+ 9 - 10
src/entt/meta/meta.hpp

@@ -971,7 +971,7 @@ private:
 }
 
 /*! @brief Opaque wrapper for member functions. */
-struct meta_func {
+struct meta_func: meta_basic_object {
     /*! @brief Unsigned integer type. */
     using size_type = typename internal::meta_func_node::size_type;
 
@@ -984,8 +984,8 @@ struct meta_func {
      * @param curr The underlying node with which to construct the instance.
      */
     meta_func(const meta_ctx &area, internal::meta_func_node curr) noexcept
-        : node{std::move(curr)},
-          ctx{&area} {}
+        : meta_basic_object{area},
+          node{std::move(curr)} {}
 
     /**
      * @brief Returns the number of arguments accepted by a member function.
@@ -1032,7 +1032,7 @@ struct meta_func {
      * @return A wrapper containing the returned value, if any.
      */
     meta_any invoke(meta_handle instance, meta_any *const args, const size_type sz) const {
-        return ((node.invoke != nullptr) && (sz == arity())) ? node.invoke(*ctx, meta_handle{*ctx, std::move(instance)}, args) : meta_any{meta_ctx_arg, *ctx};
+        return ((node.invoke != nullptr) && (sz == arity())) ? node.invoke(context(), meta_handle{context(), std::move(instance)}, args) : meta_any{meta_ctx_arg, context()};
     }
 
     /**
@@ -1045,7 +1045,7 @@ struct meta_func {
     template<typename... Args>
     // NOLINTNEXTLINE(modernize-use-nodiscard)
     meta_any invoke(meta_handle instance, Args &&...args) const {
-        return invoke(std::move(instance), std::array<meta_any, sizeof...(Args)>{meta_any{*ctx, std::forward<Args>(args)}...}.data(), sizeof...(Args));
+        return invoke(std::move(instance), std::array<meta_any, sizeof...(Args)>{meta_any{context(), std::forward<Args>(args)}...}.data(), sizeof...(Args));
     }
 
     /*! @copydoc meta_data::traits */
@@ -1064,7 +1064,7 @@ struct meta_func {
      * @return The next overload of the given function, if any.
      */
     [[nodiscard]] meta_func next() const {
-        return (node.next != nullptr) ? meta_func{*ctx, *node.next} : meta_func{};
+        return (node.next != nullptr) ? meta_func{context(), *node.next} : meta_func{};
     }
 
     /**
@@ -1077,12 +1077,11 @@ struct meta_func {
 
     /*! @copydoc meta_data::operator== */
     [[nodiscard]] bool operator==(const meta_func &other) const noexcept {
-        return (ctx == other.ctx) && (node.invoke == other.node.invoke);
+        return (&context() == &other.context()) && (node.invoke == other.node.invoke);
     }
 
 private:
     internal::meta_func_node node{};
-    const meta_ctx *ctx{&locator<meta_ctx>::value_or()};
 };
 
 /**
@@ -1616,11 +1615,11 @@ inline bool meta_any::assign(meta_any &&other) {
 }
 
 [[nodiscard]] inline meta_type meta_func::ret() const noexcept {
-    return (node.ret != nullptr) ? meta_type{*ctx, node.ret(internal::meta_context::from(*ctx))} : meta_type{};
+    return (node.ret != nullptr) ? meta_type{context(), node.ret(internal::meta_context::from(context()))} : meta_type{};
 }
 
 [[nodiscard]] inline meta_type meta_func::arg(const size_type index) const noexcept {
-    return index < arity() ? node.arg(*ctx, index) : meta_type{};
+    return index < arity() ? node.arg(context(), index) : meta_type{};
 }
 
 /*! @cond TURN_OFF_DOXYGEN */

+ 15 - 0
test/entt/meta/meta_func.cpp

@@ -5,6 +5,8 @@
 #include <entt/core/hashed_string.hpp>
 #include <entt/core/utility.hpp>
 #include <entt/entity/registry.hpp>
+#include <entt/locator/locator.hpp>
+#include <entt/meta/context.hpp>
 #include <entt/meta/factory.hpp>
 #include <entt/meta/meta.hpp>
 #include <entt/meta/policy.hpp>
@@ -158,6 +160,19 @@ struct MetaFunc: ::testing::Test {
 
 using MetaFuncDeathTest = MetaFunc;
 
+TEST_F(MetaFunc, Context) {
+    entt::meta_func func{};
+    entt::meta_ctx ctx{};
+
+    ASSERT_EQ(&func.context(), &entt::locator<entt::meta_ctx>::value_or());
+    ASSERT_NE(&func.context(), &ctx);
+
+    func = entt::meta_func{ctx, entt::internal::meta_func_node{}};
+
+    ASSERT_NE(&func.context(), &entt::locator<entt::meta_ctx>::value_or());
+    ASSERT_EQ(&func.context(), &ctx);
+}
+
 TEST_F(MetaFunc, SafeWhenEmpty) {
     const entt::meta_func func{};
     entt::meta_any *args = nullptr;