chiark / gitweb /
New function UPDATE-USER-DATA and some bug fixes
[clg] / glib / gobject.lisp
index 829277271384f3825502cd7179dad10e9456a6b0..01f3998dd0a4d29af772d82055fc33b5ad234235 100644 (file)
@@ -15,7 +15,7 @@
 ;; License along with this library; if not, write to the Free Software
 ;; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
-;; $Id: gobject.lisp,v 1.23 2004-12-16 23:19:17 espen Exp $
+;; $Id: gobject.lisp,v 1.26 2004-12-29 21:07:46 espen Exp $
 
 (in-package "GLIB")
 
@@ -41,7 +41,14 @@ (defclass effective-property-slot-definition (effective-virtual-slot-definition)
   ((pname :reader slot-definition-pname :initarg :pname)
    (readable :reader slot-readable-p :initarg :readable)
    (writable :reader slot-writable-p :initarg :writable)
-   (construct :initarg :construct)));)
+   (construct :initarg :construct)))
+
+(defclass direct-user-data-slot-definition (direct-virtual-slot-definition)
+  ())
+
+(defclass effective-user-data-slot-definition (effective-virtual-slot-definition)
+  ())
+
 
 (defbinding %object-ref () pointer
   (location pointer))
@@ -74,11 +81,13 @@ (defun signal-name-to-string (name)
 (defmethod direct-slot-definition-class ((class gobject-class) &rest initargs)
   (case (getf initargs :allocation)
     (:property (find-class 'direct-property-slot-definition))
+    (:user-data (find-class 'direct-user-data-slot-definition))
     (t (call-next-method))))
 
 (defmethod effective-slot-definition-class ((class gobject-class) &rest initargs)
   (case (getf initargs :allocation)
     (:property (find-class 'effective-property-slot-definition))
+    (:user-data (find-class 'effective-user-data-slot-definition))
     (t (call-next-method))))
 
 (defmethod compute-effective-slot-definition-initargs ((class gobject-class) direct-slotds)
@@ -103,7 +112,7 @@ (defmethod initialize-internal-slot-functions ((slotd effective-property-slot-de
        (let ((reader nil))
         #'(lambda (object)
             (unless reader
-              (setq reader (reader-function (type-from-number type-number))))
+              (setq reader (reader-function type))) ;(type-from-number type-number))))
             (let ((gvalue (gvalue-new type-number)))
               (%object-get-property object pname gvalue)
               (unwind-protect
@@ -116,7 +125,7 @@ (defmethod initialize-internal-slot-functions ((slotd effective-property-slot-de
        (let ((writer nil))
         #'(lambda (value object)
             (unless writer
-              (setq writer (writer-function (type-from-number type-number))))
+              (setq writer (writer-function type))) ;(type-from-number type-number))))
             (let ((gvalue (gvalue-new type-number)))
               (funcall writer value gvalue +gvalue-value-offset+)
               (%object-set-property object pname gvalue)
@@ -125,6 +134,22 @@ (defmethod initialize-internal-slot-functions ((slotd effective-property-slot-de
 
   (call-next-method))
 
+(defmethod initialize-internal-slot-functions ((slotd effective-user-data-slot-definition))
+  (let ((slot-name (slot-definition-name slotd)))
+    (setf 
+     (slot-value slotd 'getter)
+     #'(lambda (object)
+        (prog1 (user-data object slot-name))))
+    (setf 
+     (slot-value slotd 'setter)
+     #'(lambda (value object)
+        (setf (user-data object slot-name) value)))
+    (setf 
+     (slot-value slotd 'boundp)
+     #'(lambda (object)
+        (user-data-p object slot-name))))
+  (call-next-method))
+
 
 ;;;; Super class for all classes in the GObject type hierarchy
 
@@ -152,49 +177,50 @@ (defun initial-apply-add (object function initargs key pkey)
 
 
 (defmethod initialize-instance ((object gobject) &rest initargs)
-  ;; Extract initargs which we should pass directly to the GObeject
-  ;; constructor
-  (let* ((slotds (class-slots (class-of object)))
-        (args (when initargs
-                (loop 
-                 as (key value . rest) = initargs then rest
-                 as slotd = (find-if
-                             #'(lambda (slotd)
-                                 (member key (slot-definition-initargs slotd)))
-                             slotds)
-                 when (and (typep slotd 'effective-property-slot-definition)
-                           (slot-value slotd 'construct))
-                 collect (progn 
-                           (remf initargs key)
-                           (list 
-                            (slot-definition-pname slotd)
-                            (slot-definition-type slotd)
-                            value))
-                 while rest))))
-    (if args
-       (let* ((string-size (size-of 'string))
-              (string-writer (writer-function 'string))
-              (string-destroy (destroy-function 'string))
-              (params (allocate-memory 
-                       (* (length args) (+ string-size +gvalue-size+)))))
-         (loop
-          for (pname type value) in args
-          as tmp = params then (sap+ tmp (+ string-size +gvalue-size+))
-          do (funcall string-writer pname tmp)
-          (gvalue-init (sap+ tmp string-size) type value))
-         (unwind-protect
-              (setf  
-               (slot-value object 'location) 
-               (%gobject-newv (type-number-of object) (length args) params))
+  (unless (slot-boundp object 'location)
+    ;; Extract initargs which we should pass directly to the GObeject
+    ;; constructor
+    (let* ((slotds (class-slots (class-of object)))
+          (args (when initargs
+                  (loop 
+                   as (key value . rest) = initargs then rest
+                   as slotd = (find-if
+                               #'(lambda (slotd)
+                                   (member key (slot-definition-initargs slotd)))
+                               slotds)
+                   when (and (typep slotd 'effective-property-slot-definition)
+                             (slot-value slotd 'construct))
+                   collect (progn 
+                             (remf initargs key)
+                             (list 
+                              (slot-definition-pname slotd)
+                              (slot-definition-type slotd)
+                              value))
+                   while rest))))
+      (if args
+         (let* ((string-size (size-of 'string))
+                (string-writer (writer-function 'string))
+                (string-destroy (destroy-function 'string))
+                (params (allocate-memory 
+                         (* (length args) (+ string-size +gvalue-size+)))))
            (loop
-            repeat (length args)
+            for (pname type value) in args
             as tmp = params then (sap+ tmp (+ string-size +gvalue-size+))
-            do (funcall string-destroy tmp)
-            (gvalue-unset (sap+ tmp string-size)))
-           (deallocate-memory params)))
+            do (funcall string-writer pname tmp)
+            (gvalue-init (sap+ tmp string-size) type value))
+           (unwind-protect
+               (setf  
+                (slot-value object 'location) 
+                (%gobject-newv (type-number-of object) (length args) params))
+             (loop
+              repeat (length args)
+              as tmp = params then (sap+ tmp (+ string-size +gvalue-size+))
+              do (funcall string-destroy tmp)
+              (gvalue-unset (sap+ tmp string-size)))
+             (deallocate-memory params)))
        (setf  
         (slot-value object 'location) 
-        (%gobject-new (type-number-of object)))))
+        (%gobject-new (type-number-of object))))))
 
   (apply #'call-next-method object initargs))
 
@@ -248,19 +274,31 @@ (defbinding %object-set-qdata-full () nil
 
 ;;;; User data
 
-(defun (setf object-data) (data object key &key (test #'eq))
+(defun (setf user-data) (data object key)
   (%object-set-qdata-full
-   object (quark-from-object key :test test)
+   object (quark-from-object key)
    (register-user-data data) (callback %destroy-user-data))
   data)
 
+;; depecated
+(defun (setf object-data) (data object key &key (test #'eq))
+  (assert (eq test #'eq))
+  (setf (user-data object key) data))
+
 (defbinding %object-get-qdata () unsigned-long
   (object gobject)              
   (id quark))
 
+(defun user-data (object key)
+  (find-user-data (%object-get-qdata object (quark-from-object key))))
+
+;; depecated
 (defun object-data (object key &key (test #'eq))
-  (find-user-data
-   (%object-get-qdata object (quark-from-object key :test test))))
+  (assert (eq test #'eq))
+  (user-data object key))
+
+(defun user-data-p (object key)
+  (nth-value 1 (find-user-data (%object-get-qdata object (quark-from-object key)))))
 
 
 ;;;;
@@ -313,10 +351,8 @@ (defun slot-definition-from-property (class property &optional args)
       `(,slot-name
        :allocation :property :pname ,name
 
-       ;; temporary hack
        ,@(cond
-          ((find :unbound args) (list :unbound (getf args :unbound)))
-          ((type-is-p slot-type 'gobject) (list :unbound nil)))
+          ((find :unbound args) (list :unbound (getf args :unbound))))
        
        ;; accessors
        ,@(cond
@@ -379,3 +415,24 @@ (defun expand-gobject-type (type &optional options (metaclass 'gobject-class))
 
 
 (register-derivable-type 'gobject "GObject" 'expand-gobject-type)
+
+
+;;; Pseudo type for gobject instances which have their reference count
+;;; increased by the returning function
+
+(defmethod alien-type ((type (eql 'referenced)) &rest args)
+  (declare (ignore type args))
+  (alien-type 'gobject))
+
+(defmethod from-alien-form (form (type (eql 'referenced)) &rest args)
+  (declare (ignore type))
+  (destructuring-bind (type) args
+    (if (subtypep type 'gobject)
+       (let ((instance (make-symbol "INSTANCE")))
+         `(let ((,instance ,(from-alien-form form type)))
+            (when ,instance
+              (%object-unref (proxy-location ,instance)))
+            ,instance))
+      (error "~A is not a subclass of GOBJECT" type))))
+
+(export 'referenced)