1 ;;; luna.el --- tiny OOP system kernel
3 ;; Copyright (C) 1999,2000 Free Software Foundation, Inc.
5 ;; Author: MORIOKA Tomohiko <tomo@m17n.org>
8 ;; This file is part of FLIM (Faithful Library about Internet Message).
10 ;; This program is free software; you can redistribute it and/or
11 ;; modify it under the terms of the GNU General Public License as
12 ;; published by the Free Software Foundation; either version 2, or (at
13 ;; your option) any later version.
15 ;; This program is distributed in the hope that it will be useful, but
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 ;; General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
27 (eval-when-compile (require 'cl))
29 (eval-when-compile (require 'static))
31 (static-condition-case nil
32 :symbol-for-testing-whether-colon-keyword-is-available-or-not
34 (defconst :before ':before)
35 (defconst :after ':after)
36 (defconst :around ':around)))
42 (defmacro luna-find-class (name)
43 "Return a luna-class that has NAME."
44 (` (get (, name) 'luna-class)))
46 ;; Give NAME (symbol) the luna-class CLASS.
47 (defmacro luna-set-class (name class)
48 (` (put (, name) 'luna-class (, class))))
50 ;; Return the obarray of luna-class CLASS.
51 (defmacro luna-class-obarray (class)
52 (` (aref (, class) 1)))
54 ;; Return the parents of luna-class CLASS.
55 (defmacro luna-class-parents (class)
56 (` (aref (, class) 2)))
58 ;; Return the number of slots of luna-class CLASS.
59 (defmacro luna-class-number-of-slots (class)
60 (` (aref (, class) 3)))
62 (defmacro luna-define-class (class &optional parents slots)
63 "Define CLASS as a luna-class.
64 CLASS always inherits the luna-class `standard-object'.
66 The optional 1st arg PARENTS is a list luna-class names. These
67 luna-classes are also inheritted by CLASS.
69 The optional 2nd arg SLOTS is a list of slots CLASS will have."
70 (` (luna-define-class-function '(, class)
71 '(, (append parents '(standard-object)))
75 ;; Define CLASS as a luna-class. PARENTS, if non-nil, is a list of
76 ;; luna-class names inherited by CLASS. SLOTS, if non-nil, is a list
77 ;; of slots belonging to CLASS.
79 (defun luna-define-class-function (class &optional parents slots)
80 (static-condition-case nil
81 :symbol-for-testing-whether-colon-keyword-is-available-or-not
85 (setq key (intern (format ":%s" slot)))
87 (let ((oa (make-vector 31 0))
93 (setq parent (pop rest)
97 (when (setq j (get sym 'luna-slot-index))
98 (setq name (symbol-name sym))
99 (unless (intern-soft name oa)
100 (put (intern name oa) 'luna-slot-index (+ j b))
102 (luna-class-obarray (luna-find-class parent))))
105 (setq name (symbol-name (pop rest)))
106 (unless (intern-soft name oa)
107 (put (intern name oa) 'luna-slot-index i)
109 (luna-set-class class (vector 'class oa parents i))))
112 ;; Return a member (slot or method) of CLASS that has name
115 (defun luna-class-find-member (class member-name)
116 (or (stringp member-name)
117 (setq member-name (symbol-name member-name)))
118 (or (intern-soft member-name (luna-class-obarray class))
119 (let ((parents (luna-class-parents class))
123 (setq ret (luna-class-find-member
124 (luna-find-class (pop parents))
129 ;; Return a member (slot or method) of CLASS that has name
130 ;; MEMBER-NAME. If CLASS doesnt' have such a member, make it in
133 (defsubst luna-class-find-or-make-member (class member-name)
134 (or (stringp member-name)
135 (setq member-name (symbol-name member-name)))
136 (intern member-name (luna-class-obarray class)))
139 ;; Return the index number of SLOT-NAME in CLASS.
141 (defmacro luna-class-slot-index (class slot-name)
142 (` (get (luna-class-find-member (, class) (, slot-name)) 'luna-slot-index)))
144 (defmacro luna-define-method (name &rest definition)
145 "Define NAME as a method of a luna class.
147 Usage of this macro follows:
149 (luna-define-method NAME [METHOD-QUALIFIER] ARGLIST [DOCSTRING] BODY...)
151 The optional 1st argument METHOD-QUALIFIER specifies when and how the
154 If it is :before, call the method before calling the parents' methods.
156 If it is :after, call the method after calling the parents' methods.
158 If it is :around, call the method only. The parents' methods can be
159 executed by calling the function `luna-call-next-method' in BODY.
161 Otherwize, call the method only, and the parents' methods are never
162 executed. In this case, METHOD-QUALIFIER is treated as ARGLIST.
164 ARGLIST has the form ((VAR CLASS) METHOD-ARG ...), where VAR is a
165 variable name that should be bound to an entity that receives the
166 message NAME, CLASS is a class name. The first argument to the method
167 is VAR, and the remaining arguments are METHOD-ARGs.
169 If VAR is nil, arguments to the method are METHOD-ARGs. This kind of
170 methods can't be called from generic-function (see
171 `luna-define-generic').
173 The optional 4th argument DOCSTRING is the documentation of the
174 method. If it is not string, it is treated as BODY.
176 The optional 5th BODY is the body of the method."
177 (let ((method-qualifier (pop definition))
178 args specializer class self)
179 (if (memq method-qualifier '(:before :after :around))
180 (setq args (pop definition))
181 (setq args method-qualifier
182 method-qualifier nil))
183 (setq specializer (car args)
184 class (nth 1 specializer)
185 self (car specializer))
186 (` (let ((func (function
188 (cons self (cdr args))
191 (sym (luna-class-find-or-make-member
192 (luna-find-class '(, class)) '(, name))))
194 (put sym 'luna-method-qualifier (, method-qualifier))))))
196 (put 'luna-define-method 'lisp-indent-function 'defun)
198 (def-edebug-spec luna-define-method
199 (&define name [&optional &or ":before" ":after" ":around"]
202 [&optional ["&optional" arg &rest arg]]
203 &optional ["&rest" arg])
207 ;; Return a list of method functions named SERVICE registered in the
210 (defun luna-class-find-parents-functions (class service)
211 (let ((parents (luna-class-parents class))
215 (setq ret (luna-class-find-functions
216 (luna-find-class (pop parents))
220 ;; Return a list of method functions named SERVICE registered in CLASS
223 (defun luna-class-find-functions (class service)
224 (let ((sym (luna-class-find-member class service)))
226 (cond ((eq (get sym 'luna-method-qualifier) :before)
227 (cons (symbol-function sym)
228 (luna-class-find-parents-functions class service)))
229 ((eq (get sym 'luna-method-qualifier) :after)
230 (nconc (luna-class-find-parents-functions class service)
231 (list (symbol-function sym))))
232 ((eq (get sym 'luna-method-qualifier) :around)
233 (cons sym (luna-class-find-parents-functions class service)))
235 (list (symbol-function sym))))
236 (luna-class-find-parents-functions class service))))
239 ;;; @ instance (entity)
242 (defmacro luna-class-name (entity)
243 "Return class-name of the ENTITY."
244 (` (aref (, entity) 0)))
246 (defmacro luna-set-class-name (entity name)
247 (` (aset (, entity) 0 (, name))))
249 (defmacro luna-get-obarray (entity)
250 (` (aref (, entity) 1)))
252 (defmacro luna-set-obarray (entity obarray)
253 (` (aset (, entity) 1 (, obarray))))
255 (defmacro luna-slot-index (entity slot-name)
256 (` (luna-class-slot-index (luna-find-class (luna-class-name (, entity)))
259 (defsubst luna-slot-value (entity slot)
260 "Return the value of SLOT of ENTITY."
261 (aref entity (luna-slot-index entity slot)))
263 (defsubst luna-set-slot-value (entity slot value)
264 "Store VALUE into SLOT of ENTITY."
265 (aset entity (luna-slot-index entity slot) value))
267 (defmacro luna-find-functions (entity service)
268 (` (luna-class-find-functions (luna-find-class (luna-class-name (, entity)))
271 (defsubst luna-send (entity message &rest luna-current-method-arguments)
272 "Send MESSAGE to ENTITY, and return the result.
273 ENTITY is an instance of a luna class, and MESSAGE is a method name of
275 LUNA-CURRENT-METHOD-ARGUMENTS is arguments of the MESSAGE."
276 (let ((luna-next-methods (luna-find-functions entity message))
278 luna-previous-return-value)
279 (while (and luna-next-methods
281 (setq luna-current-method (pop luna-next-methods)
282 luna-previous-return-value
283 (apply luna-current-method
284 luna-current-method-arguments))
285 (if (symbolp luna-current-method)
286 (not (eq (get luna-current-method
287 'luna-method-qualifier) :around))
289 luna-previous-return-value))
292 (defvar luna-next-methods nil)
293 (defvar luna-current-method-arguments nil))
295 (defun luna-call-next-method ()
296 "Call the next method in the current method function.
297 A method function that has :around qualifier should call this function
298 to execute the parents' methods."
299 (let (luna-current-method
300 luna-previous-return-value)
301 (while (and luna-next-methods
303 (setq luna-current-method (pop luna-next-methods)
304 luna-previous-return-value
305 (apply luna-current-method
306 luna-current-method-arguments))
307 (if (symbolp luna-current-method)
308 (not (eq (get luna-current-method
309 'luna-method-qualifier) :around))
311 luna-previous-return-value))
313 (defun luna-make-entity (class &rest init-args)
314 "Make an entity (instance) of luna-class CLASS and return it.
315 INIT-ARGS is a plist of the form (:SLOT1 VAL1 :SLOT2 VAL2 ...),
316 where SLOTs are slots of CLASS and the VALs are initial values of
317 the corresponding SLOTs."
318 (let* ((c (get class 'luna-class))
319 (v (make-vector (luna-class-number-of-slots c) nil)))
320 (luna-set-class-name v class)
321 (luna-set-obarray v (make-vector 7 0))
322 (apply (function luna-send) v 'initialize-instance v init-args)))
325 ;;; @ interface (generic function)
328 ;; Find a method of ENTITY that handles MESSAGE, and call it with
329 ;; arguments LUNA-CURRENT-METHOD-ARGUMENTS.
331 (defun luna-apply-generic (entity message &rest luna-current-method-arguments)
332 (let* ((class (luna-class-name entity))
333 (cache (get message 'luna-method-cache))
334 (sym (intern-soft (symbol-name class) cache))
337 (setq luna-next-methods (symbol-value sym))
338 (setq luna-next-methods
339 (luna-find-functions entity message))
340 (set (intern (symbol-name class) cache)
342 (luna-call-next-method)))
345 ;; Convert ARGLIST (argument list spec for a method function) to the
346 ;; actual list of arguments.
348 (defsubst luna-arglist-to-arguments (arglist)
351 (let ((arg (car arglist)))
352 (or (memq arg '(&optional &rest))
353 (setq dest (cons arg dest))))
354 (setq arglist (cdr arglist)))
358 (defmacro luna-define-generic (name args &optional doc)
359 "Define a function NAME that provides a generic interface to the method NAME.
360 ARGS is the argument list for NAME. The first element of ARGS is an
363 The function handles a message sent to the entity by calling the
364 method with proper arguments.
366 The optional 3rd argument DOC is the documentation string for NAME."
369 (defun (, (intern (symbol-name name))) (, args)
371 (luna-apply-generic (, (car args)) '(, name)
372 (,@ (luna-arglist-to-arguments args))))
373 (put '(, name) 'luna-method-cache (make-vector 31 0))))
375 (defun (, (intern (symbol-name name))) (, args)
376 (luna-apply-generic (, (car args)) '(, name)
377 (,@ (luna-arglist-to-arguments args))))
378 (put '(, name) 'luna-method-cache (make-vector 31 0))))))
380 (put 'luna-define-generic 'lisp-indent-function 'defun)
386 (defun luna-define-internal-accessors (class-name)
387 "Define internal accessors for instances of the luna class CLASS-NAME.
389 Internal accessors are macros to refer and set a slot value of the
390 instances. For instance, if the class has SLOT, macros
391 CLASS-NAME-SLOT-internal and CLASS-NAME-set-SLOT-internal are defined.
393 CLASS-NAME-SLOT-internal accepts one argument INSTANCE, and returns
396 CLASS-NAME-set-SLOT-internal accepts two arguemnt INSTANCE and VALUE,
397 and sets SLOT to VALUE."
398 (let ((entity-class (luna-find-class class-name))
399 parents parent-class)
403 (if (luna-class-slot-index entity-class slot)
405 (setq parents (luna-class-parents entity-class))
407 (setq parent-class (luna-find-class (car parents)))
408 (if (luna-class-slot-index parent-class slot)
409 (throw 'derived nil))
410 (setq parents (cdr parents)))
413 (defmacro (, (intern (format "%s-%s-internal"
417 (, (luna-class-slot-index
419 (intern (symbol-name slot))))))
420 (defmacro (, (intern (format "%s-set-%s-internal"
424 (, (luna-class-slot-index
425 entity-class (intern (symbol-name slot))))
427 (luna-class-obarray entity-class))))
430 ;;; @ standard object
433 ;; Define super class of all luna classes.
434 (luna-define-class-function 'standard-object)
436 (luna-define-method initialize-instance ((entity standard-object)
438 "Initialize slots of ENTITY by INIT-ARGS."
439 (let* ((c (luna-find-class (luna-class-name entity)))
440 (oa (luna-class-obarray c))
443 (setq s (intern-soft (substring (symbol-name (pop init-args)) 1) oa)
446 (aset entity (get s 'luna-slot-index) i)))