35
36:- module(pce_principal,
37 [ new/2, free/1,
38
39 send/2, send/3, send/4, send/5, send/6, send/7,
40 send/8,
41
42 get/3, get/4, get/5, get/6, get/7, get/8,
43
44 send_class/3,
45 get_class/4,
46
47 object/1, object/2,
48
49 pce_class/6,
50 pce_lazy_send_method/3,
51 pce_lazy_get_method/3,
52 pce_uses_template/2,
53
54 pce_method_implementation/2,
55
56 pce_open/3, 57 in_pce_thread/1, 58 set_pce_thread/0,
59 pce_dispatch/0,
60
61 pce_postscript_stream/1, 62
63 op(200, fy, @),
64 op(250, yfx, ?),
65 op(990, xfx, :=)
66 ]). 67
68
69:- meta_predicate
70 send_class(+, +, :),
71 send(+, :),
72 send(+, :, +),
73 send(+, :, +, +),
74 send(+, :, +, +, +),
75 send(+, :, +, +, +, +),
76 send(+, :, +, +, +, +, +),
77
78 get_class(+, +, :, -),
79 get(+, :, -),
80 get(+, :, +, -),
81 get(+, :, +, +, -),
82 get(+, :, +, +, +, -),
83 get(+, :, +, +, +, +, -),
84 get(+, :, +, +, +, +, +, -),
85
86 new(?, :). 87
88:- op(100, fx, @). 89:- op(150, yfx, ?). 90:- op(990, xfx, :=). 91
92
100pce_home(PceHome) :-
101 absolute_file_name(pce('.'), PceHome,
102 [ file_type(directory),
103 file_errors(fail)
104 ]),
105 exists_directory(PceHome),
106 !.
107pce_home(PceHome) :-
108 getenv('XPCEHOME', PceHome),
109 exists_directory(PceHome),
110 !.
111pce_home(PceHome) :-
112 ( current_prolog_flag(xpce_version, Version),
113 atom_concat('/xpce-', Version, Suffix)
114 ; Suffix = '/xpce'
115 ),
116 absolute_file_name(swi(Suffix), PceHome,
117 [ file_type(directory),
118 file_errors(fail)
119 ]),
120 exists_directory(PceHome),
121 !.
122pce_home(PceHome) :-
123 current_prolog_flag(saved_program, true),
124 !,
125 ( current_prolog_flag(home, PceHome)
126 -> true
127 ; current_prolog_flag(executable, Exe)
128 -> file_directory_name(Exe, PceHome)
129 ; PceHome = '.'
130 ).
131pce_home(_) :-
132 throw(error(pce_error(no_home), _)).
133
134
135 138
139init_pce :-
140 catch(load_foreign_library(foreign(pl2xpce)),
141 error(Error, _Context), 142 ( print_message(error, error(Error, _)),
143 fail
144 )),
145 pce_home(Home),
146 pce_init(Home),
147 !,
148 create_prolog_flag(xpce, true, []),
149 thread_self(Me),
150 assert(pce:pce_thread(Me)).
151init_pce :-
152 print_message(error, error(pce_error(init_failed), _)),
153 halt(1).
154
155:- initialization(init_pce, now). 156
157:- noprofile((send_implementation/3,
158 get_implementation/4,
159 send_class/3,
160 get_class/4,
161 new/2,
162 send/2,
163 get/3)). 164
165
166
175free(Ref) :-
176 object(Ref),
177 !,
178 send(Ref, free).
179free(_).
188send(Receiver, M:Selector, A1) :-
189 functor(Message, Selector, 1),
190 arg(1, Message, A1),
191 send(Receiver, M:Message).
192
193send(Receiver, M:Selector, A1, A2) :-
194 functor(Message, Selector, 2),
195 arg(1, Message, A1),
196 arg(2, Message, A2),
197 send(Receiver, M:Message).
198
199send(Receiver, M:Selector, A1, A2, A3) :-
200 functor(Message, Selector, 3),
201 arg(1, Message, A1),
202 arg(2, Message, A2),
203 arg(3, Message, A3),
204 send(Receiver, M:Message).
205
206send(Receiver, M:Selector, A1, A2, A3, A4) :-
207 functor(Message, Selector, 4),
208 arg(1, Message, A1),
209 arg(2, Message, A2),
210 arg(3, Message, A3),
211 arg(4, Message, A4),
212 send(Receiver, M:Message).
213
214send(Receiver, M:Selector, A1, A2, A3, A4, A5) :-
215 functor(Message, Selector, 5),
216 arg(1, Message, A1),
217 arg(2, Message, A2),
218 arg(3, Message, A3),
219 arg(4, Message, A4),
220 arg(5, Message, A5),
221 send(Receiver, M:Message).
222
223send(Receiver, M:Selector, A1, A2, A3, A4, A5, A6) :-
224 functor(Message, Selector, 6),
225 arg(1, Message, A1),
226 arg(2, Message, A2),
227 arg(3, Message, A3),
228 arg(4, Message, A4),
229 arg(5, Message, A5),
230 arg(6, Message, A6),
231 send(Receiver, M:Message).
238get(Receiver, M:Selector, A1, Answer) :-
239 functor(Message, Selector, 1),
240 arg(1, Message, A1),
241 get(Receiver, M:Message, Answer).
242
243get(Receiver, M:Selector, A1, A2, Answer) :-
244 functor(Message, Selector, 2),
245 arg(1, Message, A1),
246 arg(2, Message, A2),
247 get(Receiver, M:Message, Answer).
248
249get(Receiver, M:Selector, A1, A2, A3, Answer) :-
250 functor(Message, Selector, 3),
251 arg(1, Message, A1),
252 arg(2, Message, A2),
253 arg(3, Message, A3),
254 get(Receiver, M:Message, Answer).
255
256get(Receiver, M:Selector, A1, A2, A3, A4, Answer) :-
257 functor(Message, Selector, 4),
258 arg(1, Message, A1),
259 arg(2, Message, A2),
260 arg(3, Message, A3),
261 arg(4, Message, A4),
262 get(Receiver, M:Message, Answer).
263
264get(Receiver, M:Selector, A1, A2, A3, A4, A5, Answer) :-
265 functor(Message, Selector, 5),
266 arg(1, Message, A1),
267 arg(2, Message, A2),
268 arg(3, Message, A3),
269 arg(4, Message, A4),
270 arg(5, Message, A5),
271 get(Receiver, M:Message, Answer).
272
273
274 277
278:- multifile
279 send_implementation/3,
280 get_implementation/4.
290send_implementation(true, _Args, _Obj).
291send_implementation(fail, _Args, _Obj) :- fail.
292send_implementation(once(Id), Args, Obj) :-
293 send_implementation(Id, Args, Obj),
294 !.
295send_implementation(spy(Id), Args, Obj) :-
296 ( current_prolog_flag(debug, true)
297 -> trace,
298 send_implementation(Id, Args, Obj)
299 ; send_implementation(Id, Args, Obj)
300 ).
301send_implementation(trace(Id), Args, Obj) :-
302 pce_info(pce_trace(enter, send_implementation(Id, Args, Obj))),
303 ( send_implementation(Id, Args, Obj)
304 -> pce_info(pce_trace(exit, send_implementation(Id, Args, Obj)))
305 ; pce_info(pce_trace(fail, send_implementation(Id, Args, Obj)))
306 ).
313get_implementation(true, _Args, _Obj, _Rval).
314get_implementation(fail, _Args, _Obj, _Rval) :- fail.
315get_implementation(once(Id), Args, Obj, Rval) :-
316 get_implementation(Id, Args, Obj, Rval),
317 !.
318get_implementation(spy(Id), Args, Obj, Rval) :-
319 ( current_prolog_flag(debug, true)
320 -> trace,
321 get_implementation(Id, Args, Obj, Rval)
322 ; get_implementation(Id, Args, Obj, Rval)
323 ).
324get_implementation(trace(Id), Args, Obj, Rval) :-
325 pce_info(pce_trace(enter, get_implementation(Id, Args, Obj, Rval))),
326 ( get_implementation(Id, Args, Obj, Rval)
327 -> pce_info(pce_trace(exit, get_implementation(Id, Args, Obj, Rval)))
328 ; pce_info(pce_trace(fail, get_implementation(Id, Args, Obj, Rval))),
329 fail
330 ).
331
333
334pce_ifhostproperty(prolog(swi), [
335(:- unlock_predicate(send_implementation/3)),
336(:- unlock_predicate(get_implementation/4)),
337(:- '$set_predicate_attribute'(send_implementation(_,_,_), hide_childs, false)),
338(:- '$set_predicate_attribute'(get_implementation(_,_,_,_), hide_childs, false))
339 ]).
340
341
342 345
346:- multifile
347 pce_class/6,
348 pce_lazy_send_method/3,
349 pce_lazy_get_method/3,
350 pce_uses_template/2. 351
352
353 356
357:- initialization
358 (object(@prolog) -> true ; send(@host, name_reference, prolog)).