… | |
… | |
6 | |
6 | |
7 | struct pl; |
7 | struct pl; |
8 | struct object; |
8 | struct object; |
9 | struct mapstruct; |
9 | struct mapstruct; |
10 | |
10 | |
|
|
11 | void cfperl_init (); |
|
|
12 | void cfperl_main (); |
|
|
13 | |
11 | // could have used templates, but a more traditional C api |
14 | // could have used templates, but a more traditional C api |
12 | // uses more explicit typing which is ok for this purpose. |
15 | // uses more explicit typing which is ok for this purpose. |
13 | enum data_type |
16 | enum data_type |
14 | { |
17 | { |
15 | DT_END, // no further arguments |
18 | DT_END, // no further arguments |
|
|
19 | DT_AV, // perl-only av that needs to be flattened out |
16 | DT_INT, |
20 | DT_INT, |
17 | DT_INT64, |
21 | DT_INT64, |
18 | DT_DOUBLE, |
22 | DT_DOUBLE, |
19 | DT_STRING, // 0-terminated string |
23 | DT_STRING, // 0-terminated string |
20 | DT_DATA, // string + length |
24 | DT_DATA, // string + length |
… | |
… | |
23 | DT_PLAYER, |
27 | DT_PLAYER, |
24 | DT_ARCH, |
28 | DT_ARCH, |
25 | DT_PARTY, |
29 | DT_PARTY, |
26 | DT_REGION, |
30 | DT_REGION, |
27 | |
31 | |
28 | // following are only for cfapi-compatibility |
32 | NUM_DATA_TYPES |
29 | DT_INT_PTR, |
|
|
30 | DT_INT64_PTR, |
|
|
31 | DT_DOUBLE_PTR, |
|
|
32 | }; |
33 | }; |
33 | |
34 | |
34 | enum event_klass |
35 | enum event_klass |
35 | { |
36 | { |
|
|
37 | KLASS_NONE, |
36 | KLASS_GLOBAL, |
38 | KLASS_GLOBAL, |
37 | KLASS_PLAYER, |
39 | KLASS_PLAYER, |
38 | KLASS_OBJECT, |
40 | KLASS_OBJECT, |
39 | KLASS_MAP, |
41 | KLASS_MAP, |
40 | }; |
42 | }; |
41 | |
43 | |
42 | enum event_type |
44 | enum event_type |
43 | { |
45 | { |
44 | # define def(klass,name) EV_ ## klass ## _ ## name, |
46 | # define def(klass,name) EVENT_ ## klass ## _ ## name, |
45 | # include "eventinc.h" |
47 | # include "eventinc.h" |
46 | # undef def |
48 | # undef def |
47 | NUM_EVENT_TYPES |
49 | NUM_EVENT_TYPES |
48 | }; |
50 | }; |
49 | |
51 | |
|
|
52 | #define ARG_AV(o) DT_AV , static_cast<AV *> (o) |
50 | #define ARG_INT(v) DT_INT , static_cast<int> (v) |
53 | #define ARG_INT(v) DT_INT , static_cast<int> (v) |
51 | #define ARG_INT64(v) DT_INT64 , static_cast<sint64> (v) |
54 | #define ARG_INT64(v) DT_INT64 , static_cast<sint64> (v) |
52 | #define ARG_DOUBLE(v) DT_DOUBLE, static_cast<double> (v) |
55 | #define ARG_DOUBLE(v) DT_DOUBLE, static_cast<double> (v) |
53 | #define ARG_STRING(v) DT_STRING, static_cast<const char *> (v) |
56 | #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) |
57 | #define ARG_DATA(s,l) DT_DATA , static_cast<const void *> (s), (l) |
… | |
… | |
60 | #define ARG_REGION(o) DT_REGION, (void *)static_cast<struct region *> (o) |
63 | #define ARG_REGION(o) DT_REGION, (void *)static_cast<struct region *> (o) |
61 | |
64 | |
62 | // the ", ## __VA_ARGS" is, unfortunately, a gnu-cpp extension |
65 | // the ", ## __VA_ARGS" is, unfortunately, a gnu-cpp extension |
63 | |
66 | |
64 | // all these return true when the normal event processing should be skipped (if any) |
67 | // 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) |
68 | #define INVOKE_(event, ...) cfperl_invoke (event, ## __VA_ARGS__, DT_END) |
66 | |
69 | |
|
|
70 | #define INVOKE(klass, event, ...) INVOKE_(EVENT_ ## klass ## _ ## event, ## __VA_ARGS__) |
67 | #define INVOKE_GLOBAL(event, ...) INVOKE (GLOBAL, event, ## __VA_ARGS__) |
71 | #define INVOKE_GLOBAL(event, ...) INVOKE_(EVENT_ ## GLOBAL ## _ ## event, ## __VA_ARGS__) |
68 | #define INVOKE_OBJECT(event, op, ...) INVOKE (OBJECT, event, ARG_OBJECT (op), ## __VA_ARGS__) |
72 | #define INVOKE_OBJECT(event, op, ...) INVOKE_(EVENT_ ## OBJECT ## _ ## event, ARG_OBJECT (op), ## __VA_ARGS__) |
69 | #define INVOKE_PLAYER(event, pl, ...) INVOKE (PLAYER, event, ARG_PLAYER (pl), ## __VA_ARGS__) |
73 | #define INVOKE_PLAYER(event, pl, ...) INVOKE_(EVENT_ ## PLAYER ## _ ## event, ARG_PLAYER (pl), ## __VA_ARGS__) |
70 | #define INVOKE_MAP(event, map, ...) INVOKE (MAP , event, ARG_MAP (map) , ## __VA_ARGS__) |
74 | #define INVOKE_MAP(event, map, ...) INVOKE_(EVENT_ ## MAP ## _ ## event, ARG_MAP (map) , ## __VA_ARGS__) |
71 | |
75 | |
|
|
76 | //TODO should index into @result |
|
|
77 | #define RESULT(idx,type) cfperl_result_ ## type (idx) |
|
|
78 | #define RESULT_INT(idx) RESULT(idx, INT) |
|
|
79 | |
|
|
80 | bool cfperl_invoke (event_type event, ...); |
|
|
81 | int cfperl_result_INT (int idx); |
|
|
82 | |
72 | struct extendable_base |
83 | struct attachable_base |
73 | { |
84 | { |
74 | void *self, *cb; // CF+ perl self and callback |
85 | void *self, *cb; // CF+ perl self and callback |
75 | const char *attach; // generic extension attachment information |
86 | const char *attach; // generic extension attachment information |
76 | |
87 | |
77 | void clear (); // called when free'ing objects |
88 | void clear (); // called when free'ing objects |
78 | void optimise (); // possibly save some memory by destroying unneeded data |
89 | void optimise (); // possibly save some memory by destroying unneeded data |
79 | void reattach (data_type type, void *self); // called after swapin |
90 | |
80 | void instantiate (data_type type, void *self); // called on first instantiation |
91 | void attachable_init () |
81 | void clone (data_type type, void *self, void *dest); |
92 | { |
|
|
93 | self = cb = 0; |
|
|
94 | attach = 0; |
|
|
95 | } |
82 | }; |
96 | }; |
83 | |
97 | |
84 | // objects extendable from perl (or any other extension) should include or |
98 | // objects attachable from perl (or any other extension) should include or |
85 | // derive using the curiously recurring template pattern, to avoid |
99 | // derive using the curiously recurring template pattern, to avoid |
86 | // virtual method calls etc. |
100 | // virtual method calls etc. |
87 | template<class subclass> |
101 | template<class subclass> |
88 | struct extendable : extendable_base |
102 | struct attachable : attachable_base |
89 | { |
103 | { |
90 | void clear () |
104 | static data_type get_dt () |
91 | { |
105 | { |
92 | if (self || cb) |
106 | subclass::get_dt (); |
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 | } |
107 | } |
120 | }; |
108 | }; |
121 | |
109 | |
122 | bool cfperl_invoke (event_type event, ...); |
110 | struct object_freezethaw |
123 | void cfperl_free_ob (struct object *op); |
111 | { |
|
|
112 | void *av; |
|
|
113 | FILE *fp; |
124 | |
114 | |
125 | void cfperl_init (); |
115 | operator FILE *() { return fp; } |
126 | void cfperl_main (); |
116 | }; |
|
|
117 | |
|
|
118 | struct object_freezer : object_freezethaw |
|
|
119 | { |
|
|
120 | void *filename; |
|
|
121 | |
|
|
122 | object_freezer (const char *filename); |
|
|
123 | ~object_freezer (); |
|
|
124 | |
|
|
125 | void put (attachable_base *ext); |
|
|
126 | |
|
|
127 | template<class subclass> |
|
|
128 | void put (attachable<subclass> *obj) |
|
|
129 | { |
|
|
130 | put ((attachable_base *)obj); |
|
|
131 | } |
|
|
132 | }; |
|
|
133 | |
|
|
134 | struct object_thawer : object_freezethaw |
|
|
135 | { |
|
|
136 | object_thawer (FILE *fp, const char *filename = 0); |
|
|
137 | ~object_thawer (); |
|
|
138 | |
|
|
139 | void get (data_type type, void *obj, attachable_base *ext, int oid); |
|
|
140 | |
|
|
141 | template<class subclass> |
|
|
142 | void get (attachable<subclass> *obj, int oid) |
|
|
143 | { |
|
|
144 | if (av) |
|
|
145 | get (subclass::get_dt (), (subclass *)obj, obj, oid); |
|
|
146 | } |
|
|
147 | }; |
127 | |
148 | |
128 | #endif |
149 | #endif |
129 | |
150 | |