]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/lib/registry.qh
Registry: Document it
[xonotic/xonotic-data.pk3dir.git] / qcsrc / lib / registry.qh
1 #ifndef REGISTRY_H
2 #define REGISTRY_H
3
4 #include "oo.qh"
5
6 #define REGISTER_INIT(ns, id) [[accumulate]] void Register_##ns##_##id##_init(entity this)
7 #define REGISTER_INIT_POST(ns, id) [[accumulate]] void Register_##ns##_##id##_init_post(entity this)
8
9 /**
10  * Register a new entity with a global constructor.
11  * Must be followed by a semicolon or a function body with a `this` parameter.
12  * Wrapper macros may perform actions after user initialization like so:
13  *     #define REGISTER_FOO(id) \
14  *         REGISTER(RegisterFoos, FOO, FOOS, FOOS_COUNT, id, m_id, NEW(Foo)); \
15  *         REGISTER_INIT_POST(FOO, id) { \
16  *             print("Registering foo #", this.m_id - 1, "\n"); \
17  *         } \
18  *         REGISTER_INIT(FOO, id)
19  *
20  * @param initfunc  The global constructor to accumulate into
21  * @param ns        Short for namespace, prefix for each global (ns##_##id)
22  * @param array     The array to add each entity to. Also requires `array##_first` and `array##_last` to be defined
23  * @param counter   Global to count number of entries
24  * @param id        The identifier of the current entity being registered
25  * @param fld       This field to store the current count into
26  * @param inst      An expression to create a new instance, invoked for every registration
27  */
28 #define REGISTER(initfunc, ns, array, counter, id, fld, inst)   \
29     entity ns##_##id;                                           \
30     REGISTER_INIT(ns, id) { }                                   \
31     REGISTER_INIT_POST(ns, id) { }                              \
32     .entity enemy; /* internal next pointer */                  \
33     void Register_##ns##_##id() {                               \
34         entity this = inst;                                     \
35         ns##_##id = this;                                       \
36         this.fld = counter;                                     \
37         array[counter++] = this;                                \
38         if (!array##_first)    array##_first = this;            \
39         if ( array##_last)     array##_last.enemy = this;       \
40         array##_last = this;                                    \
41         Register_##ns##_##id##_init(this);                      \
42         Register_##ns##_##id##_init_post(this);                 \
43     }                                                           \
44     ACCUMULATE_FUNCTION(initfunc, Register_##ns##_##id)         \
45     REGISTER_INIT(ns, id)
46
47 #endif