--- deliantra/server/include/cfperl.h 2006/11/17 19:40:54 1.32 +++ deliantra/server/include/cfperl.h 2006/12/19 04:58:04 1.40 @@ -12,6 +12,10 @@ #include #include +#include + +#include "callback.h" + // optimisations/workaround for functions requiring my_perl in scope (anti-bloat) #undef localtime #undef srand48 @@ -23,6 +27,10 @@ #undef ctime #undef strerror +// same here, massive symbol spamming +#undef do_open +#undef do_close + // perl bug #40256: perl does overwrite those with reentrant versions // but does not initialise their state structures. #undef random @@ -35,9 +43,11 @@ { KLASS_NONE, KLASS_GLOBAL, + KLASS_CLIENT, KLASS_PLAYER, KLASS_OBJECT, KLASS_MAP, + KLASS_COMMAND, }; enum event_type @@ -60,31 +70,33 @@ #define ARG_ARCH(o) DT_ARCH , (void *)static_cast (o) #define ARG_PARTY(o) DT_PARTY , (void *)static_cast (o) #define ARG_REGION(o) DT_REGION, (void *)static_cast (o) +#define ARG_CLIENT(o) DT_CLIENT, (void *)static_cast (o) // the ", ## __VA_ARGS" is, unfortunately, a gnu-cpp extension // all these return true when the normal event processing should be skipped (if any) #define INVOKE_(event, ...) cfperl_invoke (event, ## __VA_ARGS__, DT_END) -#define INVOKE(klass, event, ...) INVOKE_(EVENT_ ## klass ## _ ## event, ## __VA_ARGS__) -#define INVOKE_GLOBAL(event, ...) INVOKE_(EVENT_ ## GLOBAL ## _ ## event, ## __VA_ARGS__) -#define INVOKE_OBJECT(event, op, ...) INVOKE_(EVENT_ ## OBJECT ## _ ## event, ARG_OBJECT (op), ## __VA_ARGS__) -#define INVOKE_PLAYER(event, pl, ...) INVOKE_(EVENT_ ## PLAYER ## _ ## event, ARG_PLAYER (pl), ## __VA_ARGS__) -#define INVOKE_MAP(event, map, ...) INVOKE_(EVENT_ ## MAP ## _ ## event, ARG_MAP (map) , ## __VA_ARGS__) +#define INVOKE(klass, event, ...) INVOKE_(EVENT_ ## klass ## _ ## event, ## __VA_ARGS__) +#define INVOKE_GLOBAL(event, ...) INVOKE_(EVENT_ ## GLOBAL ## _ ## event, ## __VA_ARGS__) +#define INVOKE_OBJECT(event, op, ...) INVOKE_(EVENT_ ## OBJECT ## _ ## event, ARG_OBJECT (op), ## __VA_ARGS__) +#define INVOKE_CLIENT(event, ns, ...) INVOKE_(EVENT_ ## CLIENT ## _ ## event, ARG_CLIENT (ns), ## __VA_ARGS__) +#define INVOKE_PLAYER(event, pl, ...) INVOKE_(EVENT_ ## PLAYER ## _ ## event, ARG_PLAYER (pl), ## __VA_ARGS__) +#define INVOKE_MAP(event, map, ...) INVOKE_(EVENT_ ## MAP ## _ ## event, ARG_MAP (map) , ## __VA_ARGS__) //TODO should index into @result #define RESULT(idx,type) cfperl_result_ ## type (idx) -#define RESULT_INT(idx) RESULT(idx, INT) -#define RESULT_MAP(idx) RESULT(idx, MAP) +#define RESULT_DOUBLE(idx) RESULT(idx, DOUBLE) +#define RESULT_INT(idx) RESULT(idx, INT) bool cfperl_invoke (event_type event, ...); +double cfperl_result_DOUBLE (int idx); int cfperl_result_INT (int idx); -int cfperl_result_MAP (int idx); struct attachable_base { - SV *self; - AV *cb; // CF+ perl self and callback + SV *self; // CF+ perl self + AV *cb; // CF+ callbacks shstr attach; // generic extension attachment information void clear (); // called when free'ing objects @@ -113,11 +125,11 @@ { } - attachable_base &operator = (const attachable_base &src) + attachable_base &operator =(const attachable_base &src) { - clear (); - attach = src.attach; - return *this; + clear (); + attach = src.attach; + return *this; } }; @@ -322,5 +334,62 @@ char *fgets (char *s, int n, object_thawer &thawer); +struct watcher_base +{ + static struct EventAPI *GEventAPI; +}; + +template +struct watcher : watcher_base +{ + base *pe; + + void start (bool repeat = false) { GEventAPI->start ((pe_watcher *)pe, repeat); } + void stop (bool cancel_events = false) { GEventAPI->stop ((pe_watcher *)pe, cancel_events); } + void now () { GEventAPI->now ((pe_watcher *)pe); } + void suspend () { GEventAPI->suspend ((pe_watcher *)pe); } + void resume () { GEventAPI->resume ((pe_watcher *)pe); } + + void prio (int new_prio) { ((pe_watcher *)pe)->prio = new_prio; } + + ~watcher () + { + cancel (); + } + +private: + void cancel () { GEventAPI->cancel ((pe_watcher *)pe); } // private +}; + +struct iw : watcher, callback +{ + template + iw (O object, M method) + : callback (object, method) + { + alloc (); + } + +private: + void alloc (); +}; + +struct iow : watcher, callback +{ + template + iow (O object, M method) + : callback (object, method) + { + alloc (); + } + + void fd (int fd); + int poll (); + void poll (int events); + +private: + void alloc (); +}; + #endif