Require cl when compile.
[elisp/flim.git] / luna.el
1 ;;; luna.el --- tiny OOP system kernel
2
3 ;; Copyright (C) 1999 Electrotechnical Laboratory, JAPAN.
4
5 ;; Author: MORIOKA Tomohiko <tomo@m17n.org>
6 ;; Keywords: OOP
7
8 ;; This file is part of FLIM (Faithful Library about Internet Message).
9
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.
14
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.
19
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.
24
25 ;;; Code:
26
27 (eval-when-compile (require 'cl))
28
29 (defmacro luna-find-class (name)
30   "Return the luna-class of the given NAME."
31   `(get ,name 'luna-class))
32
33 (defmacro luna-set-class (name class)
34   `(put ,name 'luna-class ,class))
35
36 (defmacro luna-class-obarray (class)
37   `(aref ,class 1))
38
39 (defmacro luna-class-parents (class)
40   `(aref ,class 2))
41
42 (defmacro luna-class-number-of-slots (class)
43   `(aref ,class 3))
44
45 (defmacro luna-define-class (type &optional parents slots)
46   "Define TYPE as a luna-class.
47 If PARENTS is specified, TYPE inherits PARENTS.
48 Each parent must be name of luna-class (symbol).
49 If SLOTS is specified, TYPE will be defined to have them."
50   (let ((oa (make-vector 31 0))
51         (rest parents)
52         parent name
53         (i 2))
54     (while rest
55       (setq parent (pop rest))
56       (mapatoms (lambda (sym)
57                   (when (get sym 'luna-member-index)
58                     (setq name (symbol-name sym))
59                     (unless (intern-soft name oa)
60                       (put (intern name oa) 'luna-member-index i)
61                       (setq i (1+ i))
62                       )))
63                 (luna-class-obarray (luna-find-class parent)))
64       )
65     (setq rest slots)
66     (while rest
67       (setq name (symbol-name (pop rest)))
68       (unless (intern-soft name oa)
69         (put (intern name oa) 'luna-member-index i)
70         (setq i (1+ i))
71         ))
72     `(luna-set-class ',type
73                      (vector 'class ,oa ',parents ,i))
74     ))
75
76 (defmacro luna-class-name (entity)
77   "Return class-name of the ENTITY."
78   `(aref ,entity 0))
79
80 (defmacro luna-set-class-name (entity name)
81   `(aset ,entity 0 ,name))
82
83 (defmacro luna-get-obarray (entity)
84   `(aref ,entity 1))
85
86 (defmacro luna-set-obarray (entity obarray)
87   `(aset ,entity 1 ,obarray))
88
89 (defmacro luna-make-entity (type &rest init-args)
90   "Make instance of luna-class TYPE and return it.
91 If INIT-ARGS is specified, it is used as initial values of the slots.
92 It must be plist and each slot name must have prefix `:'."
93   `(apply #'luna-make-entity-function ',type ',init-args))
94
95 (defsubst luna-make-entity-function (type &rest init-args)
96   (let* ((c (get type 'luna-class))
97          (v (make-vector (luna-class-number-of-slots c) nil))
98          (oa (luna-class-obarray c))
99          s i)
100     (luna-set-class-name v type)
101     (luna-set-obarray v (make-vector 7 0))
102     (while init-args
103       (setq s (intern-soft (substring (symbol-name (pop init-args)) 1) oa)
104             i (pop init-args))
105       (if s
106           (aset v (get s 'luna-member-index) i)
107         ))
108     v))
109
110 (defsubst luna-class-find-member (class member-name)
111   (or (stringp member-name)
112       (setq member-name (symbol-name member-name)))
113   (or (intern-soft member-name (luna-class-obarray class))
114       (let ((parents (luna-class-parents class))
115             ret)
116         (while (and parents
117                     (null
118                      (setq ret (luna-class-find-member
119                                 (luna-find-class (pop parents))
120                                 member-name)))))
121         ret)))
122
123 (defsubst luna-class-find-or-make-member (class member-name)
124   (or (stringp member-name)
125       (setq member-name (symbol-name member-name)))
126   (intern member-name (luna-class-obarray class)))
127
128 (defmacro luna-class-slot-index (class slot-name)
129   `(get (luna-class-find-member ,class ,slot-name) 'luna-member-index))
130
131 (defmacro luna-slot-index (entity slot-name)
132   `(luna-class-slot-index (luna-find-class (luna-class-name ,entity))
133                           ,slot-name))
134
135 (defsubst luna-slot-value (entity slot-name)
136   "Return value of SLOT-NAME of ENTITY."
137   (aref entity (luna-slot-index entity slot-name)))
138
139 (defmacro luna-define-method (name args &rest body)
140   "Define NAME as a method function of (nth 1 (car ARGS)) backend.
141
142 ARGS is like an argument list of lambda, but (car ARGS) must be
143 specialized parameter.  (car (car ARGS)) is name of variable and (nth
144 1 (car ARGS)) is name of backend."
145   (let* ((specializer (car args))
146          (class (nth 1 specializer))
147          (self (car specializer)))
148     `(let ((func (lambda ,(if self
149                               (cons self (cdr args))
150                             (cdr args))
151                    ,@body)))
152        (fset (luna-class-find-or-make-member (luna-find-class ',class) ',name)
153              func))))
154
155 (put 'luna-define-method 'lisp-indent-function 'defun)
156
157 (defsubst luna-class-find-function (class service)
158   (let ((sym (luna-class-find-member class service)))
159     (if (fboundp sym)
160         (symbol-function sym)
161       (let ((parents (luna-class-parents class))
162             ret)
163         (while (and parents
164                     (null
165                      (setq ret (luna-class-find-function
166                                 (luna-find-class (pop parents))
167                                 service)))))
168         ret))))
169
170 (defmacro luna-find-function (entity service)
171   `(luna-class-find-function (luna-find-class (luna-class-name ,entity))
172                              ,service))
173
174 (defsubst luna-send (entity message &rest args)
175   "Send MESSAGE to ENTITY with ARGS, and return the result."
176   (apply (luna-find-function entity message)
177          entity args))
178
179 (defsubst luna-arglist-to-arguments (arglist)
180   (let (dest)
181     (while arglist
182       (let ((arg (car arglist)))
183         (or (memq arg '(&optional &rest))
184             (setq dest (cons arg dest)))
185         )
186       (setq arglist (cdr arglist)))
187     (nreverse dest)))
188
189 (defmacro luna-define-generic (name args &optional doc)
190   "Define generic-function NAME.
191 ARGS is argument of and DOC is DOC-string."
192   (if doc
193       `(defun ,(intern (symbol-name name)) ,args
194          ,doc
195          (luna-send ,(car args) ',name
196                     ,@(luna-arglist-to-arguments (cdr args)))
197          )
198     `(defun ,(intern (symbol-name name)) ,args
199        (luna-send ,(car args) ',name
200                   ,@(luna-arglist-to-arguments (cdr args)))
201        )))
202
203 (put 'luna-define-generic 'lisp-indent-function 'defun)
204
205
206 ;;; @ end
207 ;;;
208
209 (provide 'luna)
210
211 ;; luna.el ends here