]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/lib/Registry.qh
Expand /lib
[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 #define REGISTER(initfunc, ns, array, counter, id, fld, inst)   \
10     entity ns##_##id;                                           \
11     REGISTER_INIT(ns, id) { }                                   \
12     REGISTER_INIT_POST(ns, id) { }                              \
13     .entity enemy; /* internal next pointer */                  \
14     void Register_##ns##_##id() {                               \
15         entity this = inst;                                     \
16         ns##_##id = this;                                       \
17         this.fld = counter;                                     \
18         array[counter++] = this;                                \
19         if (!array##_first)    array##_first = this;            \
20         if ( array##_last)     array##_last.enemy = this;       \
21         array##_last = this;                                    \
22         Register_##ns##_##id##_init(this);                      \
23         Register_##ns##_##id##_init_post(this);                 \
24     }                                                           \
25     ACCUMULATE_FUNCTION(initfunc, Register_##ns##_##id)         \
26     REGISTER_INIT(ns, id)
27
28 void __static_init() { }
29 #define static_init() CALL_ACCUMULATED_FUNCTION(__static_init)
30 #define REGISTER_REGISTRY(func) ACCUMULATE_FUNCTION(__static_init, func)
31
32 #define STATIC_INIT(func) \
33     void _static_##func(); \
34     ACCUMULATE_FUNCTION(__static_init, _static_##func) \
35     void _static_##func()
36
37 #endif