chiark / gitweb /
Shared library component improved
[clg] / glib / gboxed.lisp
CommitLineData
94f15c3c 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
62f12808 18;; $Id: gboxed.lisp,v 1.14 2005-02-01 15:24:52 espen Exp $
94f15c3c 19
20(in-package "GLIB")
21
22
23(eval-when (:compile-toplevel :load-toplevel :execute)
9adccb27 24 (init-types-in-library #.(concatenate 'string
25 (pkg-config:pkg-variable "glib-2.0" "libdir")
26 "/libgobject-2.0.so")))
d4b21b08 27
2b481386 28(defclass boxed (struct)
9adccb27 29 ()
30 (:metaclass struct-class))
94f15c3c 31
1dbf4216 32(defmethod instance-finalizer ((instance boxed))
33 (let ((location (proxy-location instance))
34 (type-number (type-number-of instance)))
35 #'(lambda ()
36 (remove-cached-instance location)
37 (%boxed-free type-number location))))
38
94f15c3c 39
40;;;; Metaclass for boxed classes
41
42(eval-when (:compile-toplevel :load-toplevel :execute)
9adccb27 43 (defclass boxed-class (struct-class)
4d83a8a6 44 ())
94f15c3c 45
9adccb27 46 (defmethod validate-superclass ((class boxed-class) (super standard-class))
47 (subtypep (class-name super) 'boxed)))
94f15c3c 48
94f15c3c 49
9adccb27 50(defmethod shared-initialize ((class boxed-class) names
51 &rest initargs &key name alien-name)
52 (declare (ignore initargs names))
53 (call-next-method)
54
55 (let* ((class-name (or name (class-name class)))
56 (type-number
57 (find-type-number
58 (or (first alien-name) (default-alien-type-name class-name)))))
59 (register-type class-name type-number)))
94f15c3c 60
9adccb27 61
1dbf4216 62(defbinding %boxed-copy () pointer
63 (type-number type-number)
9adccb27 64 (location pointer))
65
1dbf4216 66(defbinding %boxed-free () nil
67 (type-number type-number)
9adccb27 68 (location pointer))
69
70(defmethod reference-foreign ((class boxed-class) location)
1dbf4216 71 (%boxed-copy (find-type-number class) location))
9adccb27 72
73(defmethod unreference-foreign ((class boxed-class) location)
1dbf4216 74 (%boxed-free (find-type-number class) location))
94f15c3c 75
76
d4b21b08 77;;;;
78
62f12808 79(defun expand-boxed-type (type-number forward-p slots)
d4b21b08 80 `(defclass ,(type-from-number type-number) (boxed)
62f12808 81 ,(unless forward-p
82 slots)
d4b21b08 83 (:metaclass boxed-class)
84 (:alien-name ,(find-type-name type-number))))
94f15c3c 85
b0bb0027 86(register-derivable-type 'boxed "GBoxed" 'expand-boxed-type)
bddc0ce5 87
88;;;; Special boxed types
89
9adccb27 90;; (defclass gstring (boxed)
91;; ()
92;; (:metaclass boxed-class)
93;; (:alien-name "GString"))
94
95;; (deftype-method translate-from-alien
96;; gstring (type-spec location &optional weak-ref)
97;; `(let ((location ,location))
98;; (unless (null-pointer-p location)
99;; (prog1
100;; (c-call::%naturalize-c-string location)
101;; ,(unless weak-ref
102;; (unreference-alien type-spec location))))))
103
104;; (deftype-method translate-to-alien
105;; gstring (type-spec string &optional weak-ref)
106;; (declare (ignore weak-ref))
107;; `(let ((string ,string))
108;; ;; Always copy strings to prevent seg fault due to GC
109;; (funcall
110;; ',(proxy-class-copy (find-class type-spec))
111;; ',type-spec
112;; (make-pointer (1+ (kernel:get-lisp-obj-address string))))))
113
114;; (deftype-method cleanup-alien gstring (type-spec c-string &optional weak-ref)
115;; (when weak-ref
116;; (unreference-alien type-spec c-string)))
bddc0ce5 117