Jelajahi Sumber

runtime_view: public constructor (close #597)

Michele Caini 5 tahun lalu
induk
melakukan
6f36723097
3 mengubah file dengan 29 tambahan dan 15 penghapusan
  1. 1 0
      TODO
  2. 23 15
      src/entt/entity/runtime_view.hpp
  3. 5 0
      test/entt/entity/runtime_view.cpp

+ 1 - 0
TODO

@@ -23,6 +23,7 @@ WIP:
 * HP: write documentation for custom storages and views!!
 * HP: any/poly: configurable sbo size, compile-time policies like sbo-required.
 * HP: registry: use a poly object for pools, no more pool_data type.
+* HP: make runtime views use opaque storage and therefore return also elements.
 * suppress warnings in meta.hpp (uninitialized members)
 * add exclude-only views to combine with packs
 * deprecate non-owning groups in favor of owning views and view packs, introduce lazy owning views

+ 23 - 15
src/entt/entity/runtime_view.hpp

@@ -55,9 +55,6 @@ namespace entt {
  */
 template<typename Entity>
 class basic_runtime_view final {
-    /*! @brief A registry is allowed to create views. */
-    friend class basic_registry<Entity>;
-
     using underlying_iterator = typename basic_sparse_set<Entity>::iterator;
 
     class view_iterator final {
@@ -129,18 +126,6 @@ class basic_runtime_view final {
         underlying_iterator it;
     };
 
-    basic_runtime_view(std::vector<const basic_sparse_set<Entity> *> cpools, std::vector<const basic_sparse_set<Entity> *> epools) ENTT_NOEXCEPT
-        : pools{std::move(cpools)},
-          filter{std::move(epools)}
-    {
-        const auto it = std::min_element(pools.begin(), pools.end(), [](const auto *lhs, const auto *rhs) {
-            return (!lhs && rhs) || (lhs && rhs && lhs->size() < rhs->size());
-        });
-
-        // brings the best candidate (if any) on front of the vector
-        std::rotate(pools.begin(), it, pools.end());
-    }
-
     [[nodiscard]] bool valid() const {
         return !pools.empty() && pools.front();
     }
@@ -153,6 +138,29 @@ public:
     /*! @brief Bidirectional iterator type. */
     using iterator = view_iterator;
 
+    /*! @brief Default constructor to use to create empty, invalid views. */
+    basic_runtime_view() ENTT_NOEXCEPT
+        : pools{},
+          filter{}
+    {}
+
+    /**
+     * @brief Constructs a runtime view from a set of storage classes.
+     * @param cpools The storage for the types to iterate.
+     * @param epools The storage for the types used to filter the view.
+     */
+    basic_runtime_view(std::vector<const basic_sparse_set<Entity> *> cpools, std::vector<const basic_sparse_set<Entity> *> epools) ENTT_NOEXCEPT
+        : pools{std::move(cpools)},
+          filter{std::move(epools)}
+    {
+        const auto it = std::min_element(pools.begin(), pools.end(), [](const auto *lhs, const auto *rhs) {
+            return (!lhs && rhs) || (lhs && rhs && lhs->size() < rhs->size());
+        });
+
+        // brings the best candidate (if any) on front of the vector
+        std::rotate(pools.begin(), it, pools.end());
+    }
+
     /**
      * @brief Estimates the number of entities iterated by the view.
      * @return Estimated number of entities iterated by the view.

+ 5 - 0
test/entt/entity/runtime_view.cpp

@@ -46,6 +46,11 @@ TEST(RuntimeView, Functionalities) {
         ASSERT_EQ(registry.get<int>(entity), 42);
         ASSERT_EQ(registry.get<char>(entity), '2');
     }
+
+    entt::runtime_view empty{};
+
+    ASSERT_EQ(empty.size_hint(), 0u);
+    ASSERT_EQ(empty.begin(), empty.end());
 }
 
 TEST(RuntimeView, Iterator) {