1/* Part of XPCE --- The SWI-Prolog GUI toolkit 2 3 Author: Jan Wielemaker and Anjo Anjewierden 4 E-mail: jan@swi.psy.uva.nl 5 WWW: http://www.swi.psy.uva.nl/projects/xpce/ 6 Copyright (c) 1985-2002, University of Amsterdam 7 All rights reserved. 8 9 Redistribution and use in source and binary forms, with or without 10 modification, are permitted provided that the following conditions 11 are met: 12 13 1. Redistributions of source code must retain the above copyright 14 notice, this list of conditions and the following disclaimer. 15 16 2. Redistributions in binary form must reproduce the above copyright 17 notice, this list of conditions and the following disclaimer in 18 the documentation and/or other materials provided with the 19 distribution. 20 21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 24 FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 25 COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 26 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 27 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 28 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 29 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 31 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 POSSIBILITY OF SUCH DAMAGE. 33*/ 34 35:- module(pce_autoload, 36 [ pce_autoload/2 37 , pce_autoload_all/0 38 ]). 39 40:- use_module(pce_boot(pce_principal), 41 [ get/3, send/2, op(_,_,_) 42 ]). 43:- use_module(pce_boot(pce_realise), 44 [ pce_realise_class/1, 45 pce_prolog_class/1 46 ]). 47:- require([ is_absolute_file_name/1 48 , atomic_list_concat/2 49 , absolute_file_name/3 50 ]). 51 52:- dynamic 53 autoload_decl/2. 54:- public 55 autoload_decl/2. 56 57%! pce_autoload(+ClassName, +FileSpec) 58% 59% States class `ClassName' can be created by loading the Prolog 60% file `FileSpec'. This will actually be done if either the class 61% is actually needed by PCE or pce_autoload_all/0 is called. 62 63pce_autoload(Class, PathAlias) :- % trap library(), demo(), contrib(), .. 64 functor(PathAlias, _, 1), 65 !, 66 retractall(autoload_decl(Class, _)), 67 assert(autoload_decl(Class, PathAlias)). 68pce_autoload(Class, Abs) :- 69 is_absolute_file_name(Abs), 70 !, 71 absolute_file_name(Abs, Canonical), 72 retractall(autoload_decl(Class, _)), 73 assert(autoload_decl(Class, Canonical)). 74pce_autoload(Class, Local) :- 75 prolog_load_context(directory, Dir), 76 atomic_list_concat([Dir, /, Local], File), 77 pce_host:property(file_extensions(Exts)), 78 absolute_file_name(File, 79 [ extensions(Exts), 80 access(exist) 81 ], Abs), 82 retractall(autoload_decl(Class, _)), 83 assert(autoload_decl(Class, Abs)). 84 85%! pce_autoload_all 86% 87% Load all classes declared using the pce_autoload/2 88% directive. Useful for debugging purposes. 89 90pce_autoload_all :- 91 autoload_decl(Class, File), 92 \+ get(@classes, member, Class, _), 93 \+ pce_prolog_class(Class), 94 ensure_loaded(user:File), 95 fail. 96pce_autoload_all. 97 98 99register_handler :- 100 send(@pce?exception_handlers, append( 101 attribute(undefined_class, 102 message(@prolog, call, trap_autoload, @arg1)))). 103 104:- initialization 105 register_handler. 106 107pce_ifhostproperty(prolog(swi), 108 (:- '$hide'(trap_autoload/1)), 109 (notrace(G) :- G)). 110 111trap_autoload(Class) :- 112 notrace(do_trap_autoload(Class)). 113 114do_trap_autoload(Class) :- 115 pce_realise_class(Class), 116 !. 117do_trap_autoload(Class) :- 118 autoload_decl(Class, File), 119 load_files(user:File, 120 [ autoload(true) 121 ]), 122 pce_realise_class(Class)