… | |
… | |
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 |
|
|
117 | void instantiate (data_type type, void *obj); |
90 | |
118 | |
91 | void attachable_init () |
119 | void attachable_init () |
92 | { |
120 | { |
93 | self = cb = 0; |
121 | self = 0; |
|
|
122 | cb = 0; |
94 | attach = 0; |
123 | attach = 0; |
95 | } |
124 | } |
96 | }; |
125 | }; |
97 | |
126 | |
98 | // objects attachable from perl (or any other extension) should include or |
127 | // objects attachable from perl (or any other extension) should include or |
… | |
… | |
101 | template<class subclass> |
130 | template<class subclass> |
102 | struct attachable : attachable_base |
131 | struct attachable : attachable_base |
103 | { |
132 | { |
104 | static data_type get_dt () |
133 | static data_type get_dt () |
105 | { |
134 | { |
106 | subclass::get_dt (); |
135 | return subclass::get_dt (); |
107 | } |
136 | } |
108 | }; |
|
|
109 | |
137 | |
|
|
138 | void instantiate () |
|
|
139 | { |
|
|
140 | if (attach) |
|
|
141 | attachable_base::instantiate (get_dt (), static_cast<subclass *>(this)); |
|
|
142 | } |
|
|
143 | }; |
|
|
144 | |
110 | struct object_freezethaw |
145 | struct object_freezer |
111 | { |
146 | { |
112 | void *av; |
147 | AV *av; |
113 | FILE *fp; |
148 | SV *text; |
114 | |
149 | |
115 | operator FILE *() { return fp; } |
150 | object_freezer (); |
116 | }; |
|
|
117 | |
|
|
118 | struct object_freezer : object_freezethaw |
|
|
119 | { |
|
|
120 | void *filename; |
|
|
121 | |
|
|
122 | object_freezer (const char *filename); |
|
|
123 | ~object_freezer (); |
151 | ~object_freezer (); |
124 | |
152 | |
125 | void put (attachable_base *ext); |
153 | void put (attachable_base *ext); |
126 | |
154 | |
127 | template<class subclass> |
155 | template<class subclass> |
128 | void put (attachable<subclass> *obj) |
156 | void put (attachable<subclass> *obj) |
129 | { |
157 | { |
130 | put ((attachable_base *)obj); |
158 | put ((attachable_base *)obj); |
131 | } |
159 | } |
132 | }; |
|
|
133 | |
160 | |
134 | 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 |
135 | { |
179 | { |
|
|
180 | AV *av; |
|
|
181 | FILE *fp; |
|
|
182 | char line[1024]; |
|
|
183 | |
136 | object_thawer (FILE *fp, const char *filename = 0); |
184 | object_thawer (const char *filename = 0); |
137 | ~object_thawer (); |
185 | ~object_thawer (); |
138 | |
186 | |
139 | 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); |
140 | |
188 | |
141 | template<class subclass> |
189 | template<class subclass> |
142 | void get (attachable<subclass> *obj, int oid) |
190 | void get (attachable<subclass> *obj, int oid) |
143 | { |
191 | { |
144 | if (av) |
192 | if (av) |
145 | get (subclass::get_dt (), (subclass *)obj, obj, oid); |
193 | get (subclass::get_dt (), (subclass *)obj, obj, oid); |
146 | } |
194 | } |
|
|
195 | |
|
|
196 | token get_token (); |
|
|
197 | |
|
|
198 | operator FILE *() { return fp; } |
147 | }; |
199 | }; |
148 | |
200 | |
149 | #endif |
201 | #endif |
150 | |
202 | |