Prechádzať zdrojové kódy

meta: setters and getters receive instances as references now

Michele Caini 6 rokov pred
rodič
commit
25afea4f9c

+ 3 - 3
src/entt/meta/factory.hpp

@@ -486,7 +486,7 @@ public:
      *
      * Setters and getters can be either free functions, member functions or a
      * mix of them.<br/>
-     * In case of free functions, setters and getters must accept a pointer to
+     * In case of free functions, setters and getters must accept a reference to
      * an instance of the parent type as their first argument. A setter has then
      * an extra argument of a type convertible to that of the parameter to
      * set.<br/>
@@ -504,8 +504,8 @@ public:
     template<auto Setter, auto Getter, typename... Property>
     meta_factory data(const ENTT_ID_TYPE identifier, Property &&... property) ENTT_NOEXCEPT {
         using owner_type = std::tuple<std::integral_constant<decltype(Setter), Setter>, std::integral_constant<decltype(Getter), Getter>>;
-        using underlying_type = std::invoke_result_t<decltype(Getter), Type *>;
-        static_assert(std::is_invocable_v<decltype(Setter), Type *, underlying_type>);
+        using underlying_type = std::invoke_result_t<decltype(Getter), Type &>;
+        static_assert(std::is_invocable_v<decltype(Setter), Type &, underlying_type>);
         auto * const type = internal::meta_info<Type>::resolve();
 
         static internal::meta_data_node node{

+ 4 - 4
src/entt/meta/meta.hpp

@@ -2179,12 +2179,12 @@ bool setter([[maybe_unused]] meta_handle handle, [[maybe_unused]] meta_any index
         if constexpr(std::is_function_v<std::remove_pointer_t<decltype(Data)>> || std::is_member_function_pointer_v<decltype(Data)>) {
             using helper_type = meta_function_helper<std::integral_constant<decltype(Data), Data>>;
             using data_type = std::decay_t<std::tuple_element_t<!std::is_member_function_pointer_v<decltype(Data)>, typename helper_type::args_type>>;
-            static_assert(std::is_invocable_v<decltype(Data), Type *, data_type>);
+            static_assert(std::is_invocable_v<decltype(Data), Type &, data_type>);
             accepted = value.can_cast<data_type>() || value.convert<data_type>();
             auto *clazz = handle.try_cast<Type>();
 
             if(accepted && clazz) {
-                std::invoke(Data, clazz, value.cast<data_type>());
+                std::invoke(Data, *clazz, value.cast<data_type>());
             }
         } else if constexpr(std::is_member_object_pointer_v<decltype(Data)>) {
             using data_type = std::remove_cv_t<std::remove_reference_t<decltype(std::declval<Type>().*Data)>>;
@@ -2233,9 +2233,9 @@ bool setter([[maybe_unused]] meta_handle handle, [[maybe_unused]] meta_any index
 template<typename Type, auto Data>
 meta_any getter([[maybe_unused]] meta_handle handle, [[maybe_unused]] meta_any index) {
     if constexpr(std::is_function_v<std::remove_pointer_t<decltype(Data)>> || std::is_member_function_pointer_v<decltype(Data)>) {
-       static_assert(std::is_invocable_v<decltype(Data), Type *>);
+       static_assert(std::is_invocable_v<decltype(Data), Type &>);
         auto *clazz = handle.try_cast<Type>();
-        return clazz ? std::invoke(Data, clazz) : meta_any{};
+        return clazz ? std::invoke(Data, *clazz) : meta_any{};
     } else if constexpr(std::is_member_object_pointer_v<decltype(Data)>) {
         using data_type = std::remove_cv_t<std::remove_reference_t<decltype(std::declval<Type>().*Data)>>;
         static_assert(std::is_invocable_v<decltype(Data), Type *>);

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

@@ -98,8 +98,8 @@ struct setter_getter_type {
     int setter_with_ref(const int &val) { return value = val; }
     const int & getter_with_ref() { return value; }
 
-    static int static_setter(setter_getter_type *type, int value) { return type->value = value; }
-    static int static_getter(const setter_getter_type *type) { return type->value; }
+    static int static_setter(setter_getter_type &type, int value) { return type.value = value; }
+    static int static_getter(const setter_getter_type &type) { return type.value; }
 };
 
 struct not_comparable_type {