ソースを参照

any:
* operator bool doesn't rely on the vtable
* valid but unspecified state after move converts to true
* compare type info objects to avoid future errors if the logic changes

Michele Caini 4 年 前
コミット
778cee3b27
2 ファイル変更8 行追加8 行削除
  1. 3 3
      src/entt/core/any.hpp
  2. 5 5
      test/entt/core/any.cpp

+ 3 - 3
src/entt/core/any.hpp

@@ -291,7 +291,7 @@ public:
      * @return False if the wrapper is empty, true otherwise.
      */
     [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
-        return !(vtable(operation::ADDR, *this, nullptr) == nullptr);
+        return descriptor != type_id<void>();
     }
 
     /**
@@ -393,7 +393,7 @@ Type any_cast(basic_any<Len, Align> &&data) ENTT_NOEXCEPT {
 /*! @copydoc any_cast */
 template<typename Type, std::size_t Len, std::size_t Align>
 const Type * any_cast(const basic_any<Len, Align> *data) ENTT_NOEXCEPT {
-    if(data->type().hash() == type_hash<std::remove_const_t<std::remove_reference_t<Type>>>::value()) {
+    if(data->type() == type_id<std::remove_const_t<std::remove_reference_t<Type>>>()) {
         return static_cast<const Type *>(data->data());
     }
 
@@ -404,7 +404,7 @@ const Type * any_cast(const basic_any<Len, Align> *data) ENTT_NOEXCEPT {
 /*! @copydoc any_cast */
 template<typename Type, std::size_t Len, std::size_t Align>
 Type * any_cast(basic_any<Len, Align> *data) ENTT_NOEXCEPT {
-    if(data->type().hash() == type_hash<std::remove_const_t<std::remove_reference_t<Type>>>::value()) {
+    if(data->type() == type_id<std::remove_const_t<std::remove_reference_t<Type>>>()) {
         // last attempt to make wrappers for const references return their values
         return static_cast<Type *>(static_cast<constness_as_t<basic_any<Len, Align>, Type> *>(data)->data());
     }

+ 5 - 5
test/entt/core/any.cpp

@@ -371,7 +371,7 @@ TEST_F(Any, NoSBOMoveConstruction) {
     entt::any any{instance};
     entt::any other{std::move(any)};
 
-    ASSERT_FALSE(any);
+    ASSERT_TRUE(any);
     ASSERT_TRUE(other);
     ASSERT_TRUE(any.owner());
     ASSERT_TRUE(other.owner());
@@ -389,7 +389,7 @@ TEST_F(Any, NoSBOMoveAssignment) {
 
     other = std::move(any);
 
-    ASSERT_FALSE(any);
+    ASSERT_TRUE(any);
     ASSERT_TRUE(other);
     ASSERT_TRUE(any.owner());
     ASSERT_TRUE(other.owner());
@@ -497,8 +497,8 @@ TEST_F(Any, NoSBOMoveValidButUnspecifiedState) {
     entt::any other{std::move(any)};
     entt::any valid = std::move(other);
 
-    ASSERT_FALSE(any);
-    ASSERT_FALSE(other);
+    ASSERT_TRUE(any);
+    ASSERT_TRUE(other);
     ASSERT_TRUE(valid);
 }
 
@@ -1077,7 +1077,7 @@ TEST_F(Any, CopyMoveReference) {
         entt::any move = std::move(any);
         entt::any copy = move;
 
-        ASSERT_FALSE(any);
+        ASSERT_TRUE(any);
         ASSERT_TRUE(move);
         ASSERT_TRUE(copy);