[retro-gtk/auto-goodness: 2/3] core: Use auto cleanups and throws where possible



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

    core: Use auto cleanups and throws where possible
    
    This makes the code simpler and safer.

 retro-gtk/retro-core.c | 169 +++++++++++--------------------------------------
 1 file changed, 38 insertions(+), 131 deletions(-)
---
diff --git a/retro-gtk/retro-core.c b/retro-gtk/retro-core.c
index e612d91..edd074f 100644
--- a/retro-gtk/retro-core.c
+++ b/retro-gtk/retro-core.c
@@ -4,6 +4,7 @@
 
 #include <string.h>
 #include "retro-controller-iterator-private.h"
+#include "retro-error-private.h"
 #include "retro-keyboard-private.h"
 #include "retro-option-iterator-private.h"
 #include "retro-pixdata.h"
@@ -903,82 +904,39 @@ retro_core_load_discs (RetroCore  *self,
 {
   guint length;
   gboolean fullpath;
-  GFile *file;
-  gchar *path;
   guint index;
-  RetroGameInfo *game_info = NULL;
-  GError *tmp_error = NULL;
+  g_autoptr (RetroError) tmp_error = NULL;
 
   g_return_if_fail (RETRO_IS_CORE (self));
 
   retro_core_set_disk_ejected (self, TRUE, &tmp_error);
-  if (G_UNLIKELY (tmp_error != NULL)) {
-    g_propagate_error (error, tmp_error);
-
-    return;
-  }
+  retro_throw_if_error (error, tmp_error);
 
   length = g_strv_length (self->media_uris);
   while (retro_core_get_disk_images_number (self, &tmp_error) < length &&
          (tmp_error != NULL)) {
     retro_core_add_disk_image_index (self, &tmp_error);
-    if (G_UNLIKELY (tmp_error != NULL)) {
-      g_propagate_error (error, tmp_error);
-
-      return;
-    }
+    retro_throw_if_error (error, tmp_error);
   }
 
-  if (G_UNLIKELY (tmp_error != NULL)) {
-    g_propagate_error (error, tmp_error);
-
-    return;
-  }
+  retro_throw_if_error (error, tmp_error);
 
   fullpath = retro_core_get_needs_full_path (self);
   for (index = 0; index < length; index++) {
-    file = g_file_new_for_uri (self->media_uris[index]);
-    path = g_file_get_path (file);
+    g_autoptr (GFile) file = g_file_new_for_uri (self->media_uris[index]);
+    g_autofree gchar *path = g_file_get_path (file);
+    g_autoptr (RetroGameInfo) game_info = fullpath ?
+      retro_game_info_new (path) :
+      retro_game_info_new_with_data (path, &tmp_error);
 
-    if (fullpath) {
-      game_info = retro_game_info_new (path);
-    }
-    else {
-      game_info = retro_game_info_new_with_data (path, &tmp_error);
-      if (G_UNLIKELY (tmp_error != NULL)) {
-        g_propagate_error (error, tmp_error);
-
-        if (game_info != NULL)
-          retro_game_info_free (game_info);
-        g_free (path);
-        g_object_unref (file);
-
-        return;
-      }
-    }
+    retro_throw_if_error (error, tmp_error);
 
     retro_core_replace_disk_image_index (self, index, game_info, &tmp_error);
-    if (G_UNLIKELY (tmp_error != NULL)) {
-      g_propagate_error (error, tmp_error);
-
-      retro_game_info_free (game_info);
-      g_free (path);
-      g_object_unref (file);
-
-      return;
-    }
-
-    retro_game_info_free (game_info);
-    g_free (path);
-    g_object_unref (file);
+    retro_throw_if_error (error, tmp_error);
   }
 
   retro_core_set_disk_ejected (self, FALSE, &tmp_error);
-  if (G_UNLIKELY (tmp_error != NULL)) {
-    g_propagate_error (error, tmp_error);
-
-    return;
-  }
+  retro_throw_if_error (error, tmp_error);
 }
 
 static gboolean
@@ -1039,12 +997,12 @@ retro_core_load_medias (RetroCore *self,
                         GError** error)
 {
   guint length;
-  gchar *uri;
-  GFile *file;
-  gchar *path;
+  g_autofree gchar *uri = NULL;
+  g_autoptr (GFile) file = NULL;
+  g_autofree gchar *path = NULL;
   gboolean fullpath;
-  RetroGameInfo *game_info = NULL;
-  GError *tmp_error = NULL;
+  g_autoptr (RetroGameInfo) game_info = NULL;
+  g_autoptr (RetroError) tmp_error = NULL;
 
   g_return_if_fail (RETRO_IS_CORE (self));
   length = self->media_uris == NULL ? 0 : g_strv_length (self->media_uris);
@@ -1059,45 +1017,19 @@ retro_core_load_medias (RetroCore *self,
   file = g_file_new_for_uri (uri);
   path = g_file_get_path (file);
   fullpath = retro_core_get_needs_full_path (self);
-  if (fullpath) {
-    game_info = retro_game_info_new (path);
-  }
-  else {
-    game_info = retro_game_info_new_with_data (path, &tmp_error);
-    if (G_UNLIKELY (tmp_error != NULL)) {
-      g_propagate_error (error, tmp_error);
-      retro_game_info_free (game_info);
-      g_free (path);
-      g_object_unref (file);
-      g_free (uri);
+  game_info = fullpath ?
+    retro_game_info_new (path) :
+    retro_game_info_new_with_data (path, &tmp_error);
+  retro_throw_if_error (error, tmp_error);
 
-      return;
-    }
-  }
-  if (!retro_core_load_game (self, game_info)) {
-    retro_game_info_free (game_info);
-    g_free (path);
-    g_object_unref (file);
-    g_free (uri);
+  if (!retro_core_load_game (self, game_info))
+    return;
 
+  if (self->disk_control_callback == NULL)
     return;
-  }
-  if (self->disk_control_callback != NULL) {
-    retro_core_load_discs (self, &tmp_error);
-    if (G_UNLIKELY (tmp_error != NULL)) {
-      g_propagate_error (error, tmp_error);
-      retro_game_info_free (game_info);
-      g_free (path);
-      g_object_unref (file);
-      g_free (uri);
 
-      return;
-    }
-  }
-  retro_game_info_free (game_info);
-  g_free (path);
-  g_object_unref (file);
-  g_free (uri);
+  retro_core_load_discs (self, &tmp_error);
+  retro_throw_if_error (error, tmp_error);
 }
 
 void retro_core_set_environment_interface (RetroCore *self);
@@ -1129,7 +1061,7 @@ retro_core_insert_variable (RetroCore           *self,
 {
   RetroOption *option;
   const gchar *key;
-  GError *tmp_error = NULL;
+  g_autoptr (RetroError) tmp_error = NULL;
 
   g_return_if_fail (RETRO_IS_CORE (self));
   g_return_if_fail (variable != NULL);
@@ -1433,11 +1365,11 @@ retro_core_boot (RetroCore  *self,
                  GError    **error)
 {
   RetroInit init;
-  RetroControllerIterator *controller_iterator;
+  g_autoptr (RetroControllerIterator) controller_iterator = NULL;
   guint *port;
   RetroController *controller;
   RetroControllerType controller_type;
-  GError *tmp_error = NULL;
+  g_autoptr (RetroError) tmp_error = NULL;
 
   g_return_if_fail (RETRO_IS_CORE (self));
 
@@ -1455,16 +1387,11 @@ retro_core_boot (RetroCore  *self,
     controller_type = retro_controller_get_controller_type (controller);
     retro_core_set_controller_port_device (self, *port, controller_type);
   }
-  g_object_unref (controller_iterator);
 
   retro_core_set_is_initiated (self, TRUE);
 
   retro_core_load_medias (self, &tmp_error);
-  if (G_UNLIKELY (tmp_error != NULL)) {
-    g_propagate_error (error, tmp_error);
-
-    return;
-  }
+  retro_throw_if_error (error, tmp_error);
 }
 
 /**
@@ -1487,7 +1414,7 @@ retro_core_set_medias (RetroCore           *self,
   if (self->media_uris != NULL)
     g_strfreev (self->media_uris);
 
-  self->media_uris = g_strdupv ((gchar **) uris);
+  self->media_uris = g_strdupv ((GStrv) uris);
 }
 
 /**
@@ -1506,7 +1433,7 @@ retro_core_set_current_media (RetroCore  *self,
                               GError    **error)
 {
   guint length;
-  GError *tmp_error = NULL;
+  g_autoptr (RetroError) tmp_error = NULL;
 
   g_return_if_fail (RETRO_IS_CORE (self));
   length = g_strv_length (self->media_uris);
@@ -1517,25 +1444,13 @@ retro_core_set_current_media (RetroCore  *self,
     return;
 
   retro_core_set_disk_ejected (self, TRUE, &tmp_error);
-  if (tmp_error != NULL) {
-    g_propagate_error (error, tmp_error);
-
-    return;
-  }
+  retro_throw_if_error (error, tmp_error);
 
   retro_core_set_disk_image_index (self, media_index, &tmp_error);
-  if (tmp_error != NULL) {
-    g_propagate_error (error, tmp_error);
-
-    return;
-  }
+  retro_throw_if_error (error, tmp_error);
 
   retro_core_set_disk_ejected (self, FALSE, &tmp_error);
-  if (tmp_error != NULL) {
-    g_propagate_error (error, tmp_error);
-
-    return;
-  }
+  retro_throw_if_error (error, tmp_error);
 }
 
 // FIXME Merge this into retro_core_set_controller().
@@ -1645,7 +1560,7 @@ retro_core_run (RetroCore *self)
   RetroSerializeSize serialize_size = NULL;
   RetroSerialize serialize = NULL;
   RetroUnserialize unserialize = NULL;
-  guint8 *data;
+  g_autofree guint8 *data = NULL;
   gsize size;
   gsize new_size;
   gboolean success;
@@ -1709,8 +1624,6 @@ retro_core_run (RetroCore *self)
   if (!success) {
     g_critical ("Couldn't run ahead: serialization unexpectedly failed.");
 
-    g_free (data);
-
     return;
   }
 
@@ -1726,8 +1639,6 @@ retro_core_run (RetroCore *self)
                 G_GSIZE_FORMAT", expected %"G_GSIZE_FORMAT" or less.",
                 new_size, size);
 
-    g_free (data);
-
     return;
   }
 
@@ -1739,12 +1650,8 @@ retro_core_run (RetroCore *self)
   if (!success) {
     g_critical ("Couldn't run ahead: deserialization unexpectedly failed.");
 
-    g_free (data);
-
     return;
   }
-
-  g_free (data);
 }
 
 /**
@@ -1994,7 +1901,7 @@ retro_core_load_memory (RetroCore        *self,
   gsize memory_region_size;
   g_autofree gchar *data = NULL;
   gsize data_size;
-  GError *tmp_error = NULL;
+  g_autoptr (RetroError) tmp_error = NULL;
 
   g_return_if_fail (RETRO_IS_CORE (self));
   g_return_if_fail (filename != NULL);


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