Skip to content

Commit

Permalink
#124 implement event data API
Browse files Browse the repository at this point in the history
  • Loading branch information
SanderMertens committed Dec 13, 2021
1 parent caddd3a commit ef52875
Show file tree
Hide file tree
Showing 11 changed files with 223 additions and 28 deletions.
2 changes: 1 addition & 1 deletion flecs.c
Original file line number Diff line number Diff line change
Expand Up @@ -13884,7 +13884,7 @@ void ecs_emit(
.other_table = desc->other_table,
.offset = row,
.count = count,
.param = desc->param
.param = (void*)desc->param
};

world->event_id ++;
Expand Down
70 changes: 58 additions & 12 deletions flecs.h
Original file line number Diff line number Diff line change
Expand Up @@ -5881,7 +5881,7 @@ typedef struct ecs_event_desc_t {
int32_t count;

/* Optional context. Assigned to iter param member */
void *param;
const void *param;

/* Observable (usually the world) */
ecs_poly_t *observable;
Expand Down Expand Up @@ -11392,8 +11392,9 @@ using filter_m_world = filter_m<flecs::world>;
namespace flecs {

// Event builder interface
struct event_builder {
event_builder(flecs::world_t *world, flecs::entity_t event)
template <typename Base, typename E>
struct event_builder_base {
event_builder_base(flecs::world_t *world, flecs::entity_t event)
: m_world(world)
, m_desc{}
, m_ids{}
Expand All @@ -11404,23 +11405,23 @@ struct event_builder {

/** Add component to trigger on */
template <typename T>
event_builder& id() {
Base& id() {
m_ids.array = m_ids_array;
m_ids.array[m_ids.count] = _::cpp_type<T>().id(m_world);
m_ids.count ++;
return *this;
}

/** Add (component) id to trigger on */
event_builder& id(flecs::id_t id) {
Base& id(flecs::id_t id) {
m_ids.array = m_ids_array;
m_ids.array[m_ids.count] = id;
m_ids.count ++;
return *this;
}

/** Set entity for which to trigger */
event_builder& entity(flecs::entity_t e) {
Base& entity(flecs::entity_t e) {
ecs_record_t *r = ecs_record_find(m_world, e);

/* can't trigger for empty entity */
Expand All @@ -11434,13 +11435,19 @@ struct event_builder {
}

/* Set table for which to trigger */
event_builder& table(flecs::table_t *t, int32_t offset = 0, int32_t count = 0) {
Base& table(flecs::table_t *t, int32_t offset = 0, int32_t count = 0) {
m_desc.table = t;
m_desc.offset = offset;
m_desc.count = count;
return *this;
}

/* Set event data */
Base& ctx(const E* ptr) {
m_desc.param = ptr;
return *this;
}

void emit() {
ecs_assert(m_ids.count != 0, ECS_INVALID_PARAMETER, NULL);
ecs_assert(m_desc.table != nullptr, ECS_INVALID_PARAMETER, NULL);
Expand All @@ -11450,11 +11457,36 @@ struct event_builder {
ecs_emit(m_world, &m_desc);
}

private:
protected:
flecs::world_t *m_world;
ecs_event_desc_t m_desc;
flecs::ids_t m_ids;
flecs::id_t m_ids_array[ECS_EVENT_DESC_ID_COUNT_MAX];

private:
operator Base&() {
return *static_cast<Base*>(this);
}
};

struct event_builder : event_builder_base<event_builder, void> {
using event_builder_base::event_builder_base;
};

template <typename E>
struct event_builder_typed : event_builder_base<event_builder_typed<E>, E> {
private:
using Class = event_builder_typed<E>;
using Base = event_builder_base<Class, E>;

public:
using Base::event_builder_base;

/* Set event data */
Class& ctx(const E& ptr) {
this->m_desc.param = &ptr;
return *this;
}
};

}
Expand All @@ -11464,6 +11496,9 @@ namespace flecs {

struct event_builder;

template <typename E>
struct event_builder_typed;

template<typename T>
struct event_m : mixin<T> { };

Expand All @@ -11474,16 +11509,18 @@ struct event_m<flecs::world> : mixin<flecs::world> {

/** Create a new event.
*
* @param evt The event id.
* @return Event builder.
*/
flecs::event_builder event(flecs::entity_t event) const;
flecs::event_builder event(flecs::entity_t evt) const;

/** Create a new event.
*
* @tparam E The event type.
* @return Event builder.
*/
template <typename E>
flecs::event_builder event() const;
flecs::event_builder_typed<E> event() const;
};

using event_m_world = event_m<flecs::world>;
Expand Down Expand Up @@ -13515,6 +13552,15 @@ struct iter {
return m_iter->param;
}

/** Access param.
* param contains the pointer passed to the param argument of system::run
*/
template <typename T>
T* param() {
/* TODO: type check */
return static_cast<T*>(m_iter->param);
}

/** Obtain mutable handle to entity being iterated over.
*
* @param row Row being iterated over.
Expand Down Expand Up @@ -17883,8 +17929,8 @@ inline flecs::event_builder event_m_world::event(flecs::entity_t evt) const {
}

template <typename E>
inline flecs::event_builder event_m_world::event() const {
return flecs::event_builder(this->me(), _::cpp_type<E>().id(this->me()));
inline flecs::event_builder_typed<E> event_m_world::event() const {
return flecs::event_builder_typed<E>(this->me(), _::cpp_type<E>().id(this->me()));
}

} // namespace flecs
Expand Down
2 changes: 1 addition & 1 deletion include/flecs.h
Original file line number Diff line number Diff line change
Expand Up @@ -2982,7 +2982,7 @@ typedef struct ecs_event_desc_t {
int32_t count;

/* Optional context. Assigned to iter param member */
void *param;
const void *param;

/* Observable (usually the world) */
ecs_poly_t *observable;
Expand Down
9 changes: 9 additions & 0 deletions include/flecs/addons/cpp/iter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,15 @@ struct iter {
return m_iter->param;
}

/** Access param.
* param contains the pointer passed to the param argument of system::run
*/
template <typename T>
T* param() {
/* TODO: type check */
return static_cast<T*>(m_iter->param);
}

/** Obtain mutable handle to entity being iterated over.
*
* @param row Row being iterated over.
Expand Down
46 changes: 39 additions & 7 deletions include/flecs/addons/cpp/mixins/event/builder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
namespace flecs {

// Event builder interface
struct event_builder {
event_builder(flecs::world_t *world, flecs::entity_t event)
template <typename Base, typename E>
struct event_builder_base {
event_builder_base(flecs::world_t *world, flecs::entity_t event)
: m_world(world)
, m_desc{}
, m_ids{}
Expand All @@ -17,23 +18,23 @@ struct event_builder {

/** Add component to trigger on */
template <typename T>
event_builder& id() {
Base& id() {
m_ids.array = m_ids_array;
m_ids.array[m_ids.count] = _::cpp_type<T>().id(m_world);
m_ids.count ++;
return *this;
}

/** Add (component) id to trigger on */
event_builder& id(flecs::id_t id) {
Base& id(flecs::id_t id) {
m_ids.array = m_ids_array;
m_ids.array[m_ids.count] = id;
m_ids.count ++;
return *this;
}

/** Set entity for which to trigger */
event_builder& entity(flecs::entity_t e) {
Base& entity(flecs::entity_t e) {
ecs_record_t *r = ecs_record_find(m_world, e);

/* can't trigger for empty entity */
Expand All @@ -47,13 +48,19 @@ struct event_builder {
}

/* Set table for which to trigger */
event_builder& table(flecs::table_t *t, int32_t offset = 0, int32_t count = 0) {
Base& table(flecs::table_t *t, int32_t offset = 0, int32_t count = 0) {
m_desc.table = t;
m_desc.offset = offset;
m_desc.count = count;
return *this;
}

/* Set event data */
Base& ctx(const E* ptr) {
m_desc.param = ptr;
return *this;
}

void emit() {
ecs_assert(m_ids.count != 0, ECS_INVALID_PARAMETER, NULL);
ecs_assert(m_desc.table != nullptr, ECS_INVALID_PARAMETER, NULL);
Expand All @@ -63,11 +70,36 @@ struct event_builder {
ecs_emit(m_world, &m_desc);
}

private:
protected:
flecs::world_t *m_world;
ecs_event_desc_t m_desc;
flecs::ids_t m_ids;
flecs::id_t m_ids_array[ECS_EVENT_DESC_ID_COUNT_MAX];

private:
operator Base&() {
return *static_cast<Base*>(this);
}
};

struct event_builder : event_builder_base<event_builder, void> {
using event_builder_base::event_builder_base;
};

template <typename E>
struct event_builder_typed : event_builder_base<event_builder_typed<E>, E> {
private:
using Class = event_builder_typed<E>;
using Base = event_builder_base<Class, E>;

public:
using Base::event_builder_base;

/* Set event data */
Class& ctx(const E& ptr) {
this->m_desc.param = &ptr;
return *this;
}
};

}
9 changes: 7 additions & 2 deletions include/flecs/addons/cpp/mixins/event/decl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ namespace flecs {

struct event_builder;

template <typename E>
struct event_builder_typed;

template<typename T>
struct event_m : mixin<T> { };

Expand All @@ -16,16 +19,18 @@ struct event_m<flecs::world> : mixin<flecs::world> {

/** Create a new event.
*
* @param evt The event id.
* @return Event builder.
*/
flecs::event_builder event(flecs::entity_t event) const;
flecs::event_builder event(flecs::entity_t evt) const;

/** Create a new event.
*
* @tparam E The event type.
* @return Event builder.
*/
template <typename E>
flecs::event_builder event() const;
flecs::event_builder_typed<E> event() const;
};

using event_m_world = event_m<flecs::world>;
Expand Down
4 changes: 2 additions & 2 deletions include/flecs/addons/cpp/mixins/event/impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ inline flecs::event_builder event_m_world::event(flecs::entity_t evt) const {
}

template <typename E>
inline flecs::event_builder event_m_world::event() const {
return flecs::event_builder(this->me(), _::cpp_type<E>().id(this->me()));
inline flecs::event_builder_typed<E> event_m_world::event() const {
return flecs::event_builder_typed<E>(this->me(), _::cpp_type<E>().id(this->me()));
}

} // namespace flecs
2 changes: 1 addition & 1 deletion src/observable.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ void ecs_emit(
.other_table = desc->other_table,
.offset = row,
.count = count,
.param = desc->param
.param = (void*)desc->param
};

world->event_id ++;
Expand Down
5 changes: 4 additions & 1 deletion test/cpp_api/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,10 @@
"evt_2_ids_table",
"evt_type",
"evt_1_component",
"evt_2_components"
"evt_2_components",
"evt_void_ctx",
"evt_typed_ctx",
"evt_implicit_typed_ctx"
]
}, {
"id": "Trigger",
Expand Down
Loading

0 comments on commit ef52875

Please sign in to comment.