Browse Source

group: make filter storage available

Michele Caini 3 years ago
parent
commit
7c4493f237
3 changed files with 82 additions and 11 deletions
  1. 0 1
      TODO
  2. 16 4
      src/entt/entity/group.hpp
  3. 66 6
      test/entt/entity/group.cpp

+ 0 - 1
TODO

@@ -16,7 +16,6 @@ TODO (high prio):
 
 WIP:
 * get rid of observers, storage based views made them pointless - document alternatives
-* add storage getter for filters to views and groups
 * exploit the tombstone mechanism to allow enabling/disabling entities (see bump, compact and clear for further details)
 * basic_storage::bind for cross-registry setups (see and remove todo from entity_copy.cpp)
 * process scheduler: reviews, use free lists internally

+ 16 - 4
src/entt/entity/group.hpp

@@ -130,7 +130,7 @@ class basic_group<owned_t<>, get_t<Get...>, exclude_t<Exclude...>> {
     using basic_common_type = std::common_type_t<typename Get::base_type..., typename Exclude::base_type...>;
 
     template<typename Type>
-    static constexpr std::size_t index_of = type_list_index_v<std::remove_const_t<Type>, type_list<typename Get::value_type...>>;
+    static constexpr std::size_t index_of = type_list_index_v<std::remove_const_t<Type>, type_list<typename Get::value_type..., typename Exclude::value_type...>>;
 
 public:
     /*! @brief Underlying entity identifier. */
@@ -186,7 +186,13 @@ public:
      */
     template<std::size_t Index>
     [[nodiscard]] decltype(auto) storage() const noexcept {
-        return *std::get<Index>(pools);
+        static constexpr auto offset = sizeof...(Get);
+
+        if constexpr(Index < offset) {
+            return *std::get<Index>(pools);
+        } else {
+            return *std::get<Index - offset>(filter);
+        }
     }
 
     /**
@@ -529,7 +535,7 @@ class basic_group<owned_t<Owned...>, get_t<Get...>, exclude_t<Exclude...>> {
     using basic_common_type = std::common_type_t<typename Owned::base_type..., typename Get::base_type..., typename Exclude::base_type...>;
 
     template<typename Type>
-    static constexpr std::size_t index_of = type_list_index_v<std::remove_const_t<Type>, type_list<typename Owned::value_type..., typename Get::value_type...>>;
+    static constexpr std::size_t index_of = type_list_index_v<std::remove_const_t<Type>, type_list<typename Owned::value_type..., typename Get::value_type..., typename Exclude::value_type...>>;
 
 public:
     /*! @brief Underlying entity identifier. */
@@ -578,7 +584,13 @@ public:
      */
     template<std::size_t Index>
     [[nodiscard]] decltype(auto) storage() const noexcept {
-        return *std::get<Index>(pools);
+        static constexpr auto offset = sizeof...(Owned) + sizeof...(Get);
+
+        if constexpr(Index < offset) {
+            return *std::get<Index>(pools);
+        } else {
+            return *std::get<Index - offset>(filter);
+        }
     }
 
     /**

+ 66 - 6
test/entt/entity/group.cpp

@@ -663,28 +663,58 @@ TEST(NonOwningGroup, IterableGroupAlgorithmCompatibility) {
 TEST(NonOwningGroup, Storage) {
     entt::registry registry;
     const auto entity = registry.create();
-    const auto group = registry.group(entt::get<int, const char>);
+    const auto group = registry.group(entt::get<int, const char>, entt::exclude<double, const float>);
 
     static_assert(std::is_same_v<decltype(group.storage<0u>()), entt::storage_type_t<int> &>);
     static_assert(std::is_same_v<decltype(group.storage<int>()), entt::storage_type_t<int> &>);
+    static_assert(std::is_same_v<decltype(group.storage<const int>()), entt::storage_type_t<int> &>);
     static_assert(std::is_same_v<decltype(group.storage<1u>()), const entt::storage_type_t<char> &>);
+    static_assert(std::is_same_v<decltype(group.storage<char>()), const entt::storage_type_t<char> &>);
     static_assert(std::is_same_v<decltype(group.storage<const char>()), const entt::storage_type_t<char> &>);
+    static_assert(std::is_same_v<decltype(group.storage<2u>()), entt::storage_type_t<double> &>);
+    static_assert(std::is_same_v<decltype(group.storage<double>()), entt::storage_type_t<double> &>);
+    static_assert(std::is_same_v<decltype(group.storage<const double>()), entt::storage_type_t<double> &>);
+    static_assert(std::is_same_v<decltype(group.storage<3u>()), const entt::storage_type_t<float> &>);
+    static_assert(std::is_same_v<decltype(group.storage<float>()), const entt::storage_type_t<float> &>);
+    static_assert(std::is_same_v<decltype(group.storage<const float>()), const entt::storage_type_t<float> &>);
 
     ASSERT_EQ(group.size(), 0u);
 
     group.storage<int>().emplace(entity);
+    group.storage<double>().emplace(entity);
     registry.emplace<char>(entity);
+    registry.emplace<float>(entity);
 
-    ASSERT_EQ(group.size(), 1u);
+    ASSERT_EQ(group.size(), 0u);
+    ASSERT_EQ(group.begin(), group.end());
     ASSERT_TRUE(group.storage<int>().contains(entity));
     ASSERT_TRUE(group.storage<const char>().contains(entity));
+    ASSERT_TRUE(group.storage<double>().contains(entity));
+    ASSERT_TRUE(group.storage<const float>().contains(entity));
+    ASSERT_TRUE((registry.all_of<int, char, double, float>(entity)));
+
+    group.storage<double>().erase(entity);
+    registry.erase<float>(entity);
+
+    ASSERT_EQ(group.size(), 1u);
+    ASSERT_NE(group.begin(), group.end());
+    ASSERT_TRUE(group.storage<const int>().contains(entity));
+    ASSERT_TRUE(group.storage<char>().contains(entity));
+    ASSERT_FALSE(group.storage<const double>().contains(entity));
+    ASSERT_FALSE(group.storage<float>().contains(entity));
     ASSERT_TRUE((registry.all_of<int, char>(entity)));
+    ASSERT_FALSE((registry.any_of<double, float>(entity)));
 
     group.storage<0u>().erase(entity);
 
     ASSERT_EQ(group.size(), 0u);
+    ASSERT_EQ(group.begin(), group.end());
+    ASSERT_FALSE(group.storage<0u>().contains(entity));
     ASSERT_TRUE(group.storage<1u>().contains(entity));
-    ASSERT_FALSE((registry.all_of<int, char>(entity)));
+    ASSERT_FALSE(group.storage<2u>().contains(entity));
+    ASSERT_FALSE(group.storage<3u>().contains(entity));
+    ASSERT_TRUE((registry.all_of<char>(entity)));
+    ASSERT_FALSE((registry.any_of<int, double, float>(entity)));
 }
 
 TEST(OwningGroup, Functionalities) {
@@ -1443,26 +1473,56 @@ TEST(OwningGroup, IterableGroupAlgorithmCompatibility) {
 TEST(OwningGroup, Storage) {
     entt::registry registry;
     const auto entity = registry.create();
-    const auto group = registry.group<int>(entt::get<const char>);
+    const auto group = registry.group<int>(entt::get<const char>, entt::exclude<double, const float>);
 
     static_assert(std::is_same_v<decltype(group.storage<0u>()), entt::storage_type_t<int> &>);
     static_assert(std::is_same_v<decltype(group.storage<int>()), entt::storage_type_t<int> &>);
+    static_assert(std::is_same_v<decltype(group.storage<const int>()), entt::storage_type_t<int> &>);
     static_assert(std::is_same_v<decltype(group.storage<1u>()), const entt::storage_type_t<char> &>);
+    static_assert(std::is_same_v<decltype(group.storage<char>()), const entt::storage_type_t<char> &>);
     static_assert(std::is_same_v<decltype(group.storage<const char>()), const entt::storage_type_t<char> &>);
+    static_assert(std::is_same_v<decltype(group.storage<2u>()), entt::storage_type_t<double> &>);
+    static_assert(std::is_same_v<decltype(group.storage<double>()), entt::storage_type_t<double> &>);
+    static_assert(std::is_same_v<decltype(group.storage<const double>()), entt::storage_type_t<double> &>);
+    static_assert(std::is_same_v<decltype(group.storage<3u>()), const entt::storage_type_t<float> &>);
+    static_assert(std::is_same_v<decltype(group.storage<float>()), const entt::storage_type_t<float> &>);
+    static_assert(std::is_same_v<decltype(group.storage<const float>()), const entt::storage_type_t<float> &>);
 
     ASSERT_EQ(group.size(), 0u);
 
     group.storage<int>().emplace(entity);
+    group.storage<double>().emplace(entity);
     registry.emplace<char>(entity);
+    registry.emplace<float>(entity);
 
-    ASSERT_EQ(group.size(), 1u);
+    ASSERT_EQ(group.size(), 0u);
+    ASSERT_EQ(group.begin(), group.end());
     ASSERT_TRUE(group.storage<int>().contains(entity));
     ASSERT_TRUE(group.storage<const char>().contains(entity));
+    ASSERT_TRUE(group.storage<double>().contains(entity));
+    ASSERT_TRUE(group.storage<const float>().contains(entity));
+    ASSERT_TRUE((registry.all_of<int, char, double, float>(entity)));
+
+    group.storage<double>().erase(entity);
+    registry.erase<float>(entity);
+
+    ASSERT_EQ(group.size(), 1u);
+    ASSERT_NE(group.begin(), group.end());
+    ASSERT_TRUE(group.storage<const int>().contains(entity));
+    ASSERT_TRUE(group.storage<char>().contains(entity));
+    ASSERT_FALSE(group.storage<const double>().contains(entity));
+    ASSERT_FALSE(group.storage<float>().contains(entity));
     ASSERT_TRUE((registry.all_of<int, char>(entity)));
+    ASSERT_FALSE((registry.any_of<double, float>(entity)));
 
     group.storage<0u>().erase(entity);
 
     ASSERT_EQ(group.size(), 0u);
+    ASSERT_EQ(group.begin(), group.end());
+    ASSERT_FALSE(group.storage<0u>().contains(entity));
     ASSERT_TRUE(group.storage<1u>().contains(entity));
-    ASSERT_FALSE((registry.all_of<int, char>(entity)));
+    ASSERT_FALSE(group.storage<2u>().contains(entity));
+    ASSERT_FALSE(group.storage<3u>().contains(entity));
+    ASSERT_TRUE((registry.all_of<char>(entity)));
+    ASSERT_FALSE((registry.any_of<int, double, float>(entity)));
 }