forked from SanderMertens/flecs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoly.h
98 lines (75 loc) · 2.32 KB
/
poly.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/**
* @file poly.h
* @brief Functions for managing poly objects.
*/
#ifndef FLECS_POLY_H
#define FLECS_POLY_H
#include <stddef.h>
/* Initialize poly */
void* _ecs_poly_init(
ecs_poly_t *object,
int32_t kind,
ecs_size_t size,
ecs_mixins_t *mixins);
#define ecs_poly_init(object, type)\
_ecs_poly_init(object, type##_magic, sizeof(type), &type##_mixins)
/* Deinitialize object for specified type */
void _ecs_poly_fini(
ecs_poly_t *object,
int32_t kind);
#define ecs_poly_fini(object, type)\
_ecs_poly_fini(object, type##_magic)
/* Utility functions for creating an object on the heap */
#define ecs_poly_new(type)\
(type*)ecs_poly_init(ecs_os_calloc_t(type), type)
#define ecs_poly_free(obj, type)\
ecs_poly_fini(obj, type);\
ecs_os_free(obj)
/* Get or create poly component for an entity */
EcsPoly* _ecs_poly_bind(
ecs_world_t *world,
ecs_entity_t entity,
ecs_entity_t tag);
#define ecs_poly_bind(world, entity, T) \
_ecs_poly_bind(world, entity, T##_tag)
void _ecs_poly_modified(
ecs_world_t *world,
ecs_entity_t entity,
ecs_entity_t tag);
#define ecs_poly_modified(world, entity, T) \
_ecs_poly_modified(world, entity, T##_tag)
/* Get poly component for an entity */
const EcsPoly* _ecs_poly_bind_get(
const ecs_world_t *world,
ecs_entity_t entity,
ecs_entity_t tag);
#define ecs_poly_bind_get(world, entity, T) \
_ecs_poly_bind_get(world, entity, T##_tag)
ecs_poly_t* _ecs_poly_get(
const ecs_world_t *world,
ecs_entity_t entity,
ecs_entity_t tag);
#define ecs_poly_get(world, entity, T) \
((T*)_ecs_poly_get(world, entity, T##_tag))
/* Utilities for testing/asserting an object type */
#ifndef FLECS_NDEBUG
void* _ecs_poly_assert(
const ecs_poly_t *object,
int32_t type,
const char *file,
int32_t line);
#define ecs_poly_assert(object, type)\
_ecs_poly_assert(object, type##_magic, __FILE__, __LINE__)
#define ecs_poly(object, T) ((T*)ecs_poly_assert(object, T))
#else
#define ecs_poly_assert(object, type)
#define ecs_poly(object, T) ((T*)object)
#endif
/* Utility functions for getting a mixin from an object */
ecs_iterable_t* ecs_get_iterable(
const ecs_poly_t *poly);
ecs_observable_t* ecs_get_observable(
const ecs_poly_t *object);
ecs_poly_dtor_t* ecs_get_dtor(
const ecs_poly_t *poly);
#endif