Skip to content

Commit d49c3f1

Browse files
committed
#680 simplify component lifecycle signatures
1 parent 98e722f commit d49c3f1

23 files changed

+834
-1158
lines changed

flecs.c

Lines changed: 222 additions & 272 deletions
Large diffs are not rendered by default.

flecs.h

Lines changed: 41 additions & 173 deletions
Large diffs are not rendered by default.

include/flecs.h

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -321,27 +321,19 @@ typedef uint64_t (*ecs_hash_value_action_t)(
321321

322322
/** Constructor/destructor callback */
323323
typedef void (*ecs_xtor_t)(
324-
ecs_world_t *world,
325-
const ecs_entity_t *entities,
326324
void *ptr,
327325
int32_t count,
328326
const ecs_type_info_t *type_info);
329327

330328
/** Copy is invoked when a component is copied into another component. */
331329
typedef void (*ecs_copy_t)(
332-
ecs_world_t *world,
333-
const ecs_entity_t *dst_entities,
334-
const ecs_entity_t *src_entities,
335330
void *dst_ptr,
336331
const void *src_ptr,
337332
int32_t count,
338333
const ecs_type_info_t *type_info);
339334

340335
/** Move is invoked when a component is moved to another component. */
341336
typedef void (*ecs_move_t)(
342-
ecs_world_t *world,
343-
const ecs_entity_t *dst_entities,
344-
const ecs_entity_t *src_entities,
345337
void *dst_ptr,
346338
void *src_ptr,
347339
int32_t count,
@@ -881,6 +873,10 @@ typedef struct EcsComponentLifecycle {
881873
* not set explicitly it will be derived from other callbacks. */
882874
ecs_move_t move_dtor;
883875

876+
/* Callback that is invoked when an instance of a component is added. This
877+
* callback is invoked before triggers are invoked. */
878+
ecs_iter_action_t on_add;
879+
884880
/* Callback that is invoked when an instance of the component is set. This
885881
* callback is invoked before triggers are invoked, and enable the component
886882
* to respond to changes on itself before others can. */

include/flecs/addons/cpp/impl.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11

2-
#include "impl/lifecycle_traits.hpp"
32
#include "impl/iter.hpp"
43
#include "impl/world.hpp"

include/flecs/addons/cpp/impl/lifecycle_traits.hpp

Lines changed: 0 additions & 25 deletions
This file was deleted.

include/flecs/addons/cpp/lifecycle_traits.hpp

Lines changed: 34 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -4,75 +4,41 @@ namespace flecs
44
namespace _
55
{
66

7-
inline void ecs_ctor_illegal(ecs_world_t* w, const ecs_entity_t*,
8-
void *, int32_t, const ecs_type_info_t* info)
9-
{
10-
char *path = ecs_get_path_w_sep(w, 0, info->component, "::", "::");
11-
ecs_abort(ECS_INVALID_OPERATION,
12-
"cannnot default construct %s, add %s::%s() or use emplace<T>",
13-
path, path, ecs_get_name(w, info->component));
14-
ecs_os_free(path);
7+
inline void ecs_ctor_illegal(void *, int32_t, const ecs_type_info_t*) {
8+
ecs_abort(ECS_INVALID_OPERATION, "invalid constructor");
159
}
1610

17-
inline void ecs_dtor_illegal(ecs_world_t* w, const ecs_entity_t*,
18-
void *, int32_t, const ecs_type_info_t *info)
19-
{
20-
char *path = ecs_get_path_w_sep(w, 0, info->component, "::", "::");
21-
ecs_abort(ECS_INVALID_OPERATION, "cannnot destruct %s, add ~%s::%s()",
22-
path, path, ecs_get_name(w, info->component));
23-
ecs_os_free(path);
11+
inline void ecs_dtor_illegal(void *, int32_t, const ecs_type_info_t*) {
12+
ecs_abort(ECS_INVALID_OPERATION, "invalid destructor");
2413
}
2514

26-
inline void ecs_copy_illegal(ecs_world_t* w, const ecs_entity_t*,
27-
const ecs_entity_t*, void *, const void *, int32_t, const ecs_type_info_t *info)
15+
inline void ecs_copy_illegal(
16+
void *, const void *, int32_t, const ecs_type_info_t *)
2817
{
29-
char *path = ecs_get_path_w_sep(w, 0, info->component, "::", "::");
30-
ecs_abort(ECS_INVALID_OPERATION,
31-
"cannnot copy assign %s, add %s& %s::operator =(const %s&)", path,
32-
ecs_get_name(w, info->component), path, ecs_get_name(w, info->component), ecs_get_name(w, info->component));
33-
ecs_os_free(path);
18+
ecs_abort(ECS_INVALID_OPERATION, "invalid copy assignment");
3419
}
3520

36-
inline void ecs_move_illegal(ecs_world_t* w, const ecs_entity_t*,
37-
const ecs_entity_t*, void *, void *, int32_t, const ecs_type_info_t *info)
38-
{
39-
char *path = ecs_get_path_w_sep(w, 0, info->component, "::", "::");
40-
ecs_abort(ECS_INVALID_OPERATION,
41-
"cannnot move assign %s, add %s& %s::operator =(%s&&)", path,
42-
ecs_get_name(w, info->component), path, ecs_get_name(w, info->component), ecs_get_name(w, info->component));
43-
ecs_os_free(path);
21+
inline void ecs_move_illegal(void *, void *, int32_t, const ecs_type_info_t *) {
22+
ecs_abort(ECS_INVALID_OPERATION, "invalid move assignment");
4423
}
4524

46-
inline void ecs_copy_ctor_illegal(ecs_world_t* w,
47-
const ecs_entity_t*, const ecs_entity_t*,
48-
void *, const void *, int32_t, const ecs_type_info_t *info)
25+
inline void ecs_copy_ctor_illegal(
26+
void *, const void *, int32_t, const ecs_type_info_t *)
4927
{
50-
char *path = ecs_get_path_w_sep(w, 0, info->component, "::", "::");
51-
ecs_abort(ECS_INVALID_OPERATION,
52-
"cannnot copy construct %s, add %s::%s(const %s&)",
53-
path, path, ecs_get_name(w, info->component), ecs_get_name(w, info->component));
54-
ecs_os_free(path);
28+
ecs_abort(ECS_INVALID_OPERATION, "invalid copy construct");
5529
}
5630

57-
inline void ecs_move_ctor_illegal(ecs_world_t* w,
58-
const ecs_entity_t*, const ecs_entity_t*,
59-
void *, void *, int32_t, const ecs_type_info_t *info)
31+
inline void ecs_move_ctor_illegal(
32+
void *, void *, int32_t, const ecs_type_info_t *)
6033
{
61-
char *path = ecs_get_path_w_sep(w, 0, info->component, "::", "::");
62-
ecs_abort(ECS_INVALID_OPERATION,
63-
"cannnot move construct %s, add %s::%s(%s&&)",
64-
path, path, ecs_get_name(w, info->component), ecs_get_name(w, info->component));
65-
ecs_os_free(path);
34+
ecs_abort(ECS_INVALID_OPERATION, "invalid move construct");
6635
}
6736

6837

6938
// T()
7039
// Can't coexist with T(flecs::entity) or T(flecs::world, flecs::entity)
7140
template <typename T>
72-
void ctor_impl(
73-
ecs_world_t*, const ecs_entity_t*, void *ptr, int32_t count,
74-
const ecs_type_info_t *info)
75-
{
41+
void ctor_impl(void *ptr, int32_t count, const ecs_type_info_t *info) {
7642
(void)info; ecs_assert(info->size == ECS_SIZEOF(T),
7743
ECS_INTERNAL_ERROR, NULL);
7844
T *arr = static_cast<T*>(ptr);
@@ -81,18 +47,9 @@ void ctor_impl(
8147
}
8248
}
8349

84-
// T(flecs::world, flecs::entity)
85-
template <typename T>
86-
void ctor_world_entity_impl(
87-
ecs_world_t* world, const ecs_entity_t* ids, void *ptr, int32_t count,
88-
const ecs_type_info_t *info);
89-
9050
// ~T()
9151
template <typename T>
92-
void dtor_impl(
93-
ecs_world_t*, const ecs_entity_t*, void *ptr, int32_t count,
94-
const ecs_type_info_t *info)
95-
{
52+
void dtor_impl(void *ptr, int32_t count, const ecs_type_info_t *info) {
9653
(void)info; ecs_assert(info->size == ECS_SIZEOF(T),
9754
ECS_INTERNAL_ERROR, NULL);
9855
T *arr = static_cast<T*>(ptr);
@@ -103,9 +60,8 @@ void dtor_impl(
10360

10461
// T& operator=(const T&)
10562
template <typename T>
106-
void copy_impl(
107-
ecs_world_t*, const ecs_entity_t*, const ecs_entity_t*, void *dst_ptr,
108-
const void *src_ptr, int32_t count, const ecs_type_info_t *info)
63+
void copy_impl(void *dst_ptr, const void *src_ptr, int32_t count,
64+
const ecs_type_info_t *info)
10965
{
11066
(void)info; ecs_assert(info->size == ECS_SIZEOF(T),
11167
ECS_INTERNAL_ERROR, NULL);
@@ -118,9 +74,8 @@ void copy_impl(
11874

11975
// T& operator=(T&&)
12076
template <typename T>
121-
void move_impl(
122-
ecs_world_t*, const ecs_entity_t*, const ecs_entity_t*, void *dst_ptr,
123-
void *src_ptr, int32_t count, const ecs_type_info_t *info)
77+
void move_impl(void *dst_ptr, void *src_ptr, int32_t count,
78+
const ecs_type_info_t *info)
12479
{
12580
(void)info; ecs_assert(info->size == ECS_SIZEOF(T),
12681
ECS_INTERNAL_ERROR, NULL);
@@ -133,9 +88,8 @@ void move_impl(
13388

13489
// T(T&)
13590
template <typename T>
136-
void copy_ctor_impl(
137-
ecs_world_t*, const ecs_entity_t*, const ecs_entity_t*, void *dst_ptr,
138-
const void *src_ptr, int32_t count, const ecs_type_info_t *info)
91+
void copy_ctor_impl(void *dst_ptr, const void *src_ptr, int32_t count,
92+
const ecs_type_info_t *info)
13993
{
14094
(void)info; ecs_assert(info->size == ECS_SIZEOF(T),
14195
ECS_INTERNAL_ERROR, NULL);
@@ -148,9 +102,8 @@ void copy_ctor_impl(
148102

149103
// T(T&&)
150104
template <typename T>
151-
void move_ctor_impl(
152-
ecs_world_t*, const ecs_entity_t*, const ecs_entity_t*, void *dst_ptr,
153-
void *src_ptr, int32_t count, const ecs_type_info_t *info)
105+
void move_ctor_impl(void *dst_ptr, void *src_ptr, int32_t count,
106+
const ecs_type_info_t *info)
154107
{
155108
(void)info; ecs_assert(info->size == ECS_SIZEOF(T),
156109
ECS_INTERNAL_ERROR, NULL);
@@ -164,9 +117,8 @@ void move_ctor_impl(
164117
// T(T&&), ~T()
165118
// Typically used when moving to a new table, and removing from the old table
166119
template <typename T>
167-
void ctor_move_dtor_impl(
168-
ecs_world_t*, const ecs_entity_t*, const ecs_entity_t*, void *dst_ptr,
169-
void *src_ptr, int32_t count, const ecs_type_info_t *info)
120+
void ctor_move_dtor_impl(void *dst_ptr, void *src_ptr, int32_t count,
121+
const ecs_type_info_t *info)
170122
{
171123
(void)info; ecs_assert(info->size == ECS_SIZEOF(T),
172124
ECS_INTERNAL_ERROR, NULL);
@@ -182,9 +134,8 @@ void ctor_move_dtor_impl(
182134
// Typically used when moving a component to a deleted component
183135
template <typename T, if_not_t<
184136
std::is_trivially_move_assignable<T>::value > = 0>
185-
void move_dtor_impl(
186-
ecs_world_t*, const ecs_entity_t*, const ecs_entity_t*, void *dst_ptr,
187-
void *src_ptr, int32_t count, const ecs_type_info_t *info)
137+
void move_dtor_impl(void *dst_ptr, void *src_ptr, int32_t count,
138+
const ecs_type_info_t *info)
188139
{
189140
(void)info; ecs_assert(info->size == ECS_SIZEOF(T),
190141
ECS_INTERNAL_ERROR, NULL);
@@ -203,9 +154,8 @@ void move_dtor_impl(
203154
// Typically used when moving a component to a deleted component
204155
template <typename T, if_t<
205156
std::is_trivially_move_assignable<T>::value > = 0>
206-
void move_dtor_impl(
207-
ecs_world_t*, const ecs_entity_t*, const ecs_entity_t*, void *dst_ptr,
208-
void *src_ptr, int32_t count, const ecs_type_info_t *info)
157+
void move_dtor_impl(void *dst_ptr, void *src_ptr, int32_t count,
158+
const ecs_type_info_t *info)
209159
{
210160
(void)info; ecs_assert(info->size == ECS_SIZEOF(T),
211161
ECS_INTERNAL_ERROR, NULL);
@@ -224,29 +174,11 @@ void move_dtor_impl(
224174

225175
} // _
226176

227-
// Trait to test if type has flecs constructor
228-
template <typename T>
229-
struct has_flecs_ctor {
230-
static constexpr bool value =
231-
std::is_constructible<actual_type_t<T>,
232-
flecs::world&, flecs::entity>::value;
233-
};
234-
235177
// Trait to test if type is constructible by flecs
236178
template <typename T>
237179
struct is_flecs_constructible {
238180
static constexpr bool value =
239-
std::is_default_constructible<actual_type_t<T>>::value ||
240-
std::is_constructible<actual_type_t<T>,
241-
flecs::world&, flecs::entity>::value;
242-
};
243-
244-
// Trait to test if type has a self constructor (flecs::entity, Args...)
245-
template <typename T, typename ... Args>
246-
struct is_self_constructible {
247-
static constexpr bool value =
248-
std::is_constructible<actual_type_t<T>,
249-
flecs::entity, Args...>::value;
181+
std::is_default_constructible<actual_type_t<T>>::value;
250182
};
251183

252184
namespace _
@@ -260,27 +192,19 @@ ecs_xtor_t ctor() {
260192

261193
// Not constructible by flecs
262194
template <typename T, if_t<
263-
! std::is_default_constructible<T>::value &&
264-
! has_flecs_ctor<T>::value > = 0>
195+
! std::is_default_constructible<T>::value > = 0>
265196
ecs_xtor_t ctor() {
266197
return ecs_ctor_illegal;
267198
}
268199

269200
// Default constructible
270201
template <typename T, if_t<
271202
! std::is_trivially_constructible<T>::value &&
272-
std::is_default_constructible<T>::value &&
273-
! has_flecs_ctor<T>::value > = 0>
203+
std::is_default_constructible<T>::value > = 0>
274204
ecs_xtor_t ctor() {
275205
return ctor_impl<T>;
276206
}
277207

278-
// Flecs constructible: T(flecs::world, flecs::entity)
279-
template <typename T, if_t< has_flecs_ctor<T>::value > = 0>
280-
ecs_xtor_t ctor() {
281-
return ctor_world_entity_impl<T>;
282-
}
283-
284208
// No dtor
285209
template <typename T, if_t< std::is_trivially_destructible<T>::value > = 0>
286210
ecs_xtor_t dtor() {

include/flecs/addons/flecs_c.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,8 @@
402402
#define ecs_copy(type) type##_copy
403403
#define ecs_move(type) type##_move
404404
#define ecs_on_set(type) type##_on_set
405+
#define ecs_on_add(type) type##_on_add
406+
#define ecs_on_remove(type) type##_on_remove
405407

406408
#define ecs_query_new(world, q_expr)\
407409
ecs_query_init(world, &(ecs_query_desc_t){\

0 commit comments

Comments
 (0)