+#+glib2.8
+(progn
+ (define-callback toggle-ref-callback nil
+ ((data pointer) (location pointer) (last-ref-p boolean))
+ #+debug-ref-counting
+ (if last-ref-p
+ (format t "Object at 0x~8,'0X has no foreign references~%" (sap-int location))
+ (format t "Foreign reference added to object at 0x~8,'0X~%" (sap-int location)))
+ (if last-ref-p
+ (cache-instance (find-cached-instance location) t)
+ (cache-instance (find-cached-instance location) nil)))
+
+ (defbinding %object-add-toggle-ref () pointer
+ (location pointer)
+ (toggle-ref-callback callback)
+ (nil null))
+
+ (defbinding %object-remove-toggle-ref () pointer
+ (location pointer)
+ (toggle-ref-callback callback)
+ (nil null)))
+
+(defmethod reference-foreign ((class gobject-class) location)
+ (declare (ignore class))
+ (%object-ref location))
+
+(defmethod unreference-foreign ((class gobject-class) location)
+ (declare (ignore class))
+ (%object-unref location))
+
+#+debug-ref-counting
+(progn
+ (define-callback weak-ref-callback nil ((data pointer) (location pointer))
+ (format t "Object at 0x~8,'0X being finalized~%" (sap-int location)))
+
+ (defbinding %object-weak-ref () pointer
+ (location pointer)
+ (weak-ref-callback callback)
+ (nil null)))
+
+
+; (defbinding object-class-install-param () nil
+; (class pointer)
+; (id unsigned-int)
+; (parameter parameter))
+
+; (defbinding object-class-find-param-spec () parameter
+; (class pointer)
+; (name string))
+
+(defun signal-name-to-string (name)
+ (substitute #\_ #\- (string-downcase (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)
+ (if (eq (slot-definition-allocation (first direct-slotds)) :property)
+ (nconc
+ (list :pname (signal-name-to-string
+ (most-specific-slot-value direct-slotds 'pname
+ (slot-definition-name (first direct-slotds))))
+ :readable (most-specific-slot-value direct-slotds 'readable t)
+ :writable (most-specific-slot-value direct-slotds 'writable t)
+ :construct-only (most-specific-slot-value direct-slotds
+ 'construct-only nil))
+ (call-next-method))
+ (call-next-method)))
+
+
+(defvar *ignore-setting-construct-only-property* nil)
+(declaim (special *ignore-setting-construct-only-property*))
+
+(defmethod initialize-internal-slot-functions ((slotd effective-property-slot-definition))
+ (let ((type (slot-definition-type slotd))
+ (pname (slot-definition-pname slotd)))
+ (when (and (not (slot-boundp slotd 'getter)) (slot-readable-p slotd))
+ (setf
+ (slot-value slotd 'getter)
+ (let ((reader nil))
+ #'(lambda (object)
+ (unless reader
+ (setq reader (reader-function type)))
+ (let ((gvalue (gvalue-new type)))
+ (%object-get-property object pname gvalue)
+ (unwind-protect
+ (funcall reader gvalue +gvalue-value-offset+)
+ (gvalue-free gvalue t)))))))
+
+ (when (not (slot-boundp slotd 'setter))
+ (cond
+ ((slot-writable-p slotd)
+ (setf
+ (slot-value slotd 'setter)
+ (let ((writer nil))
+ #'(lambda (value object)
+ (unless writer
+ (setq writer (writer-function type)))
+ (let ((gvalue (gvalue-new type)))
+ (funcall writer value gvalue +gvalue-value-offset+)
+ (%object-set-property object pname gvalue)
+ (gvalue-free gvalue t)
+ value)))))
+
+ ((construct-only-property-p slotd)
+ (setf
+ (slot-value slotd 'setter)
+ #'(lambda (value object)
+ (declare (ignore value object))
+ (unless *ignore-setting-construct-only-property*
+ (error "Slot is not writable: ~A" (slot-definition-name slotd)))))))))
+
+ (call-next-method))
+
+(defmethod initialize-internal-slot-functions ((slotd effective-user-data-slot-definition))
+ (let ((slot-name (slot-definition-name slotd)))
+ (unless (slot-boundp slotd 'getter)
+ (setf
+ (slot-value slotd 'getter)
+ #'(lambda (object)
+ (prog1 (user-data object slot-name)))))
+ (unless (slot-boundp slotd 'setter)
+ (setf
+ (slot-value slotd 'setter)
+ #'(lambda (value object)
+ (setf (user-data object slot-name) value))))
+ (unless (slot-boundp slotd 'boundp)
+ (setf
+ (slot-value slotd 'boundp)
+ #'(lambda (object)
+ (user-data-p object slot-name)))))
+ (call-next-method))
+
+(defmethod shared-initialize :after ((class gobject-class) names &rest initargs)
+ (declare (ignore initargs))
+ (when (some #'(lambda (slotd)
+ (and
+ (eq (slot-definition-allocation slotd) :instance)
+ (not (typep slotd 'effective-special-slot-definition))))
+ (class-slots class))
+ (setf (slot-value class 'instance-slots-p) t)))
+
+
+
+;;;; Super class for all classes in the GObject type hierarchy
+
+(eval-when (:compile-toplevel :load-toplevel :execute)
+ (defclass gobject (ginstance)
+ (#+debug-ref-counting
+ (ref-count :allocation :alien :type int :reader ref-count))
+ (:metaclass gobject-class)
+ (:gtype "GObject")))
+
+#+debug-ref-counting
+(defmethod print-object ((instance gobject) stream)
+ (print-unreadable-object (instance stream :type t :identity nil)
+ (if (proxy-valid-p instance)
+ (format stream "at 0x~X (~D)" (sap-int (foreign-location instance)) (ref-count instance))
+ (write-string "at \"unbound\"" stream))))
+
+
+(defun initial-add (object function initargs key pkey)
+ (loop
+ as (initarg value . rest) = initargs then rest
+ do (cond
+ ((eq initarg key) (funcall function object value))
+ ((eq initarg pkey) (mapc #'(lambda (value)
+ (funcall function object value))
+ value)))
+ while rest))
+
+(defun initial-apply-add (object function initargs key pkey)
+ (initial-add object #'(lambda (object value)
+ (apply function object (mklist value)))
+ initargs key pkey))
+
+
+(defmethod make-proxy-instance ((class gobject-class) location &rest initargs)
+ (declare (ignore location initargs))
+ (if (slot-value class 'instance-slots-p)
+ (error "An object of class ~A has instance slots and should only be created with MAKE-INSTANCE" class)
+ (call-next-method)))
+
+
+(defmethod allocate-foreign ((object gobject) &rest initargs)
+ (let ((init-slots ()))
+ (flet ((value-from-initargs (slotd)
+ (loop
+ with slot-initargs = (slot-definition-initargs slotd)
+ for (initarg value) on initargs by #'cddr
+ when (find initarg slot-initargs)
+ do (return (values value t)))))
+
+ (loop
+ for slotd in (class-slots (class-of object))
+ when (and
+ (eq (slot-definition-allocation slotd) :property)
+ (construct-only-property-p slotd))
+ do (multiple-value-bind (value initarg-p) (value-from-initargs slotd)
+ (cond
+ (initarg-p (push (cons slotd value) init-slots))
+ ((slot-definition-initfunction slotd)
+ (push
+ (cons slotd (funcall (slot-definition-initfunction slotd)))
+ init-slots))))))
+
+ (cond
+ (init-slots
+ (let ((element-size (+ +gvalue-size+ +size-of-pointer+))
+ (num-slots (length init-slots)))
+ (with-allocated-memory (params (* num-slots element-size))
+ (loop
+ with string-writer = (writer-function 'string)
+ for (slotd . value) in init-slots
+ as offset = params then (sap+ offset element-size)
+ as type = (slot-definition-type slotd)
+ as pname = (slot-definition-pname slotd)
+ do (funcall string-writer pname offset)
+ (gvalue-init (sap+ offset +size-of-pointer+) type value))
+
+ (unwind-protect
+ (%gobject-newv (type-number-of object) num-slots params)
+
+ (loop
+ with string-destroy = (destroy-function 'string)
+ repeat num-slots
+ as offset = params then (sap+ offset element-size)
+ do (funcall string-destroy offset)
+ (gvalue-unset (sap+ offset +size-of-pointer+)))))))
+
+ (t (%gobject-new (type-number-of object))))))
+
+
+(defmethod shared-initialize ((object gobject) names &rest initargs)
+ (declare (ignore names initargs))
+ (let ((*ignore-setting-construct-only-property* t))
+ (call-next-method)))
+
+(defmethod initialize-instance :around ((object gobject) &rest initargs)
+ (declare (ignore initargs))
+ (prog1
+ (call-next-method)
+ #+debug-ref-counting(%object-weak-ref (foreign-location object))
+ #+glib2.8
+ (when (slot-value (class-of object) 'instance-slots-p)
+ (with-slots (location) object
+ (%object-add-toggle-ref location)
+ (%object-unref location)))))
+
+
+(defmethod instance-finalizer ((instance gobject))
+ (let ((location (foreign-location instance)))
+ #+glib2.8
+ (if (slot-value (class-of instance) 'instance-slots-p)
+ #'(lambda ()
+ #+debug-ref-counting
+ (format t "Finalizing proxy for 0x~8,'0X~%" (sap-int location))
+ (remove-cached-instance location)
+ (%object-remove-toggle-ref location))
+ #'(lambda ()
+ #+debug-ref-counting
+ (format t "Finalizing proxy for 0x~8,'0X~%" (sap-int location))
+ (remove-cached-instance location)
+ (%object-unref location)))
+ #-glib2.8
+ #'(lambda ()
+ (remove-cached-instance location)
+ (%object-unref location))))
+