forked from Shirakumo/trial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubject.lisp
More file actions
163 lines (132 loc) · 6.05 KB
/
subject.lisp
File metadata and controls
163 lines (132 loc) · 6.05 KB
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
#|
This file is a part of trial
(c) 2016 Shirakumo http://tymoon.eu (shinmera@tymoon.eu)
Author: Nicolas Hafner <shinmera@tymoon.eu>
|#
(in-package #:org.shirakumo.fraf.trial)
(defclass subject-class (standard-class handler-container)
((effective-handlers :initform NIL :accessor effective-handlers)))
(defmethod c2mop:validate-superclass ((class subject-class) (superclass t))
NIL)
(defmethod c2mop:validate-superclass ((class standard-class) (superclass subject-class))
NIL)
(defmethod c2mop:validate-superclass ((class subject-class) (superclass standard-class))
T)
(defmethod c2mop:validate-superclass ((class subject-class) (superclass subject-class))
T)
(defmethod compute-effective-handlers ((class subject-class))
;; Recompute effective handlers
(loop with effective-handlers = (handlers class)
for super in (c2mop:class-direct-superclasses class)
do (when (typep super 'subject-class)
(dolist (handler (effective-handlers super))
(pushnew handler effective-handlers :key #'name)))
finally (return effective-handlers)))
(defmethod reinitialize-instance :after ((class subject-class) &key)
(apply-class-changes class))
(defmethod c2mop:finalize-inheritance :after ((class subject-class))
(dolist (super (c2mop:class-direct-superclasses class))
(unless (c2mop:class-finalized-p super)
(c2mop:finalize-inheritance super)))
(setf (effective-handlers class) (compute-effective-handlers class)))
(defmethod apply-class-changes ((class subject-class))
(call-next-method)
(setf (effective-handlers class) (compute-effective-handlers class)))
(defmethod add-handler :after (handler (class subject-class))
(when (c2mop:class-finalized-p class)
(apply-class-changes class)))
(defmethod remove-handler :after (handler (class subject-class))
(when (c2mop:class-finalized-p class)
(apply-class-changes class)))
(defmethod add-handler (handler (class symbol))
(add-handler handler (find-class class)))
(defmethod remove-handler (handler (class symbol))
(remove-handler handler (find-class class)))
(defclass subject (entity handler-container)
((event-loops :initarg :event-loops :initform NIL :accessor event-loops))
(:metaclass subject-class))
(defmethod banned-slots append ((object subject))
'(event-loops))
(defmethod shared-initialize :after ((subject subject) slots &key)
(regenerate-handlers subject))
(defmethod update-instance-for-redefined-class ((subject subject) aslots dslots plist &key)
(declare (ignore aslots dslots plist))
(when (slot-boundp subject 'handlers)
(regenerate-handlers subject)))
(defmethod regenerate-handlers ((subject subject))
(setf (handlers subject)
(remove-if (lambda (handler)
(when (typep handler 'subject-handler)
(dolist (event-loop (event-loops subject))
(remove-handler handler event-loop))
T))
(handlers subject)))
(loop for prototype in (effective-handlers (class-of subject))
for handler = (make-instance
'subject-handler
:subject subject
:name (name prototype)
:event-type (event-type prototype)
:priority (priority prototype)
:delivery-function (delivery-function prototype))
do (push handler (handlers subject))
(dolist (event-loop (event-loops subject))
(add-handler handler event-loop))))
(defmethod register :before ((subject subject) (loop event-loop))
(when (find loop (event-loops subject))
(error "~s is already registered on the event-loop ~s."
subject loop)))
(defmethod register :after ((subject subject) (loop event-loop))
(push loop (event-loops subject)))
(defmethod deregister :before ((subject subject) (loop event-loop))
(unless (find loop (event-loops subject))
(error "~s is not registered on the event-loop ~s."
subject loop)))
(defmethod deregister :after ((subject subject) (loop event-loop))
(setf (event-loops subject) (remove loop (event-loops subject))))
(defmacro define-subject (&environment env name direct-superclasses direct-slots &rest options)
(setf direct-superclasses (append direct-superclasses (list 'subject)))
(unless (find :metaclass options :key #'first)
(push '(:metaclass subject-class) options))
`(defclass ,name ,direct-superclasses
,direct-slots
,@options))
(defclass subject-handler (handler)
((subject :initarg :subject :accessor subject))
(:default-initargs
:subject (error "SUBJECT required.")))
(defmethod matches ((a subject-handler) (b subject-handler))
(and (eq (subject a) (subject b))
(eql (name a) (name b))))
(defmethod matches ((a subject-handler) (b handler))
NIL)
(defmethod matches ((a handler) (b subject-handler))
NIL)
(defmethod handle (event (handler subject-handler))
(funcall (delivery-function handler) (subject handler) event))
(defmacro define-handler ((class name &optional (event-type name) (priority 0)) args &body body)
(let ((event (first args))
(args (rest args)))
`(add-handler (make-instance
'subject-handler
:name ',name
:event-type ',event-type
:subject ',class
:priority ,priority
:delivery-function (lambda (,class ,event)
(declare (ignorable ,class ,event))
(with-slots ,args ,event
,@body)))
',class)))
(defmacro define-generic-handler ((class name &optional (event-type name) (priority 0)) &body options)
`(progn
(defgeneric ,name (,class event)
,@options)
(add-handler (make-instance
'subject-handler
:name ',name
:event-type ',event-type
:subject ',class
:priority ,priority
:delivery-function #',name)
',class)))