… | |
… | |
110 | #define ARG_CLIENT(o) DT_CLIENT, (void *)(static_cast<client *> (o)) |
110 | #define ARG_CLIENT(o) DT_CLIENT, (void *)(static_cast<client *> (o)) |
111 | #define ARG_PARTY(o) DT_PARTY , (void *)(static_cast<party *> (o)) |
111 | #define ARG_PARTY(o) DT_PARTY , (void *)(static_cast<party *> (o)) |
112 | #define ARG_REGION(o) DT_REGION, (void *)(static_cast<region *> (o)) |
112 | #define ARG_REGION(o) DT_REGION, (void *)(static_cast<region *> (o)) |
113 | |
113 | |
114 | // the ", ## __VA_ARGS" is, unfortunately, a gnu-cpp extension |
114 | // the ", ## __VA_ARGS" is, unfortunately, a gnu-cpp extension |
115 | |
115 | #define INVOKE(obj,event, ...) (expect_false ((obj)->should_invoke (event)) ? (obj)->invoke (event, ## __VA_ARGS__, DT_END) : 0) |
116 | // all these return true when the normal event processing should be skipped (if any) |
|
|
117 | #define INVOKE_GLOBAL(event, ...) gbl_ev.invoke (EVENT_ ## GLOBAL ## _ ## event, ## __VA_ARGS__, DT_END) |
116 | #define INVOKE_GLOBAL(event, ...) INVOKE (&gbl_ev, EVENT_ ## GLOBAL ## _ ## event, ## __VA_ARGS__) |
118 | #define INVOKE_ATTACHABLE(event, obj, ...) (obj)->invoke (EVENT_ ## ATTACHABLE ## _ ## event, ## __VA_ARGS__, DT_END) |
117 | #define INVOKE_ATTACHABLE(event, obj, ...) INVOKE (obj , EVENT_ ## ATTACHABLE ## _ ## event, ## __VA_ARGS__) |
119 | #define INVOKE_OBJECT(event, obj, ...) (obj)->invoke (EVENT_ ## OBJECT ## _ ## event, ## __VA_ARGS__, DT_END) |
118 | #define INVOKE_OBJECT(event, obj, ...) INVOKE (obj , EVENT_ ## OBJECT ## _ ## event, ## __VA_ARGS__) |
120 | #define INVOKE_CLIENT(event, obj, ...) (obj)->invoke (EVENT_ ## CLIENT ## _ ## event, ## __VA_ARGS__, DT_END) |
119 | #define INVOKE_CLIENT(event, obj, ...) INVOKE (obj , EVENT_ ## CLIENT ## _ ## event, ## __VA_ARGS__) |
121 | #define INVOKE_PLAYER(event, obj, ...) (obj)->invoke (EVENT_ ## PLAYER ## _ ## event, ## __VA_ARGS__, DT_END) |
120 | #define INVOKE_PLAYER(event, obj, ...) INVOKE (obj , EVENT_ ## PLAYER ## _ ## event, ## __VA_ARGS__) |
122 | #define INVOKE_MAP(event, obj, ...) (obj)->invoke (EVENT_ ## MAP ## _ ## event, ## __VA_ARGS__, DT_END) |
121 | #define INVOKE_MAP(event, obj, ...) INVOKE (obj , EVENT_ ## MAP ## _ ## event, ## __VA_ARGS__) |
123 | |
122 | |
124 | //TODO should index into @result |
123 | //TODO should index into @result |
125 | #define RESULT(idx,type) cfperl_result_ ## type (idx) |
124 | #define RESULT(idx,type) cfperl_result_ ## type (idx) |
126 | #define RESULT_DOUBLE(idx) RESULT(idx, DOUBLE) |
125 | #define RESULT_DOUBLE(idx) RESULT(idx, DOUBLE) |
127 | #define RESULT_INT(idx) RESULT(idx, INT) |
126 | #define RESULT_INT(idx) RESULT(idx, INT) |
… | |
… | |
190 | |
189 | |
191 | virtual ~attachable (); |
190 | virtual ~attachable (); |
192 | |
191 | |
193 | attachable &operator =(const attachable &src); |
192 | attachable &operator =(const attachable &src); |
194 | |
193 | |
195 | bool vinvoke (event_type event, va_list &ap); |
194 | // used to _quickly_ device wether to shortcut the evaluation |
|
|
195 | bool should_invoke (event_type event) |
|
|
196 | { |
|
|
197 | return ev_want_event [event] || cb; |
|
|
198 | } |
|
|
199 | |
196 | bool invoke (event_type event, ...) |
200 | bool invoke (event_type event, ...); |
197 | { |
|
|
198 | if (ev_want_event [event] || cb) |
|
|
199 | { |
|
|
200 | va_list ap; |
|
|
201 | va_start (ap, event); |
|
|
202 | vinvoke (event, ap); |
|
|
203 | va_end (ap); |
|
|
204 | } |
|
|
205 | } |
|
|
206 | |
201 | |
207 | MTH void instantiate (); |
202 | MTH void instantiate (); |
208 | void reattach (); |
203 | void reattach (); |
209 | |
204 | |
210 | protected: |
205 | protected: |
… | |
… | |
237 | // used only for user-defined key-value pairs |
232 | // used only for user-defined key-value pairs |
238 | void put (const shstr &k, const shstr &v) |
233 | void put (const shstr &k, const shstr &v) |
239 | { |
234 | { |
240 | add (k); |
235 | add (k); |
241 | |
236 | |
242 | if (v) |
237 | if (expect_true (v)) |
243 | add (' '), add (v); |
238 | add (' '), add (v); |
244 | |
239 | |
245 | add ('\n'); |
240 | add ('\n'); |
246 | } |
241 | } |
247 | |
242 | |
… | |
… | |
262 | |
257 | |
263 | char *p = (char *)alloc (klen + vlen + 1); |
258 | char *p = (char *)alloc (klen + vlen + 1); |
264 | |
259 | |
265 | memcpy (p, keyword_str [k], klen); p += klen; |
260 | memcpy (p, keyword_str [k], klen); p += klen; |
266 | |
261 | |
267 | if (v) |
262 | if (expect_true (v)) |
268 | { |
263 | { |
269 | *p++ = ' '; vlen--; |
264 | *p++ = ' '; vlen--; |
270 | memcpy (p, v, vlen); p += vlen; |
265 | memcpy (p, v, vlen); p += vlen; |
271 | } |
266 | } |
272 | |
267 | |
… | |
… | |
283 | put_string (k, v); |
278 | put_string (k, v); |
284 | } |
279 | } |
285 | |
280 | |
286 | void put (keyword k, double v) |
281 | void put (keyword k, double v) |
287 | { |
282 | { |
288 | char buf [128]; |
283 | force (32); |
289 | |
284 | ptr += sprintf (ptr, "%.7g", v); |
290 | snprintf (buf, 128, "%.7g", v); |
|
|
291 | |
|
|
292 | put (k, (const char *)buf); |
|
|
293 | } |
285 | } |
294 | |
286 | |
295 | void put_(keyword k, sint64 v) |
287 | void put_(keyword k, sint64 v) |
296 | { |
288 | { |
297 | add (keyword_str [k], keyword_len [k]); |
289 | add (keyword_str [k], keyword_len [k]); |
… | |
… | |
310 | |
302 | |
311 | void put (keyword kbeg, keyword kend, const shstr &v) |
303 | void put (keyword kbeg, keyword kend, const shstr &v) |
312 | { |
304 | { |
313 | add (keyword_str [kbeg], keyword_len [kbeg]); add ('\n'); |
305 | add (keyword_str [kbeg], keyword_len [kbeg]); add ('\n'); |
314 | |
306 | |
315 | if (v) |
307 | if (expect_true (v)) |
316 | { |
308 | { |
317 | add (v); |
309 | add (v); |
318 | add ('\n'); |
310 | add ('\n'); |
319 | } |
311 | } |
320 | |
312 | |
… | |
… | |
334 | void put (keyword k, unsigned long long v) { put_(k, (sint64)v); } |
326 | void put (keyword k, unsigned long long v) { put_(k, (sint64)v); } |
335 | |
327 | |
336 | template<typename T> |
328 | template<typename T> |
337 | void put (keyword k, const T *v) |
329 | void put (keyword k, const T *v) |
338 | { |
330 | { |
339 | if (v) |
331 | if (expect_true (v)) |
340 | put (k, v->name); |
332 | put (k, v->name); |
341 | else |
333 | else |
342 | put (k, (const char *)0); |
334 | put (k, (const char *)0); |
343 | } |
335 | } |
344 | |
336 | |
… | |
… | |
352 | char *as_string (); // like strdup |
344 | char *as_string (); // like strdup |
353 | |
345 | |
354 | operator bool () { return !!av; } |
346 | operator bool () { return !!av; } |
355 | }; |
347 | }; |
356 | |
348 | |
357 | // compatibility support, should be removed when no longer needed |
|
|
358 | void fprintf (object_freezer &freezer, const char *format, ...); |
|
|
359 | void fputs (const char *s, object_freezer &freezer); |
|
|
360 | |
|
|
361 | struct object_thawer |
349 | struct object_thawer |
362 | { |
350 | { |
|
|
351 | char *line; // current beginning of line |
363 | SV *text; // text part |
352 | SV *text; // text part |
364 | AV *av; // perl part |
353 | AV *av; // perl part |
365 | int linenum; |
354 | int linenum; |
366 | char *line; // current beginning of line |
|
|
367 | keyword kw; |
355 | keyword kw; |
368 | char *kw_str, *value; |
356 | char *kw_str; // the keyword parsed, as string |
|
|
357 | char *value; // the value, or 0 if no value |
|
|
358 | char *value_nn; // the value, or the empty string if no value |
369 | const char *name; |
359 | const char *name; |
370 | |
360 | |
371 | operator bool () { return !!text; } |
361 | operator bool () { return !!text; } |
372 | |
362 | |
373 | object_thawer (const char *path = 0); |
363 | object_thawer (const char *path = 0); |
… | |
… | |
388 | next (); |
378 | next (); |
389 | return kw; |
379 | return kw; |
390 | } |
380 | } |
391 | |
381 | |
392 | char *get_str () { return value; } // may be 0 |
382 | char *get_str () { return value; } // may be 0 |
|
|
383 | void get_ml (keyword kend, shstr &sh); |
393 | |
384 | |
394 | void get (shstr &sh) const; |
|
|
395 | void get_ornull (shstr &sh) const { sh = value; } |
385 | void get_ornull (shstr &sh) const { sh = value; } |
396 | void get_ml (keyword kend, shstr &sh); |
386 | void get (shstr &sh) const { sh = value; } // might want to check for non-null here |
397 | |
387 | |
|
|
388 | bool get_bool () const { return *value_nn == '1'; } |
398 | sint32 get_sint32 () const; |
389 | sint32 get_sint32 () const; |
399 | sint64 get_sint64 () const; |
390 | sint64 get_sint64 () const { return atoll (value_nn); } |
400 | double get_double () const; |
391 | double get_double () const { return atof (value_nn); } |
401 | |
392 | |
402 | void get (float &v) { v = get_double (); } |
393 | void get (float &v) { v = get_double (); } |
403 | void get (double &v) { v = get_double (); } |
394 | void get (double &v) { v = get_double (); } |
404 | |
395 | |
405 | void get (bool &i) { i = get_sint32 (); } |
396 | void get (bool &i) { i = get_bool (); } |
406 | void get (sint8 &i) { i = get_sint32 (); } |
397 | void get (sint8 &i) { i = get_sint32 (); } |
407 | void get (uint8 &i) { i = get_sint32 (); } |
398 | void get (uint8 &i) { i = get_sint32 (); } |
408 | void get (sint16 &i) { i = get_sint32 (); } |
399 | void get (sint16 &i) { i = get_sint32 (); } |
409 | void get (uint16 &i) { i = get_sint32 (); } |
400 | void get (uint16 &i) { i = get_sint32 (); } |
410 | void get (sint32 &i) { i = get_sint32 (); } |
401 | void get (sint32 &i) { i = get_sint32 (); } |
… | |
… | |
434 | static void do_cede_to_tick (); |
425 | static void do_cede_to_tick (); |
435 | static void do_cede_to_tick_every (); |
426 | static void do_cede_to_tick_every (); |
436 | |
427 | |
437 | static void cede_every (int count) |
428 | static void cede_every (int count) |
438 | { |
429 | { |
439 | if (++cede_counter >= count) |
430 | if (expect_false (++cede_counter >= count)) |
440 | do_cede_every (); |
431 | do_cede_every (); |
441 | } |
432 | } |
442 | |
433 | |
443 | static void cede_to_tick () |
434 | static void cede_to_tick () |
444 | { |
435 | { |
445 | if (time () >= next_cede) |
436 | if (expect_false (time () >= next_cede)) |
446 | do_cede_to_tick (); |
437 | do_cede_to_tick (); |
447 | } |
438 | } |
448 | |
439 | |
449 | static void cede_to_tick_every (int count) |
440 | static void cede_to_tick_every (int count) |
450 | { |
441 | { |
451 | if (++cede_counter >= count) |
442 | if (expect_false (++cede_counter >= count)) |
452 | cede_to_tick (); |
443 | cede_to_tick (); |
453 | } |
444 | } |
454 | |
445 | |
455 | static void wait_for_tick (); |
446 | static void wait_for_tick (); |
456 | static void wait_for_tick_begin (); |
447 | static void wait_for_tick_begin (); |