… | |
… | |
5 | #define CFPERL_H__ |
5 | #define CFPERL_H__ |
6 | |
6 | |
7 | struct pl; |
7 | struct pl; |
8 | struct object; |
8 | struct object; |
9 | struct mapstruct; |
9 | struct mapstruct; |
|
|
10 | |
|
|
11 | void cfperl_init (); |
|
|
12 | void cfperl_main (); |
10 | |
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 | { |
… | |
… | |
22 | DT_MAP, |
25 | DT_MAP, |
23 | DT_PLAYER, |
26 | DT_PLAYER, |
24 | DT_ARCH, |
27 | DT_ARCH, |
25 | DT_PARTY, |
28 | DT_PARTY, |
26 | DT_REGION, |
29 | DT_REGION, |
27 | |
30 | DT_AV, // av that needs to be flattened out |
28 | // following are only for cfapi-compatibility |
|
|
29 | DT_INT_PTR, |
|
|
30 | DT_INT64_PTR, |
|
|
31 | DT_DOUBLE_PTR, |
|
|
32 | }; |
31 | }; |
33 | |
32 | |
34 | enum event_klass |
33 | enum event_klass |
35 | { |
34 | { |
36 | KLASS_GLOBAL, |
35 | KLASS_GLOBAL, |
… | |
… | |
39 | KLASS_MAP, |
38 | KLASS_MAP, |
40 | }; |
39 | }; |
41 | |
40 | |
42 | enum event_type |
41 | enum event_type |
43 | { |
42 | { |
44 | # define def(klass,name) EV_ ## klass ## _ ## name, |
43 | # define def(klass,name) EVENT_ ## klass ## _ ## name, |
45 | # include "eventinc.h" |
44 | # include "eventinc.h" |
46 | # undef def |
45 | # undef def |
47 | NUM_EVENT_TYPES |
46 | NUM_EVENT_TYPES |
48 | }; |
47 | }; |
49 | |
48 | |
… | |
… | |
60 | #define ARG_REGION(o) DT_REGION, (void *)static_cast<struct region *> (o) |
59 | #define ARG_REGION(o) DT_REGION, (void *)static_cast<struct region *> (o) |
61 | |
60 | |
62 | // the ", ## __VA_ARGS" is, unfortunately, a gnu-cpp extension |
61 | // the ", ## __VA_ARGS" is, unfortunately, a gnu-cpp extension |
63 | |
62 | |
64 | // all these return true when the normal event processing should be skipped (if any) |
63 | // 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) |
64 | #define INVOKE(klass, event, ...) cfperl_invoke (EVENT_ ## klass ## _ ## event, ## __VA_ARGS__, DT_END) |
66 | |
65 | |
67 | #define INVOKE_GLOBAL(event, ...) INVOKE (GLOBAL, event, ## __VA_ARGS__) |
66 | #define INVOKE_GLOBAL(event, ...) INVOKE (GLOBAL, event, ## __VA_ARGS__) |
68 | #define INVOKE_OBJECT(event, op, ...) INVOKE (OBJECT, event, ARG_OBJECT (op), ## __VA_ARGS__) |
67 | #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__) |
68 | #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__) |
69 | #define INVOKE_MAP(event, map, ...) INVOKE (MAP , event, ARG_MAP (map) , ## __VA_ARGS__) |
|
|
70 | |
|
|
71 | //TODO should index into @result |
|
|
72 | #define RESULT(idx,type) cfperl_result_ ## type (idx) |
|
|
73 | #define RESULT_INT(idx) RESULT(idx, INT) |
|
|
74 | |
|
|
75 | bool cfperl_invoke (event_type event, ...); |
|
|
76 | int cfperl_result_INT (int idx); |
71 | |
77 | |
72 | struct extendable_base |
78 | struct extendable_base |
73 | { |
79 | { |
74 | void *self, *cb; // CF+ perl self and callback |
80 | void *self, *cb; // CF+ perl self and callback |
75 | const char *attach; // generic extension attachment information |
81 | const char *attach; // generic extension attachment information |
76 | |
82 | |
77 | void clear (); // called when free'ing objects |
83 | void clear (); // called when free'ing objects |
78 | void optimise (); // possibly save some memory by destroying unneeded data |
84 | void optimise (); // possibly save some memory by destroying unneeded data |
79 | void reattach (data_type type, void *self); // called after swapin |
85 | 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 | }; |
86 | }; |
83 | |
87 | |
84 | // objects extendable from perl (or any other extension) should include or |
88 | // objects extendable from perl (or any other extension) should include or |
85 | // derive using the curiously recurring template pattern, to avoid |
89 | // derive using the curiously recurring template pattern, to avoid |
86 | // virtual method calls etc. |
90 | // virtual method calls etc. |
87 | template<class subclass> |
91 | template<class subclass> |
88 | struct extendable : extendable_base |
92 | struct extendable : extendable_base |
89 | { |
93 | { |
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 () |
94 | void reattach () |
104 | { |
95 | { |
105 | if (attach) |
96 | if (attach) |
106 | extendable_base::reattach ( |
97 | extendable_base::reattach ( |
107 | static_cast<subclass *>(this)->get_dt (), |
98 | static_cast<subclass *>(this)->get_dt (), |
108 | static_cast<subclass *>(this) |
99 | static_cast<subclass *>(this) |
109 | ); |
100 | ); |
110 | } |
101 | } |
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 | }; |
102 | }; |
121 | |
103 | |
122 | bool cfperl_invoke (event_type event, ...); |
104 | struct object_freezer |
123 | void cfperl_free_ob (struct object *op); |
105 | { |
|
|
106 | const char *filename; |
|
|
107 | void *av; |
124 | |
108 | |
125 | void cfperl_init (); |
109 | object_freezer (const char *filename) : filename (filename) { } |
126 | void cfperl_main (); |
110 | void put (extendable_base *object); |
|
|
111 | void finish (); |
|
|
112 | }; |
|
|
113 | |
|
|
114 | struct object_thawer |
|
|
115 | { |
|
|
116 | void *av; |
|
|
117 | |
|
|
118 | object_thawer (const char *filename); |
|
|
119 | void get (extendable_base *object); |
|
|
120 | void finish (); |
|
|
121 | }; |
127 | |
122 | |
128 | #endif |
123 | #endif |
129 | |
124 | |