View source with formatted 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_global,
   36        [ pce_global/2                            % Ref x Goal
   37        ]).   38
   39:- meta_predicate
   40      pce_global(+, :).   41
   42:- use_module(pce_boot(pce_principal)).   43
   44:- require([strip_module/3, gensym/2, append/3]).   45
   46:- dynamic
   47    'pce global goal'/3,                      % Ref, Module, Goal
   48    'pce catcher'/2.                          % Module, Goal
   49
   50
   51                /********************************
   52                *            USER PART          *
   53                ********************************/
   54
   55%!  pce_global(+Object, :Goal) is det.
   56%
   57%   Register Goal to be a goal that creates @Reference
   58
   59pce_global(@Ref, MGoal) :-
   60    strip_module(MGoal, Module, Goal),
   61    global(Ref, Module, Goal).
   62
   63global(Ref, Module, Goal) :-
   64    var(Ref),
   65    !,
   66    retractall('pce catcher'(Module, Goal)),
   67    asserta('pce catcher'(Module, Goal)).
   68global(Ref, Module, Goal) :-                      % just reconsult
   69    'pce global goal'(Ref, Module, Goal),
   70    !,
   71    (   Goal = new(_)
   72    ->  true
   73    ;   reload_global(@Ref)
   74    ).
   75global(Ref, Module, Goal) :-
   76    'pce global goal'(Ref, Module, _),       % definition changed
   77    !,
   78    reload_global(@Ref),
   79    retractall('pce global goal'(Ref, Module, _)),
   80    asserta('pce global goal'(Ref, Module, Goal)).
   81global(Ref, _M1, new(Term)) :-                    % same definition
   82    'pce global goal'(Ref, _M2, new(Term)),
   83    !.
   84global(Ref, M1, G1) :-
   85    'pce global goal'(Ref, M2, G2),
   86    !,
   87    print_message(warning, object_already_defined(Ref, M2)),
   88    retractall('pce global goal'(Ref, M2, G2)),
   89    asserta('pce global goal'(Ref, M1, G1)).
   90global(Ref, Module, Goal) :-
   91    asserta('pce global goal'(Ref, Module, Goal)).
   92
   93reload_global(Ref) :-
   94    object(Ref),
   95    !,
   96    (   get(Ref, references, 0)
   97    ->  free(Ref)
   98    ;   Ref = @Name,
   99        gensym(Name, NewName),
  100        send(Ref, name_reference, NewName),
  101        print_message(informational, renamed_reference(Name, NewName))
  102    ).
  103reload_global(_).
  104
  105
  106                /********************************
  107                *            SYSTEM             *
  108                ********************************/
  109
  110register_handler :-
  111    send(@pce?exception_handlers,
  112         append(attribute(undefined_assoc,
  113                          message(@prolog, call, trap_ref, @arg1)))).
  114
  115:- initialization
  116    register_handler.  117
  118trap_ref(Ref) :-
  119    'pce global goal'(Ref, Module, Goal),
  120    !,
  121    (   Goal = new(Term)
  122    ->  (   new(@Ref, Module:Term)
  123        ->  true
  124        ;   print_message(error, create_failed(Term)),
  125            trace,
  126            fail
  127        )
  128    ;   Goal =.. List,
  129        append(List, [@Ref], GoalList),
  130        GoalTerm =.. GoalList,
  131        (   Module:GoalTerm
  132        ->  true
  133        ;   print_message(error, make_global_failed(Module:GoalTerm)),
  134            trace,
  135            fail
  136        )
  137    ).
  138trap_ref(Ref) :-
  139    'pce catcher'(Module, Goal),
  140    call(Module:Goal, Ref)