فهرست منبع

meta: update doc + minor stylistic changes

Michele Caini 5 سال پیش
والد
کامیت
77865ab4a1
3فایلهای تغییر یافته به همراه51 افزوده شده و 47 حذف شده
  1. 21 33
      src/entt/meta/meta.hpp
  2. 11 13
      test/entt/meta/meta_any.cpp
  3. 19 1
      test/entt/meta/meta_type.cpp

+ 21 - 33
src/entt/meta/meta.hpp

@@ -297,30 +297,28 @@ public:
     }
 
     /**
-     * @copybrief invoke
+     * @brief Invokes the underlying function, if possible.
      *
      * @sa invoke
      *
-     * @param id identifier of function to invoke.
      * @tparam Args Types of arguments to use to invoke the function.
-     * @param instance An opaque instance of the underlying type.
+     * @param id Unique identifier.
      * @param args Parameters to use to invoke the function.
-     * @return A meta any containing the new instance, if any.
+     * @return A meta any containing the returned value, if any.
      */
     template<typename... Args>
-    meta_any invoke(const id_type id, Args &&... args) const;
+    meta_any invoke(const id_type id, Args &&... args) const {
+        return type().invoke(id, *this, std::forward<Args>(args)...);
+    }
 
     /**
      * @brief Sets the value of a given variable.
      *
-     * It must be possible to cast the instance to the parent type of the meta
-     * data. Otherwise, invoking the setter results in an undefined
-     * behavior.<br/>
      * The type of the value must be such that a cast or conversion to the type
      * of the variable is possible. Otherwise, invoking the setter does nothing.
      *
      * @tparam Type Type of value to assign.
-     * @param id identifier of the variable to set.
+     * @param id Unique identifier.
      * @param value Parameter to use to set the underlying variable.
      * @return True in case of success, false otherwise.
      */
@@ -329,11 +327,7 @@ public:
 
     /**
      * @brief Gets the value of a given variable.
-     *
-     * It must be possible to cast the instance to the parent type of the meta
-     * data. Otherwise, invoking the getter results in an undefined behavior.
-     *
-     * @param id identifier of the variable to get.
+     * @param id Unique identifier.
      * @return A meta any containing the value of the underlying variable.
      */
     [[nodiscard]] meta_any get(const id_type id) const;
