Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1516 Fix issue with entity::remove that prevents removing enum components #1609

Merged
merged 1 commit into from
Mar 17, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
#1516 Fix issue with entity::remove that prevents removing enum compo…
…nents
SanderMertens committed Mar 17, 2025
commit d0c9f9cfa38892715bf97a09e2a5fac8641a9d32
13 changes: 1 addition & 12 deletions distr/flecs.h
Original file line number Diff line number Diff line change
@@ -25470,23 +25470,12 @@ struct entity_builder : entity_view {
*
* @tparam T the type of the component to remove.
*/
template <typename T, if_not_t< is_enum<T>::value > = 0>
template <typename T>
const Self& remove() const {
ecs_remove_id(this->world_, this->id_, _::type<T>::id(this->world_));
return to_base();
}

/** Remove pair for enum.
* This operation will remove any `(Enum, *)` pair from the entity.
*
* @tparam E The enumeration type.
*/
template <typename E, if_t< is_enum<E>::value > = 0>
const Self& remove() const {
flecs::entity_t first = _::type<E>::id(this->world_);
return this->remove(first, flecs::Wildcard);
}

/** Remove an entity from an entity.
*
* @param entity The entity to remove.
2 changes: 1 addition & 1 deletion examples/cpp/relationships/enum_relations/src/main.cpp
Original file line number Diff line number Diff line change
@@ -86,5 +86,5 @@ int main(int, char *[]) {
// ::Tile::Sand

// Remove any instance of the TileStatus relationship
tile.remove<TileStatus>();
tile.remove<TileStatus>(flecs::Wildcard);
}
13 changes: 1 addition & 12 deletions include/flecs/addons/cpp/mixins/entity/builder.hpp
Original file line number Diff line number Diff line change
@@ -299,23 +299,12 @@ struct entity_builder : entity_view {
*
* @tparam T the type of the component to remove.
*/
template <typename T, if_not_t< is_enum<T>::value > = 0>
template <typename T>
const Self& remove() const {
ecs_remove_id(this->world_, this->id_, _::type<T>::id(this->world_));
return to_base();
}

/** Remove pair for enum.
* This operation will remove any `(Enum, *)` pair from the entity.
*
* @tparam E The enumeration type.
*/
template <typename E, if_t< is_enum<E>::value > = 0>
const Self& remove() const {
flecs::entity_t first = _::type<E>::id(this->world_);
return this->remove(first, flecs::Wildcard);
}

/** Remove an entity from an entity.
*
* @param entity The entity to remove.
6 changes: 4 additions & 2 deletions test/cpp/project.json
Original file line number Diff line number Diff line change
@@ -300,7 +300,8 @@
"set_pair_second_invalid_type",
"get_ref_pair_second_invalid_type",
"iter_type",
"iter_empty_type"
"iter_empty_type",
"add_remove_enum_component"
]
}, {
"id": "Pairs",
@@ -1353,7 +1354,8 @@
"add_remove_singleton_pair_R_T",
"add_remove_singleton_pair_R_t",
"add_remove_singleton_pair_r_t",
"get_target"
"get_target",
"singleton_enum"
]
}, {
"id": "Misc",
30 changes: 30 additions & 0 deletions test/cpp/src/Entity.cpp
Original file line number Diff line number Diff line change
@@ -4937,3 +4937,33 @@ void Entity_iter_empty_type(void) {
test_int(count, 0);
}

enum Color {
Red, Green, Blue
};

void Entity_add_remove_enum_component(void) {
flecs::world world;

flecs::entity e = world.entity();

e.set<Color>(Blue);
test_assert(e.has<Color>());

{
const Color *c = e.get<Color>();
test_assert(c != nullptr);
test_assert(*c == Blue);
}

e.set<Color>(Green);
test_assert(e.has<Color>());

{
const Color *c = e.get<Color>();
test_assert(c != nullptr);
test_assert(*c == Green);
}

e.remove<Color>();
test_assert(!e.has<Color>());
}
2 changes: 1 addition & 1 deletion test/cpp/src/Enum.cpp
Original file line number Diff line number Diff line change
@@ -597,7 +597,7 @@ void Enum_remove_enum(void) {
auto e = ecs.entity().add(StandardEnum::Green);
test_assert(e.has(StandardEnum::Green));

e.remove<StandardEnum>();
e.remove<StandardEnum>(flecs::Wildcard);
test_assert(!e.has(StandardEnum::Green));
}

29 changes: 29 additions & 0 deletions test/cpp/src/Singleton.cpp
Original file line number Diff line number Diff line change
@@ -329,3 +329,32 @@ void Singleton_get_target(void) {
test_assert(p == entities[i]);
}
}

enum Color {
Red, Green, Blue
};

void Singleton_singleton_enum(void) {
flecs::world world;

world.set<Color>(Blue);
test_assert(world.has<Color>());

{
const Color *c = world.get<Color>();
test_assert(c != nullptr);
test_assert(*c == Blue);
}

world.set<Color>(Green);
test_assert(world.has<Color>());

{
const Color *c = world.get<Color>();
test_assert(c != nullptr);
test_assert(*c == Green);
}

world.remove<Color>();
test_assert(!world.has<Color>());
}
2 changes: 1 addition & 1 deletion test/cpp/src/Union.cpp
Original file line number Diff line number Diff line change
@@ -126,7 +126,7 @@ void Union_switch_enum_type(void) {
test_assert(e.has<Color>(flecs::Wildcard));
test_assert(e.table() == table);

e.remove<Color>();
e.remove<Color>(flecs::Wildcard);
test_assert(!e.has(Red));
test_assert(!e.has(Green));
test_assert(!e.has(Blue));
14 changes: 12 additions & 2 deletions test/cpp/src/main.cpp
Original file line number Diff line number Diff line change
@@ -293,6 +293,7 @@ void Entity_set_pair_second_invalid_type(void);
void Entity_get_ref_pair_second_invalid_type(void);
void Entity_iter_type(void);
void Entity_iter_empty_type(void);
void Entity_add_remove_enum_component(void);

// Testsuite 'Pairs'
void Pairs_add_component_pair(void);
@@ -1307,6 +1308,7 @@ void Singleton_add_remove_singleton_pair_R_T(void);
void Singleton_add_remove_singleton_pair_R_t(void);
void Singleton_add_remove_singleton_pair_r_t(void);
void Singleton_get_target(void);
void Singleton_singleton_enum(void);

// Testsuite 'Misc'
void Misc_setup(void);
@@ -2599,6 +2601,10 @@ bake_test_case Entity_testcases[] = {
{
"iter_empty_type",
Entity_iter_empty_type
},
{
"add_remove_enum_component",
Entity_add_remove_enum_component
}
};

@@ -6561,6 +6567,10 @@ bake_test_case Singleton_testcases[] = {
{
"get_target",
Singleton_get_target
},
{
"singleton_enum",
Singleton_singleton_enum
}
};

@@ -7197,7 +7207,7 @@ static bake_test_suite suites[] = {
"Entity",
NULL,
NULL,
280,
281,
Entity_testcases
},
{
@@ -7325,7 +7335,7 @@ static bake_test_suite suites[] = {
"Singleton",
NULL,
NULL,
20,
21,
Singleton_testcases
},
{