Renamed VECTOR-NULL to NULL-TERMINATED-VECTOR
[clg] / glib / gboxed.lisp
CommitLineData
b44caf77 1;; Common Lisp bindings for GTK+ v2.0
2;; Copyright (C) 2001 Espen S. Johnsen <esj@stud.cs.uit.no>
3;;
4;; This library is free software; you can redistribute it and/or
5;; modify it under the terms of the GNU Lesser General Public
6;; License as published by the Free Software Foundation; either
7;; version 2 of the License, or (at your option) any later version.
8;;
9;; This library is distributed in the hope that it will be useful,
10;; but WITHOUT ANY WARRANTY; without even the implied warranty of
11;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12;; Lesser General Public License for more details.
13;;
14;; You should have received a copy of the GNU Lesser General Public
15;; License along with this library; if not, write to the Free Software
16;; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
48ae54db 18;; $Id: gboxed.lisp,v 1.16 2005/02/14 00:44:26 espen Exp $
b44caf77 19
20(in-package "GLIB")
21
22
ce63ad97 23(defclass boxed (struct)
6baf860c 24 ()
25 (:metaclass struct-class))
b44caf77 26
29933e83 27(defmethod instance-finalizer ((instance boxed))
28 (let ((location (proxy-location instance))
29 (type-number (type-number-of instance)))
30 #'(lambda ()
31 (remove-cached-instance location)
32 (%boxed-free type-number location))))
33
b44caf77 34
35;;;; Metaclass for boxed classes
36
37(eval-when (:compile-toplevel :load-toplevel :execute)
6baf860c 38 (defclass boxed-class (struct-class)
935a783c 39 ())
b44caf77 40
6baf860c 41 (defmethod validate-superclass ((class boxed-class) (super standard-class))
42 (subtypep (class-name super) 'boxed)))
b44caf77 43
b44caf77 44
6baf860c 45(defmethod shared-initialize ((class boxed-class) names
46 &rest initargs &key name alien-name)
47 (declare (ignore initargs names))
48 (call-next-method)
49
50 (let* ((class-name (or name (class-name class)))
51 (type-number
52 (find-type-number
53 (or (first alien-name) (default-alien-type-name class-name)))))
54 (register-type class-name type-number)))
b44caf77 55
6baf860c 56
29933e83 57(defbinding %boxed-copy () pointer
58 (type-number type-number)
6baf860c 59 (location pointer))
60
29933e83 61(defbinding %boxed-free () nil
62 (type-number type-number)
6baf860c 63 (location pointer))
64
65(defmethod reference-foreign ((class boxed-class) location)
29933e83 66 (%boxed-copy (find-type-number class) location))
6baf860c 67
68(defmethod unreference-foreign ((class boxed-class) location)
29933e83 69 (%boxed-free (find-type-number class) location))
b44caf77 70
71
3a935dfa 72;;;;
73
e9934f39 74(defun expand-boxed-type (type-number forward-p slots)
3a935dfa 75 `(defclass ,(type-from-number type-number) (boxed)
e9934f39 76 ,(unless forward-p
77 slots)
3a935dfa 78 (:metaclass boxed-class)
79 (:alien-name ,(find-type-name type-number))))
b44caf77 80
6895c081 81(register-derivable-type 'boxed "GBoxed" 'expand-boxed-type)
888d25fb 82
83;;;; Special boxed types
84
6baf860c 85;; (defclass gstring (boxed)
86;; ()
87;; (:metaclass boxed-class)
88;; (:alien-name "GString"))
89
90;; (deftype-method translate-from-alien
91;; gstring (type-spec location &optional weak-ref)
92;; `(let ((location ,location))
93;; (unless (null-pointer-p location)
94;; (prog1
95;; (c-call::%naturalize-c-string location)
96;; ,(unless weak-ref
97;; (unreference-alien type-spec location))))))
98
99;; (deftype-method translate-to-alien
100;; gstring (type-spec string &optional weak-ref)
101;; (declare (ignore weak-ref))
102;; `(let ((string ,string))
103;; ;; Always copy strings to prevent seg fault due to GC
104;; (funcall
105;; ',(proxy-class-copy (find-class type-spec))
106;; ',type-spec
107;; (make-pointer (1+ (kernel:get-lisp-obj-address string))))))
108
109;; (deftype-method cleanup-alien gstring (type-spec c-string &optional weak-ref)
110;; (when weak-ref
111;; (unreference-alien type-spec c-string)))
888d25fb 112
8574c8ad 113
114
48ae54db 115;;;; NULL terminated vector of strings
8574c8ad 116
48ae54db 117(deftype strings () '(null-terminated-vector string))
8574c8ad 118(register-type 'strings "GStrv")