Răsfoiți Sursa

group: default constructible groups

Michele Caini 5 ani în urmă
părinte
comite
f8ec4027ee
2 a modificat fișierele cu 38 adăugiri și 0 ștergeri
  1. 26 0
      src/entt/entity/group.hpp
  2. 12 0
      test/entt/entity/group.cpp

+ 26 - 0
src/entt/entity/group.hpp

@@ -163,6 +163,11 @@ public:
     /*! @brief Reversed iterator type. */
     using reverse_iterator = typename basic_sparse_set<Entity>::reverse_iterator;
 
+    /*! @brief Default constructor to use to create empty, invalid groups. */
+    basic_group() ENTT_NOEXCEPT
+        : handler{}
+    {}
+
     /**
      * @brief Returns the number of entities that have the given components.
      * @return Number of entities that have the given components.
@@ -298,6 +303,14 @@ public:
         return begin()[pos];
     }
 
+    /**
+     * @brief Checks if a group is properly initialized.
+     * @return True if the group is properly initialized, false otherwise.
+     */
+    [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
+        return handler != nullptr;
+    }
+
     /**
      * @brief Checks if a group contains an entity.
      * @param entt A valid entity identifier.
@@ -640,6 +653,11 @@ public:
     /*! @brief Reversed iterator type. */
     using reverse_iterator = typename basic_sparse_set<Entity>::reverse_iterator;
 
+    /*! @brief Default constructor to use to create empty, invalid groups. */
+    basic_group() ENTT_NOEXCEPT
+        : length{}
+    {}
+
     /**
      * @brief Returns the number of entities that have the given components.
      * @return Number of entities that have the given components.
@@ -780,6 +798,14 @@ public:
         return begin()[pos];
     }
 
+    /**
+     * @brief Checks if a group is properly initialized.
+     * @return True if the group is properly initialized, false otherwise.
+     */
+    [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
+        return length != nullptr;
+    }
+
     /**
      * @brief Checks if a group contains an entity.
      * @param entt A valid entity identifier.

+ 12 - 0
test/entt/entity/group.cpp

@@ -69,6 +69,12 @@ TEST(NonOwningGroup, Functionalities) {
     group.shrink_to_fit();
 
     ASSERT_EQ(group.capacity(), 0u);
+
+    decltype(group) invalid{};
+
+    ASSERT_TRUE(group);
+    ASSERT_TRUE(cgroup);
+    ASSERT_FALSE(invalid);
 }
 
 TEST(NonOwningGroup, ElementAccess) {
@@ -601,6 +607,12 @@ TEST(OwningGroup, Functionalities) {
     ASSERT_EQ(group.rbegin(), group.rend());
     ASSERT_EQ(cgroup.rbegin(), cgroup.rend());
     ASSERT_TRUE(group.empty());
+
+    decltype(group) invalid{};
+
+    ASSERT_TRUE(group);
+    ASSERT_TRUE(cgroup);
+    ASSERT_FALSE(invalid);
 }
 
 TEST(OwningGroup, ElementAccess) {