X-Git-Url: http://repo.macrolet.net/gitweb/?a=blobdiff_plain;ds=sidebyside;f=glib%2Fgobject.meta.lisp;h=83b0ef2d34e874e298c1811870a32675bc2d6187;hb=db0d829452f27bbef68da2afd755b7accc5be5be;hp=e95aa55f234636c2644265fdf21b61e535d78015;hpb=2ef2ba47e1176d67f2d49308329c8d5db59b7c77;p=cl-gtk2.git diff --git a/glib/gobject.meta.lisp b/glib/gobject.meta.lisp index e95aa55..83b0ef2 100644 --- a/glib/gobject.meta.lisp +++ b/glib/gobject.meta.lisp @@ -9,21 +9,36 @@ :reader gobject-class-g-type-initializer) (interface-p :initform nil :initarg :g-interface-p - :reader gobject-class-interface-p))) + :reader gobject-class-interface-p)) + (:documentation "Metaclass for GObject-based classes.")) + +(defun initialize-gobject-class-g-type (class) + (if (gobject-class-g-type-initializer class) + (let* ((initializer-fn-ptr (foreign-symbol-pointer (gobject-class-g-type-initializer class))) + (type (when initializer-fn-ptr + (foreign-funcall-pointer initializer-fn-ptr nil + g-type)))) + (if (null initializer-fn-ptr) + (warn "Type initializer for class '~A' (GType '~A') is invalid: foreign symbol '~A'" + (gobject-class-g-type-name class) (class-name class) (gobject-class-g-type-initializer class)) + (progn + (when (= +g-type-invalid+ type) + (warn "Declared GType name '~A' for class '~A' is invalid ('~A' returned 0)" + (gobject-class-g-type-name class) (class-name class) + (gobject-class-g-type-initializer class))) + (unless (string= (gobject-class-g-type-name class) + (g-type-name type)) + (warn "Declared GType name '~A' for class '~A' does not match actual GType name '~A'" + (gobject-class-g-type-name class) + (class-name class) + (g-type-name type)))))) + (unless (g-type-from-name (gobject-class-g-type-name class)) + (warn "Declared GType name '~A' for class '~A' is invalid (g_type_name returned 0)" + (gobject-class-g-type-name class) (class-name class))))) (defmethod initialize-instance :after ((object gobject-class) &key &allow-other-keys) (register-object-type (gobject-class-g-type-name object) (class-name object)) - (when (gobject-class-g-type-initializer object) - (let ((type (foreign-funcall-pointer (foreign-symbol-pointer (gobject-class-g-type-initializer object)) nil - g-type))) - (assert (/= +g-type-invalid+ type) nil "Declared GType name '~A' for class '~A' is invalid (g_type_name returned 0)" - (gobject-class-g-type-name object) (class-name object)) - (assert (string= (gobject-class-g-type-name object) - (g-type-name type)) - nil "Declared GType name '~A' for class '~A' does not match actual GType name '~A'" - (gobject-class-g-type-name object) - (class-name object) - (g-type-name type))))) + (at-init (object) (initialize-gobject-class-g-type object))) (defclass gobject-direct-slot-definition (standard-direct-slot-definition) ((g-property-type :initform nil @@ -150,12 +165,12 @@ (defun create-gobject-from-class-and-initargs (class initargs) (when (gobject-class-interface-p class) (error "Trying to create instance of GInterface '~A' (class '~A')" (gobject-class-g-type-name class) (class-name class))) - (let (arg-names arg-values arg-types nc-setters nc-arg-values rest-initargs) + (let (arg-names arg-values arg-types nc-setters nc-arg-values) (declare (dynamic-extent arg-names arg-values arg-types nc-setters nc-arg-values)) (loop for (arg-name arg-value) on initargs by #'cddr for slot = (find arg-name (class-slots class) :key 'slot-definition-initargs :test 'member) - if (and slot (typep slot 'gobject-effective-slot-definition)) + when (and slot (typep slot 'gobject-effective-slot-definition)) do (typecase slot (gobject-property-effective-slot-definition (push (gobject-property-effective-slot-definition-g-property-name slot) arg-names) @@ -163,8 +178,7 @@ (push (gobject-effective-slot-definition-g-property-type slot) arg-types)) (gobject-fn-effective-slot-definition (push (gobject-fn-effective-slot-definition-g-setter-fn slot) nc-setters) - (push arg-value nc-arg-values))) - else do (setf rest-initargs (nconc rest-initargs (list arg-name arg-value)))) + (push arg-value nc-arg-values)))) (let ((object (g-object-call-constructor (gobject-class-g-type-name class) arg-names arg-values arg-types))) (loop for fn in nc-setters @@ -172,15 +186,24 @@ do (funcall fn object value)) object))) +(defun filter-initargs-by-class (class initargs) + (iter (with slots = (class-slots class)) + (for (arg-name arg-value) on initargs by #'cddr) + (for slot = (find arg-name slots :key #'slot-definition-initargs :test 'member)) + (unless (and slot (typep slot 'gobject-effective-slot-definition)) + (nconcing (list arg-name arg-value))))) + +(defmethod initialize-instance ((instance g-object) &rest initargs &key &allow-other-keys) + (let ((filtered-initargs (filter-initargs-by-class (class-of instance) initargs))) + (apply #'call-next-method instance filtered-initargs))) + (defmethod make-instance ((class gobject-class) &rest initargs &key pointer) (if pointer (progn (assert (= (length initargs) 2) nil "POINTER can not be combined with other initargs (~A)" initargs) (call-next-method)) - (multiple-value-bind (pointer rest-initargs) - (create-gobject-from-class-and-initargs class initargs) - (declare (dynamic-extent rest-initargs)) - (apply #'call-next-method class :pointer pointer rest-initargs)))) + (let ((pointer (create-gobject-from-class-and-initargs class initargs))) + (apply #'call-next-method class :pointer pointer initargs)))) (defmethod slot-boundp-using-class ((class gobject-class) object (slot gobject-property-effective-slot-definition)) (handler-case