From 6f3672309715bfbd9a591baa6bd5aec07a56f81f Mon Sep 17 00:00:00 2001 From: Michele Caini Date: Thu, 3 Dec 2020 12:52:46 +0100 Subject: [PATCH] runtime_view: public constructor (close #597) --- TODO | 1 + src/entt/entity/runtime_view.hpp | 38 +++++++++++++++++++------------ test/entt/entity/runtime_view.cpp | 5 ++++ 3 files changed, 29 insertions(+), 15 deletions(-) diff --git a/TODO b/TODO index fe1bae9c1..4f8d103cc 100644 --- a/TODO +++ b/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 diff --git a/src/entt/entity/runtime_view.hpp b/src/entt/entity/runtime_view.hpp index 5e58aca54..e093c546c 100644 --- a/src/entt/entity/runtime_view.hpp +++ b/src/entt/entity/runtime_view.hpp @@ -55,9 +55,6 @@ namespace entt { */ template class basic_runtime_view final { - /*! @brief A registry is allowed to create views. */ - friend class basic_registry; - using underlying_iterator = typename basic_sparse_set::iterator; class view_iterator final { @@ -129,18 +126,6 @@ class basic_runtime_view final { underlying_iterator it; }; - basic_runtime_view(std::vector *> cpools, std::vector *> 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 *> cpools, std::vector *> 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. diff --git a/test/entt/entity/runtime_view.cpp b/test/entt/entity/runtime_view.cpp index 2e57116c9..e81d31f34 100644 --- a/test/entt/entity/runtime_view.cpp +++ b/test/entt/entity/runtime_view.cpp @@ -46,6 +46,11 @@ TEST(RuntimeView, Functionalities) { ASSERT_EQ(registry.get(entity), 42); ASSERT_EQ(registry.get(entity), '2'); } + + entt::runtime_view empty{}; + + ASSERT_EQ(empty.size_hint(), 0u); + ASSERT_EQ(empty.begin(), empty.end()); } TEST(RuntimeView, Iterator) {