@@ -958,7 +952,7 @@ struct meta_func {
      * @param sz Number of parameters to use to invoke the function.
      * @return A meta any containing the returned value, if any.
      */
-    [[nodiscard]] meta_any invoke(meta_handle instance, meta_any * const args, const std::size_t sz) const {
+    meta_any invoke(meta_handle instance, meta_any * const args, const std::size_t sz) const {
         return sz == size() ? node->invoke(instance, args) : meta_any{};
     }
 
@@ -1357,15 +1351,15 @@ public:
      * function. Otherwise, invoking the underlying function results in an
      * undefined behavior.
      *
-     * @param id identifier of function to invoke.
+     * @param id Unique identifier.
      * @param instance An opaque instance of the underlying type.
      * @param args Parameters to use to invoke the function.
      * @param sz Number of parameters to use to invoke the function.
      * @return A meta any containing the returned value, if any.
      */
-    [[nodiscard]] meta_any invoke(const id_type id, meta_handle instance, meta_any * const args, const std::size_t sz) const {
-        auto const f = func(id);
-        return f ? f.invoke(instance, args, sz) : meta_any{};
+    meta_any invoke(const id_type id, meta_handle instance, meta_any * const args, const std::size_t sz) const {
+        auto const candidate = func(id);
+        return candidate ? candidate.invoke(std::move(instance), args, sz) : meta_any{};
     }
 
     /**
@@ -1373,7 +1367,7 @@ public:
      *
      * @sa invoke
      *
-     * @param id identifier of function to invoke.
+     * @param id Unique identifier.
      * @tparam Args Types of arguments to use to invoke the function.
      * @param instance An opaque instance of the underlying type.
      * @param args Parameters to use to invoke the function.
@@ -1382,7 +1376,7 @@ public:
     template<typename... Args>
     meta_any invoke(const id_type id, meta_handle instance, Args &&... args) const {
         std::array<meta_any, sizeof...(Args)> arguments{std::forward<Args>(args)...};
-        return invoke(id, instance, arguments.data(), sizeof...(Args));
+        return invoke(id, std::move(instance), arguments.data(), sizeof...(Args));
     }
 
     /**
@@ -1395,15 +1389,15 @@ public:
      * of the variable is possible. Otherwise, invoking the setter does nothing.
      *
      * @tparam Type Type of value to assign.
-     * @param id identifier of the variable to set.
+     * @param id Unique identifier.
      * @param instance An opaque instance of the underlying type.
      * @param value Parameter to use to set the underlying variable.
      * @return True in case of success, false otherwise.
      */
     template<typename Type>
     bool set(const id_type id, meta_handle instance, Type &&value) const {
-        auto const d = data(id);
-        return d ? d.set(std::move(instance), std::forward<Type>(value)) : false;
+        auto const candidate = data(id);
+        return candidate ? candidate.set(std::move(instance), std::forward<Type>(value)) : false;
     }
 
     /**
@@ -1412,13 +1406,13 @@ public:
      * It must be possible to cast the instance to the parent type of the meta
      * data. Otherwise, invoking the getter results in an undefined behavior.
      *
-     * @param id identifier of the variable to get.
+     * @param id Unique identifier.
      * @param instance An opaque instance of the underlying type.
      * @return A meta any containing the value of the underlying variable.
      */
     [[nodiscard]] meta_any get(const id_type id, meta_handle instance) const {
-        auto const d = data(id);
-        return d ? d.get(std::move(instance)) : meta_any{};
+        auto const candidate = data(id);
+        return candidate ? candidate.get(std::move(instance)) : meta_any{};
     }
 
     /**
@@ -1525,12 +1519,6 @@ private:
 }
 
 
-template<typename... Args>
-meta_any meta_any::invoke(const id_type id, Args &&... args) const {
-    return type().invoke(id, *this, std::forward<Args>(args)...);
-}
-
-
 template<typename Type>
 bool meta_any::set(const id_type id, Type &&value) const {
     return type().set(id, *this, std::forward<Type>(value));

+ 11 - 13
test/entt/meta/meta_any.cpp

@@ -7,8 +7,6 @@
 
 
 struct clazz_t {
-    clazz_t() = default;
-
     void member(int i) { value = i; }
     static void func() { c = 'd'; }
 
@@ -59,10 +57,10 @@ struct MetaAny: ::testing::Test {
         entt::meta<fat_t>().base<empty_t>().dtor<&fat_t::destroy>();
 
         entt::meta<clazz_t>()
-                .type("clazz"_hs)
-                .data<&clazz_t::value>("value"_hs)
-                .func<&clazz_t::member>("member"_hs)
-                .func<&clazz_t::func>("func"_hs);
+            .type("clazz"_hs)
+            .data<&clazz_t::value>("value"_hs)
+            .func<&clazz_t::member>("member"_hs)
+            .func<&clazz_t::func>("func"_hs);
     }
 
     void SetUp() override {
@@ -640,7 +638,7 @@ TEST_F(MetaAny, UnmanageableType) {
     ASSERT_FALSE(std::as_const(any).convert<int>());
 }
 
-TEST_F(MetaAny, FuncInvokeShortcut) {
+TEST_F(MetaAny, Invoke) {
     clazz_t instance;
     entt::meta_any any{std::ref(instance)};
 
@@ -652,18 +650,18 @@ TEST_F(MetaAny, FuncInvokeShortcut) {
 	ASSERT_EQ(instance.value, 42);
 }
 
-TEST_F(MetaAny, DataAccessShortcut) {
+TEST_F(MetaAny, SetGet) {
     clazz_t instance;
     entt::meta_any any{std::ref(instance)};
 
 	ASSERT_TRUE(any.set("value"_hs, 42));
 
-	auto const value_any = any.get("value"_hs);
+    const auto value = any.get("value"_hs);
 
-	ASSERT_EQ(instance.value, 42);
-	ASSERT_TRUE(value_any);
-	ASSERT_TRUE(value_any.try_cast<int>());
-	ASSERT_EQ(value_any.cast<int>(), 42);
+	ASSERT_TRUE(value);
+	ASSERT_TRUE(value.try_cast<int>());
+	ASSERT_EQ(value.cast<int>(), 42);
+    ASSERT_EQ(instance.value, 42);
 
 	ASSERT_FALSE(any.set("non_existent"_hs, 42));
 	ASSERT_FALSE(any.get("non_existent"_hs));

+ 19 - 1
test/entt/meta/meta_type.cpp

@@ -270,9 +270,27 @@ TEST_F(MetaType, Func) {
     ASSERT_TRUE(type.func("func"_hs));
     ASSERT_TRUE(type.func("member"_hs).invoke(instance));
     ASSERT_TRUE(type.func("func"_hs).invoke({}));
+}
+
+TEST_F(MetaType, Invoke) {
+    auto type = entt::resolve<clazz_t>();
+    clazz_t instance{};
 
     ASSERT_TRUE(type.invoke("member"_hs, instance));
-    ASSERT_TRUE(type.invoke("func"_hs, {}));
+    ASSERT_FALSE(type.invoke("rebmem"_hs, {}));
+}
+
+TEST_F(MetaType, SetGet) {
+    auto type = entt::resolve<clazz_t>();
+    clazz_t instance{};
+
+    ASSERT_TRUE(type.set("value"_hs, instance, 42));
+    ASSERT_FALSE(type.set("eulav"_hs, instance, 3));
+    ASSERT_EQ(instance.value, 42);
+
+    ASSERT_FALSE(type.get("eulav"_hs, instance));
+    ASSERT_TRUE(type.get("value"_hs, instance));
+    ASSERT_EQ(type.get("value"_hs, instance).cast<int>(), 42);
 }
 
 TEST_F(MetaType, Construct) {