Basic GError binding
authorDmitry Kalyanov <Kalyanov.Dmitry@gmail.com>
Mon, 12 Oct 2009 20:30:27 +0000 (00:30 +0400)
committerDmitry Kalyanov <Kalyanov.Dmitry@gmail.com>
Mon, 12 Oct 2009 20:30:27 +0000 (00:30 +0400)
glib/cl-gtk2-glib.asd
glib/glib.gerror.lisp [new file with mode: 0644]
glib/glib.lisp

index 0007491..9132635 100644 (file)
@@ -8,6 +8,7 @@
                (:file "glib.gstrv")
                (:file "glib.string")
                (:file "glib.quark")
+               (:file "glib.gerror")
 
                (:file "gobject.init")
                (:file "gobject.ffi.package")
diff --git a/glib/glib.gerror.lisp b/glib/glib.gerror.lisp
new file mode 100644 (file)
index 0000000..65019c0
--- /dev/null
@@ -0,0 +1,77 @@
+(in-package :glib)
+
+(defcstruct g-error
+  (:domain g-quark)
+  (:code :int)
+  (:message (:string :free-from-foreign nil)))
+
+(defcfun g-error-new-literal :pointer
+  (domain g-quark)
+  (code :int)
+  (message :string))
+
+(defcfun g-error-free :void
+  (error :pointer))
+
+(defcfun g-error-copy :pointer
+  (error :pointer))
+
+(defcfun g-error-matches :boolean
+  (error :pointer)
+  (domain g-quark)
+  (code :int))
+
+(defcfun g-set-error-literal :void
+  (err-ptr :pointer)
+  (domain g-quark)
+  (code :int)
+  (message :string))
+
+(defcfun g-propagate-error :void
+  (dest-ptr :pointer)
+  (src-ptr :pointer))
+
+(defcfun g-clear-error :void
+  (err-ptr :pointer))
+
+(define-condition g-error-condition (error)
+  ((domain :initarg :domain :initform nil :reader g-error-condition-domain)
+   (code :initarg :code :initform nil :reader g-error-condition-code)
+   (message :initarg :message :initform nil :reader g-error-condition-message))
+  (:report (lambda (e stream)
+             (format stream "GError was raised. Domain: ~S, code: ~S, message: ~A"
+                     (g-error-condition-domain e)
+                     (g-error-condition-code e)
+                     (g-error-condition-message e)))))
+
+(defun mayber-raise-g-error-condition (err)
+  (unless (null-pointer-p err)
+    (error 'g-error-condition
+           :domain (foreign-slot-value err 'g-error :domain)
+           :code (foreign-slot-value err 'g-error :code)
+           :message (foreign-slot-value err 'g-error :message))))
+
+(defmacro with-g-error ((err) &body body)
+  `(with-foreign-object (,err :pointer)
+     (setf (mem-ref ,err :pointer) (null-pointer))
+     (unwind-protect
+          (progn ,@body)
+       (mayber-raise-g-error-condition (mem-ref ,err :pointer))
+       (g-clear-error ,err))))
+
+(defmacro with-catching-to-g-error ((err) &body body)
+  `(handler-case
+       (progn ,@body)
+     (g-error-condition (e)
+       (g-set-error-literal ,err
+                            (g-error-condition-domain e)
+                            (g-error-condition-code e)
+                            (g-error-condition-message e)))))
+
+;; void                g_prefix_error                      (GError **err,
+;;                                                          const gchar *format,
+;;                                                          ...);
+;; void                g_propagate_prefixed_error          (GError **dest,
+;;                                                          GError *src,
+;;                                                          const gchar *format,
+;;                                                          ...);
\ No newline at end of file
index 13f3dfe..9fa7dcb 100755 (executable)
            #:g-idle-add
            #:g-timeout-add-full
            #:g-source-remove
-           #:at-finalize)
+           #:at-finalize
+           #:with-g-error
+           #:with-catching-to-g-error
+           #:g-error-condition
+           #:g-error-condition-domain
+           #:g-error-condition-code
+           #:g-error-condition-message)
   (:documentation
    "Cl-gtk2-glib is wrapper for @a[http://library.gnome.org/devel/glib/]{GLib}."))