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) {
31 * Callback chains contain zero or more callbacks.
33 CLASS(CallbackChain, Object)
34 CLASS(CallbackNode, Object)
35 ATTRIB(CallbackNode, cbc, Callback, NULL)
36 ATTRIB(CallbackNode, cbc_next, CallbackNode, NULL)
37 ATTRIB(CallbackNode, cbc_order, int, 0)
38 CONSTRUCTOR(CallbackNode, Callback it, int order) {
39 CONSTRUCT(CallbackNode);
41 this.cbc_order = order;
43 ENDCLASS(CallbackNode)
45 ATTRIB(CallbackChain, cbc_next, CallbackNode, NULL)
46 ATTRIB(CallbackChain, cbc_order, int, 0)
47 CONSTRUCTOR(CallbackChain, string _name) {
48 CONSTRUCT(CallbackChain);
52 bool CallbackChain_Add(CallbackChain this, Callback cb, int order)
54 if (order & CBC_ORDER_FIRST) {
55 if (order & CBC_ORDER_LAST)
56 if (this.cbc_order & CBC_ORDER_ANY)
58 if (this.cbc_order & CBC_ORDER_FIRST)
60 } else if (order & CBC_ORDER_LAST) {
61 if (this.cbc_order & CBC_ORDER_LAST)
64 entity node = NEW(CallbackNode, cb, order);
65 if (order & CBC_ORDER_FIRST) {
66 node.cbc_next = this.cbc_next;
68 } else if (order & CBC_ORDER_LAST) {
69 CallbackNode prev = NULL, it = this.cbc_next;
70 while (it) { prev = it, it = it.cbc_next; }
71 if (prev) prev.cbc_next = node;
72 else this.cbc_next = node;
74 // by default we execute last, but before a possible CBC_ORDER_LAST callback
75 CallbackNode prev = NULL, it = this.cbc_next;
76 while (it && !(it.cbc_order & CBC_ORDER_LAST)) { prev = it, it = it.cbc_next; }
78 if (prev) prev.cbc_next = node;
79 else this.cbc_next = node;
81 this.cbc_order |= (order | CBC_ORDER_ANY);
84 int CallbackChain_Remove(CallbackChain this, Callback cb)
87 for (Callback prev = NULL, it = this.cbc_next; it; prev = it, it = it.cbc_next) {
89 // remove it from the chain
90 Callback next = it.cbc_next;
91 if (prev) prev.cbc_next = next;
92 else this.cbc_next = next;
95 // it is now something we want to keep
96 order |= (it.cbc_order & CBC_ORDER_ANY);
98 this.cbc_order = order;
101 bool CallbackChain_Call(CallbackChain this)
104 for (Callback it = this.cbc_next; it; it = it.cbc_next) {
105 CallbackChain_ReturnValue = r;
106 r |= it.cbc.cbc_func();
108 return r; // callbacks return an error status, so 0 is default return value
110 ENDCLASS(CallbackChain)
112 #define _MUTATOR_HANDLE_NOP(type, id)
113 #define _MUTATOR_HANDLE_PARAMS(type, id) , type in_##id
114 #define _MUTATOR_HANDLE_PREPARE(type, id) id = in_##id;
115 #define _MUTATOR_HANDLE_PUSHTMP(type, id) type tmp_##id = id;
116 #define _MUTATOR_HANDLE_PUSHOUT(type, id) type out_##id = id;
117 #define _MUTATOR_HANDLE_POPTMP(type, id) id = tmp_##id;
118 #define _MUTATOR_HANDLE_POPOUT(type, id) id = out_##id;
120 void RegisterHooks() {};
121 void RegisterCallbacks() {};
122 void RegisterMutators() {};
124 #define _MUTATOR_HOOKABLE(id, ...) CallbackChain HOOK_##id; bool __Mutator_Send_##id(__VA_ARGS__)
125 #define MUTATOR_HOOKABLE(id, params) \
126 _MUTATOR_HOOKABLE(id, int params(_MUTATOR_HANDLE_PARAMS, _MUTATOR_HANDLE_NOP)) { \
127 params(_MUTATOR_HANDLE_PUSHTMP, _MUTATOR_HANDLE_NOP) \
128 params(_MUTATOR_HANDLE_PREPARE, _MUTATOR_HANDLE_NOP) \
129 bool ret = CallbackChain_Call(HOOK_##id); \
130 params(_MUTATOR_HANDLE_NOP, _MUTATOR_HANDLE_PUSHOUT) \
131 params(_MUTATOR_HANDLE_POPTMP, _MUTATOR_HANDLE_NOP) \
132 params(_MUTATOR_HANDLE_NOP, _MUTATOR_HANDLE_POPOUT) \
135 [[accumulate]] void RegisterHooks() { HOOK_##id = NEW(CallbackChain, #id); }
136 #define MUTATOR_CALLHOOK(id, ...) APPLY(__Mutator_Send_##id, 0, ##__VA_ARGS__)
144 typedef bool(int) mutatorfunc_t;
146 CLASS(Mutator, Object)
147 ATTRIB(Mutator, m_id, int, 0)
148 ATTRIB(Mutator, mutatorname, string, string_null)
149 ATTRIB(Mutator, mutatorfunc, mutatorfunc_t, func_null)
150 ATTRIB(Mutator, mutatorcheck, bool(), func_null)
151 CONSTRUCTOR(Mutator, string _name, mutatorfunc_t func) {
153 this.mutatorname = _name;
154 this.mutatorfunc = func;
158 const int MAX_MUTATORS = 15;
159 Mutator loaded_mutators[MAX_MUTATORS];
161 bool Mutator_Add(Mutator mut)
164 for (int i = 0; i < MAX_MUTATORS; ++i) {
165 if (loaded_mutators[i] == mut)
166 return true; // already added
167 if (!(loaded_mutators[i]))
171 backtrace("WARNING: too many mutators, cannot add any more\n");
174 loaded_mutators[j] = mut;
175 mutatorfunc_t func = mut.mutatorfunc;
176 if (!func(MUTATOR_ADDING)) {
180 backtrace("WARNING: when adding mutator: adding failed, rolling back\n");
181 if (func(MUTATOR_ROLLING_BACK)) {
183 error("WARNING: when adding mutator: rolling back failed");
188 void Mutator_Remove(Mutator mut)
191 for (i = 0; i < MAX_MUTATORS; ++i)
192 if (loaded_mutators[i] == mut)
194 if (i >= MAX_MUTATORS) {
195 backtrace("WARNING: removing not-added mutator\n");
198 loaded_mutators[i] = NULL;
199 mutatorfunc_t func = mut.mutatorfunc;
200 if (func(MUTATOR_REMOVING)) {
202 error("Mutator_Remove: removing mutator failed");
206 #define MUTATOR_DECLARATION(name) \
207 Mutator MUTATOR_##name
208 #define MUTATOR_DEFINITION(name) \
209 bool MUTATORFUNCTION_##name(int mode); \
210 [[accumulate]] void RegisterMutators() { MUTATOR_##name = NEW(Mutator, #name, MUTATORFUNCTION_##name); } \
211 [[last]] bool MUTATORFUNCTION_##name(int mode)
213 const int MUTATORS_MAX = MAX_MUTATORS;
214 noref entity MUTATORS[MUTATORS_MAX], MUTATORS_first, MUTATORS_last;
215 noref int MUTATORS_COUNT;
216 #define REGISTER_MUTATOR(id, dependence) \
217 bool MUTATORFUNCTION_##id##_hooks(int mode) { return = false; } \
218 bool MUTATORFUNCTION_##id(int mode) { \
220 bool ret = MUTATORFUNCTION_##id##_hooks(mode); if (ret) return ret; \
222 bool MUTATOR_##id##_check() { return dependence; } \
223 REGISTER(RegisterMutators, MUTATOR, MUTATORS, MUTATORS_COUNT, id, m_id, NEW(Mutator, #id, MUTATORFUNCTION_##id)) \
224 { this.mutatorcheck = MUTATOR_##id##_check; } \
225 [[accumulate]] bool MUTATORFUNCTION_##id(int mode)
227 STATIC_INIT(Mutators) {
233 STATIC_INIT_LATE(Mutators) {
234 FOREACH(MUTATORS, it.mutatorcheck(), LAMBDA(Mutator_Add(it)));
237 #define MUTATOR_ONADD if (mode == MUTATOR_ADDING)
238 #define MUTATOR_ONREMOVE if (mode == MUTATOR_REMOVING)
239 #define MUTATOR_ONROLLBACK_OR_REMOVE if (mode == MUTATOR_REMOVING || mode == MUTATOR_ROLLING_BACK)
240 #define MUTATOR_ADD(name) Mutator_Add(MUTATOR_##name)
241 #define MUTATOR_REMOVE(name) Mutator_Remove(MUTATOR_##name)
242 #define MUTATOR_RETURNVALUE CallbackChain_ReturnValue
244 #define _MUTATOR_CALLBACK(name, func) \
245 Callback CALLBACK_##name; \
247 [[accumulate]] void RegisterCallbacks() { CALLBACK_##name = NEW(Callback, func); }
249 #define MUTATOR_HOOKFUNCTION(...) \
250 OVERLOAD(MUTATOR_HOOKFUNCTION, __VA_ARGS__)
252 #define MUTATOR_HOOKFUNCTION_1(name) \
253 _MUTATOR_CALLBACK(name, HOOKFUNCTION_##name) \
254 bool HOOKFUNCTION_##name()
256 #define MUTATOR_HOOKFUNCTION_2(mut, cb) \
257 MUTATOR_HOOKFUNCTION(mut, cb, CBC_ORDER_ANY)
259 #define MUTATOR_HOOKFUNCTION_3(mut, cb, order) \
260 _MUTATOR_CALLBACK(mut##_##cb, mut##_##cb) \
261 [[accumulate]] bool MUTATORFUNCTION_##mut##_hooks(int mode) { MUTATOR_HOOK(cb, mut##_##cb, order); } \
262 bool mut##_##cb() { return = false; } \
263 [[accumulate]] bool mut##_##cb()
265 #define MUTATOR_HOOK(cb, func, order) do { \
267 if (!CallbackChain_Add(HOOK_##cb, CALLBACK_##func, order)) { \
268 LOG_INFO("HOOK FAILED: ", #cb, ":", #func, "\n"); \
272 MUTATOR_ONROLLBACK_OR_REMOVE { \
273 CallbackChain_Remove(HOOK_##cb, CALLBACK_##func); \