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.8 by root, Sun Aug 27 16:15:12 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(klass, event, ...) cfperl_invoke (EVENT_ ## klass ## _ ## event, ## __VA_ARGS__, DT_END)
66 67
67#define INVOKE_GLOBAL(event, ...) INVOKE (GLOBAL, event, ## __VA_ARGS__) 68#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_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_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#define INVOKE_MAP(event, map, ...) INVOKE (MAP , event, ARG_MAP (map) , ## __VA_ARGS__)
72
73//TODO should index into @result
74#define RESULT(idx,type) cfperl_result_ ## type (idx)
75#define RESULT_INT(idx) RESULT(idx, INT)
76
77bool cfperl_invoke (event_type event, ...);
78int cfperl_result_INT (int idx);
71 79
72struct extendable_base 80struct extendable_base
73{ 81{
74 void *self, *cb; // CF+ perl self and callback 82 void *self, *cb; // CF+ perl self and callback
75 const char *attach; // generic extension attachment information 83 const char *attach; // generic extension attachment information
76 84
77 void clear (); // called when free'ing objects 85 void clear (); // called when free'ing objects
78 void optimise (); // possibly save some memory by destroying unneeded data 86 void optimise (); // possibly save some memory by destroying unneeded data
79 void reattach (data_type type, void *self); // called after swapin 87
80 void instantiate (data_type type, void *self); // called on first instantiation 88 void extendable_init ()
81 void clone (data_type type, void *self, void *dest); 89 {
90 self = cb = 0;
91 attach = 0;
92 }
82}; 93};
83 94
84// objects extendable from perl (or any other extension) should include or 95// objects extendable from perl (or any other extension) should include or
85// derive using the curiously recurring template pattern, to avoid 96// derive using the curiously recurring template pattern, to avoid
86// virtual method calls etc. 97// virtual method calls etc.
87template<class subclass> 98template<class subclass>
88struct extendable : extendable_base 99struct extendable : extendable_base
89{ 100{
90 void clear () 101 static data_type get_dt ()
91 { 102 {
92 if (self || cb) 103 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 } 104 }
120}; 105};
121 106
122bool cfperl_invoke (event_type event, ...); 107struct object_freezer
123void cfperl_free_ob (struct object *op); 108{
109 const char *filename;
110 void *av;
111 int idx;
124 112
125void cfperl_init (); 113 object_freezer (const char *filename);
126void cfperl_main (); 114 ~object_freezer ();
115
116 void put (extendable_base *ext);
117
118 template<class subclass>
119 void put (extendable<subclass> *obj)
120 {
121 put ((extendable_base *)obj);
122 }
123
124 void finish ();
125};
126
127struct object_thawer
128{
129 void *av;
130 int idx;
131
132 object_thawer (const char *filename = 0);
133 ~object_thawer ();
134
135 void get (data_type type, void *obj, extendable_base *ext);
136
137 template<class subclass>
138 void get (extendable<subclass> *obj)
139 {
140 if (av)
141 get (subclass::get_dt (), (subclass *)obj, obj);
142 }
143
144 void finish ();
145};
127 146
128#endif 147#endif
129 148

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines