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:- use_module(pce_boot(pce_realise),
   42              [ pce_realise_class/1,
   43                pce_prolog_class/1
   44              ]).   45:- require([ is_absolute_file_name/1
   46           , atomic_list_concat/2
   47           , absolute_file_name/3
   48           , get/3
   49           ]).   50
   51:- dynamic
   52    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.
   60pce_autoload(Class, PathAlias) :-       % trap library(), demo(), contrib(), ..
   61    functor(PathAlias, _, 1),
   62    !,
   63    retractall(autoload(Class, _)),
   64    assert(autoload(Class, PathAlias)).
   65pce_autoload(Class, Abs) :-
   66    is_absolute_file_name(Abs),
   67    !,
   68    absolute_file_name(Abs, Canonical),
   69    retractall(autoload(Class, _)),
   70    assert(autoload(Class, Canonical)).
   71pce_autoload(Class, Local) :-
   72    prolog_load_context(directory, Dir),
   73    atomic_list_concat([Dir, /, Local], File),
   74    pce_host:property(file_extensions(Exts)),
   75    absolute_file_name(File,
   76                       [ extensions(Exts),
   77                         access(exist)
   78                       ], Abs),
   79    retractall(autoload(Class, _)),
   80    assert(autoload(Class, Abs)).
 pce_autoload_all
Load all classes declared using the pce_autoload/2 directive. Useful for debugging purposes.
   87pce_autoload_all :-
   88    autoload(Class, File),
   89    \+ get(@classes, member, Class, _),
   90    \+ pce_prolog_class(Class),
   91    ensure_loaded(user:File),
   92    fail.
   93pce_autoload_all.
   94
   95
   96register_handler :-
   97    send(@pce?exception_handlers, append(
   98         attribute(undefined_class,
   99                   message(@prolog, call, trap_autoload, @arg1)))).
  100
  101:- initialization
  102    register_handler.  103
  104pce_ifhostproperty(prolog(swi),
  105                   (:- '$hide'(trap_autoload/1)),
  106                   (notrace(G) :- G)).
  107
  108trap_autoload(Class) :-
  109    notrace(do_trap_autoload(Class)).
  110
  111do_trap_autoload(Class) :-
  112    pce_realise_class(Class),
  113    !.
  114do_trap_autoload(Class) :-
  115    autoload(Class, File),
  116    load_files(user:File,
  117               [ autoload(true)
  118               ]),
  119    pce_realise_class(Class)