6 #define _R_MAP(r, max) ArrayList r; STATIC_INIT(r) { AL_NEW(r, max, NULL, e); }
7 #define _R_GET(r, i) AL_gete(r, i)
8 #define _R_SET(r, i, e) AL_sete(r, i, e)
9 #define _R_DEL(r) AL_DELETE(r)
11 #define _R_MAP(r, max) entity r[max]
12 #define _R_GET(r, i) r[i]
13 #define _R_SET(r, i, e) r[i] = e
18 * Declare a new registry.
20 * Don't forget to call `REGISTER_REGISTRY`:
21 * REGISTER_REGISTRY(Foos)
23 #define REGISTRY(id, max) \
24 void Register##id(); \
25 [[accumulate]] REGISTRY_BEGIN(id) {} \
26 [[accumulate]] REGISTRY_END(id) {} \
27 void _Register##id() {} \
28 void Register##id() { REGISTRY_BEGIN_(id); _Register##id(); REGISTRY_END_(id); } \
29 const int id##_MAX = max; \
31 noref entity id##_first, id##_last; \
32 _R_MAP(_##id, id##_MAX); \
33 SHUTDOWN(id) { _R_DEL(_##id); } \
34 entity _##id##_from(int i, entity null) { if (i >= 0 && i < id##_COUNT) { entity e = _R_GET(_##id, i); if (e) return e; } return null; }
36 /** Called before initializing a registry. */
37 #define REGISTRY_BEGIN(id) [[accumulate]] void REGISTRY_BEGIN_(id) { noref void() f = Register##id; } void REGISTRY_BEGIN_(id)
38 #define REGISTRY_BEGIN_(id) Register##id##_First()
40 /** Called after initializing a registry. */
41 #define REGISTRY_END(id) [[accumulate]] void REGISTRY_END_(id) { noref void() f = Register##id; } void REGISTRY_END_(id)
42 #define REGISTRY_END_(id) Register##id##_Done()
44 REGISTRY(Registries, BITS(8))
46 /** registered item identifier */
47 .string registered_id;
50 * Register a new entity with a registry.
51 * Must be followed by a semicolon or a function body with a `this` parameter.
52 * Wrapper macros may perform actions after user initialization like so:
53 * #define REGISTER_FOO(id) \
54 * REGISTER(Foos, FOO, id, m_id, NEW(Foo)) { \
55 * print("Registering foo #", this.m_id + 1, "\n"); \
57 * REGISTER_INIT(FOO, id)
60 * @param registry The registry to add each entity to.
61 * @param ns Short for namespace, prefix for each global (ns##_##id)
62 * @param id The identifier of the current entity being registered
63 * @param fld The field to store the locally unique unique entity id
64 * @param inst An expression to create a new instance, invoked for every registration
66 #define REGISTER(...) EVAL_REGISTER(OVERLOAD_(REGISTER, __VA_ARGS__))
67 #define EVAL_REGISTER(...) __VA_ARGS__
68 #define REGISTER_5(registry, ns, id, fld, inst) REGISTER_4(registry, ns##_##id, fld, inst)
69 #define REGISTER_4(registry, id, fld, inst) \
71 REGISTER_INIT(id) {} \
72 void Register_##id() \
76 if (registry##_COUNT >= registry##_MAX) LOG_FATALF("Registry capacity exceeded (%d)", registry##_MAX); \
78 this.registered_id = #id; \
79 REGISTRY_PUSH(registry, fld, this); \
81 Register_##id##_init(this); \
83 ACCUMULATE_FUNCTION(_Register##registry, Register_##id) \
86 #define REGISTRY_PUSH(registry, fld, it) MACRO_BEGIN { \
87 it.fld = registry##_COUNT; \
88 _R_SET(_##registry, registry##_COUNT, it); \
90 if (!registry##_first) registry##_first = it; \
91 if (registry##_last) registry##_last.REGISTRY_NEXT = it; \
92 registry##_last = it; \
95 #define REGISTRY_RESERVE(registry, fld, id, suffix) MACRO_BEGIN { \
96 entity e = new_pure(registry_reserved); \
97 e.registered_id = #id "/" #suffix; \
98 REGISTRY_PUSH(registry, fld, e); \
101 #define REGISTER_INIT(id) [[accumulate]] void Register_##id##_init(entity this)
103 /** internal next pointer */
104 #define REGISTRY_NEXT enemy
105 .entity REGISTRY_NEXT;
107 #define REGISTRY_SORT(...) EVAL_REGISTRY_SORT(OVERLOAD(REGISTRY_SORT, __VA_ARGS__))
108 #define EVAL_REGISTRY_SORT(...) __VA_ARGS__
109 #define REGISTRY_SORT_1(id) REGISTRY_SORT_2(id, 0)
110 #define REGISTRY_SORT_2(id, skip) \
111 void _REGISTRY_SWAP_##id(int i, int j, entity pass) \
113 i += skip; j += skip; \
115 entity a = _R_GET(_##id, i), b = _R_GET(_##id, j); \
116 _R_SET(_##id, i, b); \
117 _R_SET(_##id, j, a); \
119 entity a_next = a.REGISTRY_NEXT, b_next = b.REGISTRY_NEXT; \
120 a.REGISTRY_NEXT = b_next; \
121 b.REGISTRY_NEXT = a_next; \
123 if (i == 0) id##_first = b; \
124 else _R_GET(_##id, i - 1).REGISTRY_NEXT = b; \
126 if (j == 0) id##_first = a; \
127 else _R_GET(_##id, j - 1).REGISTRY_NEXT = a; \
129 int _REGISTRY_CMP_##id(int i, int j, entity pass) \
131 i += skip; j += skip; \
132 string a = _R_GET(_##id, i).registered_id; \
133 string b = _R_GET(_##id, j).registered_id; \
134 return strcmp(a, b); \
136 STATIC_INIT(Registry_sort_##id) \
138 heapsort(id##_COUNT - (skip), _REGISTRY_SWAP_##id, _REGISTRY_CMP_##id, NULL); \
141 #define REGISTRY_HASH(id) Registry_hash_##id
143 [[accumulate]] void Registry_check(string r, string server) { }
144 [[accumulate]] void Registry_send_all() { }
147 void Registry_send(string id, string hash);
149 #define Registry_send(id, hash)
152 #define REGISTRY_CHECK(id) \
153 string REGISTRY_HASH(id); \
154 STATIC_INIT(Registry_check_##id) \
156 /* Note: SHA256 isn't always available, use MD4 instead */ \
157 string algo = "MD4"; \
160 FOREACH(id, true, s = strcat(s, join, it.registered_id)); \
161 s = substring(s, strlen(join), -1); \
162 string h = REGISTRY_HASH(id) = strzone(digest_hex(algo, s)); \
163 LOG_DEBUGF(#id ": %s\n[%s]", h, s); \
165 void Registry_check(string r, string sv) \
169 string cl = REGISTRY_HASH(id); \
172 LOG_FATALF("client/server mismatch (%s).\nCL: %s\nSV: %s", r, cl, sv); \
176 void Registry_send_all() { Registry_send(#id, REGISTRY_HASH(id)); } \
178 #define REGISTER_REGISTRY(...) EVAL_REGISTER_REGISTRY(OVERLOAD(REGISTER_REGISTRY, __VA_ARGS__))
179 #define EVAL_REGISTER_REGISTRY(...) __VA_ARGS__
180 #define REGISTER_REGISTRY_1(id) REGISTER_REGISTRY_2(id, #id)
181 #define REGISTER_REGISTRY_2(id, str) \
182 ACCUMULATE_FUNCTION(__static_init, Register##id) \
183 CLASS(id##Registry, Object) \
184 ATTRIB(id##Registry, m_name, string, str); \
185 ATTRIB(id##Registry, REGISTRY_NEXT, entity, id##_first); \
186 METHOD(id##Registry, m_reload, void()); \
187 ENDCLASS(id##Registry) \
188 REGISTER(Registries, REGISTRY, id, m_id, NEW(id##Registry)); \
189 METHOD(id##Registry, m_reload, void()) { \