1 #ifndef MUTATORS_BASE_H
2 #define MUTATORS_BASE_H
4 const int CBC_ORDER_FIRST = 1;
5 const int CBC_ORDER_LAST = 2;
6 const int CBC_ORDER_EXCLUSIVE = 3;
7 const int CBC_ORDER_ANY = 4;
9 bool CallbackChain_ReturnValue; // read-only field of the current return value
12 * Callbacks may be added to zero or more callback chains.
14 CLASS(Callback, Object)
16 * a callback function is like this:
23 ATTRIB(Callback, cbc_func, bool(), func_null)
24 CONSTRUCTOR(Callback, bool() func) {
32 * Callback chains contain zero or more callbacks.
34 CLASS(CallbackChain, Object)
35 CLASS(CallbackNode, Object)
36 ATTRIB(CallbackNode, cbc, Callback, NULL)
37 ATTRIB(CallbackNode, cbc_next, CallbackNode, NULL)
38 ATTRIB(CallbackNode, cbc_order, int, 0)
39 CONSTRUCTOR(CallbackNode, Callback it, int order) {
40 CONSTRUCT(CallbackNode);
42 this.cbc_order = order;
45 ENDCLASS(CallbackNode)
47 ATTRIB(CallbackChain, cbc_next, CallbackNode, NULL)
48 ATTRIB(CallbackChain, cbc_order, int, 0)
49 CONSTRUCTOR(CallbackChain, string _name) {
50 CONSTRUCT(CallbackChain);
55 bool CallbackChain_Add(CallbackChain this, Callback cb, int order)
57 if (order & CBC_ORDER_FIRST) {
58 if (order & CBC_ORDER_LAST)
59 if (this.cbc_order & CBC_ORDER_ANY)
61 if (this.cbc_order & CBC_ORDER_FIRST)
63 } else if (order & CBC_ORDER_LAST) {
64 if (this.cbc_order & CBC_ORDER_LAST)
67 entity node = NEW(CallbackNode, cb, order);
68 if (order & CBC_ORDER_FIRST) {
69 node.cbc_next = this.cbc_next;
71 } else if (order & CBC_ORDER_LAST) {
72 CallbackNode prev = NULL, it = this.cbc_next;
73 while (it) { prev = it, it = it.cbc_next; }
74 if (prev) prev.cbc_next = node;
75 else this.cbc_next = node;
77 // by default we execute last, but before a possible CBC_ORDER_LAST callback
78 CallbackNode prev = NULL, it = this.cbc_next;
79 while (it && !(it.cbc_order & CBC_ORDER_LAST)) { prev = it, it = it.cbc_next; }
81 if (prev) prev.cbc_next = node;
82 else this.cbc_next = node;
84 this.cbc_order |= (order | CBC_ORDER_ANY);
87 int CallbackChain_Remove(CallbackChain this, Callback cb)
90 for (Callback prev = NULL, it = this.cbc_next; it; prev = it, it = it.cbc_next) {
92 // remove it from the chain
93 Callback next = it.cbc_next;
94 if (prev) prev.cbc_next = next;
95 else this.cbc_next = next;
98 // it is now something we want to keep
99 order |= (it.cbc_order & CBC_ORDER_ANY);
101 this.cbc_order = order;
104 bool CallbackChain_Call(CallbackChain this)
107 for (Callback it = this.cbc_next; it; it = it.cbc_next) {
108 CallbackChain_ReturnValue = r;
109 r |= it.cbc.cbc_func();
111 return r; // callbacks return an error status, so 0 is default return value
113 ENDCLASS(CallbackChain)
115 #define _MUTATOR_HANDLE_NOP(type, id)
116 #define _MUTATOR_HANDLE_PARAMS(type, id) , type in_##id
117 #define _MUTATOR_HANDLE_PREPARE(type, id) id = in_##id;
118 #define _MUTATOR_HANDLE_PUSHTMP(type, id) type tmp_##id = id;
119 #define _MUTATOR_HANDLE_PUSHOUT(type, id) type out_##id = id;
120 #define _MUTATOR_HANDLE_POPTMP(type, id) id = tmp_##id;
121 #define _MUTATOR_HANDLE_POPOUT(type, id) id = out_##id;
123 void RegisterHooks() {};
124 void RegisterCallbacks() {};
125 void RegisterMutators() {};
127 #define _MUTATOR_HOOKABLE(id, ...) CallbackChain HOOK_##id; bool __Mutator_Send_##id(__VA_ARGS__)
128 #define MUTATOR_HOOKABLE(id, params) \
129 _MUTATOR_HOOKABLE(id, int params(_MUTATOR_HANDLE_PARAMS, _MUTATOR_HANDLE_NOP)) { \
130 params(_MUTATOR_HANDLE_PUSHTMP, _MUTATOR_HANDLE_NOP) \
131 params(_MUTATOR_HANDLE_PREPARE, _MUTATOR_HANDLE_NOP) \
132 bool ret = CallbackChain_Call(HOOK_##id); \
133 params(_MUTATOR_HANDLE_NOP, _MUTATOR_HANDLE_PUSHOUT) \
134 params(_MUTATOR_HANDLE_POPTMP, _MUTATOR_HANDLE_NOP) \
135 params(_MUTATOR_HANDLE_NOP, _MUTATOR_HANDLE_POPOUT) \
138 [[accumulate]] void RegisterHooks() { HOOK_##id = NEW(CallbackChain, #id); }
139 #define MUTATOR_CALLHOOK(id, ...) APPLY(__Mutator_Send_##id, 0, ##__VA_ARGS__)
147 typedef bool(int) mutatorfunc_t;
149 CLASS(Mutator, Object)
150 ATTRIB(Mutator, m_id, int, 0)
151 ATTRIB(Mutator, mutatorname, string, string_null)
152 ATTRIB(Mutator, mutatorfunc, mutatorfunc_t, func_null)
153 ATTRIB(Mutator, mutatorcheck, bool(), func_null)
154 CONSTRUCTOR(Mutator, string _name, mutatorfunc_t func) {
156 this.mutatorname = _name;
157 this.mutatorfunc = func;
162 const int MAX_MUTATORS = 15;
163 Mutator loaded_mutators[MAX_MUTATORS];
165 bool Mutator_Add(Mutator mut)
168 for (int i = 0; i < MAX_MUTATORS; ++i) {
169 if (loaded_mutators[i] == mut)
170 return true; // already added
171 if (!(loaded_mutators[i]))
175 backtrace("WARNING: too many mutators, cannot add any more\n");
178 loaded_mutators[j] = mut;
179 mutatorfunc_t func = mut.mutatorfunc;
180 if (!func(MUTATOR_ADDING)) {
184 backtrace("WARNING: when adding mutator: adding failed, rolling back\n");
185 if (func(MUTATOR_ROLLING_BACK)) {
187 error("WARNING: when adding mutator: rolling back failed");
192 void Mutator_Remove(Mutator mut)
195 for (i = 0; i < MAX_MUTATORS; ++i)
196 if (loaded_mutators[i] == mut)
198 if (i >= MAX_MUTATORS) {
199 backtrace("WARNING: removing not-added mutator\n");
202 loaded_mutators[i] = NULL;
203 mutatorfunc_t func = mut.mutatorfunc;
204 if (func(MUTATOR_REMOVING)) {
206 error("Mutator_Remove: removing mutator failed");
210 #define MUTATOR_DECLARATION(name) \
211 Mutator MUTATOR_##name
212 #define MUTATOR_DEFINITION(name) \
213 bool MUTATORFUNCTION_##name(int mode); \
214 [[accumulate]] void RegisterMutators() { MUTATOR_##name = NEW(Mutator, #name, MUTATORFUNCTION_##name); } \
215 [[last]] bool MUTATORFUNCTION_##name(int mode)
217 const int MUTATORS_MAX = MAX_MUTATORS;
218 noref entity MUTATORS[MUTATORS_MAX], MUTATORS_first, MUTATORS_last;
219 noref int MUTATORS_COUNT;
220 #define REGISTER_MUTATOR(id, dependence) \
221 bool MUTATORFUNCTION_##id##_hooks(int mode) { return = false; } \
222 bool MUTATORFUNCTION_##id(int mode) { \
224 bool ret = MUTATORFUNCTION_##id##_hooks(mode); if (ret) return ret; \
226 bool MUTATOR_##id##_check() { return dependence; } \
227 REGISTER(RegisterMutators, MUTATOR, MUTATORS, MUTATORS_COUNT, id, m_id, NEW(Mutator, #id, MUTATORFUNCTION_##id)) \
228 { this.mutatorcheck = MUTATOR_##id##_check; } \
229 [[accumulate]] bool MUTATORFUNCTION_##id(int mode)
231 STATIC_INIT(Mutators) {
235 FOREACH(MUTATORS, it.mutatorcheck(), LAMBDA(Mutator_Add(it)));
238 #define MUTATOR_ONADD if (mode == MUTATOR_ADDING)
239 #define MUTATOR_ONREMOVE if (mode == MUTATOR_REMOVING)
240 #define MUTATOR_ONROLLBACK_OR_REMOVE if (mode == MUTATOR_REMOVING || mode == MUTATOR_ROLLING_BACK)
241 #define MUTATOR_ADD(name) Mutator_Add(MUTATOR_##name)
242 #define MUTATOR_REMOVE(name) Mutator_Remove(MUTATOR_##name)
243 #define MUTATOR_RETURNVALUE CallbackChain_ReturnValue
245 #define _MUTATOR_CALLBACK(name, func) \
246 Callback CALLBACK_##name; \
248 [[accumulate]] void RegisterCallbacks() { CALLBACK_##name = NEW(Callback, func); }
250 #define MUTATOR_HOOKFUNCTION(...) \
251 OVERLOAD(MUTATOR_HOOKFUNCTION, __VA_ARGS__)
253 #define MUTATOR_HOOKFUNCTION_1(name) \
254 _MUTATOR_CALLBACK(name, HOOKFUNCTION_##name) \
255 bool HOOKFUNCTION_##name()
257 #define MUTATOR_HOOKFUNCTION_2(mut, cb) \
258 MUTATOR_HOOKFUNCTION(mut, cb, CBC_ORDER_ANY)
260 #define MUTATOR_HOOKFUNCTION_3(mut, cb, order) \
261 _MUTATOR_CALLBACK(mut##_##cb, mut##_##cb) \
262 [[accumulate]] bool MUTATORFUNCTION_##mut##_hooks(int mode) { MUTATOR_HOOK(cb, mut##_##cb, order); } \
263 bool mut##_##cb() { return = false; } \
264 [[accumulate]] bool mut##_##cb()
266 #define MUTATOR_HOOK(cb, func, order) do { \
268 if (!CallbackChain_Add(HOOK_##cb, CALLBACK_##func, order)) { \
269 print("HOOK FAILED: ", #cb, ":", #func, "\n"); \
273 MUTATOR_ONROLLBACK_OR_REMOVE { \
274 CallbackChain_Remove(HOOK_##cb, CALLBACK_##func); \