Browse Source

storage: extended_storage_iterator::base to return the underlying iterator

Michele Caini 3 years ago
parent
commit
1ab2815823
3 changed files with 29 additions and 2 deletions
  1. 1 0
      TODO
  2. 5 0
      src/entt/entity/storage.hpp
  3. 23 2
      test/entt/entity/storage.cpp

+ 1 - 0
TODO

@@ -15,6 +15,7 @@ TODO (high prio):
 * check natvis files (periodically :)
 * remove the static storage from the const assure in the registry
 * pop_if to improve further destroying entities (drastically)
+* add iterator_type/base to extended storage iterators (view, group)
 
 WIP:
 * get rid of observers, storage based views made them pointless - document alternatives

+ 5 - 0
src/entt/entity/storage.hpp

@@ -155,6 +155,7 @@ class extended_storage_iterator final {
     friend class extended_storage_iterator;
 
 public:
+    using iterator_type = It;
     using value_type = decltype(std::tuple_cat(std::make_tuple(*std::declval<It>()), std::forward_as_tuple(*std::declval<Other>()...)));
     using pointer = input_iterator_pointer<value_type>;
     using reference = value_type;
@@ -188,6 +189,10 @@ public:
         return {*std::get<It>(it), *std::get<Other>(it)...};
     }
 
+    constexpr iterator_type base() const noexcept {
+        return std::get<It>(it);
+    }
+
     template<typename... Lhs, typename... Rhs>
     friend constexpr bool operator==(const extended_storage_iterator<Lhs...> &, const extended_storage_iterator<Rhs...> &) noexcept;
 

+ 23 - 2
test/entt/entity/storage.cpp

@@ -1220,6 +1220,8 @@ TEST(Storage, Iterable) {
     static_assert(std::is_same_v<typename iterator::reference, typename iterator::value_type>);
 
     entt::storage<boxed_int> pool;
+    entt::sparse_set &base = pool;
+
     pool.emplace(entt::entity{1}, 99);
     pool.emplace(entt::entity{3}, 42);
     auto iterable = pool.each();
@@ -1233,13 +1235,18 @@ TEST(Storage, Iterable) {
     ASSERT_EQ(end, iterable.end());
     ASSERT_NE(begin, end);
 
+    ASSERT_EQ(begin.base(), base.begin());
+    ASSERT_EQ(end.base(), base.end());
+
     ASSERT_EQ(std::get<0>(*begin.operator->().operator->()), entt::entity{3});
     ASSERT_EQ(std::get<1>(*begin.operator->().operator->()), boxed_int{42});
     ASSERT_EQ(std::get<0>(*begin), entt::entity{3});
     ASSERT_EQ(std::get<1>(*begin), boxed_int{42});
 
     ASSERT_EQ(begin++, iterable.begin());
+    ASSERT_EQ(begin.base(), ++base.begin());
     ASSERT_EQ(++begin, iterable.end());
+    ASSERT_EQ(begin.base(), base.end());
 
     for(auto [entity, element]: iterable) {
         static_assert(std::is_same_v<decltype(entity), entt::entity>);
@@ -1258,6 +1265,8 @@ TEST(Storage, ConstIterable) {
     static_assert(std::is_same_v<typename iterator::reference, typename iterator::value_type>);
 
     entt::storage<boxed_int> pool;
+    entt::sparse_set &base = pool;
+
     pool.emplace(entt::entity{1}, 99);
     pool.emplace(entt::entity{3}, 42);
     auto iterable = std::as_const(pool).each();
@@ -1271,13 +1280,18 @@ TEST(Storage, ConstIterable) {
     ASSERT_EQ(end, iterable.cend());
     ASSERT_NE(begin, end);
 
+    ASSERT_EQ(begin.base(), base.begin());
+    ASSERT_EQ(end.base(), base.end());
+
     ASSERT_EQ(std::get<0>(*begin.operator->().operator->()), entt::entity{3});
     ASSERT_EQ(std::get<1>(*begin.operator->().operator->()), boxed_int{42});
     ASSERT_EQ(std::get<0>(*begin), entt::entity{3});
     ASSERT_EQ(std::get<1>(*begin), boxed_int{42});
 
-    ASSERT_EQ(begin++, iterable.cbegin());
-    ASSERT_EQ(++begin, iterable.cend());
+    ASSERT_EQ(begin++, iterable.begin());
+    ASSERT_EQ(begin.base(), ++base.begin());
+    ASSERT_EQ(++begin, iterable.end());
+    ASSERT_EQ(begin.base(), base.end());
 
     for(auto [entity, element]: iterable) {
         static_assert(std::is_same_v<decltype(entity), entt::entity>);
@@ -1310,6 +1324,8 @@ TEST(Storage, EmptyTypeIterable) {
     static_assert(std::is_same_v<typename iterator::reference, typename iterator::value_type>);
 
     entt::storage<empty_stable_type> pool;
+    entt::sparse_set &base = pool;
+
     pool.emplace(entt::entity{1});
     pool.emplace(entt::entity{3});
     auto iterable = pool.each();
@@ -1323,11 +1339,16 @@ TEST(Storage, EmptyTypeIterable) {
     ASSERT_EQ(end, iterable.end());
     ASSERT_NE(begin, end);
 
+    ASSERT_EQ(begin.base(), base.begin());
+    ASSERT_EQ(end.base(), base.end());
+
     ASSERT_EQ(std::get<0>(*begin.operator->().operator->()), entt::entity{3});
     ASSERT_EQ(std::get<0>(*begin), entt::entity{3});
 
     ASSERT_EQ(begin++, iterable.begin());
+    ASSERT_EQ(begin.base(), ++base.begin());
     ASSERT_EQ(++begin, iterable.end());
+    ASSERT_EQ(begin.base(), base.end());
 
     for(auto [entity]: iterable) {
         static_assert(std::is_same_v<decltype(entity), entt::entity>);