ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/include/cfperl.h
(Generate patch)

Comparing deliantra/server/include/cfperl.h (file contents):
Revision 1.61 by root, Mon Apr 16 11:09:31 2007 UTC vs.
Revision 1.66 by root, Sat May 26 15:44:03 2007 UTC

2// cfperl.h perl interface 2// cfperl.h perl interface
3// 3//
4#ifndef CFPERL_H__ 4#ifndef CFPERL_H__
5#define CFPERL_H__ 5#define CFPERL_H__
6 6
7#include <cstdarg>
7#include <cstdio> 8#include <cstdio>
9#include <bitset>
8 10
9using namespace std; 11using namespace std;
10 12
11#include <EXTERN.h> 13#include <EXTERN.h>
12#include <perl.h> 14#include <perl.h>
62 64
63void cfperl_init (); 65void cfperl_init ();
64void cfperl_main (); 66void cfperl_main ();
65void cfperl_emergency_save (); 67void cfperl_emergency_save ();
66void cfperl_cleanup (int make_core); 68void cfperl_cleanup (int make_core);
69void cfperl_make_book (object *book, int level);
67 70
68////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 71//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
69 72
70// virtual server time, excluding time jumps and lag 73// virtual server time, excluding time jumps and lag
71extern double runtime; 74extern double runtime;
87# define def(klass,name) EVENT_ ## klass ## _ ## name, 90# define def(klass,name) EVENT_ ## klass ## _ ## name,
88# include "eventinc.h" 91# include "eventinc.h"
89# undef def 92# undef def
90 NUM_EVENT_TYPES 93 NUM_EVENT_TYPES
91}; 94};
95
96// in which global events or per-type events are we interested
97extern bitset<NUM_EVENT_TYPES> ev_want_event;
98extern bitset<NUM_TYPES> ev_want_type;
92 99
93#define ARG_AV(o) DT_AV , static_cast<AV *> (o) 100#define ARG_AV(o) DT_AV , static_cast<AV *> (o)
94#define ARG_INT(v) DT_INT , static_cast<int> (v) 101#define ARG_INT(v) DT_INT , static_cast<int> (v)
95#define ARG_INT64(v) DT_INT64 , static_cast<sint64> (v) 102#define ARG_INT64(v) DT_INT64 , static_cast<sint64> (v)
96#define ARG_DOUBLE(v) DT_DOUBLE, static_cast<double> (v) 103#define ARG_DOUBLE(v) DT_DOUBLE, static_cast<double> (v)
103#define ARG_CLIENT(o) DT_CLIENT, (void *)(static_cast<client *> (o)) 110#define ARG_CLIENT(o) DT_CLIENT, (void *)(static_cast<client *> (o))
104#define ARG_PARTY(o) DT_PARTY , (void *)(static_cast<party *> (o)) 111#define ARG_PARTY(o) DT_PARTY , (void *)(static_cast<party *> (o))
105#define ARG_REGION(o) DT_REGION, (void *)(static_cast<region *> (o)) 112#define ARG_REGION(o) DT_REGION, (void *)(static_cast<region *> (o))
106 113
107// the ", ## __VA_ARGS" is, unfortunately, a gnu-cpp extension 114// the ", ## __VA_ARGS" is, unfortunately, a gnu-cpp extension
108 115#define INVOKE(obj,event, ...) (expect_false ((obj)->should_invoke (event)) ? (obj)->invoke (event, ## __VA_ARGS__, DT_END) : 0)
109// all these return true when the normal event processing should be skipped (if any)
110#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__)
111#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__)
112#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__)
113#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__)
114#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__)
115#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__)
116 122
117//TODO should index into @result 123//TODO should index into @result
118#define RESULT(idx,type) cfperl_result_ ## type (idx) 124#define RESULT(idx,type) cfperl_result_ ## type (idx)
119#define RESULT_DOUBLE(idx) RESULT(idx, DOUBLE) 125#define RESULT_DOUBLE(idx) RESULT(idx, DOUBLE)
120#define RESULT_INT(idx) RESULT(idx, INT) 126#define RESULT_INT(idx) RESULT(idx, INT)
183 189
184 virtual ~attachable (); 190 virtual ~attachable ();
185 191
186 attachable &operator =(const attachable &src); 192 attachable &operator =(const attachable &src);
187 193
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
188 bool invoke (event_type event, ...); 200 bool invoke (event_type event, ...);
201
189 MTH void instantiate (); 202 MTH void instantiate ();
190 void reattach (); 203 void reattach ();
191 204
192protected: 205protected:
193 // do the real refcount checking work 206 // do the real refcount checking work
205 218
206extern struct global gbl_ev; 219extern struct global gbl_ev;
207 220
208////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 221//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
209 222
210struct object_freezer : dynbuf 223struct object_freezer : dynbuf_text
211{ 224{
212 AV *av; 225 AV *av;
213 226
214 object_freezer (); 227 object_freezer ();
215 ~object_freezer (); 228 ~object_freezer ();
219 // used only for user-defined key-value pairs 232 // used only for user-defined key-value pairs
220 void put (const shstr &k, const shstr &v) 233 void put (const shstr &k, const shstr &v)
221 { 234 {
222 add (k); 235 add (k);
223 236
224 if (v) 237 if (expect_true (v))
225 add (' '), add (v); 238 add (' '), add (v);
226 239
227 add ('\n'); 240 add ('\n');
228 } 241 }
229 242
244 257
245 char *p = (char *)alloc (klen + vlen + 1); 258 char *p = (char *)alloc (klen + vlen + 1);
246 259
247 memcpy (p, keyword_str [k], klen); p += klen; 260 memcpy (p, keyword_str [k], klen); p += klen;
248 261
249 if (v) 262 if (expect_true (v))
250 { 263 {
251 *p++ = ' '; vlen--; 264 *p++ = ' '; vlen--;
252 memcpy (p, v, vlen); p += vlen; 265 memcpy (p, v, vlen); p += vlen;
253 } 266 }
254 267
292 305
293 void put (keyword kbeg, keyword kend, const shstr &v) 306 void put (keyword kbeg, keyword kend, const shstr &v)
294 { 307 {
295 add (keyword_str [kbeg], keyword_len [kbeg]); add ('\n'); 308 add (keyword_str [kbeg], keyword_len [kbeg]); add ('\n');
296 309
297 if (v) 310 if (expect_true (v))
298 { 311 {
299 add (v); 312 add (v);
300 add ('\n'); 313 add ('\n');
301 } 314 }
302 315
316 void put (keyword k, unsigned long long v) { put_(k, (sint64)v); } 329 void put (keyword k, unsigned long long v) { put_(k, (sint64)v); }
317 330
318 template<typename T> 331 template<typename T>
319 void put (keyword k, const T *v) 332 void put (keyword k, const T *v)
320 { 333 {
321 if (v) 334 if (expect_true (v))
322 put (k, v->name); 335 put (k, v->name);
323 else 336 else
324 put (k, (const char *)0); 337 put (k, (const char *)0);
325 } 338 }
326 339
340void fprintf (object_freezer &freezer, const char *format, ...); 353void fprintf (object_freezer &freezer, const char *format, ...);
341void fputs (const char *s, object_freezer &freezer); 354void fputs (const char *s, object_freezer &freezer);
342 355
343struct object_thawer 356struct object_thawer
344{ 357{
358 char *line; // current beginning of line
345 SV *text; // text part 359 SV *text; // text part
346 AV *av; // perl part 360 AV *av; // perl part
347 int linenum; 361 int linenum;
348 char *line; // current beginning of line
349 keyword kw; 362 keyword kw;
350 char *kw_str, *value; 363 char *kw_str; // the keyword parsed, as string
364 char *value; // the value, or 0 if no value
365 char *value_nn; // the value, or the empty string if no value
351 const char *name; 366 const char *name;
352 367
353 operator bool () { return !!text; } 368 operator bool () { return !!text; }
354 369
355 object_thawer (const char *path = 0); 370 object_thawer (const char *path = 0);
370 next (); 385 next ();
371 return kw; 386 return kw;
372 } 387 }
373 388
374 char *get_str () { return value; } // may be 0 389 char *get_str () { return value; } // may be 0
390 void get_ml (keyword kend, shstr &sh);
375 391
376 void get (shstr &sh) const;
377 void get_ornull (shstr &sh) const { sh = value; } 392 void get_ornull (shstr &sh) const { sh = value; }
378 void get_ml (keyword kend, shstr &sh); 393 void get (shstr &sh) const { sh = value; } // might want to check for non-null here
379 394
395 bool get_bool () const { return *value_nn == '1'; }
380 sint32 get_sint32 () const; 396 sint32 get_sint32 () const;
381 sint64 get_sint64 () const; 397 sint64 get_sint64 () const { return atoll (value_nn); }
382 double get_double () const; 398 double get_double () const { return atof (value_nn); }
383 399
384 void get (float &v) { v = get_double (); } 400 void get (float &v) { v = get_double (); }
385 void get (double &v) { v = get_double (); } 401 void get (double &v) { v = get_double (); }
386 402
387 void get (bool &i) { i = get_sint32 (); } 403 void get (bool &i) { i = get_bool (); }
388 void get (sint8 &i) { i = get_sint32 (); } 404 void get (sint8 &i) { i = get_sint32 (); }
389 void get (uint8 &i) { i = get_sint32 (); } 405 void get (uint8 &i) { i = get_sint32 (); }
390 void get (sint16 &i) { i = get_sint32 (); } 406 void get (sint16 &i) { i = get_sint32 (); }
391 void get (uint16 &i) { i = get_sint32 (); } 407 void get (uint16 &i) { i = get_sint32 (); }
392 void get (sint32 &i) { i = get_sint32 (); } 408 void get (sint32 &i) { i = get_sint32 (); }
406 static struct CoroAPI *GCoroAPI; 422 static struct CoroAPI *GCoroAPI;
407 423
408 static int nready () { return CORO_NREADY; } 424 static int nready () { return CORO_NREADY; }
409 static int cede () { return CORO_CEDE ; } 425 static int cede () { return CORO_CEDE ; }
410 426
427 static double (*time)();
428 static double next_cede;
411 static int cede_counter; 429 static int cede_counter;
412 430
431 static void do_cede_every ();
432 static void do_cede_to_tick ();
433 static void do_cede_to_tick_every ();
434
413 static void cede_every (int count) 435 static void cede_every (int count)
414 { 436 {
415 if (++cede_counter >= count) 437 if (expect_false (++cede_counter >= count))
416 { 438 do_cede_every ();
417 cede_counter = 0; 439 }
418 440
419 if (coroapi::nready ()) 441 static void cede_to_tick ()
420 coroapi::cede (); 442 {
421 } 443 if (expect_false (time () >= next_cede))
444 do_cede_to_tick ();
445 }
446
447 static void cede_to_tick_every (int count)
448 {
449 if (expect_false (++cede_counter >= count))
450 cede_to_tick ();
422 } 451 }
423 452
424 static void wait_for_tick (); 453 static void wait_for_tick ();
425 static void wait_for_tick_begin (); 454 static void wait_for_tick_begin ();
426}; 455};

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines