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.11 by root, Mon Aug 28 07:07:41 2006 UTC

6 6
7struct pl; 7struct pl;
8struct object; 8struct object;
9struct mapstruct; 9struct mapstruct;
10 10
11void cfperl_init ();
12void 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.
13enum data_type 16enum 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
34enum event_klass 35enum 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
42enum event_type 44enum 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
80bool cfperl_invoke (event_type event, ...);
81int cfperl_result_INT (int idx);
82
72struct extendable_base 83struct 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.
87template<class subclass> 101template<class subclass>
88struct extendable : extendable_base 102struct 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
122bool cfperl_invoke (event_type event, ...); 110struct object_freezer
123void cfperl_free_ob (struct object *op); 111{
112 const char *filename;
113 void *av;
124 114
125void cfperl_init (); 115 object_freezer (const char *filename);
126void cfperl_main (); 116 ~object_freezer ();
117
118 void put (FILE *fp, attachable_base *ext);
119
120 template<class subclass>
121 void put (FILE *fp, attachable<subclass> *obj)
122 {
123 put (fp, (attachable_base *)obj);
124 }
125
126 void finish ();
127};
128
129struct object_thawer
130{
131 void *av;
132
133 object_thawer (const char *filename = 0);
134 ~object_thawer ();
135
136 void get (data_type type, void *obj, attachable_base *ext, int oid);
137
138 template<class subclass>
139 void get (attachable<subclass> *obj, int oid)
140 {
141 if (av)
142 get (subclass::get_dt (), (subclass *)obj, obj, oid);
143 }
144
145 void finish ();
146};
127 147
128#endif 148#endif
129 149

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines