1 ;;; luna.el --- tiny OOP system kernel
3 ;; Copyright (C) 1999 Electrotechnical Laboratory, JAPAN.
4 ;; Licensed to the Free Software Foundation.
6 ;; Author: MORIOKA Tomohiko <tomo@m17n.org>
9 ;; This file is part of FLIM (Faithful Library about Internet Message).
11 ;; This program is free software; you can redistribute it and/or
12 ;; modify it under the terms of the GNU General Public License as
13 ;; published by the Free Software Foundation; either version 2, or (at
14 ;; your option) any later version.
16 ;; This program is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ;; General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
28 (eval-when-compile (require 'cl))
30 (defmacro luna-find-class (name)
31 "Return the luna-class of the given NAME."
32 `(get ,name 'luna-class))
34 (defmacro luna-set-class (name class)
35 `(put ,name 'luna-class ,class))
37 (defmacro luna-class-obarray (class)
40 (defmacro luna-class-parents (class)
43 (defmacro luna-class-number-of-slots (class)
46 (defmacro luna-define-class (type &optional parents slots)
47 "Define TYPE as a luna-class.
48 If PARENTS is specified, TYPE inherits PARENTS.
49 Each parent must be name of luna-class (symbol).
50 If SLOTS is specified, TYPE will be defined to have them."
51 `(luna-define-class-function ',type ',(append parents '(standard-object))
54 (defun luna-define-class-function (type &optional parents slots)
55 (let ((oa (make-vector 31 0))
61 (setq parent (pop rest)
63 (mapatoms (lambda (sym)
64 (when (setq j (get sym 'luna-slot-index))
65 (setq name (symbol-name sym))
66 (unless (intern-soft name oa)
67 (put (intern name oa) 'luna-slot-index (+ j b))
70 (luna-class-obarray (luna-find-class parent)))
74 (setq name (symbol-name (pop rest)))
75 (unless (intern-soft name oa)
76 (put (intern name oa) 'luna-slot-index i)
79 (luna-set-class type (vector 'class oa parents i))
82 (defun luna-class-find-member (class member-name)
83 (or (stringp member-name)
84 (setq member-name (symbol-name member-name)))
85 (or (intern-soft member-name (luna-class-obarray class))
86 (let ((parents (luna-class-parents class))
90 (setq ret (luna-class-find-member
91 (luna-find-class (pop parents))
95 (defsubst luna-class-find-or-make-member (class member-name)
96 (or (stringp member-name)
97 (setq member-name (symbol-name member-name)))
98 (intern member-name (luna-class-obarray class)))
100 (defmacro luna-class-slot-index (class slot-name)
101 `(get (luna-class-find-member ,class ,slot-name) 'luna-slot-index))
103 (defmacro luna-slot-index (entity slot-name)
104 `(luna-class-slot-index (luna-find-class (luna-class-name ,entity))
107 (defsubst luna-slot-value (entity slot)
108 "Return the value of SLOT of ENTITY."
109 (aref entity (luna-slot-index entity slot)))
111 (defsubst luna-set-slot-value (entity slot value)
112 "Store VALUE into SLOT of ENTITY."
113 (aset entity (luna-slot-index entity slot) value))
115 (defmacro luna-define-method (name &rest definition)
116 "Define NAME as a method function of a class.
118 Usage of this macro follows:
120 (luna-define-method NAME [METHOD-QUALIFIER] ARGLIST [DOCSTRING] BODY...)
122 NAME is the name of method.
124 Optional argument METHOD-QUALIFIER must be :before or :after. If it
125 is :before / :after, the method is called before / after a method of
126 parent class is finished. ARGLIST is like an argument list of lambda,
127 but (car ARGLIST) must be specialized parameter. (car (car ARGLIST))
128 is name of variable and \(nth 1 (car ARGLIST)) is name of class.
130 Optional argument DOCSTRING is the documentation of method.
132 BODY is the body of method."
133 (let ((method-qualifier (pop definition))
134 args specializer class self)
135 (if (memq method-qualifier '(:before :after))
136 (setq args (pop definition))
137 (setq args method-qualifier
138 method-qualifier nil)
140 (setq specializer (car args)
141 class (nth 1 specializer)
142 self (car specializer))
143 `(let ((func (lambda ,(if self
144 (cons self (cdr args))
147 (sym (luna-class-find-or-make-member
148 (luna-find-class ',class) ',name)))
150 (put sym 'luna-method-qualifier ,method-qualifier)
153 (put 'luna-define-method 'lisp-indent-function 'defun)
155 (def-edebug-spec luna-define-method
156 (&define name [&optional &or ":before" ":after"]
159 [&optional ["&optional" arg &rest arg]]
160 &optional ["&rest" arg]
164 (defun luna-class-find-parents-functions (class service)
165 (let ((parents (luna-class-parents class))
169 (setq ret (luna-class-find-functions
170 (luna-find-class (pop parents))
174 (defun luna-class-find-functions (class service)
175 (let ((sym (luna-class-find-member class service)))
177 (cond ((eq (get sym 'luna-method-qualifier) :before)
178 (cons (symbol-function sym)
179 (luna-class-find-parents-functions class service))
181 ((eq (get sym 'luna-method-qualifier) :after)
182 (nconc (luna-class-find-parents-functions class service)
183 (list (symbol-function sym)))
186 (list (symbol-function sym))
188 (luna-class-find-parents-functions class service)
191 (defmacro luna-find-functions (entity service)
192 `(luna-class-find-functions (luna-find-class (luna-class-name ,entity))
195 (defsubst luna-send (entity message &rest args)
196 "Send MESSAGE to ENTITY with ARGS, and return the result."
197 (let ((functions (luna-find-functions entity message))
200 (setq ret (apply (car functions) args)
201 functions (cdr functions))
205 (defmacro luna-class-name (entity)
206 "Return class-name of the ENTITY."
209 (defmacro luna-set-class-name (entity name)
210 `(aset ,entity 0 ,name))
212 (defmacro luna-get-obarray (entity)
215 (defmacro luna-set-obarray (entity obarray)
216 `(aset ,entity 1 ,obarray))
218 (defun luna-make-entity (type &rest init-args)
219 "Make instance of luna-class TYPE and return it.
220 If INIT-ARGS is specified, it is used as initial values of the slots.
221 It must be plist and each slot name must have prefix `:'."
222 (let* ((c (get type 'luna-class))
223 (v (make-vector (luna-class-number-of-slots c) nil)))
224 (luna-set-class-name v type)
225 (luna-set-obarray v (make-vector 7 0))
226 (apply #'luna-send v 'initialize-instance v init-args)
229 (defsubst luna-arglist-to-arguments (arglist)
232 (let ((arg (car arglist)))
233 (or (memq arg '(&optional &rest))
234 (setq dest (cons arg dest)))
236 (setq arglist (cdr arglist)))
239 (defmacro luna-define-generic (name args &optional doc)
240 "Define generic-function NAME.
241 ARGS is argument of and DOC is DOC-string."
243 `(defun ,(intern (symbol-name name)) ,args
245 (luna-send ,(car args) ',name
246 ,@(luna-arglist-to-arguments args))
248 `(defun ,(intern (symbol-name name)) ,args
249 (luna-send ,(car args) ',name
250 ,@(luna-arglist-to-arguments args))
253 (put 'luna-define-generic 'lisp-indent-function 'defun)
255 (defun luna-define-internal-accessors (class-name)
256 "Define internal accessors for an entity of CLASS-NAME."
257 (let ((entity-class (luna-find-class class-name))
258 parents parent-class)
261 (if (luna-class-slot-index entity-class slot)
263 (setq parents (luna-class-parents entity-class))
265 (setq parent-class (luna-find-class (car parents)))
266 (if (luna-class-slot-index parent-class slot)
267 (throw 'derived nil))
268 (setq parents (cdr parents))
272 (defmacro ,(intern (format "%s-%s-internal"
276 ,(luna-class-slot-index entity-class
277 (intern (symbol-name slot)))
279 (defmacro ,(intern (format "%s-set-%s-internal"
283 ,(luna-class-slot-index
284 entity-class (intern (symbol-name slot)))
288 (luna-class-obarray entity-class))))
290 (luna-define-class-function 'standard-object)
292 (luna-define-method initialize-instance ((entity standard-object)
294 (let* ((c (luna-find-class (luna-class-name entity)))
295 (oa (luna-class-obarray c))
298 (setq s (intern-soft (substring (symbol-name (pop init-args)) 1) oa)
301 (aset entity (get s 'luna-slot-index) i)