+;; (push :debug-ref-counting *features*)
+ (defclass gobject-class (ginstance-class)
+ ((instance-slots-p :initform nil :reader instance-slots-p
+ :documentation "Non NIL if the class has slots with instance allocation")))
+
+ (defmethod validate-superclass ((class gobject-class) (super standard-class))
+; (subtypep (class-name super) 'gobject)
+ t))
+
+(defmethod slot-unbound (metaclass (class gobject-class) (slot (eql 'ref)))
+ (assert (class-direct-superclasses class))
+ (setf (slot-value class 'ref)
+ #?-(pkg-exists-p "glib-2.0" :atleast-version "2.10.0") '%object-ref
+ #?(pkg-exists-p "glib-2.0" :atleast-version "2.10.0")
+ ;; We do this hack instead of creating a new metaclass to avoid
+ ;; breaking backward compatibility
+ (if (subtypep (class-name class) 'initially-unowned)
+ '%object-ref-sink
+ '%object-ref)))
+
+(defmethod slot-unbound (metaclass (class gobject-class) (slot (eql 'unref)))
+ (setf (slot-value class 'unref) '%object-unref))
+
+
+(defclass direct-property-slot-definition (direct-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-only :initarg :construct-only :reader construct-only-property-p)))
+
+(defclass effective-property-slot-definition (effective-virtual-slot-definition)
+ ((pname :reader slot-definition-pname :initarg :pname)
+ (readable :initform t :reader slot-readable-p :initarg :readable)
+ (writable :initform t :reader slot-writable-p :initarg :writable)
+ (construct-only :initform nil :initarg :construct-only :reader construct-only-property-p)))
+
+(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))
+
+(defbinding %object-unref () nil
+ (location pointer))
+
+#?(pkg-exists-p "glib-2.0" :atleast-version "2.8.0")
+(progn
+ (define-callback toggle-ref-callback nil
+ ((data pointer) (location pointer) (last-ref-p boolean))
+ (declare (ignore data))
+ #+debug-ref-counting
+ (if last-ref-p
+ (format t "Object at 0x~8,'0X has no foreign references~%" (pointer-address location))
+ (format t "Foreign reference added to object at 0x~8,'0X~%" (pointer-address location)))
+ (if last-ref-p
+ (cache-instance (find-cached-instance location) t)
+ (cache-instance (find-cached-instance location) nil)))
+
+ (defbinding %object-add-toggle-ref (location) pointer
+ (location pointer)
+ (toggle-ref-callback callback)
+ (nil null))
+
+ (defbinding %object-remove-toggle-ref (location) pointer
+ (location pointer)
+ (toggle-ref-callback callback)
+ (nil null)))
+
+#+debug-ref-counting
+(progn
+ (define-callback weak-ref-callback nil ((data pointer) (location pointer))
+ (format t "Object at 0x~8,'0X (~A) being finalized~%" (pointer-address location) (type-from-number (%type-number-of-ginstance location))))
+
+ (defbinding %object-weak-ref (location) 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
+ (compute-most-specific-initargs direct-slotds
+ '(pname construct-only readable writable))
+ (call-next-method))
+ (call-next-method)))
+
+
+(defvar *ignore-setting-construct-only-property* nil)
+(declaim (special *ignore-setting-construct-only-property*))
+
+(defmethod compute-slot-reader-function ((slotd effective-property-slot-definition) &optional signal-unbound-p)
+ (declare (ignore signal-unbound-p))
+ (let* ((type (slot-definition-type slotd))
+ (pname (slot-definition-pname slotd))
+ (reader (reader-function type :ref :get)))
+ #'(lambda (object)
+ (with-memory (gvalue +gvalue-size+)
+ (%gvalue-init gvalue (find-type-number type))
+ (%object-get-property object pname gvalue)
+ (funcall reader gvalue +gvalue-value-offset+)))))
+
+(defmethod compute-slot-writer-function :around ((slotd effective-property-slot-definition))
+ (if (construct-only-property-p slotd)
+ #'(lambda (value object)
+ (declare (ignore value))
+ (unless *ignore-setting-construct-only-property*
+ (error 'unwritable-slot :name (slot-definition-name slotd) :instance object)))
+ (call-next-method)))
+
+(defmethod compute-slot-writer-function ((slotd effective-property-slot-definition))
+ (let* ((type (slot-definition-type slotd))
+ (pname (slot-definition-pname slotd))
+ (writer (writer-function type :temp t))
+ (destroy (destroy-function type :temp t)))
+ #'(lambda (value object)
+ (with-memory (gvalue +gvalue-size+)
+ (%gvalue-init gvalue (find-type-number type))
+ (funcall writer value gvalue +gvalue-value-offset+)
+ (%object-set-property object pname gvalue)
+ (funcall destroy gvalue +gvalue-value-offset+))
+ value)))
+
+(defmethod slot-readable-p ((slotd effective-user-data-slot-definition))
+ (declare (ignore slotd))
+ t)
+
+(defmethod compute-slot-reader-function ((slotd effective-user-data-slot-definition) &optional signal-unbound-p)
+ (declare (ignore signal-unbound-p))
+ (let ((slot-name (slot-definition-name slotd)))
+ #'(lambda (object)
+ (user-data object slot-name))))
+
+(defmethod compute-slot-boundp-function ((slotd effective-user-data-slot-definition))
+ (let ((slot-name (slot-definition-name slotd)))
+ #'(lambda (object)
+ (user-data-p object slot-name))))
+
+(defmethod slot-writable-p ((slotd effective-user-data-slot-definition))
+ (declare (ignore slotd))
+ t)
+
+(defmethod compute-slot-writer-function ((slotd effective-user-data-slot-definition))
+ (let ((slot-name (slot-definition-name slotd)))
+ #'(lambda (value object)
+ (setf (user-data object slot-name) value))))
+
+(defmethod compute-slot-makunbound-function ((slotd effective-user-data-slot-definition))
+ (let ((slot-name (slot-definition-name slotd)))
+ #'(lambda (object)
+ (unset-user-data object slot-name))))
+
+(defmethod compute-slots :around ((class gobject-class))
+ (let ((slots (call-next-method)))
+ (when (some #'(lambda (slotd)
+ (and
+ (eq (slot-definition-allocation slotd) :instance)
+ (not (typep slotd 'effective-special-slot-definition))))
+ slots)
+ (setf (slot-value class 'instance-slots-p) t))
+ slots))
+
+
+;;;; Super class for all classes in the GObject type hierarchy