-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass-utils.lisp
435 lines (397 loc) · 16.4 KB
/
class-utils.lisp
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
;;; :FILE mon-systems/class-utils.lisp
;;; ==============================
;;; ==============================
;;; MOP related stuff is in the SB-MOP package.
;;; Relevant MOP related files:
;;; :FILE sbcl/src/pcl/generic-functions.lisp
;;; :FILE sbcl/src/pcl/defs.lisp
;;;
;;; :SEE-ALSO The closer-mop system provided by Pascal Costanza.
;;; (ql:quickload :closer-mop)
;;; `subclassp', `classp'
;;;
;;; ==============================
;;; The class PCL-CLASS is an implementation-specific common
;;; superclass of all specified subclasses of the class CLASS.
;;;
;;; The class STD-CLASS is an implementation-specific common
;;; superclass of the classes STANDARD-CLASS and
;;; FUNCALLABLE-STANDARD-CLASS.
;;;
;;; (sb-mop:class-direct-subclasses (class-of 'find-class))
;;; (sb-mop:class-direct-superclasses (class-of 'find-class))
;;;
;;; ==============================
;;; Usefull symbols, functions, etc.
;;;
;;; object-plist ;; ;; SB-PCL class plist-mixin (standard-object)
;;;
;;; class-p
;;; standard-class-p
;;; class-precedence-list
;;; class-slots
;;; class-direct-slots
;;; class-slot-value
;;;
;;; slots-fetcher
;;; slots-to-inspect
;;;
;;; :FILE sbcl/src/pcl/env.lisp
;;; trace-method, untrace-method, *traced-methods*, traced-method <CLASS>
;;;
;;; ==============================
;; :SEE (info "(sbcl)Metaobject Protocol")
;;
;; (mon:where-is "class-finalized-p") => (SB-MOP:CLASS-FINALIZED-P)
;;
;; `ensure-class', `ensure-class-using-class', `frob-ensure-class-args',
;; `legal-class-name-p', `classp', `metaclassp'
;;
;; (mon:where-is "ensure-class") => (SB-MOP:ENSURE-CLASS)
;;
;; ../sbcl/src/pcl/std-class.lisp
;;
;;; ==============================
;;; ==============================
;;;
;;; `cl:find-method' usage:
;;; SIGNATURE (<generic-function-spec> qualifiers* (specializers*) &optional errorp)
;;;
;;; Getting at <generic-function-spec> requires finding the fdefinition of the
;;; generic function symbol. This is accomplished with either:
;;; (fdefinition '<SYMBOL>) or (fdefinition '(setf <SYMBOL>))
;;; or alternatively (find-method #'<SYMBOL>)
;;;
;;; :NOTE That when the generic-function is of type (setf <SYMBOL>)
;;; It is a "writer specializer" and has the form '(t <OBJECT>)
;;; Else, it is a "reader-specializer" of the form '(slot-object)
;;;
;;; (find-method #'(setf <GF-SYMBOL>) {...} ) nil '(t dbc-system-subdir) nil)
;;; (find-method #'(setf dbc-var-binding) nil '(t dbc-system-subdir) nil)
;;; (find-method (function (setf dbc-var-binding) nil '(t dbc-system-subdir) nil))
;;; (find-method (function (setf dbc-var-binding)) nil '(t dbc-system-subdir) nil)
;;; (sb-mop:generic-function-methods (fdefinition 'dbc-system-described))
;;; (sb-mop:generic-function-methods (fdefinition '(setf dbc-var-binding)))
;;; (find-method (fdefinition 'dbc-var-binding) nil '(dbc-system-subdir))
;;; (find-method (fdefinition (setf dbc-var-binding)) nil '(dbc-system-subdir t) nil)
;;; (find-method (fdefinition '(setf dbc-var-binding)) nil '(t dbc-system-subdir) nil)
;;; (find-method #'(setf <SYMBOL>) nil '(<SPECIALIZER(S)>) nil)
;;; (find-method (function <SYMBOL>) nil '(<CLASS>) nil)
;;; (find-method (fdefinition '(setf <SYMBOL>)) nil '(<SPECIALIZER(S)>) nil)
;;;
;;; :NOTE How to remove-method on generic-function:
;;;
;;; Find methods specialized on class
;;; (slime-who-specializes 'my-class)
;;;
;;; Find callers of generic:
;;; (slime-who-calls 'my-generic)
;;;
;;; (remove-method (fdefinition 'my-generic) (find-method #'my-generic nil '(my-class) '(t)))
;;; (remove-method (fdefinition 'my-generic) (find-method #'my-generic nil '(t) '(t)))
;;; (remove-method (fdefinition 'my-generic) (find-method #'my-generic nil '(t) '(eql <THING>)))
;;;
;;; ==============================
(in-package #:mon)
;;; :COURTESY bknr-datastore-20100901-git/src/utils/utils.lisp
(defun class-subclasses (class)
(labels ((collect-subclasses (class)
(let ((subclasses
#+allegro
(aclmop:class-direct-subclasses class)
#+cmu
(pcl:class-direct-subclasses class)
#+openmcl
(openmcl-mop:class-direct-subclasses class)
#+sbcl
(sb-mop:class-direct-subclasses class)))
(apply #'append subclasses
(mapcar #'collect-subclasses subclasses)))))
(mapcar #'class-name (remove-duplicates (collect-subclasses
(if (symbolp class)
(find-class class) class))))))
;; :SOURCE clocc/cllib/port/sys.lisp
(macrolet ((class-slots* (class)
`(sb-pcl::class-slots ,class))
(class-slots1 (obj)
`(class-slots*
(typecase ,obj
(class ,obj)
(symbol (find-class ,obj))
(t (class-of ,obj)))))
(slot-name (slot)
`(slot-value ,slot 'sb-pcl::name))
(slot-initargs (slot)
`(slot-value ,slot 'sb-pcl::initargs))
(slot-one-initarg (slot)
`(car (slot-initargs ,slot)))
(slot-alloc (slot)
`(sb-pcl::slot-definition-allocation ,slot)))
;;
(defun class-slot-list (class &optional (all t))
(mapcan (if all
(%compose list slot-name)
(lambda (slot)
(when (eq (slot-alloc slot) :instance)
(list (slot-name slot)))))
(class-slots1 class)))
;;
(defun class-slot-list-direct (class)
(class-slots1 class))
;;
(defun class-slot-initargs (class &optional (all t))
(mapcan (if all
(%compose list slot-one-initarg)
(lambda (slot)
(when (eq (slot-alloc slot) :instance)
(list (slot-one-initarg slot)))))
(class-slots1 class)))
;;
(defun structure-slots (struct)
(class-slot-list (find-class struct)))
) ;; :CLOSE macrolet
;; (defun slot-val (obj slot &optional default)
;; (or (when (slot-boundp obj slot)
;; (slot-value obj slot))
;; default))
;;; :SOURCE mcclim/Apps/Scigraph/dwim/extensions.lisp
;;; Zetalisp function. :WAS `instancep'
(defgeneric class-instance-p (object)
(:documentation "Is OBJECT an instance of the class standard-object."))
(defmethod class-instance-p ((object t)) nil)
(defmethod class-instance-p ((object standard-object)) t)
(defun slot-value-or (obj slot &optional default)
(or (when (slot-boundp obj slot)
(slot-value obj slot))
default))
(defun class-name-of (object)
(if (class-instance-p object)
(class-name (class-of object))
(values nil (class-of object))))
(defun find-class-name-as-string (class &optional package)
;; (format nil "~S" (class-name (find-class 'parsed-ref)))
(declare (optimize (speed 0) (safety 0) (compilation-speed 0) (debug 3)))
(symbol-not-null-or-error class :w-locus "CLASS" :signal-or-only nil)
(let* ((pkg (or (and package
(or (find-package* package)
(package-error-not package
:w-sym 'find-class-name-as-string
:w-type 'function
:w-spec "Arg PACKAGE provided but~
`mon:find-package*' doesn't find it"
:signal-or-only nil)))
#+sbcl (sb-int:sane-package)
#-sbcl *package*))
(class-sym-p (and
(ref-bind gotit (multiple-value-list
(where-is-local (symbol-name class) pkg))
;; (progn (break "refbind got: ~S" gotit)
(and (car gotit)
;; (memq (cadr gotit) (list :internal :external))
gotit) ;)
)))
(fnd-cls (ref-bind fc (car class-sym-p)
;; This is still searching for non-existent classes.
;; (progn
;; (break "refbind got: ~S" fc)
;; (find-class fc)) )))
(find-class fc) )))
(and fnd-cls (values (string (class-name fnd-cls))
class (car class-sym-p)
(cadr class-sym-p)))))
;;; ==============================
;;; :COURTESY Stas Boukarev :SEE (URL `http://paste.lisp.org/+2KXX')
;;; :PASTE 120453 :TITLE shallow-copy-object :WAS `shallow-copy-object'
;;; :NOTE Following requires closer-mop
;;;
;;; (defun shallow-copy-object (object)
;;; (let* ((class (class-of object))
;;; (new (allocate-instance class)))
;;; (loop for slot in (c2mop:class-slots class)
;;; when (c2mop:slot-boundp-using-class class object slot)
;;; do (setf (c2mop:slot-value-using-class class new slot)
;;; (c2mop:slot-value-using-class class object slot)))
;;; new))
;;;
;;; Attempt to do it with sb-mop
;;; (where-is "class-slots") ;=> (SB-MOP:CLASS-SLOTS :CLASS-SLOTS SB-KERNEL::CLASS-SLOTS) c2mop imports-from sb-mop
;;; (where-is "slot-boundp-using-class") ;=> (SB-MOP:SLOT-BOUNDP-USING-CLASS) c2mop imports-from sb-mop
;;; (where-is "slot-value-using-class") ;=> (SB-MOP:SLOT-VALUE-USING-CLASS) c2mop imports-from sb-mop
#+sbcl
(defun copy-instance-of-class-shallowly (instance-of-class)
(let* ((class (class-of instance-of-class))
(new (allocate-instance class)))
(loop
for slot in (sb-mop:class-slots class)
when (sb-mop:slot-boundp-using-class class instance-of-class slot)
do (setf (sb-mop:slot-value-using-class class new slot)
(sb-mop:slot-value-using-class class instance-of-class slot)))
new))
;;; ==============================
;; #lisp 2011-01-27
;; <Kruppe> Is there any way to look at the slots of a class (short of using
;; inspect or looking at the source)? [12:00]
;; <stassats> sb-mop:class-slots
;;; ==============================
(defun slot-definition-and-name (class slot-name-or-def)
(let (;; Don't signal directly when find-class doesn't
;; so we may provide a handler later if desired.
(fc (find-class class nil)))
(if fc
(if (sb-mop:class-finalized-p fc)
(values
(find slot-name-or-def
(sb-mop:class-slots fc)
:key #'sb-mop:slot-definition-name)
slot-name-or-def)
;; Don't finalize if it isn't yet.
(values nil nil (format nil ";; class ~S not `sb-mop:class-finalized-p'" fc)))
(error 'simple-error
:format-control "class ~S not found with `cl:find-class'"
:format-arguments `(,class)))))
;; :SOURCE roflcopter/rtracker.lisp :WAS `bound-slot-names'
(defun class-bound-slot-names (object)
;; :EXAMPLE (class-bound-slot-names (ql-dist:find-system "closer-mop"))
(let ((class (class-of object)))
(loop
for slotd in (closer-mop:class-slots class)
when (closer-mop:slot-boundp-using-class class object slotd)
collect (closer-mop:slot-definition-name slotd))))
;; :COURTESY lokedhs dhs-db/persistmetaclasses.lisp
;; :WAS `find-slot-instance'
(defun find-class-slot-instance (class slot-name)
(dolist (s (closer-mop:class-slots class))
(when (eql (closer-mop:slot-definition-name s) slot-name)
(return s))))
;;; ==============================
;; (sb-mop:class-slots (find-class 'dbc:base-description))
;; (sb-mop:class-finalized-p (find-class 'dbc:base-description)) (where-is "base-entity")
;; (slot-definition-and-name 'dbc:entity-regexp 'dbc::match-entity-class)
;; (slot-definition-and-name 'bubba nil)
;;; ==============================
;;; ==============================
;;; :CLASS-UTILS-DOCUMENTATION
;;; ==============================
(fundoc 'find-class-slot-instance
"Return the effective slot-definition-name for SLOT-NAME of class.
:EXAMPLE~%~@
{ ... <EXAMPLE> ... } ~%~@
:SEE-ALSO `<XREF>'.~%▶▶▶")
(fundoc 'slot-definition-and-name
"Return SLOT-DEFINITION-NAME object of CLASS.~%~@
Return value is as if by `cl:values':
SLOT-DEFINITION-NAME
SLOT-NAME-OR-DEF
If `class-finalized-p' fails to return nth-value 0 and 1 are nil and a third
value is a string indicating that class was not yet finalized.~%~@
Signal an error if CLASS is not found with `cl:find-class'.~%~@
:EXAMPLE~%~@
{ ... <EXAMPLE> ... } ~%~@
:SEE-ALSO `<XREF>'.~%▶▶▶")
(fundoc 'class-subclasses
"Return a list of the names of all subclasses of a given CLASS.~%~@
:EXAMPLE~%~@
{ ... <EXAMPLE> ... } ~%~@
:SEE-ALSO `class-slot-list', `class-slot-initargs',
`structure-slots'.~%▶▶▶")
(fundoc 'class-name-of
"Return the `cl:class-name' that OBJECT is `cl:class-of'.~%~@
When OBJECT is an instance of `cl:standard-object' and satisfies
`class-instance-p' return value is the name of the class it belongs to..~%~@
When OBJECT is an instance of some other class e.g. built-in-class
return value is as if by `cl:values' with the first value nil and the second the class that OBJECT is class-of.
:EXAMPLE~%~@
(class-name-of (class-of 'find-class))~%~@
(class-name-of (fdefinition 'fdefinition))
:NOTE The C-P-L of the class standard-class is:~%~@
\(standard-class class standard-object t\)
The class standard-class is default class of classes defined by `cl:defclass'.~%~@
The C-P-L of standard-object is:~%~@
\(standard-object t\)
However, the class `standard-object' _is_ an instance of `standard-class'
It is also a superclass of every class that is an instance of `standard-class'
_except itself_.~%~@
:SEE-ALSO `mon:find-class-name-as-string'.~%▶▶▶")
(fundoc 'find-class-name-as-string
"Find the class-name of CLASS in PACKAGE.~%~@
When found return-value is as if by `cl:values'.
Returned values have the form:~%
\"<CLASS>\"
<CLASS>
<PKG>[:|::]<CLASS>
{ :INTERNAL | :EXTERNAL | :INHERITED | :PRESENT }~%~@
- First value is the `cl:class-name' of CLASS;
- Second value is CLASS;
- Third value is CLASS with package qualified name;
- Fourth value inidicates the symbol status of CLASS in package;
:EXAMPLE~%~@
\(find-class-name-as-string 'parsed-ref :dbc\)
\(find-class-name-as-string 'parsed-ref \"DBC\"\)
\(find-class-name-as-string 'parsed-ref\)~%
:SEE-ALSO `cl:find-class', `cl:class-name', `mon:class-name-of',
`mon:where-is-local'.~%▶▶▶")
(fundoc 'slot-value-or
"Like `cl:slot-value' but return DEFAULT if value of OBJ's SLOT is not `slot-boundp'.~%~@
:EXAMPLE~%~@
{ ... <EXAMPLE> ... } ~%~@
:SEE-ALSO `<XREF>'.~%▶▶▶")
#+sbcl
(fundoc 'copy-instance-of-class-shallowly
"Return value is a shallow copy of INSTANCE-OF-CLASS.~%~@
The copy is \"shallow\" int that the copy of INSTANCE-OF-CLASS does not
allocate a new object for each slot-value contained of INSTANCE-OF-CLASS
and therefor each slot-value of the instance returned will share structure
with the original INSTANCE-OF-CLASS.~%~@
IOW, if one of the slots of INSTANCE-OF-CLASS holds a list, then the slot-value
of returned instance points to the same list, i.e. it's not \"a deep copy\".~%~@
:EXAMPLE~%~@
{ ... <EXAMPLE> ... } ~%~@
:SEE-ALSO `<XREF>'.~%▶▶▶")
#+sbcl
(setf (documentation 'class-slot-initargs 'function)
#.(format nil
"Return the list of initargs of a CLASS.~%~@
CLASS can be a symbol, a class object (as returned by `class-of') or an instance
of a class.~%~@
If the second optional argument ALL is non-nil \(the default\), initargs for all
slots are returned, otherwise only the slots with :allocation type :instance are
returned.~%~@
:EXAMPLE~%~@
{ ... <EXAMPLE> ... } ~%~@
:SEE-ALSO `class-subclasses', `class-slot-list', `structure-slots'.~%▶▶▶"))
#+sbcl
(setf (documentation 'class-slot-list 'function)
#.(format nil
"Return the list of slots of a CLASS.~%~@
CLASS can be a symbol, a class object \(as returned by `class-of'\) or an
instance of a class.~%~@
If the second optional argument ALL is non-NIL \(default\), all slots are
returned.~%~@
Otherwise, only the slots with :allocation type :instance are returned.~%~@
:EXAMPLE~%~@
{ ... <EXAMPLE> ... } ~%~@
:SEE-ALSO `class-subclasses', `class-slot-initargs', `structure-slots'.~%▶▶▶"))
#+sbcl
(setf (documentation 'class-slot-list-direct 'function)
#.(format nil
"Return the list of slots of a CLASS with :allocation type :instance.~%~@
CLASS can be a symbol, a class object \(as returned by `class-of'\) or an
:EXAMPLE~%~@
{ ... <EXAMPLE> ... } ~%~@
:SEE-ALSO `class-subclasses', `class-slot-initargs', `structure-slots'.~%▶▶▶"))
#+sbcl
(setf (documentation 'structure-slots 'function)
#.(format nil
"Return the list of slot names of structure STRUCT.~%~@
:EXAMPLE~%~@
{ ... <EXAMPLE> ... } ~%~@
:SEE-ALSO `class-subclasses', `class-slot-list', `class-slot-initargs'.~%▶▶▶"))
;;; ==============================
;; Local Variables:
;; indent-tabs-mode: nil
;; show-trailing-whitespace: t
;; mode: lisp-interaction
;; package: mon
;; End:
;;; ==============================
;;; EOF