Skip to content

Commit

Permalink
Add test for using pair type with world::id, fix code example in quic…
Browse files Browse the repository at this point in the history
…kstart
  • Loading branch information
SanderMertens committed May 11, 2023
1 parent 66dca4e commit f786277
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 31 deletions.
8 changes: 4 additions & 4 deletions docs/Quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ printf("Name: %s\n", ecs_get_name(world, pos_e)); // outputs 'Name: Position'
ecs_add(world, pos_e, Serializable);
```
```cpp
flecs::entity pos_e = world.id<Position>();
flecs::entity pos_e = world.entity<Position>();
std::cout << "Name: " << pos_e.name() << std::endl; // outputs 'Name: Position'
// It's possible to add components like you would for any entity
Expand All @@ -256,7 +256,7 @@ const EcsComponent *c = ecs_get(world, pos_e, EcsComponent);
printf("Component size: %u\n", c->size);
```
```cpp
flecs::entity pos_e = world.id<Position>();
flecs::entity pos_e = world.entity<Position>();
const EcsComponent *c = pos_e.get<flecs::Component>();
std::cout << "Component size: " << c->size << std::endl;
Expand Down Expand Up @@ -548,7 +548,7 @@ e.each([&](flecs:id id) {
if (id == world.id<Position>()) {
// Found Position component!
}
}
});
```

### Singleton
Expand Down Expand Up @@ -577,7 +577,7 @@ ecs_set(world, ecs_id(Gravity), Gravity, {10, 20});
const Gravity *g = ecs_get(world, ecs_id(Gravity), Gravity);
```
```cpp
flecs::entity grav_e = world.id<Gravity>();
flecs::entity grav_e = world.entity<Gravity>();
grav_e.set<Gravity>({10, 20});
Expand Down
8 changes: 4 additions & 4 deletions flecs.h
Original file line number Diff line number Diff line change
Expand Up @@ -19189,7 +19189,7 @@ struct world {
* \memberof flecs::world
*/
template <typename T>
flecs::entity id() const;
flecs::id id() const;

/** Id factory.
*
Expand Down Expand Up @@ -23319,7 +23319,7 @@ struct iterable {
* The "each" iterator accepts a function that is invoked for each matching
* entity. The following function signatures are valid:
* - func(flecs::entity e, Components& ...)
* - func(flecs::iter& it, int32_t index, Components& ....)
* - func(flecs::iter& it, size_t index, Components& ....)
* - func(Components& ...)
*
* Each iterators are automatically instanced.
Expand Down Expand Up @@ -24810,8 +24810,8 @@ inline flecs::entity id::type_id() const {
// Id mixin implementation

template <typename T>
inline flecs::entity world::id() const {
return flecs::entity(m_world, _::cpp_type<T>::id(m_world));
inline flecs::id world::id() const {
return flecs::id(m_world, _::cpp_type<T>::id(m_world));
}

template <typename ... Args>
Expand Down
4 changes: 2 additions & 2 deletions include/flecs/addons/cpp/mixins/id/impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ inline flecs::entity id::type_id() const {
// Id mixin implementation

template <typename T>
inline flecs::entity world::id() const {
return flecs::entity(m_world, _::cpp_type<T>::id(m_world));
inline flecs::id world::id() const {
return flecs::id(m_world, _::cpp_type<T>::id(m_world));
}

template <typename ... Args>
Expand Down
2 changes: 1 addition & 1 deletion include/flecs/addons/cpp/mixins/id/mixin.inl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* \memberof flecs::world
*/
template <typename T>
flecs::entity id() const;
flecs::id id() const;

/** Id factory.
*
Expand Down
6 changes: 3 additions & 3 deletions test/cpp_api/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,7 @@
"to_view",
"to_view_from_stage",
"set_alias",
"emplace_w_observer",
"world_id_to_entity"
"emplace_w_observer"
]
}, {
"id": "Pairs",
Expand Down Expand Up @@ -1129,7 +1128,8 @@
"run_post_frame",
"component_w_low_id",
"get_set_log_level",
"reset_world"
"reset_world",
"id_from_pair_type"
]
}, {
"id": "Singleton",
Expand Down
10 changes: 0 additions & 10 deletions test/cpp_api/src/Entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4305,13 +4305,3 @@ void Entity_emplace_w_observer() {
test_int(e.get<Position>()->x, 10);
test_int(e.get<Position>()->y, 20);
}

void Entity_world_id_to_entity() {
flecs::world ecs;

flecs::entity e1 = ecs.component<Position>();
flecs::entity e2 = ecs.id<Position>();
test_assert(e1 == e2);
flecs::id id = ecs.id<Position>();
test_assert(e1 == id);
}
9 changes: 9 additions & 0 deletions test/cpp_api/src/World.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1694,3 +1694,12 @@ void World_reset_world() {
ecs.reset();
test_assert(!ecs.exists(e));
}

void World_id_from_pair_type() {
flecs::world ecs;

flecs::id id = ecs.id<flecs::pair<Position, Velocity>>();
test_assert(id.is_pair());
test_assert(id.first() == ecs.id<Position>());
test_assert(id.second() == ecs.id<Velocity>());
}
14 changes: 7 additions & 7 deletions test/cpp_api/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,6 @@ void Entity_to_view(void);
void Entity_to_view_from_stage(void);
void Entity_set_alias(void);
void Entity_emplace_w_observer(void);
void Entity_world_id_to_entity(void);

// Testsuite 'Pairs'
void Pairs_add_component_pair(void);
Expand Down Expand Up @@ -1080,6 +1079,7 @@ void World_run_post_frame(void);
void World_component_w_low_id(void);
void World_get_set_log_level(void);
void World_reset_world(void);
void World_id_from_pair_type(void);

// Testsuite 'Singleton'
void Singleton_set_get_singleton(void);
Expand Down Expand Up @@ -2180,10 +2180,6 @@ bake_test_case Entity_testcases[] = {
{
"emplace_w_observer",
Entity_emplace_w_observer
},
{
"world_id_to_entity",
Entity_world_id_to_entity
}
};

Expand Down Expand Up @@ -5387,6 +5383,10 @@ bake_test_case World_testcases[] = {
{
"reset_world",
World_reset_world
},
{
"id_from_pair_type",
World_id_from_pair_type
}
};

Expand Down Expand Up @@ -5901,7 +5901,7 @@ static bake_test_suite suites[] = {
"Entity",
NULL,
NULL,
240,
239,
Entity_testcases
},
{
Expand Down Expand Up @@ -6048,7 +6048,7 @@ static bake_test_suite suites[] = {
"World",
NULL,
NULL,
95,
96,
World_testcases
},
{
Expand Down

0 comments on commit f786277

Please sign in to comment.