+;;;; Metaclass used for subclasses of gobject
+
+(eval-when (:compile-toplevel :load-toplevel :execute)
+;; (push :debug-ref-counting *features*)
+ (defclass gobject-class (ginstance-class)
+ ((instance-slots-p :initform nil
+ :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))
+
+(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 :reader slot-readable-p :initarg :readable)
+ (writable :reader slot-writable-p :initarg :writable)
+ (construct-only :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))
+
+#+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
+