File: cx-layer.lisp

package info (click to toggle)
cl-contextl 0.30-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 148 kB
  • ctags: 246
  • sloc: lisp: 1,767; makefile: 29
file content (388 lines) | stat: -rw-r--r-- 15,821 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
(in-package :contextl)

(eval-when (:compile-toplevel :load-toplevel :execute)
  (defun check-op/layer (op layer-name)
    (unless (member op '(+ -) :test #'eq)
      (error "Incorrect activation operator ~S. Must be + or -." op))
    (unless (symbolp layer-name)
      (error "Not a layer name: ~S." layer-name))
    (when (eq layer-name 't)
      (case op
        (+ (error "The root layer cannot be placed in front of other layers."))
        (- (error "The root layer must always be active."))))))

(defclass standard-layer-object (special-object)
  ())

(defclass standard-layer-class (special-class singleton-class)
  ((layer-name :initarg original-name
               :reader %layer-name)))

(defmethod validate-superclass
           ((class standard-layer-class)
            (superclass standard-class))
  t)

(defmethod initialize-instance :around
  ((class standard-layer-class) &rest initargs &key direct-superclasses)
  (declare (dynamic-extent initargs))
  (if (loop for direct-superclass in direct-superclasses
            thereis (subtypep direct-superclass (load-time-value (find-class 'standard-layer-object))))
    (call-next-method)
    (apply #'call-next-method
           class
           :direct-superclasses
           (append direct-superclasses
                   (load-time-value (list (find-class 'standard-layer-object))))
           initargs)))

(defmethod reinitialize-instance :around
  ((class standard-layer-class) &rest initargs &key (direct-superclasses '() direct-superclasses-p))
  (declare (dynamic-extent initargs))
  (if (or (not direct-superclasses-p)
          (loop for direct-superclass in direct-superclasses
                thereis (subtypep direct-superclass (load-time-value (find-class 'standard-layer-object)))))
    (call-next-method)
    (apply #'call-next-method
           class
           :direct-superclasses
           (append direct-superclasses
                   (load-time-value (list (find-class 'standard-layer-object))))
           initargs)))

(defclass layer-direct-slot-definition (singleton-direct-slot-definition
                                        special-direct-slot-definition)
  ())

(defmethod direct-slot-definition-class ((class standard-layer-class) &key &allow-other-keys)
  (find-class 'layer-direct-slot-definition))

(defmacro deflayer (&whole form name &optional superlayers &body options)
  (when (assoc :metaclass options)
    (error "DEFLAYER does not support the :metaclass option in ~S. Use :layer-class instead." form))
  `(defclass ,(defining-layer name) ,(mapcar #'defining-layer superlayers)
     ,@(if options
         (loop for option in options
               if (eq (car option) :layer-class)
               collect `(:metaclass ,@(cdr option))
               else collect option)
         '(()))
     ,@(unless (assoc :layer-class options)
         '((:metaclass standard-layer-class)))
     (original-name ,name)))

(defgeneric layer-name (layer)
  (:method ((layer symbol)) layer)
  (:method ((layer standard-layer-object)) (layer-name (class-of layer)))
  (:method ((layer standard-layer-class)) (car (%layer-name layer))))

(defgeneric find-layer-class (layer &optional errorp environment)
  (:method ((layer symbol) &optional (errorp t) environment)
   (or (find-class (defining-layer layer) nil environment)
       (when errorp
         (error "There is no layer named ~S." layer))))
  (:method ((layer standard-layer-object) &optional errorp environment)
   (declare (ignore errorp environment))
   (class-of layer))
  (:method ((layer standard-layer-class) &optional errorp environment)
   (declare (ignore errorp environment))
   layer))

(defgeneric find-layer (layer &optional errorp)
  (:method ((layer symbol) &optional (errorp t))
   (let ((layer-class (find-layer-class layer errorp)))
     (when layer-class
       #-lispworks (ensure-finalized layer-class)
       (class-prototype layer-class))))
  (:method ((layer standard-layer-object) &optional errorp)
   (declare (ignore errorp))
   layer)
  (:method ((layer standard-layer-class) &optional errorp)
   (declare (ignore errorp))
   #-lispworks (ensure-finalized layer)
   (class-prototype layer)))

(defgeneric layer-makunbound (layer)
  (:method ((layer symbol))
   (setf (find-class (defining-layer layer)) nil))
  (:method ((layer standard-layer-object))
   (setf (find-class (class-name (class-of layer))) nil))
  (:method ((layer standard-layer-class))
   (setf (find-class (class-name layer)) nil)))

(defclass root-specializer () ()
  (:metaclass standard-layer-class))
(ensure-finalized (find-class 'root-specializer))

(defstruct layer-context
  (prototype (class-prototype (find-class 'root-specializer))
             :type standard-object
             :read-only t)
  (specializer (find-class 'root-specializer)
               :type standard-layer-class
               :read-only t)
  (children/ensure-active () :type list)
  (children/ensure-inactive () :type list))

#-allegro
(declaim (type layer-context *root-context* *active-context*))
#+allegro
(eval-when (:load-toplevel :execute)
  (proclaim '(type layer-context *root-context* *active-context*)))

(defparameter *root-context* (make-layer-context))

(defparameter *active-context* *root-context*)

(defun layer-active-p (layer &optional (context *active-context*))
  (subtypep (layer-context-specializer context)
            (find-layer-class layer)))

(define-layered-function activate-layer-using-class (layer-class active-context)
  (:method ((layer-class standard-layer-class) active-context)
   (let ((active-context-specializer (layer-context-specializer active-context)))
     (values
      (if (subtypep active-context-specializer layer-class)
        active-context
        (let ((new-specializer
	       (ensure-finalized
		(make-instance 'standard-layer-class
			       :direct-superclasses
			       (list layer-class active-context-specializer)))))
          (make-layer-context
           :prototype (class-prototype new-specializer)
           :specializer new-specializer)))
      t))))

(defun activate-layer (layer active-context)
  (multiple-value-bind
      (new-layer-context cacheablep)
      (activate-layer-using-class (find-layer-class layer) active-context)
    (if cacheablep
      (setf (getf (layer-context-children/ensure-active active-context) layer)
            new-layer-context)
      new-layer-context)))

(declaim (inline ensure-active-layer-context))

(defun ensure-active-layer-context (layer active-context)
  (declare (optimize (speed 3) (debug 0) (safety 0)
                     (compilation-speed 0)))
  (or (getf (layer-context-children/ensure-active active-context) layer)
      (activate-layer layer active-context)))

(defun ensure-active-layer (layer-name)
  (check-op/layer '+ layer-name)
  (setf *active-context*
        (locally
          (declare (optimize (speed 3) (debug 0) (safety 0)
                             (compilation-speed 0)))
          (ensure-active-layer-context layer-name *active-context*))))

(define-layered-function deactivate-layer-using-class (layer-class active-context)
  (:method ((layer-class standard-layer-class) active-context)
   (let ((active-context-specializer (layer-context-specializer active-context)))
     (values
      (loop for context-specializer = active-context-specializer
            then (second (class-direct-superclasses context-specializer))
            for active-layers = (list (first (class-direct-superclasses context-specializer)))
            then (cons (first (class-direct-superclasses context-specializer)) active-layers)
            until (eq context-specializer (load-time-value (find-class 'root-specializer)))
            finally
            (return (loop for new-layer-context = *root-context*
                          then (if (subtypep active-layer layer-class)
                                 new-layer-context
                                 (ensure-active-layer-context (layer-name active-layer) new-layer-context))
                          for active-layer in (cdr active-layers)
                          finally (return new-layer-context))))
      t))))

(defun deactivate-layer (layer active-context)
  (multiple-value-bind
      (new-layer-context cacheablep)
      (deactivate-layer-using-class (find-layer-class layer) active-context)
    (if cacheablep
      (setf (getf (layer-context-children/ensure-inactive active-context) layer)
            new-layer-context)
      new-layer-context)))

(declaim (inline ensure-inactive-layer-context))

(defun ensure-inactive-layer-context (layer active-context)
  (declare (optimize (speed 3) (debug 0) (safety 0)
                     (compilation-speed 0)))
  (or (getf (layer-context-children/ensure-inactive active-context) layer)
      (deactivate-layer layer active-context)))

(defun ensure-inactive-layer (layer-name)
  (check-op/layer '- layer-name)
  (setf *active-context*
        (locally
          (declare (optimize (speed 3) (debug 0) (safety 0)
                             (compilation-speed 0)))
          (ensure-inactive-layer-context layer-name *active-context*))))

(defmacro with-active-layer (layer-name &body body)
  (check-op/layer '+ layer-name)
  `(let ((*active-context*
          (locally
            (declare (optimize (speed 3) (debug 0) (safety 0)
                               (compilation-speed 0)))
            (ensure-active-layer-context ',layer-name *active-context*))))
     ,@body))

(defmacro with-active-layers ((&rest layer-names) &body body)
  (if (every #'symbolp layer-names)
    (if layer-names
      `(with-active-layer ,(car layer-names)
         (with-active-layers ,(cdr layer-names)
           ,@body))
      `(progn ,@body))
    (loop for form = `(with-special-initargs ,(loop for layer-spec in layer-names
                                                    when (consp layer-spec)
                                                    collect `((find-layer ',(car layer-spec)) ,@(cdr layer-spec)))
                        ,@body)
          then `(with-active-layer ,layer-name ,form)
          for layer-spec in (reverse layer-names)
          for layer-name = (if (symbolp layer-spec)
                             layer-spec
                             (car layer-spec))
          finally (return form))))

(defmacro with-active-layers* ((&rest layer-names) &body body)
  (if (every #'symbolp layer-names)
    (if layer-names
      `(with-active-layer ,(car layer-names)
         (with-active-layers ,(cdr layer-names)
           ,@body))
      `(progn ,@body))
    (loop for form = `(with-special-initargs* ,(loop for layer-spec in layer-names
                                                     when (consp layer-spec)
                                                     collect `((find-layer ',(car layer-spec)) ,@(cdr layer-spec)))
                        ,@body)
          then `(with-active-layer ,layer-name ,form)
          for layer-spec in (reverse layer-names)
          for layer-name = (if (symbolp layer-spec)
                             layer-spec
                             (car layer-spec))
          finally (return form))))

(defmacro with-inactive-layer (layer-name &body body)
  (check-op/layer '- layer-name)
  `(let ((*active-context*
          (locally
            (declare (optimize (speed 3) (debug 0) (safety 0)
                               (compilation-speed 0)))
            (ensure-inactive-layer-context ',layer-name *active-context*))))
     ,@body))

(defmacro with-inactive-layers ((&rest layer-names) &body body)
  (if layer-names
    `(with-inactive-layer ,(car layer-names)
       (with-inactive-layers ,(cdr layer-names)
         ,@body))
    `(progn ,@body)))

(defun funcall-with-layer (op layer function &rest args)
  (declare (dynamic-extent args))
  (check-op/layer op layer)
  (let ((*active-context*
         (locally
           (declare (optimize (speed 3) (debug 0) (safety 0)
                              (compilation-speed 0)))
           (ecase op
             (+ (ensure-active-layer-context layer *active-context*))
             (- (ensure-inactive-layer-context layer *active-context*))))))
    (apply function args)))

(define-compiler-macro funcall-with-layer (&whole form op layer function &rest args)
  (if (and (consp op) (eq (car op) 'quote)
           (consp layer) (eq (car layer) 'quote))
    (let ((evop (eval op))
          (evlayer (eval layer)))
      (check-op/layer evop evlayer)
      (ecase evop
        (+ `(with-active-layer ,evlayer
              (funcall ,function ,@args)))
        (- `(with-inactive-layer ,evlayer
              (funcall ,function ,@args)))))
    form))

(defun funcall-with-layers (layers function &rest args)
  (declare (dynamic-extent args))
  (if layers
    (funcall-with-layer
     (car layers)
     (cadr layers)
     (lambda ()
       (apply #'funcall-with-layers (cddr layers) function args)))
    (apply function args)))

(define-compiler-macro funcall-with-layers (&whole form layers function &rest args)
  (cond ((null layers) `(funcall ,function ,@args))
        ((and (consp layers) (eq (car layers) 'quote))
         (let ((evlayers (eval layers)))
           (unless (listp evlayers)
             (error "Incorrect layers argument to funcall-with-layers: ~S." layers))
           (if (null evlayers)
             `(funcall ,function ,@args)
             (progn
               (check-op/layer (car evlayers) (cadr evlayers))
               (ecase (car evlayers)
                 (+ `(with-active-layer ,(cadr evlayers)
                       (funcall-with-layers ',(cddr evlayers) ,function ,@args)))
                 (- `(with-inactive-layer ,(cadr evlayers)
                       (funcall-with-layers ',(cddr evlayers) ,function ,@args))))))))
        (t form)))

(defun apply-with-layer (op layer function &rest args)
  (declare (dynamic-extent args))
  (check-op/layer op layer)
  (let ((*active-context*
         (locally
           (declare (optimize (speed 3) (debug 0) (safety 0)
                              (compilation-speed 0)))
           (ecase op
             (+ (ensure-active-layer-context layer *active-context*))
             (- (ensure-inactive-layer-context layer *active-context*))))))
    (apply #'apply function args)))

(define-compiler-macro apply-with-layer (&whole form op layer function &rest args)
  (if (and (consp op) (eq (car op) 'quote)
           (consp layer) (eq (car layer) 'quote))
    (let ((evop (eval op))
          (evlayer (eval layer)))
      (check-op/layer evop evlayer)
      (ecase evop
        (+ `(with-active-layer ,evlayer
              (apply ,function ,@args)))
        (- `(with-inactive-layer ,evlayer
              (apply ,function ,@args)))))
    form))

(defun apply-with-layers (layers function &rest args)
  (declare (dynamic-extent args))
  (if layers
    (funcall-with-layer
     (car layers)
     (cadr layers)
     (lambda ()
       (apply #'apply-with-layers (cddr layers) function args)))
    (apply #'apply function args)))

(define-compiler-macro apply-with-layers (&whole form layers function &rest args)
  (cond ((null layers) `(apply ,function ,@args))
        ((and (consp layers) (eq (car layers) 'quote))
         (let ((evlayers (eval layers)))
           (unless (listp evlayers)
             (error "Incorrect layers argument to apply-with-layers: ~S." layers))
           (if (null evlayers)
             `(apply ,function ,@args)
             (progn
               (check-op/layer (car evlayers) (cadr evlayers))
               (ecase (car evlayers)
                 (+ `(with-active-layer ,(cadr evlayers)
                       (apply-with-layers ',(cddr evlayers) ,function ,@args)))
                 (- `(with-inactive-layer ,(cadr evlayers)
                       (apply-with-layers ',(cddr evlayers) ,function ,@args))))))))
        (t form)))