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)
9 #define REGISTRY(id, max) \
10 void Register##id() {} \
11 const int id##_MAX = max; \
12 noref entity _##id[id##_MAX], id##_first, id##_last; \
14 entity _##id##_from(int i, entity null) { if (i >= 0 && i < id##_COUNT) { entity e = _##id[i]; if (e) return e; } return null; } \
17 /** registered item identifier */
18 .string registered_id;
21 * Register a new entity with a global constructor.
22 * Must be followed by a semicolon or a function body with a `this` parameter.
23 * Wrapper macros may perform actions after user initialization like so:
24 * #define REGISTER_FOO(id) \
25 * REGISTER(RegisterFoos, FOO, FOOS, id, m_id, NEW(Foo)); \
26 * REGISTER_INIT_POST(FOO, id) { \
27 * print("Registering foo #", this.m_id + 1, "\n"); \
29 * REGISTER_INIT(FOO, id)
31 * Don't forget to forward declare `initfunc` and call `REGISTER_REGISTRY`:
32 * void RegisterFoos();
33 * REGISTER_REGISTRY(RegisterFoos)
35 * @param initfunc The global constructor to accumulate into
36 * @param ns Short for namespace, prefix for each global (ns##_##id)
37 * @param array The array to add each entity to. Also requires `array##_first` and `array##_last` to be defined
38 * @param id The identifier of the current entity being registered
39 * @param fld The field to store the current count into
40 * @param inst An expression to create a new instance, invoked for every registration
42 #define REGISTER(initfunc, ns, array, id, fld, inst) \
44 REGISTER_INIT(ns, id) {} \
45 REGISTER_INIT_POST(ns, id) {} \
46 void Register_##ns##_##id() \
48 if (array##_COUNT >= array##_MAX) LOG_FATALF("Registry capacity exceeded (%s)", ftos(array##_MAX)); \
51 this.registered_id = #id; \
52 this.fld = array##_COUNT; \
53 _##array[array##_COUNT++] = this; \
54 if (!array##_first) array##_first = this; \
55 if (array##_last) array##_last.REGISTRY_NEXT = this; \
56 array##_last = this; \
57 Register_##ns##_##id##_init(this); \
58 Register_##ns##_##id##_init_post(this); \
60 ACCUMULATE_FUNCTION(initfunc, Register_##ns##_##id) \
63 /** internal next pointer */
64 #define REGISTRY_NEXT enemy
65 .entity REGISTRY_NEXT;
67 #define REGISTRY_SORT(id, field, skip) \
68 void _REGISTRY_SWAP_##id(int i, int j, entity pass) \
70 i += skip; j += skip; \
72 entity a = _##id[i], b = _##id[j]; \
76 entity a_next = a.REGISTRY_NEXT, b_next = b.REGISTRY_NEXT; \
77 a.REGISTRY_NEXT = b_next; \
78 b.REGISTRY_NEXT = a_next; \
80 if (i == 0) id##_first = b; \
81 else _##id[i - 1].REGISTRY_NEXT = b; \
83 if (j == 0) id##_first = a; \
84 else _##id[j - 1].REGISTRY_NEXT = a; \
86 float _REGISTRY_CMP_##id(int i, int j, entity pass) \
88 i += skip; j += skip; \
89 string a = _##id[i].field; \
90 string b = _##id[j].field; \
91 return strcasecmp(a, b); \
93 STATIC_INIT(Registry_sort_##id) \
95 heapsort(id##_COUNT - (skip), _REGISTRY_SWAP_##id, _REGISTRY_CMP_##id, NULL); \
98 #define REGISTRY_CHECK(id) \
99 STATIC_INIT_LATE(Registry_check_##id) \
101 string algo = "SHA256"; \
104 FOREACH(id, true, LAMBDA(s = strcat(s, join, it.registered_id))); \
105 s = substring(s, strlen(join), -1); \
106 LOG_TRACEF(#id ": %s\n[%s]\n", digest_hex(algo, s), s); \