ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/include/cfperl.h
Revision: 1.16
Committed: Wed Aug 30 16:30:37 2006 UTC (17 years, 8 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.15: +34 -5 lines
Log Message:
remove compression support, intiialise perl earlier etc. etc.

File Contents

# User Rev Content
1 root 1.1 //
2     // cfperl.h perl interface
3     //
4 root 1.2 #ifndef CFPERL_H__
5     #define CFPERL_H__
6    
7 root 1.16 #include <cstdio>
8    
9     using 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    
30 root 1.6 struct pl;
31     struct object;
32     struct mapstruct;
33    
34 root 1.7 void cfperl_init ();
35 root 1.16 void cfperl_boot ();
36 root 1.7 void cfperl_main ();
37    
38 root 1.2 // could have used templates, but a more traditional C api
39     // uses more explicit typing which is ok for this purpose.
40 root 1.6 enum data_type
41     {
42 root 1.2 DT_END, // no further arguments
43 root 1.11 DT_AV, // perl-only av that needs to be flattened out
44 root 1.2 DT_INT,
45 root 1.5 DT_INT64,
46 root 1.2 DT_DOUBLE,
47     DT_STRING, // 0-terminated string
48     DT_DATA, // string + length
49     DT_OBJECT,
50     DT_MAP,
51     DT_PLAYER,
52     DT_ARCH,
53     DT_PARTY,
54     DT_REGION,
55 root 1.8
56     NUM_DATA_TYPES
57 root 1.2 };
58    
59 root 1.6 enum event_klass
60     {
61 root 1.11 KLASS_NONE,
62 root 1.3 KLASS_GLOBAL,
63     KLASS_PLAYER,
64     KLASS_OBJECT,
65     KLASS_MAP,
66     };
67    
68 root 1.6 enum event_type
69     {
70 root 1.7 # define def(klass,name) EVENT_ ## klass ## _ ## name,
71 root 1.2 # include "eventinc.h"
72     # undef def
73 root 1.4 NUM_EVENT_TYPES
74 root 1.2 };
75    
76 root 1.11 #define ARG_AV(o) DT_AV , static_cast<AV *> (o)
77 root 1.3 #define ARG_INT(v) DT_INT , static_cast<int> (v)
78 root 1.5 #define ARG_INT64(v) DT_INT64 , static_cast<sint64> (v)
79 root 1.3 #define ARG_DOUBLE(v) DT_DOUBLE, static_cast<double> (v)
80 root 1.5 #define ARG_STRING(v) DT_STRING, static_cast<const char *> (v)
81     #define ARG_DATA(s,l) DT_DATA , static_cast<const void *> (s), (l)
82 root 1.6 #define ARG_OBJECT(o) DT_OBJECT, (void *)static_cast<struct object *> (o)
83     #define ARG_MAP(o) DT_MAP , (void *)static_cast<struct mapstruct *> (o)
84     #define ARG_PLAYER(o) DT_PLAYER, (void *)static_cast<struct pl *> (o)
85     #define ARG_ARCH(o) DT_ARCH , (void *)static_cast<struct archetype *> (o)
86     #define ARG_PARTY(o) DT_PARTY , (void *)static_cast<struct party *> (o)
87     #define ARG_REGION(o) DT_REGION, (void *)static_cast<struct region *> (o)
88 root 1.3
89     // the ", ## __VA_ARGS" is, unfortunately, a gnu-cpp extension
90 root 1.2
91 root 1.4 // all these return true when the normal event processing should be skipped (if any)
92 root 1.9 #define INVOKE_(event, ...) cfperl_invoke (event, ## __VA_ARGS__, DT_END)
93 root 1.1
94 root 1.9 #define INVOKE(klass, event, ...) INVOKE_(EVENT_ ## klass ## _ ## event, ## __VA_ARGS__)
95     #define INVOKE_GLOBAL(event, ...) INVOKE_(EVENT_ ## GLOBAL ## _ ## event, ## __VA_ARGS__)
96     #define INVOKE_OBJECT(event, op, ...) INVOKE_(EVENT_ ## OBJECT ## _ ## event, ARG_OBJECT (op), ## __VA_ARGS__)
97     #define INVOKE_PLAYER(event, pl, ...) INVOKE_(EVENT_ ## PLAYER ## _ ## event, ARG_PLAYER (pl), ## __VA_ARGS__)
98     #define INVOKE_MAP(event, map, ...) INVOKE_(EVENT_ ## MAP ## _ ## event, ARG_MAP (map) , ## __VA_ARGS__)
99 root 1.3
100 root 1.7 //TODO should index into @result
101     #define RESULT(idx,type) cfperl_result_ ## type (idx)
102     #define RESULT_INT(idx) RESULT(idx, INT)
103    
104     bool cfperl_invoke (event_type event, ...);
105     int cfperl_result_INT (int idx);
106    
107 root 1.10 struct attachable_base
108 root 1.6 {
109 root 1.16 struct sv *self;
110     struct av *cb; // CF+ perl self and callback
111 root 1.6 const char *attach; // generic extension attachment information
112    
113 root 1.13 void clear (); // called when free'ing objects
114     void optimise (); // possibly save some memory by destroying unneeded data
115     void instantiate (data_type type, void *obj);
116 root 1.8
117 root 1.10 void attachable_init ()
118 root 1.8 {
119 root 1.16 self = 0;
120     cb = 0;
121 root 1.8 attach = 0;
122     }
123 root 1.6 };
124    
125 root 1.10 // objects attachable from perl (or any other extension) should include or
126 root 1.6 // derive using the curiously recurring template pattern, to avoid
127     // virtual method calls etc.
128     template<class subclass>
129 root 1.10 struct attachable : attachable_base
130 root 1.6 {
131 root 1.8 static data_type get_dt ()
132 root 1.6 {
133 root 1.13 return subclass::get_dt ();
134     }
135    
136     void instantiate ()
137     {
138     if (attach)
139     attachable_base::instantiate (get_dt (), static_cast<subclass *>(this));
140 root 1.6 }
141     };
142    
143 root 1.12 struct object_freezethaw
144 root 1.7 {
145 root 1.16 struct av *av;
146 root 1.12 };
147    
148     struct object_freezer : object_freezethaw
149     {
150 root 1.16 struct sv *text;
151 root 1.12 void *filename;
152 root 1.8
153     object_freezer (const char *filename);
154     ~object_freezer ();
155    
156 root 1.12 void put (attachable_base *ext);
157 root 1.8
158     template<class subclass>
159 root 1.12 void put (attachable<subclass> *obj)
160 root 1.8 {
161 root 1.12 put ((attachable_base *)obj);
162 root 1.8 }
163 root 1.15
164 root 1.16 operator bool () { return !!av; }
165 root 1.7 };
166    
167 root 1.12 struct object_thawer : object_freezethaw
168 root 1.7 {
169 root 1.16 FILE *fp;
170    
171 root 1.14 object_thawer (const char *filename = 0);
172 root 1.8 ~object_thawer ();
173    
174 root 1.11 void get (data_type type, void *obj, attachable_base *ext, int oid);
175 root 1.8
176     template<class subclass>
177 root 1.11 void get (attachable<subclass> *obj, int oid)
178 root 1.8 {
179     if (av)
180 root 1.11 get (subclass::get_dt (), (subclass *)obj, obj, oid);
181 root 1.8 }
182 root 1.15
183     operator FILE *() { return fp; }
184 root 1.7 };
185 root 1.2
186 root 1.16 // compatibility support, should be removed when no longer needed
187 root 1.15 int fprintf (object_freezer &freezer, const char *format, ...);
188     int fputs (const char *s, object_freezer &freezer);
189    
190 root 1.2 #endif
191 root 1.3