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.14 by root, Wed Aug 30 06:06:27 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 void instantiate (data_type type, void *obj);
80 void instantiate (data_type type, void *self); // called on first instantiation 91
81 void clone (data_type type, void *self, void *dest); 92 void attachable_init ()
93 {
94 self = cb = 0;
95 attach = 0;
96 }
82}; 97};
83 98
84// objects extendable from perl (or any other extension) should include or 99// objects attachable from perl (or any other extension) should include or
85// derive using the curiously recurring template pattern, to avoid 100// derive using the curiously recurring template pattern, to avoid
86// virtual method calls etc. 101// virtual method calls etc.
87template<class subclass> 102template<class subclass>
88struct extendable : extendable_base 103struct attachable : attachable_base
89{ 104{
90 void clear () 105 static data_type get_dt ()
91 { 106 {
92 if (self || cb) 107 return subclass::get_dt ();
93 extendable_base::clear ();
94 } 108 }
109
95 void instantiate () 110 void instantiate ()
96 { 111 {
97 if (attach) 112 if (attach)
98 extendable_base::instantiate ( 113 attachable_base::instantiate (get_dt (), static_cast<subclass *>(this));
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 } 114 }
120}; 115};
121 116
122bool cfperl_invoke (event_type event, ...); 117struct object_freezethaw
123void cfperl_free_ob (struct object *op); 118{
119 void *av;
120 FILE *fp;
124 121
125void cfperl_init (); 122 operator FILE *() { return fp; }
126void cfperl_main (); 123};
124
125struct object_freezer : object_freezethaw
126{
127 void *filename;
128
129 object_freezer (const char *filename);
130 ~object_freezer ();
131
132 void put (attachable_base *ext);
133
134 template<class subclass>
135 void put (attachable<subclass> *obj)
136 {
137 put ((attachable_base *)obj);
138 }
139};
140
141struct object_thawer : object_freezethaw
142{
143 object_thawer (const char *filename = 0);
144 ~object_thawer ();
145
146 void get (data_type type, void *obj, attachable_base *ext, int oid);
147
148 template<class subclass>
149 void get (attachable<subclass> *obj, int oid)
150 {
151 if (av)
152 get (subclass::get_dt (), (subclass *)obj, obj, oid);
153 }
154};
127 155
128#endif 156#endif
129 157

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines