[retro-gtk/auto-goodness: 1/3] Add error handling helpers



commit 9065ee67d8fa1f1dae16da41352a1bd86a82d961
Author: Adrien Plazas <kekun plazas laposte net>
Date:   Mon Jan 27 10:19:26 2020 +0100

    Add error handling helpers
    
    Add RetroError, retro_error_ensure_free(), retro_throw_if_fail() and
    retro_throw_val_if_fail() to streamline error handling.

 retro-gtk/meson.build           |  1 +
 retro-gtk/retro-error-private.h | 65 +++++++++++++++++++++++++++++++++++++++++
 retro-gtk/retro-error.c         | 13 +++++++++
 3 files changed, 79 insertions(+)
---
diff --git a/retro-gtk/meson.build b/retro-gtk/meson.build
index 28229f8..0923d33 100644
--- a/retro-gtk/meson.build
+++ b/retro-gtk/meson.build
@@ -17,6 +17,7 @@ retro_gtk_sources = [
   'retro-core-view.c',
   'retro-core-view-controller.c',
   'retro-environment.c',
+  'retro-error.c',
   'retro-game-info.c',
   'retro-gl-display.c',
   'retro-glsl-filter.c',
diff --git a/retro-gtk/retro-error-private.h b/retro-gtk/retro-error-private.h
new file mode 100644
index 0000000..5c2d04e
--- /dev/null
+++ b/retro-gtk/retro-error-private.h
@@ -0,0 +1,65 @@
+// This file is part of retro-gtk. License: GPL-3.0+.
+
+#pragma once
+
+#if !defined(__RETRO_GTK_INSIDE__) && !defined(RETRO_GTK_COMPILATION)
+# error "Only <retro-gtk.h> can be included directly."
+#endif
+
+#include <glib.h>
+
+G_BEGIN_DECLS
+
+typedef GError RetroError;
+
+void retro_error_ensure_free (RetroError *error);
+
+/**
+ * retro_throw_if_error:
+ * @dest: (out callee-allocates) (optional) (nullable): error return location
+ * @src: (transfer full): error to checkmove into the return location
+ *
+ * Verifies that the source error @src has not been set, which imlplies it is
+ * %NULL.
+ * If the function returns a value, use retro_throw_val_if_error() instead.
+ *
+ * If @src is non-%NULL and @dest is %NULL, free @src; otherwise, moves @src
+ * into *@dest and return.
+ * The error variable @dest points to must be %NULL.
+ */
+#define retro_throw_if_error(dest, src) \
+  G_STMT_START { \
+    if (G_UNLIKELY (src != NULL)) { \
+      g_propagate_error (dest, src); \
+      src = NULL; \
+      return; \
+    } \
+  } G_STMT_END
+
+/**
+ * retro_throw_val_if_error:
+ * @dest: (out callee-allocates) (optional) (nullable): error return location
+ * @src: (transfer full): error to checkmove into the return location
+ * @val: the value to return from the current function
+ *       if the error is propagated
+ *
+ * Verifies that the source error @src has not been set, which imlplies it is
+ * %NULL.
+ * If the function does not return a value, use retro_throw_if_error() instead.
+ *
+ * If @src is non-%NULL and @dest is %NULL, free @src; otherwise, moves @src
+ * into *@dest and return @val.
+ * The error variable @dest points to must be %NULL.
+ */
+#define retro_throw_val_if_error(dest, src, val) \
+  G_STMT_START { \
+    if (G_UNLIKELY (src != NULL)) { \
+      g_propagate_error (dest, src); \
+      src = NULL; \
+      return (val); \
+    } \
+  } G_STMT_END
+
+G_DEFINE_AUTOPTR_CLEANUP_FUNC (RetroError, retro_error_ensure_free)
+
+G_END_DECLS
diff --git a/retro-gtk/retro-error.c b/retro-gtk/retro-error.c
new file mode 100644
index 0000000..9aa5821
--- /dev/null
+++ b/retro-gtk/retro-error.c
@@ -0,0 +1,13 @@
+// This file is part of retro-gtk. License: GPL-3.0+.
+
+#include "retro-error-private.h"
+
+void
+retro_error_ensure_free (RetroError *error)
+{
+  if (error == NULL)
+    return;
+
+  g_critical ("Unhandled error: %s", error->message);
+  g_error_free (error);
+}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]