1 |
// |
2 |
// cfperl.h perl interface |
3 |
// |
4 |
#ifndef CFPERL_H__ |
5 |
#define CFPERL_H__ |
6 |
|
7 |
struct pl; |
8 |
struct object; |
9 |
struct mapstruct; |
10 |
|
11 |
// could have used templates, but a more traditional C api |
12 |
// uses more explicit typing which is ok for this purpose. |
13 |
enum data_type |
14 |
{ |
15 |
DT_END, // no further arguments |
16 |
DT_INT, |
17 |
DT_INT64, |
18 |
DT_DOUBLE, |
19 |
DT_STRING, // 0-terminated string |
20 |
DT_DATA, // string + length |
21 |
DT_OBJECT, |
22 |
DT_MAP, |
23 |
DT_PLAYER, |
24 |
DT_ARCH, |
25 |
DT_PARTY, |
26 |
DT_REGION, |
27 |
|
28 |
// following are only for cfapi-compatibility |
29 |
DT_INT_PTR, |
30 |
DT_INT64_PTR, |
31 |
DT_DOUBLE_PTR, |
32 |
}; |
33 |
|
34 |
enum event_klass |
35 |
{ |
36 |
KLASS_GLOBAL, |
37 |
KLASS_PLAYER, |
38 |
KLASS_OBJECT, |
39 |
KLASS_MAP, |
40 |
}; |
41 |
|
42 |
enum event_type |
43 |
{ |
44 |
# define def(klass,name) EV_ ## klass ## _ ## name, |
45 |
# include "eventinc.h" |
46 |
# undef def |
47 |
NUM_EVENT_TYPES |
48 |
}; |
49 |
|
50 |
#define ARG_INT(v) DT_INT , static_cast<int> (v) |
51 |
#define ARG_INT64(v) DT_INT64 , static_cast<sint64> (v) |
52 |
#define ARG_DOUBLE(v) DT_DOUBLE, static_cast<double> (v) |
53 |
#define ARG_STRING(v) DT_STRING, static_cast<const char *> (v) |
54 |
#define ARG_DATA(s,l) DT_DATA , static_cast<const void *> (s), (l) |
55 |
#define ARG_OBJECT(o) DT_OBJECT, (void *)static_cast<struct object *> (o) |
56 |
#define ARG_MAP(o) DT_MAP , (void *)static_cast<struct mapstruct *> (o) |
57 |
#define ARG_PLAYER(o) DT_PLAYER, (void *)static_cast<struct pl *> (o) |
58 |
#define ARG_ARCH(o) DT_ARCH , (void *)static_cast<struct archetype *> (o) |
59 |
#define ARG_PARTY(o) DT_PARTY , (void *)static_cast<struct party *> (o) |
60 |
#define ARG_REGION(o) DT_REGION, (void *)static_cast<struct region *> (o) |
61 |
|
62 |
// the ", ## __VA_ARGS" is, unfortunately, a gnu-cpp extension |
63 |
|
64 |
// all these return true when the normal event processing should be skipped (if any) |
65 |
#define INVOKE(klass, event, ...) cfperl_invoke (EV_ ## klass ## _ ## event, ## __VA_ARGS__, DT_END) |
66 |
|
67 |
#define INVOKE_GLOBAL(event, ...) INVOKE (GLOBAL, event, ## __VA_ARGS__) |
68 |
#define INVOKE_OBJECT(event, op, ...) INVOKE (OBJECT, event, ARG_OBJECT (op), ## __VA_ARGS__) |
69 |
#define INVOKE_PLAYER(event, pl, ...) INVOKE (PLAYER, event, ARG_PLAYER (pl), ## __VA_ARGS__) |
70 |
#define INVOKE_MAP(event, map, ...) INVOKE (MAP , event, ARG_MAP (map) , ## __VA_ARGS__) |
71 |
|
72 |
struct extendable_base |
73 |
{ |
74 |
void *self, *cb; // CF+ perl self and callback |
75 |
const char *attach; // generic extension attachment information |
76 |
|
77 |
void clear (); // called when free'ing objects |
78 |
void optimise (); // possibly save some memory by destroying unneeded data |
79 |
void reattach (data_type type, void *self); // called after swapin |
80 |
void instantiate (data_type type, void *self); // called on first instantiation |
81 |
void clone (data_type type, void *self, void *dest); |
82 |
}; |
83 |
|
84 |
// objects extendable from perl (or any other extension) should include or |
85 |
// derive using the curiously recurring template pattern, to avoid |
86 |
// virtual method calls etc. |
87 |
template<class subclass> |
88 |
struct extendable : extendable_base |
89 |
{ |
90 |
void clear () |
91 |
{ |
92 |
if (self || cb) |
93 |
extendable_base::clear (); |
94 |
} |
95 |
void instantiate () |
96 |
{ |
97 |
if (attach) |
98 |
extendable_base::instantiate ( |
99 |
static_cast<subclass *>(this)->get_dt (), |
100 |
static_cast<subclass *>(this) |
101 |
); |
102 |
} |
103 |
void reattach () |
104 |
{ |
105 |
if (attach) |
106 |
extendable_base::reattach ( |
107 |
static_cast<subclass *>(this)->get_dt (), |
108 |
static_cast<subclass *>(this) |
109 |
); |
110 |
} |
111 |
void clone (subclass *destination) |
112 |
{ |
113 |
if (self || cb) |
114 |
extendable_base::clone ( |
115 |
static_cast<subclass *>(this)->get_dt (), |
116 |
static_cast<subclass *>(this), |
117 |
destination |
118 |
); |
119 |
} |
120 |
}; |
121 |
|
122 |
bool cfperl_invoke (event_type event, ...); |
123 |
void cfperl_free_ob (struct object *op); |
124 |
|
125 |
void cfperl_init (); |
126 |
void cfperl_main (); |
127 |
|
128 |
#endif |
129 |
|