registry: group -> group_if_exists (close #626)

This commit is contained in:
Michele Caini
2021-01-04 08:34:56 +01:00
parent 68a0e4f39a
commit 18ffbd0027
3 changed files with 19 additions and 15 deletions

View File

@@ -89,7 +89,11 @@ struct as_group {
*/
template<typename Exclude, typename Get, typename... Owned>
operator basic_group<entity_type, Exclude, Get, Owned...>() const {
return reg.template group<Owned...>(Get{}, Exclude{});
if constexpr(std::is_const_v<registry_type>) {
return reg.template group_if_exists<Owned...>(Get{}, Exclude{});
} else {
return reg.template group<Owned...>(Get{}, Exclude{});
}
}
private:

View File

@@ -1298,7 +1298,7 @@ public:
* @return A newly created group.
*/
template<typename... Owned, typename... Get, typename... Exclude>
[[nodiscard]] basic_group<Entity, exclude_t<Exclude...>, get_t<Get...>, Owned...> group(get_t<Get...>, exclude_t<Exclude...> = {}) const {
[[nodiscard]] basic_group<Entity, exclude_t<Exclude...>, get_t<Get...>, Owned...> group_if_exists(get_t<Get...>, exclude_t<Exclude...> = {}) const {
static_assert(std::conjunction_v<std::is_const<Owned>..., std::is_const<Get>...>, "Invalid non-const type");
if(auto it = std::find_if(groups.cbegin(), groups.cend(), [](const auto &gdata) {
@@ -1332,15 +1332,15 @@ public:
/**
* @brief Returns a group for the given components.
*
* @sa group
* @sa group_if_exists
*
* @tparam Owned Types of components owned by the group.
* @tparam Exclude Types of components used to filter the group.
* @return A newly created group.
*/
template<typename... Owned, typename... Exclude>
[[nodiscard]] basic_group<Entity, exclude_t<Exclude...>, get_t<>, Owned...> group(exclude_t<Exclude...> = {}) const {
return group<Owned...>(get_t<>{}, exclude<Exclude...>);
[[nodiscard]] basic_group<Entity, exclude_t<Exclude...>, get_t<>, Owned...> group_if_exists(exclude_t<Exclude...> = {}) const {
return group_if_exists<Owned...>(get_t<>{}, exclude<Exclude...>);
}
/**

View File

@@ -16,7 +16,7 @@ bool operator==(const boxed_int &lhs, const boxed_int &rhs) {
TEST(NonOwningGroup, Functionalities) {
entt::registry registry;
auto group = registry.group(entt::get<int, char>);
auto cgroup = std::as_const(registry).group(entt::get<const int, const char>);
auto cgroup = std::as_const(registry).group_if_exists(entt::get<const int, const char>);
ASSERT_TRUE(group.empty());
@@ -79,7 +79,7 @@ TEST(NonOwningGroup, Functionalities) {
TEST(NonOwningGroup, Invalid) {
entt::registry registry{};
auto group = std::as_const(registry).group(entt::get<const empty_type, const int>);
auto group = std::as_const(registry).group_if_exists(entt::get<const empty_type, const int>);
const auto entity = registry.create();
registry.emplace<empty_type>(entity);
@@ -115,7 +115,7 @@ TEST(NonOwningGroup, Invalid) {
TEST(NonOwningGroup, ElementAccess) {
entt::registry registry;
auto group = registry.group(entt::get<int, char>);
auto cgroup = std::as_const(registry).group(entt::get<const int, const char>);
auto cgroup = std::as_const(registry).group_if_exists(entt::get<const int, const char>);
const auto e0 = registry.create();
registry.emplace<int>(e0);
@@ -177,7 +177,7 @@ TEST(NonOwningGroup, Each) {
registry.emplace<int>(e1, 1);
registry.emplace<char>(e1);
auto cgroup = std::as_const(registry).group(entt::get<const int, const char>);
auto cgroup = std::as_const(registry).group_if_exists(entt::get<const int, const char>);
std::size_t cnt = 0;
for(auto first = cgroup.each().rbegin(), last = cgroup.each().rend(); first != last; ++first) {
@@ -487,7 +487,7 @@ TEST(NonOwningGroup, EmptyAndNonEmptyTypes) {
TEST(NonOwningGroup, TrackEntitiesOnComponentDestruction) {
entt::registry registry;
const auto group = registry.group(entt::get<int>, entt::exclude<char>);
const auto cgroup = std::as_const(registry).group(entt::get<const int>, entt::exclude<char>);
const auto cgroup = std::as_const(registry).group_if_exists(entt::get<const int>, entt::exclude<char>);
const auto entity = registry.create();
registry.emplace<int>(entity);
@@ -591,7 +591,7 @@ TEST(NonOwningGroup, ExtendedGet) {
TEST(OwningGroup, Functionalities) {
entt::registry registry;
auto group = registry.group<int>(entt::get<char>);
auto cgroup = std::as_const(registry).group<const int>(entt::get<const char>);
auto cgroup = std::as_const(registry).group_if_exists<const int>(entt::get<const char>);
ASSERT_TRUE(group.empty());
@@ -652,7 +652,7 @@ TEST(OwningGroup, Functionalities) {
TEST(OwningGroup, Invalid) {
entt::registry registry{};
auto group = std::as_const(registry).group<const int>(entt::get<const empty_type>);
auto group = std::as_const(registry).group_if_exists<const int>(entt::get<const empty_type>);
const auto entity = registry.create();
registry.emplace<empty_type>(entity);
@@ -684,7 +684,7 @@ TEST(OwningGroup, Invalid) {
TEST(OwningGroup, ElementAccess) {
entt::registry registry;
auto group = registry.group<int>(entt::get<char>);
auto cgroup = std::as_const(registry).group<const int>(entt::get<const char>);
auto cgroup = std::as_const(registry).group_if_exists<const int>(entt::get<const char>);
const auto e0 = registry.create();
registry.emplace<int>(e0);
@@ -746,7 +746,7 @@ TEST(OwningGroup, Each) {
registry.emplace<int>(e1, 1);
registry.emplace<char>(e1);
auto cgroup = std::as_const(registry).group<const int>(entt::get<const char>);
auto cgroup = std::as_const(registry).group_if_exists<const int>(entt::get<const char>);
std::size_t cnt = 0;
for(auto first = cgroup.each().rbegin(), last = cgroup.each().rend(); first != last; ++first) {
@@ -1160,7 +1160,7 @@ TEST(OwningGroup, EmptyAndNonEmptyTypes) {
TEST(OwningGroup, TrackEntitiesOnComponentDestruction) {
entt::registry registry;
const auto group = registry.group<int>(entt::exclude<char>);
const auto cgroup = std::as_const(registry).group<const int>(entt::exclude<char>);
const auto cgroup = std::as_const(registry).group_if_exists<const int>(entt::exclude<char>);
const auto entity = registry.create();
registry.emplace<int>(entity);