Explorar o código

type_info: it's now copyable and moveable as it ought be

Michele Caini %!s(int64=5) %!d(string=hai) anos
pai
achega
aa5fdac522
Modificáronse 2 ficheiros con 77 adicións e 10 borrados
  1. 55 10
      src/entt/core/type_info.hpp
  2. 22 0
      test/entt/core/type_info.cpp

+ 55 - 10
src/entt/core/type_info.hpp

@@ -133,19 +133,64 @@ class type_info final {
     template<typename>
     friend type_info type_id() ENTT_NOEXCEPT;
 
-    type_info(seq_fn *seq_func, hash_fn *hash_func, name_fn *name_func)
-        : seq{seq_func},
-          hash{hash_func},
-          name{name_func}
+    type_info(seq_fn *seq_ptr, hash_fn *hash_ptr, name_fn *name_ptr)
+        : seq_func{seq_ptr},
+          hash_func{hash_ptr},
+          name_func{name_ptr}
     {}
 
 public:
-    /*! @brief Type sequential identifier. */
-    seq_fn * const seq;
-    /*! @brief Type hash. */
-    hash_fn * const hash;
-    /*! @brief Type name. */
-    name_fn * const name;
+    /*! Default constructor. */
+    type_info()
+        : type_info{nullptr, nullptr, nullptr}
+    {}
+
+    /*! Default copy constructor. */
+    type_info(const type_info &) = default;
+    /*! Default move constructor. */
+    type_info(type_info &&) = default;
+
+    /*! Default copy assignment operator. */
+    type_info & operator=(const type_info &) = default;
+    /*! Default move assignment operator. */
+    type_info & operator=(type_info &&) = default;
+
+    /**
+    * @brief Checks if a type info object is properly initialized.
+    * @return True if the object is properly initialized, false otherwise.
+    */
+    [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
+        return !(seq_func == nullptr);
+    }
+
+    /**
+     * @brief Type sequential identifier.
+     * @return Type sequential identifier.
+     */
+    [[nodiscard]] id_type seq() const ENTT_NOEXCEPT {
+        return seq_func();
+    }
+
+    /**
+     * @brief Type hash.
+     * @return Type hash.
+     */
+    [[nodiscard]] id_type hash() const ENTT_NOEXCEPT {
+        return hash_func();
+    }
+
+    /**
+    * @brief Type name.
+    * @return Type name.
+    */
+    [[nodiscard]] std::string_view name() const ENTT_NOEXCEPT {
+        return name_func();
+    }
+
+private:
+    seq_fn *seq_func;
+    hash_fn *hash_func;
+    name_fn *name_func;
 };
 
 

+ 22 - 0
test/entt/core/type_info.cpp

@@ -32,9 +32,13 @@ TEST(TypeName, Functionalities) {
 TEST(TypeInfo, Functionalities) {
     auto info = entt::type_id<int>();
     auto other = entt::type_id(42);
+    entt::type_info empty{};
 
+    static_assert(std::is_default_constructible_v<decltype(info)>);
     static_assert(std::is_copy_constructible_v<decltype(info)>);
     static_assert(std::is_move_constructible_v<decltype(info)>);
+    static_assert(std::is_copy_assignable_v<decltype(info)>);
+    static_assert(std::is_move_assignable_v<decltype(info)>);
 
     ASSERT_EQ(info.seq(), other.seq());
     ASSERT_EQ(info.hash(), other.hash());
@@ -43,4 +47,22 @@ TEST(TypeInfo, Functionalities) {
     ASSERT_EQ(info.seq(), entt::type_seq<int>::value());
     ASSERT_EQ(info.hash(), entt::type_hash<int>::value());
     ASSERT_EQ(info.name(), entt::type_name<int>::value());
+
+    ASSERT_FALSE(empty);
+    ASSERT_TRUE(info);
+    ASSERT_TRUE(other);
+
+    empty = info;
+
+    ASSERT_TRUE(empty);
+    ASSERT_EQ(empty.hash(), info.hash());
+
+    empty = {};
+
+    ASSERT_FALSE(empty);
+
+    empty = std::move(other);
+
+    ASSERT_TRUE(empty);
+    ASSERT_EQ(empty.hash(), other.hash());
 }