Browse Source

runtime view: const/non-const support

Michele Caini 3 years ago
parent
commit
e65b3a790d
4 changed files with 58 additions and 29 deletions
  1. 1 2
      TODO
  2. 5 2
      src/entt/entity/fwd.hpp
  3. 7 7
      src/entt/entity/runtime_view.hpp
  4. 45 18
      test/entt/entity/runtime_view.cpp

+ 1 - 2
TODO

@@ -13,7 +13,7 @@ DOC:
 * update entity doc when the storage based model is in place
 
 WIP:
-* runtime view: const/non-const support, allocator support, storage access
+* runtime view: allocator support, storage access
 * get rid of observers, storage based views made them pointless - document alternatives
 * dense_map/set and registry: move ctor/op should be noexcept (op is conditionally noexcept though)
 * add storage getter for filters to views and groups
@@ -28,5 +28,4 @@ WIP:
 * custom allocators all over (registry, ...)
 * add test for maximum number of entities reached
 * add user data to type_info
-* make runtime views use opaque storage and therefore return also elements.
 * deprecate non-owning groups in favor of owning views and view packs, introduce lazy owning views

+ 5 - 2
src/entt/entity/fwd.hpp

@@ -31,8 +31,8 @@ class basic_registry;
 template<typename, typename, typename = void>
 class basic_view;
 
-template<typename>
-struct basic_runtime_view;
+template<typename Type>
+class basic_runtime_view;
 
 template<typename, typename, typename>
 class basic_group;
@@ -156,6 +156,9 @@ using view = basic_view<type_list_transform_t<Get, storage_for>, type_list_trans
 /*! @brief Alias declaration for the most common use case. */
 using runtime_view = basic_runtime_view<sparse_set>;
 
+/*! @brief Alias declaration for the most common use case. */
+using const_runtime_view = basic_runtime_view<const sparse_set>;
+
 /**
  * @brief Alias declaration for the most common use case.
  * @tparam Owned Types of storage _owned_ by the group.

+ 7 - 7
src/entt/entity/runtime_view.hpp

@@ -41,7 +41,7 @@ public:
           it{},
           tombstone_check{} {}
 
-    runtime_view_iterator(const std::vector<const Set *> &cpools, const std::vector<const Set *> &ignore, iterator_type curr) noexcept
+    runtime_view_iterator(const std::vector<Set *> &cpools, const std::vector<Set *> &ignore, iterator_type curr) noexcept
         : pools{&cpools},
           filter{&ignore},
           it{curr},
@@ -88,8 +88,8 @@ public:
     }
 
 private:
-    const std::vector<const Set *> *pools;
-    const std::vector<const Set *> *filter;
+    const std::vector<Set *> *pools;
+    const std::vector<Set *> *filter;
     iterator_type it;
     bool tombstone_check;
 };
@@ -160,7 +160,7 @@ struct basic_runtime_view {
      * @param base An opaque reference to a storage object.
      * @return This runtime view.
      */
