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.6 by root, Sat Aug 26 08:44:06 2006 UTC vs.
Revision 1.9 by root, Sun Aug 27 15:24:21 2006 UTC

5#define CFPERL_H__ 5#define CFPERL_H__
6 6
7struct pl; 7struct pl;
8struct object; 8struct object;
9struct mapstruct; 9struct mapstruct;
10
11void cfperl_init ();
12void 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.
13enum data_type 16enum 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,
30 DT_AV, // av that needs to be flattened out
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
34enum event_klass 35enum event_klass
35{ 36{
36 KLASS_GLOBAL, 37 KLASS_GLOBAL,
39 KLASS_MAP, 40 KLASS_MAP,
40}; 41};
41 42
42enum event_type 43enum event_type
43{ 44{
44# define def(klass,name) EV_ ## klass ## _ ## name, 45# define def(klass,name) EVENT_ ## klass ## _ ## name,
45# include "eventinc.h" 46# include "eventinc.h"
46# undef def 47# undef def
47 NUM_EVENT_TYPES 48 NUM_EVENT_TYPES
48}; 49};
49 50
60#define ARG_REGION(o) DT_REGION, (void *)static_cast<struct region *> (o) 61#define ARG_REGION(o) DT_REGION, (void *)static_cast<struct region *> (o)
61 62
62// the ", ## __VA_ARGS" is, unfortunately, a gnu-cpp extension 63// the ", ## __VA_ARGS" is, unfortunately, a gnu-cpp extension
63 64
64// all these return true when the normal event processing should be skipped (if any) 65// 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#define INVOKE_(event, ...) cfperl_invoke (event, ## __VA_ARGS__, DT_END)
66 67
68#define INVOKE(klass, event, ...) INVOKE_(EVENT_ ## klass ## _ ## event, ## __VA_ARGS__)
67#define INVOKE_GLOBAL(event, ...) INVOKE (GLOBAL, event, ## __VA_ARGS__) 69#define INVOKE_GLOBAL(event, ...) INVOKE_(EVENT_ ## GLOBAL ## _ ## event, ## __VA_ARGS__)
68#define INVOKE_OBJECT(event, op, ...) INVOKE (OBJECT, event, ARG_OBJECT (op), ## __VA_ARGS__) 70#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__) 71#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__) 72#define INVOKE_MAP(event, map, ...) INVOKE_(EVENT_ ## MAP ## _ ## event, ARG_MAP (map) , ## __VA_ARGS__)
73
74//TODO should index into @result
75#define RESULT(idx,type) cfperl_result_ ## type (idx)
76#define RESULT_INT(idx) RESULT(idx, INT)
77
78bool cfperl_invoke (event_type event, ...);
79int cfperl_result_INT (int idx);
71 80
72struct extendable_base 81struct extendable_base
73{ 82{
74 void *self, *cb; // CF+ perl self and callback 83 void *self, *cb; // CF+ perl self and callback
75 const char *attach; // generic extension attachment information 84 const char *attach; // generic extension attachment information
76 85
77 void clear (); // called when free'ing objects 86 void clear (); // called when free'ing objects
78 void optimise (); // possibly save some memory by destroying unneeded data 87 void optimise (); // possibly save some memory by destroying unneeded data
79 void reattach (data_type type, void *self); // called after swapin 88
80 void instantiate (data_type type, void *self); // called on first instantiation 89 void extendable_init ()
81 void clone (data_type type, void *self, void *dest); 90 {
91 self = cb = 0;
92 attach = 0;
93 }
82}; 94};
83 95
84// objects extendable from perl (or any other extension) should include or 96// objects extendable from perl (or any other extension) should include or
85// derive using the curiously recurring template pattern, to avoid 97// derive using the curiously recurring template pattern, to avoid
86// virtual method calls etc. 98// virtual method calls etc.
87template<class subclass> 99template<class subclass>
88struct extendable : extendable_base 100struct extendable : extendable_base
89{ 101{
90 void clear () 102 static data_type get_dt ()
91 { 103 {
92 if (self || cb) 104 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 } 105 }
120}; 106};
121 107
122bool cfperl_invoke (event_type event, ...); 108struct object_freezer
123void cfperl_free_ob (struct object *op); 109{
110 const char *filename;
111 void *av;
112 int idx;
124 113
125void cfperl_init (); 114 object_freezer (const char *filename);
126void cfperl_main (); 115 ~object_freezer ();
116
117 void put (extendable_base *ext);
118
119 template<class subclass>
120 void put (extendable<subclass> *obj)
121 {
122 put ((extendable_base *)obj);
123 }
124
125 void finish ();
126};
127
128struct object_thawer
129{
130 void *av;
131 int idx;
132
133 object_thawer (const char *filename = 0);
134 ~object_thawer ();
135
136 void get (data_type type, void *obj, extendable_base *ext);
137
138 template<class subclass>
139 void get (extendable<subclass> *obj)
140 {
141 if (av)
142 get (subclass::get_dt (), (subclass *)obj, obj);
143 }
144
145 void finish ();
146};
127 147
128#endif 148#endif
129 149

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines