Przeglądaj źródła

runtime_view: removed redundant validity check

Michele Caini 4 lat temu
rodzic
commit
97fe978d14

+ 1 - 1
src/entt/entity/fwd.hpp

@@ -20,7 +20,7 @@ template<typename, typename, typename, typename = void>
 class basic_view;
 
 template<typename>
-class basic_runtime_view;
+struct basic_runtime_view;
 
 template<typename, typename, typename, typename>
 class basic_group;

+ 8 - 4
src/entt/entity/registry.hpp

@@ -1193,13 +1193,17 @@ public:
         filter.reserve(std::distance(from, to));
 
         for(; first != last; ++first) {
-            const auto it = pools.find(*first);
-            component.emplace_back(it == pools.cend() ? nullptr : it->second.get());
+            if(const auto it = pools.find(*first); it == pools.cend()) {
+                return {};
+            } else {
+                component.emplace_back(it->second.get());
+            }
         }
 
         for(; from != to; ++from) {
-            const auto it = pools.find(*from);
-            filter.emplace_back(it == pools.cend() ? nullptr : it->second.get());
+            if(const auto it = pools.find(*from); it != pools.cend()) {
+                filter.emplace_back(it->second.get());
+            }
         }
 
         return {std::move(component), std::move(filter)};

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

@@ -105,7 +105,7 @@ private:
  * compile-time error, but for a few reasonable cases.
  */
 template<typename>
-class basic_runtime_view;
+struct basic_runtime_view;
 
 /**
  * @brief Generic runtime view.
@@ -147,12 +147,7 @@ class basic_runtime_view;
  * @tparam Allocator Type of allocator used to manage memory and elements.
  */
 template<typename Entity, typename Allocator>
-class basic_runtime_view<basic_sparse_set<Entity, Allocator>> final {
-    [[nodiscard]] bool valid() const {
-        return !pools.empty() && pools.front();
-    }
-
-public:
+struct basic_runtime_view<basic_sparse_set<Entity, Allocator>> final {
     /*! @brief Underlying entity identifier. */
     using entity_type = Entity;
     /*! @brief Unsigned integer type. */
@@ -188,7 +183,7 @@ public:
      * @return Estimated number of entities iterated by the view.
      */
     [[nodiscard]] size_type size_hint() const {
-        return valid() ? pools.front()->size() : size_type{};
+        return pools.empty() ? size_type{} : pools.front()->size();
     }
 
     /**
@@ -202,7 +197,7 @@ public:
      * @return An iterator to the first entity that has the given components.
      */
     [[nodiscard]] iterator begin() const {
-        return valid() ? iterator{pools, filter, pools[0]->begin()} : iterator{};
+        return pools.empty() ? iterator{} : iterator{pools, filter, pools[0]->begin()};
     }
 
     /**
@@ -217,7 +212,7 @@ public:
      * given components.
      */
     [[nodiscard]] iterator end() const {
-        return valid() ? iterator{pools, filter, pools[0]->end()} : iterator{};
+        return pools.empty() ? iterator{} : iterator{pools, filter, pools[0]->end()};
     }
 
     /**
@@ -226,7 +221,8 @@ public:
      * @return True if the view contains the given entity, false otherwise.
      */
     [[nodiscard]] bool contains(const entity_type entt) const {
-        return valid() && std::all_of(pools.cbegin(), pools.cend(), [entt](const auto *curr) { return curr->contains(entt); })
+        return !pools.empty()
+               && std::all_of(pools.cbegin(), pools.cend(), [entt](const auto *curr) { return curr->contains(entt); })
                && std::none_of(filter.cbegin(), filter.cend(), [entt](const auto *curr) { return curr && curr->contains(entt); });
     }