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