Skip to content

Commit

Permalink
view: joining views with storages (#1196)
Browse files Browse the repository at this point in the history
  • Loading branch information
TerensTare authored Dec 18, 2024
1 parent da8e4bf commit 832ff4a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/entt/entity/view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,21 @@ class basic_view<get_t<Get...>, exclude_t<Exclude...>, std::enable_if_t<(sizeof.
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.
Expand Down Expand Up @@ -1072,6 +1087,21 @@ class basic_view<get_t<Get>, exclude_t<>>
}
}

/**
* @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.
Expand Down
4 changes: 4 additions & 0 deletions test/entt/entity/view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)>();
Expand Down

0 comments on commit 832ff4a

Please sign in to comment.