… | |
… | |
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 | |
|
|
9 | using namespace std; |
|
|
10 | |
|
|
11 | #include <EXTERN.h> |
|
|
12 | #include <perl.h> |
|
|
13 | |
|
|
14 | #include "keyword.h" |
|
|
15 | |
|
|
16 | // optimisations/workaround for functions requiring my_perl in scope (anti-bloat) |
|
|
17 | #undef localtime |
|
|
18 | #undef srand48 |
|
|
19 | #undef drand48 |
|
|
20 | #undef srandom |
|
|
21 | #undef readdir |
|
|
22 | #undef getprotobyname |
|
|
23 | #undef gethostbyname |
|
|
24 | #undef ctime |
|
|
25 | #undef strerror |
|
|
26 | |
|
|
27 | // perl bug #40256: perl does overwrite those with reentrant versions |
|
|
28 | // but does not initialise their state structures. |
|
|
29 | #undef random |
|
|
30 | #undef crypt |
|
|
31 | |
7 | struct pl; |
32 | struct pl; |
8 | struct object; |
33 | struct object; |
9 | struct mapstruct; |
34 | struct mapstruct; |
10 | |
35 | |
11 | void cfperl_init (); |
36 | void cfperl_init (); |
|
|
37 | void cfperl_boot (); |
12 | void cfperl_main (); |
38 | void cfperl_main (); |
13 | |
39 | |
14 | // could have used templates, but a more traditional C api |
40 | // could have used templates, but a more traditional C api |
15 | // uses more explicit typing which is ok for this purpose. |
41 | // uses more explicit typing which is ok for this purpose. |
16 | enum data_type |
42 | enum data_type |
… | |
… | |
80 | bool cfperl_invoke (event_type event, ...); |
106 | bool cfperl_invoke (event_type event, ...); |
81 | int cfperl_result_INT (int idx); |
107 | int cfperl_result_INT (int idx); |
82 | |
108 | |
83 | struct attachable_base |
109 | struct attachable_base |
84 | { |
110 | { |
85 | void *self, *cb; // CF+ perl self and callback |
111 | SV *self; |
|
|
112 | AV *cb; // CF+ perl self and callback |
86 | const char *attach; // generic extension attachment information |
113 | const char *attach; // generic extension attachment information |
87 | |
114 | |
88 | void clear (); // called when free'ing objects |
115 | void clear (); // called when free'ing objects |
89 | void optimise (); // possibly save some memory by destroying unneeded data |
116 | void optimise (); // possibly save some memory by destroying unneeded data |
90 | void instantiate (data_type type, void *obj); |
117 | void instantiate (data_type type, void *obj); |
91 | |
118 | |
92 | void attachable_init () |
119 | void attachable_init () |
93 | { |
120 | { |
94 | self = cb = 0; |
121 | self = 0; |
|
|
122 | cb = 0; |
95 | attach = 0; |
123 | attach = 0; |
96 | } |
124 | } |
97 | }; |
125 | }; |
98 | |
126 | |
99 | // objects attachable from perl (or any other extension) should include or |
127 | // objects attachable from perl (or any other extension) should include or |
… | |
… | |
112 | if (attach) |
140 | if (attach) |
113 | attachable_base::instantiate (get_dt (), static_cast<subclass *>(this)); |
141 | attachable_base::instantiate (get_dt (), static_cast<subclass *>(this)); |
114 | } |
142 | } |
115 | }; |
143 | }; |
116 | |
144 | |
117 | struct object_freezethaw |
145 | struct object_freezer |
118 | { |
146 | { |
119 | void *av; |
147 | AV *av; |
120 | FILE *fp; |
148 | SV *text; |
121 | |
149 | |
122 | operator FILE *() { return fp; } |
150 | object_freezer (); |
123 | }; |
|
|
124 | |
|
|
125 | struct object_freezer : object_freezethaw |
|
|
126 | { |
|
|
127 | void *filename; |
|
|
128 | |
|
|
129 | object_freezer (const char *filename); |
|
|
130 | ~object_freezer (); |
151 | ~object_freezer (); |
131 | |
152 | |
132 | void put (attachable_base *ext); |
153 | void put (attachable_base *ext); |
133 | |
154 | |
134 | template<class subclass> |
155 | template<class subclass> |
135 | void put (attachable<subclass> *obj) |
156 | void put (attachable<subclass> *obj) |
136 | { |
157 | { |
137 | put ((attachable_base *)obj); |
158 | put ((attachable_base *)obj); |
138 | } |
159 | } |
139 | }; |
|
|
140 | |
160 | |
141 | struct object_thawer : object_freezethaw |
161 | bool save (const char *filename); |
|
|
162 | |
|
|
163 | operator bool () { return !!av; } |
|
|
164 | }; |
|
|
165 | |
|
|
166 | // compatibility support, should be removed when no longer needed |
|
|
167 | int fprintf (object_freezer &freezer, const char *format, ...); |
|
|
168 | int fputs (const char *s, object_freezer &freezer); |
|
|
169 | |
|
|
170 | // a single key-value line from a file |
|
|
171 | struct token { |
|
|
172 | keyword k; |
|
|
173 | const char *v; |
|
|
174 | |
|
|
175 | token (keyword k, const char *v = 0) : k(k), v(v) { } |
|
|
176 | }; |
|
|
177 | |
|
|
178 | struct object_thawer |
142 | { |
179 | { |
|
|
180 | AV *av; |
|
|
181 | FILE *fp; |
|
|
182 | char line[1024]; |
|
|
183 | |
143 | object_thawer (FILE *fp, const char *filename = 0); |
184 | object_thawer (const char *filename = 0); |
144 | ~object_thawer (); |
185 | ~object_thawer (); |
145 | |
186 | |
146 | void get (data_type type, void *obj, attachable_base *ext, int oid); |
187 | void get (data_type type, void *obj, attachable_base *ext, int oid); |
147 | |
188 | |
148 | template<class subclass> |
189 | template<class subclass> |
149 | void get (attachable<subclass> *obj, int oid) |
190 | void get (attachable<subclass> *obj, int oid) |
150 | { |
191 | { |
151 | if (av) |
192 | if (av) |
152 | get (subclass::get_dt (), (subclass *)obj, obj, oid); |
193 | get (subclass::get_dt (), (subclass *)obj, obj, oid); |
153 | } |
194 | } |
|
|
195 | |
|
|
196 | token get_token (); |
|
|
197 | |
|
|
198 | operator FILE *() { return fp; } |
154 | }; |
199 | }; |
155 | |
200 | |
156 | #endif |
201 | #endif |
157 | |
202 | |