-    basic_runtime_view &iterate(const base_type &base) {
+    basic_runtime_view &iterate(base_type &base) {
         if(pools.empty() || !(base.size() < pools[0u]->size())) {
             pools.push_back(&base);
         } else {
@@ -175,7 +175,7 @@ struct basic_runtime_view {
      * @param base An opaque reference to a storage object.
      * @return This runtime view.
      */
-    basic_runtime_view &exclude(const base_type &base) {
+    basic_runtime_view &exclude(base_type &base) {
         filter.push_back(&base);
         return *this;
     }
@@ -251,8 +251,8 @@ struct basic_runtime_view {
     }
 
 private:
-    std::vector<const base_type *> pools;
-    std::vector<const base_type *> filter;
+    std::vector<base_type *> pools;
+    std::vector<base_type *> filter;
 };
 
 } // namespace entt

+ 45 - 18
test/entt/entity/runtime_view.cpp

@@ -11,9 +11,20 @@ struct stable_type {
     int value;
 };
 
-TEST(RuntimeView, Functionalities) {
+template<typename Type>
+struct RuntimeView: testing::Test {
+    using type = Type;
+};
+
+using RuntimeViewTypes = ::testing::Types<entt::runtime_view, entt::const_runtime_view>;
+
+TYPED_TEST_SUITE(RuntimeView, RuntimeViewTypes, );
+
+TYPED_TEST(RuntimeView, Functionalities) {
+    using runtime_view_type = typename TestFixture::type;
+
     entt::registry registry;
-    entt::runtime_view view{};
+    runtime_view_type view{};
 
     const auto e0 = registry.create();
     const auto e1 = registry.create();
@@ -66,9 +77,11 @@ TEST(RuntimeView, Functionalities) {
     ASSERT_EQ(empty.begin(), empty.end());
 }
 
-TEST(RuntimeView, Iterator) {
+TYPED_TEST(RuntimeView, Iterator) {
+    using runtime_view_type = typename TestFixture::type;
+
     entt::registry registry;
-    entt::runtime_view view{};
+    runtime_view_type view{};
 
     const auto entity = registry.create();
     registry.emplace<int>(entity);
@@ -96,9 +109,11 @@ TEST(RuntimeView, Iterator) {
     ASSERT_EQ(*begin.operator->(), entity);
 }
 
-TEST(RuntimeView, Contains) {
+TYPED_TEST(RuntimeView, Contains) {
+    using runtime_view_type = typename TestFixture::type;
+
     entt::registry registry;
-    entt::runtime_view view{};
+    runtime_view_type view{};
 
     const auto e0 = registry.create();
     registry.emplace<int>(e0);
@@ -116,9 +131,11 @@ TEST(RuntimeView, Contains) {
     ASSERT_TRUE(view.contains(e1));
 }
 
-TEST(RuntimeView, Empty) {
+TYPED_TEST(RuntimeView, Empty) {
+    using runtime_view_type = typename TestFixture::type;
+
     entt::registry registry;
-    entt::runtime_view view{};
+    runtime_view_type view{};
 
     const auto e0 = registry.create();
     registry.emplace<double>(e0);
@@ -140,9 +157,11 @@ TEST(RuntimeView, Empty) {
     ASSERT_EQ((std::find(view.begin(), view.end(), e1)), view.end());
 }
 
-TEST(RuntimeView, Each) {
+TYPED_TEST(RuntimeView, Each) {
+    using runtime_view_type = typename TestFixture::type;
+
     entt::registry registry;
-    entt::runtime_view view{};
+    runtime_view_type view{};
 
     const auto e0 = registry.create();
     registry.emplace<int>(e0);
@@ -158,9 +177,11 @@ TEST(RuntimeView, Each) {
     });
 }
 
-TEST(RuntimeView, EachWithHoles) {
+TYPED_TEST(RuntimeView, EachWithHoles) {
+    using runtime_view_type = typename TestFixture::type;
+
     entt::registry registry;
-    entt::runtime_view view{};
+    runtime_view_type view{};
 
     const auto e0 = registry.create();
     const auto e1 = registry.create();
@@ -179,9 +200,11 @@ TEST(RuntimeView, EachWithHoles) {
     });
 }
 
-TEST(RuntimeView, ExcludedComponents) {
+TYPED_TEST(RuntimeView, ExcludedComponents) {
+    using runtime_view_type = typename TestFixture::type;
+
     entt::registry registry;
-    entt::runtime_view view{};
+    runtime_view_type view{};
 
     const auto e0 = registry.create();
     registry.emplace<int>(e0);
@@ -202,9 +225,11 @@ TEST(RuntimeView, ExcludedComponents) {
     });
 }
 
-TEST(RuntimeView, StableType) {
+TYPED_TEST(RuntimeView, StableType) {
+    using runtime_view_type = typename TestFixture::type;
+
     entt::registry registry;
-    entt::runtime_view view{};
+    runtime_view_type view{};
 
     const auto e0 = registry.create();
     const auto e1 = registry.create();
@@ -242,9 +267,11 @@ TEST(RuntimeView, StableType) {
     ASSERT_EQ(view.size_hint(), 1u);
 }
 
-TEST(RuntimeView, StableTypeWithExcludedComponent) {
+TYPED_TEST(RuntimeView, StableTypeWithExcludedComponent) {
+    using runtime_view_type = typename TestFixture::type;
+
     entt::registry registry;
-    entt::runtime_view view{};
+    runtime_view_type view{};
 
     const auto entity = registry.create();
     const auto other = registry.create();