6 #define _R_MAP(r, max) \
7 ArrayList r; STATIC_INIT(r) \
9 AL_NEW(r, max, NULL, e); \
11 #define _R_GET(r, i) AL_gete(r, i)
12 #define _R_SET(r, i, e) AL_sete(r, i, e)
13 #define _R_DEL(r) AL_DELETE(r)
15 #define _R_MAP(r, max) entity r[max]
16 #define _R_GET(r, i) r[i]
17 #define _R_SET(r, i, e) r[i] = e
22 * Declare a new registry.
24 * Don't forget to call `REGISTER_REGISTRY`:
25 * REGISTER_REGISTRY(Foos)
27 #define REGISTRY(id, max) \
28 void Register##id(); \
29 [[accumulate]] void REGISTRY_DEPENDS_(id) {} \
30 [[accumulate]] REGISTRY_BEGIN(id) {} \
31 [[accumulate]] REGISTRY_END(id) {} \
32 void _Register##id() {} \
39 id##_state = 1; REGISTRY_DEPENDS_(id); REGISTRY_BEGIN_(id); _Register##id(); id##_state = 2; REGISTRY_END_(id); \
41 const int id##_MAX = max; \
43 noref entity id##_first, id##_last; \
44 _R_MAP(_##id, id##_MAX); \
49 entity _##id##_from(int i, entity null) \
51 if (i >= 0 && i < id##_COUNT) { \
52 entity e = _R_GET(_##id, i); if (e) { \
59 /** Add registry dependencies to a registry */
60 #define REGISTRY_DEPENDS(id, dep) \
61 void Register##dep(); void REGISTRY_DEPENDS_(id) \
65 #define REGISTRY_DEPENDS_(id) Register##id##_Depends()
67 /** Called before initializing a registry. */
68 #define REGISTRY_BEGIN(id) \
69 [[accumulate]] void REGISTRY_BEGIN_(id) \
71 noref void() f = Register##id; \
72 } void REGISTRY_BEGIN_(id)
73 #define REGISTRY_BEGIN_(id) Register##id##_First()
75 /** Called after initializing a registry. */
76 #define REGISTRY_END(id) \
77 [[accumulate]] void REGISTRY_END_(id) \
79 noref void() f = Register##id; \
80 } void REGISTRY_END_(id)
81 #define REGISTRY_END_(id) Register##id##_Done()
83 REGISTRY(Registries, BITS(8))
85 /** registered item identifier */
86 .string registered_id;
89 * Register a new entity with a registry.
90 * Must be followed by a semicolon or a function body with a `this` parameter.
91 * Wrapper macros may perform actions after user initialization like so:
92 * #define REGISTER_FOO(id) \
93 * REGISTER(Foos, FOO, id, m_id, NEW(Foo)) { \
94 * print("Registering foo #", this.m_id + 1, "\n"); \
96 * REGISTER_INIT(FOO, id)
99 * @param registry The registry to add each entity to.
100 * @param ns Short for namespace, prefix for each global (ns##_##id)
101 * @param id The identifier of the current entity being registered
102 * @param fld The field to store the locally unique unique entity id
103 * @param inst An expression to create a new instance, invoked for every registration
105 #define REGISTER(...) EVAL_REGISTER(OVERLOAD_(REGISTER, __VA_ARGS__))
106 #define EVAL_REGISTER(...) __VA_ARGS__
107 #define REGISTER_5(registry, ns, id, fld, inst) REGISTER_4(registry, ns##_##id, fld, inst)
108 #define REGISTER_4(registry, id, fld, inst) \
110 REGISTER_INIT(id) {} \
111 void Register_##id() \
114 if (this == NULL) { \
115 if (registry##_COUNT >= registry##_MAX) { LOG_FATALF("Registry capacity exceeded (%d)", registry##_MAX); } \
117 this.registered_id = #id; \
118 REGISTRY_PUSH(registry, fld, this); \
120 Register_##id##_init(this); \
122 ACCUMULATE_FUNCTION(_Register##registry, Register_##id) \
125 #define REGISTRY_PUSH(registry, fld, it) \
127 it.fld = registry##_COUNT; \
128 _R_SET(_##registry, registry##_COUNT, it); \
129 ++registry##_COUNT; \
130 if (!registry##_first) { registry##_first = it; } \
131 if (registry##_last) { registry##_last.REGISTRY_NEXT = it; } \
132 registry##_last = it; \
135 #define REGISTRY_RESERVE(registry, fld, id, suffix) \
137 entity e = new_pure(registry_reserved); \
138 e.registered_id = #id "/" #suffix; \
139 REGISTRY_PUSH(registry, fld, e); \
142 #define REGISTER_INIT(id) [[accumulate]] void Register_##id##_init(entity this)
144 /** internal next pointer */
145 #define REGISTRY_NEXT enemy
146 .entity REGISTRY_NEXT;
148 #define REGISTRY_SORT(...) EVAL_REGISTRY_SORT(OVERLOAD(REGISTRY_SORT, __VA_ARGS__))
149 #define EVAL_REGISTRY_SORT(...) __VA_ARGS__
150 #define REGISTRY_SORT_1(id) REGISTRY_SORT_2(id, 0)
151 #define REGISTRY_SORT_2(id, skip) \
152 void _REGISTRY_SWAP_##id(int i, int j, entity pass) \
154 i += skip; j += skip; \
156 entity a = _R_GET(_##id, i), b = _R_GET(_##id, j); \
157 _R_SET(_##id, i, b); \
158 _R_SET(_##id, j, a); \
160 entity a_next = a.REGISTRY_NEXT, b_next = b.REGISTRY_NEXT; \
161 a.REGISTRY_NEXT = b_next; \
162 b.REGISTRY_NEXT = a_next; \
164 if (i == 0) { id##_first = b; } else { _R_GET(_##id, i - 1).REGISTRY_NEXT = b; } \
166 if (j == 0) { id##_first = a; } else { _R_GET(_##id, j - 1).REGISTRY_NEXT = a; } \
168 int _REGISTRY_CMP_##id(int i, int j, entity pass) \
170 i += skip; j += skip; \
171 string a = _R_GET(_##id, i).registered_id; \
172 string b = _R_GET(_##id, j).registered_id; \
173 return strcmp(a, b); \
175 STATIC_INIT(Registry_sort_##id) \
177 heapsort(id##_COUNT - (skip), _REGISTRY_SWAP_##id, _REGISTRY_CMP_##id, NULL); \
180 #define REGISTRY_HASH(id) Registry_hash_##id
183 [[accumulate]] void Registry_check(string r, string server) {}
185 [[accumulate]] void Registry_send_all() {}
188 void Registry_send(string id, string hash);
190 #define Registry_send(id, hash)
193 #define REGISTRY_CHECK(id) \
194 string REGISTRY_HASH(id); \
195 STATIC_INIT(Registry_check_##id) \
197 /* Note: SHA256 isn't always available, use MD4 instead */ \
198 string algo = "MD4"; \
201 FOREACH(id, true, s = strcat(s, join, it.registered_id)); \
202 s = substring(s, strlen(join), -1); \
203 string h = REGISTRY_HASH(id) = strzone(digest_hex(algo, s)); \
204 LOG_DEBUGF( #id ": %s\n[%s]", h, s); \
206 void Registry_check(string r, string sv) \
209 string cl = REGISTRY_HASH(id); \
211 LOG_FATALF("client/server mismatch (%s).\nCL: %s\nSV: %s", r, cl, sv); \
215 void Registry_send_all() \
217 Registry_send( #id, REGISTRY_HASH(id)); \
220 #define REGISTER_REGISTRY(...) EVAL_REGISTER_REGISTRY(OVERLOAD(REGISTER_REGISTRY, __VA_ARGS__))
221 #define EVAL_REGISTER_REGISTRY(...) __VA_ARGS__
222 #define REGISTER_REGISTRY_1(id) REGISTER_REGISTRY_2(id, #id)
223 #define REGISTER_REGISTRY_2(id, str) \
224 ACCUMULATE_FUNCTION(__static_init_1, Register##id) \
225 CLASS(id##Registry, Object) \
226 ATTRIB(id##Registry, m_name, string, str); \
227 ATTRIB(id##Registry, REGISTRY_NEXT, entity, id##_first); \
228 METHOD(id##Registry, m_reload, void()); \
229 ENDCLASS(id##Registry) \
230 REGISTER(Registries, REGISTRY, id, m_id, NEW(id##Registry)); \
231 METHOD(id##Registry, m_reload, void()) \