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.16 by root, Wed Aug 30 16:30:37 2006 UTC

2// cfperl.h perl interface 2// cfperl.h perl interface
3// 3//
4#ifndef CFPERL_H__ 4#ifndef CFPERL_H__
5#define CFPERL_H__ 5#define CFPERL_H__
6 6
7#include <cstdio>
8
9using namespace std;
10
11#include <EXTERN.h>
12#include <perl.h>
13
14// optimisations/workaround for functions requiring my_perl in scope (anti-bloat)
15#undef localtime
16#undef srand48
17#undef drand48
18#undef srandom
19#undef readdir
20#undef getprotobyname
21#undef gethostbyname
22#undef ctime
23#undef strerror
24
25// perl bug #40256: perl does overwrite those with reentrant versions
26// but does not initialise their state structures.
27#undef random
28#undef crypt
29
7struct pl; 30struct pl;
8struct object; 31struct object;
9struct mapstruct; 32struct mapstruct;
33
34void cfperl_init ();
35void cfperl_boot ();
36void cfperl_main ();
10 37
11// could have used templates, but a more traditional C api 38// could have used templates, but a more traditional C api
12// uses more explicit typing which is ok for this purpose. 39// uses more explicit typing which is ok for this purpose.
13enum data_type 40enum data_type
14{ 41{
15 DT_END, // no further arguments 42 DT_END, // no further arguments
43 DT_AV, // perl-only av that needs to be flattened out
16 DT_INT, 44 DT_INT,
17 DT_INT64, 45 DT_INT64,
18 DT_DOUBLE, 46 DT_DOUBLE,
19 DT_STRING, // 0-terminated string 47 DT_STRING, // 0-terminated string
20 DT_DATA, // string + length 48 DT_DATA, // string + length
23 DT_PLAYER, 51 DT_PLAYER,
24 DT_ARCH, 52 DT_ARCH,
25 DT_PARTY, 53 DT_PARTY,
26 DT_REGION, 54 DT_REGION,
27 55
28 // following are only for cfapi-compatibility 56 NUM_DATA_TYPES
29 DT_INT_PTR,
30 DT_INT64_PTR,
31 DT_DOUBLE_PTR,
32}; 57};
33 58
34enum event_klass 59enum event_klass
35{ 60{
61 KLASS_NONE,
36 KLASS_GLOBAL, 62 KLASS_GLOBAL,
37 KLASS_PLAYER, 63 KLASS_PLAYER,
38 KLASS_OBJECT, 64 KLASS_OBJECT,
39 KLASS_MAP, 65 KLASS_MAP,
40}; 66};
41 67
42enum event_type 68enum event_type
43{ 69{
44# define def(klass,name) EV_ ## klass ## _ ## name, 70# define def(klass,name) EVENT_ ## klass ## _ ## name,
45# include "eventinc.h" 71# include "eventinc.h"
46# undef def 72# undef def
47 NUM_EVENT_TYPES 73 NUM_EVENT_TYPES
48}; 74};
49 75
76#define ARG_AV(o) DT_AV , static_cast<AV *> (o)
50#define ARG_INT(v) DT_INT , static_cast<int> (v) 77#define ARG_INT(v) DT_INT , static_cast<int> (v)
51#define ARG_INT64(v) DT_INT64 , static_cast<sint64> (v) 78#define ARG_INT64(v) DT_INT64 , static_cast<sint64> (v)
52#define ARG_DOUBLE(v) DT_DOUBLE, static_cast<double> (v) 79#define ARG_DOUBLE(v) DT_DOUBLE, static_cast<double> (v)
53#define ARG_STRING(v) DT_STRING, static_cast<const char *> (v) 80#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) 81#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) 87#define ARG_REGION(o) DT_REGION, (void *)static_cast<struct region *> (o)
61 88
62// the ", ## __VA_ARGS" is, unfortunately, a gnu-cpp extension 89// the ", ## __VA_ARGS" is, unfortunately, a gnu-cpp extension
63 90
64// all these return true when the normal event processing should be skipped (if any) 91// 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) 92#define INVOKE_(event, ...) cfperl_invoke (event, ## __VA_ARGS__, DT_END)
66 93
94#define INVOKE(klass, event, ...) INVOKE_(EVENT_ ## klass ## _ ## event, ## __VA_ARGS__)
67#define INVOKE_GLOBAL(event, ...) INVOKE (GLOBAL, event, ## __VA_ARGS__) 95#define INVOKE_GLOBAL(event, ...) INVOKE_(EVENT_ ## GLOBAL ## _ ## event, ## __VA_ARGS__)
68#define INVOKE_OBJECT(event, op, ...) INVOKE (OBJECT, event, ARG_OBJECT (op), ## __VA_ARGS__) 96#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__) 97#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__) 98#define INVOKE_MAP(event, map, ...) INVOKE_(EVENT_ ## MAP ## _ ## event, ARG_MAP (map) , ## __VA_ARGS__)
71 99
100//TODO should index into @result
101#define RESULT(idx,type) cfperl_result_ ## type (idx)
102#define RESULT_INT(idx) RESULT(idx, INT)
103
104bool cfperl_invoke (event_type event, ...);
105int cfperl_result_INT (int idx);
106
72struct extendable_base 107struct attachable_base
73{ 108{
74 void *self, *cb; // CF+ perl self and callback 109 struct sv *self;
110 struct av *cb; // CF+ perl self and callback
75 const char *attach; // generic extension attachment information 111 const char *attach; // generic extension attachment information
76 112
77 void clear (); // called when free'ing objects 113 void clear (); // called when free'ing objects
78 void optimise (); // possibly save some memory by destroying unneeded data 114 void optimise (); // possibly save some memory by destroying unneeded data
79 void reattach (data_type type, void *self); // called after swapin 115 void instantiate (data_type type, void *obj);
80 void instantiate (data_type type, void *self); // called on first instantiation 116
81 void clone (data_type type, void *self, void *dest); 117 void attachable_init ()
118 {
119 self = 0;
120 cb = 0;
121 attach = 0;
122 }
82}; 123};
83 124
84// objects extendable from perl (or any other extension) should include or 125// objects attachable from perl (or any other extension) should include or
85// derive using the curiously recurring template pattern, to avoid 126// derive using the curiously recurring template pattern, to avoid
86// virtual method calls etc. 127// virtual method calls etc.
87template<class subclass> 128template<class subclass>
88struct extendable : extendable_base 129struct attachable : attachable_base
89{ 130{
90 void clear () 131 static data_type get_dt ()
91 { 132 {
92 if (self || cb) 133 return subclass::get_dt ();
93 extendable_base::clear ();
94 } 134 }
135
95 void instantiate () 136 void instantiate ()
96 { 137 {
97 if (attach) 138 if (attach)
98 extendable_base::instantiate ( 139 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 } 140 }
120}; 141};
121 142
122bool cfperl_invoke (event_type event, ...); 143struct object_freezethaw
123void cfperl_free_ob (struct object *op); 144{
145 struct av *av;
146};
124 147
125void cfperl_init (); 148struct object_freezer : object_freezethaw
126void cfperl_main (); 149{
150 struct sv *text;
151 void *filename;
152
153 object_freezer (const char *filename);
154 ~object_freezer ();
155
156 void put (attachable_base *ext);
157
158 template<class subclass>
159 void put (attachable<subclass> *obj)
160 {
161 put ((attachable_base *)obj);
162 }
163
164 operator bool () { return !!av; }
165};
166
167struct object_thawer : object_freezethaw
168{
169 FILE *fp;
170
171 object_thawer (const char *filename = 0);
172 ~object_thawer ();
173
174 void get (data_type type, void *obj, attachable_base *ext, int oid);
175
176 template<class subclass>
177 void get (attachable<subclass> *obj, int oid)
178 {
179 if (av)
180 get (subclass::get_dt (), (subclass *)obj, obj, oid);
181 }
182
183 operator FILE *() { return fp; }
184};
185
186// compatibility support, should be removed when no longer needed
187int fprintf (object_freezer &freezer, const char *format, ...);
188int fputs (const char *s, object_freezer &freezer);
127 189
128#endif 190#endif
129 191

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines