Browse Source

view: joining views with storages (#1196)

Terens 1 year ago
parent
commit
832ff4afcb
2 changed files with 34 additions and 0 deletions
  1. 30 0
      src/entt/entity/view.hpp
  2. 4 0
      test/entt/entity/view.cpp

+ 30 - 0
src/entt/entity/view.hpp

@@ -642,6 +642,21 @@ public:
         return iterable{base_type::begin(), base_type::end()};
     }
 
+    /**
+     * @brief Attaches a storage to the end of the view for iterating.
+     *
+     * @tparam OGet The type of the storage to attach to the view.
+     * @param other The storage to attach to the view.
+     * @return A new view with the given storage attached.
+     * @note This function only supports adding one storage, use @ref operator| to join two views instead.
+     * @sa operator|
+     */
+    template<typename OGet>
+    [[nodiscard]] auto join(OGet &other) const noexcept {
+        return internal::view_pack<basic_view<get_t<Get..., OGet>, exclude_t<Exclude...>>>(
+            *this, basic_view<get_t<OGet>, exclude_t<>>{other}, std::index_sequence_for<Get...>{}, std::index_sequence_for<Exclude...>{}, std::index_sequence_for<OGet>{}, std::index_sequence_for<>{});
+    }
+
     /**
      * @brief Combines two views in a _more specific_ one.
      * @tparam OGet Element list of the view to combine with.
@@ -1072,6 +1087,21 @@ public:
         }
     }
 
+    /**
+     * @brief Attaches a storage to the end of the view for iterating.
+     *
+     * @tparam OGet The type of the storage to attach to the view.
+     * @param other The storage to attach to the view.
+     * @return A new view with the given storage attached.
+     * @note This function only supports adding one storage, use @ref operator| to join two views instead.
+     * @sa operator|
+     */
+    template<typename OGet>
+    [[nodiscard]] auto join(OGet &other) const noexcept {
+        return internal::view_pack<basic_view<get_t<Get, OGet>, exclude_t<>>>(
+            *this, basic_view<get_t<OGet>, exclude_t<>>{other}, std::index_sequence_for<Get>{}, std::index_sequence_for<>{}, std::index_sequence_for<OGet>{}, std::index_sequence_for<>{});
+    }
+
     /**
      * @brief Combines two views in a _more specific_ one.
      * @tparam OGet Element list of the view to combine with.

+ 4 - 0
test/entt/entity/view.cpp

@@ -1540,6 +1540,10 @@ TEST(View, Pipe) {
     entt::basic_view view2{std::forward_as_tuple(std::as_const(std::get<0>(storage))), std::forward_as_tuple(std::get<4>(storage))};
     entt::basic_view view3{std::get<2>(storage)};
     entt::basic_view view4{std::get<3>(storage)};
+    entt::basic_view view5{std::get<3>(std::as_const(storage))};
+
+    testing::StaticAssertTypeEq<decltype(view1.join(std::get<2>(storage))), decltype(view1 | view3)>();
+    testing::StaticAssertTypeEq<decltype(view1.join(std::get<3>(std::as_const(storage)))), decltype(view1 | view5)>();
 
     testing::StaticAssertTypeEq<entt::basic_view<entt::get_t<entt::storage<int>, const entt::storage<int>>, entt::exclude_t<const entt::storage<double>, entt::storage<float>>>, decltype(view1 | view2)>();
     testing::StaticAssertTypeEq<entt::basic_view<entt::get_t<const entt::storage<int>, entt::storage<int>>, entt::exclude_t<entt::storage<float>, const entt::storage<double>>>, decltype(view2 | view1)>();