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 :after. If it is :after,
125 the method is called after a method of parent class is finished.
126 ARGLIST is like an argument list of lambda, but (car ARGLIST) must be
127 specialized parameter. (car (car ARGLIST)) is name of variable and
128 \(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 (eq method-qualifier :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 ":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 (if (eq (get sym 'luna-method-qualifier) :after)
178 (nconc (luna-class-find-parents-functions class service)
179 (list (symbol-function sym)))
180 (list (symbol-function sym))
182 (luna-class-find-parents-functions class service)
185 (defmacro luna-find-functions (entity service)
186 `(luna-class-find-functions (luna-find-class (luna-class-name ,entity))
189 (defsubst luna-send (entity message &rest args)
190 "Send MESSAGE to ENTITY with ARGS, and return the result."
191 (let ((functions (luna-find-functions entity message))
194 (setq ret (apply (car functions) args)
195 functions (cdr functions))
199 (defmacro luna-class-name (entity)
200 "Return class-name of the ENTITY."
203 (defmacro luna-set-class-name (entity name)
204 `(aset ,entity 0 ,name))
206 (defmacro luna-get-obarray (entity)
209 (defmacro luna-set-obarray (entity obarray)
210 `(aset ,entity 1 ,obarray))
212 (defun luna-make-entity (type &rest init-args)
213 "Make instance of luna-class TYPE and return it.
214 If INIT-ARGS is specified, it is used as initial values of the slots.
215 It must be plist and each slot name must have prefix `:'."
216 (let* ((c (get type 'luna-class))
217 (v (make-vector (luna-class-number-of-slots c) nil)))
218 (luna-set-class-name v type)
219 (luna-set-obarray v (make-vector 7 0))
220 (apply #'luna-send v 'initialize-instance v init-args)
223 (defsubst luna-arglist-to-arguments (arglist)
226 (let ((arg (car arglist)))
227 (or (memq arg '(&optional &rest))
228 (setq dest (cons arg dest)))
230 (setq arglist (cdr arglist)))
233 (defmacro luna-define-generic (name args &optional doc)
234 "Define generic-function NAME.
235 ARGS is argument of and DOC is DOC-string."
237 `(defun ,(intern (symbol-name name)) ,args
239 (luna-send ,(car args) ',name
240 ,@(luna-arglist-to-arguments args))
242 `(defun ,(intern (symbol-name name)) ,args
243 (luna-send ,(car args) ',name
244 ,@(luna-arglist-to-arguments args))
247 (put 'luna-define-generic 'lisp-indent-function 'defun)
249 (defun luna-define-internal-accessors (class-name)
250 "Define internal accessors for an entity of CLASS-NAME."
251 (let ((entity-class (luna-find-class class-name))
252 parents parent-class)
255 (if (luna-class-slot-index entity-class slot)
257 (setq parents (luna-class-parents entity-class))
259 (setq parent-class (luna-find-class (car parents)))
260 (if (luna-class-slot-index parent-class slot)
261 (throw 'derived nil))
262 (setq parents (cdr parents))
266 (defmacro ,(intern (format "%s-%s-internal"
270 ,(luna-class-slot-index entity-class
271 (intern (symbol-name slot)))
273 (defmacro ,(intern (format "%s-set-%s-internal"
277 ,(luna-class-slot-index
278 entity-class (intern (symbol-name slot)))
282 (luna-class-obarray entity-class))))
284 (luna-define-class-function 'standard-object)
286 (luna-define-method initialize-instance ((entity standard-object)
288 (let* ((c (luna-find-class (luna-class-name entity)))
289 (oa (luna-class-obarray c))
292 (setq s (intern-soft (substring (symbol-name (pop init-args)) 1) oa)
295 (aset entity (get s 'luna-slot-index) i)