From 78c171e0c257be0ac73f7234f10db367ec49107c Mon Sep 17 00:00:00 2001 From: Michele Caini Date: Fri, 1 Mar 2019 17:07:08 +0100 Subject: [PATCH] meta: review --- docs/meta.md | 2 +- src/entt/meta/factory.hpp | 17 +++++++++-------- src/entt/meta/meta.hpp | 33 +++++++++------------------------ test/entt/meta/meta.cpp | 6 +++--- 4 files changed, 22 insertions(+), 36 deletions(-) diff --git a/docs/meta.md b/docs/meta.md index f297134c4..4c940885b 100644 --- a/docs/meta.md +++ b/docs/meta.md @@ -63,7 +63,7 @@ It can be used to extend the reflected type and add the following: * _Destructors_. Free functions can be set as destructors of reflected types. The purpose is to give users the ability to free up resources that require - special treatment before an object is actually destroyed.
+ special treatment before an object is actually destroyed.
Use the `dtor` member function for this purpose: ```cpp diff --git a/src/entt/meta/factory.hpp b/src/entt/meta/factory.hpp index 631185601..0de5d86e0 100644 --- a/src/entt/meta/factory.hpp +++ b/src/entt/meta/factory.hpp @@ -272,7 +272,7 @@ class meta_factory { * The signature of the function should identical to the following: * * @code{.cpp} - * void(Type &); + * void(Type *); * @endcode * * From a client's point of view, nothing changes if the destructor of a @@ -283,14 +283,14 @@ class meta_factory { */ template meta_factory dtor() ENTT_NOEXCEPT { - static_assert(std::is_invocable_v); + static_assert(std::is_invocable_v); auto * const type = internal::meta_info::resolve(); static internal::meta_dtor_node node{ type, [](meta_handle handle) { return handle.type() == internal::meta_info::resolve()->meta() - ? ((*Func)(*static_cast(handle.data())), true) + ? ((*Func)(static_cast(handle.data())), true) : false; }, []() -> meta_dtor { @@ -379,9 +379,10 @@ class meta_factory { * * Setters and getters can be either free functions, member functions or a * mix of them.
- * In case of free functions, setters and getters must accept 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.
+ * In case of free functions, setters and getters must accept a pointer 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.
* In case of member functions, getters have no arguments at all, while * setters has an argument of a type convertible to that of the parameter to * set. @@ -396,8 +397,8 @@ class meta_factory { template meta_factory data(const char *str, Property &&... property) ENTT_NOEXCEPT { using owner_type = std::tuple, std::integral_constant>; - using data_type = std::invoke_result_t; - static_assert(std::is_invocable_v); + using data_type = std::invoke_result_t; + static_assert(std::is_invocable_v); auto * const type = internal::meta_info::resolve(); static internal::meta_data_node node{ diff --git a/src/entt/meta/meta.hpp b/src/entt/meta/meta.hpp index 9f59192bf..cff472854 100644 --- a/src/entt/meta/meta.hpp +++ b/src/entt/meta/meta.hpp @@ -656,9 +656,6 @@ class meta_handle { : meta_handle{0, std::forward(instance)} {} - /*! @brief Default destructor. */ - ~meta_handle() ENTT_NOEXCEPT = default; - /** * @brief Returns the meta type of the underlying object. * @return The meta type of the underlying object, if any. @@ -2042,17 +2039,12 @@ bool setter([[maybe_unused]] meta_handle handle, [[maybe_unused]] meta_any &any) } else if constexpr(std::is_function_v> || std::is_member_function_pointer_v) { using helper_type = meta_function_helper>; using data_type = std::decay_t, typename helper_type::args_type>>; + static_assert(std::is_invocable_v); accepted = any.can_cast() || any.convert(); auto *clazz = handle.try_cast(); if(accepted && clazz) { - if constexpr(std::is_function_v>) { - static_assert(std::is_invocable_v); - Data(*clazz, any.cast()); - } else if constexpr(std::is_member_function_pointer_v) { - static_assert(std::is_invocable_v); - (clazz->*Data)(any.cast()); - } + std::invoke(Data, clazz, any.cast()); } } else if constexpr(std::is_member_object_pointer_v) { using data_type = std::decay_t().*Data)>; @@ -2061,7 +2053,7 @@ bool setter([[maybe_unused]] meta_handle handle, [[maybe_unused]] meta_any &any) auto *clazz = handle.try_cast(); if(accepted && clazz) { - clazz->*Data = any.cast(); + std::invoke(Data, clazz) = any.cast(); } } else { static_assert(std::is_pointer_v); @@ -2080,16 +2072,9 @@ bool setter([[maybe_unused]] meta_handle handle, [[maybe_unused]] meta_any &any) template inline meta_any getter([[maybe_unused]] meta_handle handle) { if constexpr(std::is_function_v> || std::is_member_pointer_v) { - static_assert(std::is_invocable_v); + static_assert(std::is_invocable_v); auto *clazz = handle.try_cast(); - - if constexpr(std::is_function_v>) { - return clazz ? meta_any{Data(*clazz)} : meta_any{}; - } else if constexpr(std::is_member_function_pointer_v) { - return clazz ? meta_any{(clazz->*Data)()} : meta_any{}; - } else /* if constexpr(std::is_member_object_pointer_v) */ { - return clazz ? meta_any{clazz->*Data} : meta_any{}; - } + return clazz ? std::invoke(Data, clazz) : meta_any{}; } else { static_assert(std::is_pointer_v); return meta_any{*Data}; @@ -2107,9 +2092,9 @@ invoke(const meta_handle &, meta_any *args, std::index_sequence) { || (args+Indexes)->convert>()) && ...)) { if constexpr(std::is_void_v) { - (*Func)((args+Indexes)->cast>()...); + std::invoke(Func, (args+Indexes)->cast>()...); } else { - any = meta_any{(*Func)((args+Indexes)->cast>()...)}; + any = meta_any{std::invoke(Func, (args+Indexes)->cast>()...)}; } } @@ -2129,9 +2114,9 @@ invoke(meta_handle &handle, meta_any *args, std::index_sequence) { || (args+Indexes)->convert>()) && ...)) { if constexpr(std::is_void_v) { - (clazz->*Member)((args+Indexes)->cast>()...); + std::invoke(Member, clazz, (args+Indexes)->cast>()...); } else { - any = meta_any{(clazz->*Member)((args+Indexes)->cast>()...)}; + any = meta_any{std::invoke(Member, clazz, (args+Indexes)->cast>()...)}; } } diff --git a/test/entt/meta/meta.cpp b/test/entt/meta/meta.cpp index 652467edb..c2113165d 100644 --- a/test/entt/meta/meta.cpp +++ b/test/entt/meta/meta.cpp @@ -13,7 +13,7 @@ enum class properties { struct empty_type { virtual ~empty_type() = default; - static void destroy(empty_type &instance) { + static void destroy(empty_type *) { ++counter; } @@ -89,8 +89,8 @@ struct setter_getter_type { int setter_with_ref(const int &value) { return this->value = value; } 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 {