View source with raw comments or as raw
    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/2.
 pce_autoload(+ClassName, +FileSpec)
States class `ClassName' can be created by loading the Prolog file `FileSpec'. This will actually be done if either the class is actually needed by PCE or pce_autoload_all/0 is called.
   61pce_autoload(Class, PathAlias) :-       % trap library(), demo(), contrib(), ..
   62    functor(PathAlias, _, 1),
   63    !,
   64    retractall(autoload(Class, _)),
   65    assert(autoload(Class, PathAlias)).
   66pce_autoload(Class, Abs) :-
   67    is_absolute_file_name(Abs),
   68    !,
   69    absolute_file_name(Abs, Canonical),
   70    retractall(autoload(Class, _)),
   71    assert(autoload(Class, Canonical)).
   72pce_autoload(Class, Local) :-
   73    prolog_load_context(directory, Dir),
   74    atomic_list_concat([Dir, /, Local], File),
   75    pce_host:property(file_extensions(Exts)),
   76    absolute_file_name(File,
   77                       [ extensions(Exts),
   78                         access(exist)
   79                       ], Abs),
   80    retractall(autoload(Class, _)),
   81    assert(autoload(Class, Abs)).
 pce_autoload_all
Load all classes declared using the pce_autoload/2 directive. Useful for debugging purposes.
   88pce_autoload_all :-
   89    autoload(Class, File),
   90    \+ get(@classes, member, Class, _),
   91    \+ pce_prolog_class(Class),
   92    ensure_loaded(user:File),
   93    fail.
   94pce_autoload_all.
   95
   96
   97register_handler :-
   98    send(@pce?exception_handlers, append(
   99         attribute(undefined_class,
  100                   message(@prolog, call, trap_autoload, @arg1)))).
  101
  102:- initialization
  103    register_handler.  104
  105pce_ifhostproperty(prolog(swi),
  106                   (:- '$hide'(trap_autoload/1)),
  107                   (notrace(G) :- G)).
  108
  109trap_autoload(Class) :-
  110    notrace(do_trap_autoload(Class)).
  111
  112do_trap_autoload(Class) :-
  113    pce_realise_class(Class),
  114    !.
  115do_trap_autoload(Class) :-
  116    autoload(Class, File),
  117    load_files(user:File,
  118               [ autoload(true)
  119               ]),
  120    pce_realise_class(Class)