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



commit 0d854fb7485639bf28f37dd54e3e2e7ae4e6dbe1
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-runner/retro-core.c | 246 +++++++++++++---------------------------------
 1 file changed, 67 insertions(+), 179 deletions(-)
---
diff --git a/retro-runner/retro-core.c b/retro-runner/retro-core.c
index f695e88..e2ac0f5 100644
--- a/retro-runner/retro-core.c
+++ b/retro-runner/retro-core.c
@@ -4,6 +4,7 @@
 
 #include <gio/gio.h>
 #include <string.h>
+#include "retro-error-private.h"
 #include "retro-input-private.h"
 #include "retro-main-loop-source-private.h"
 #include "retro-memfd-private.h"
@@ -839,82 +840,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_propagate_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_propagate_if_error (error, tmp_error);
   }
 
-  if (G_UNLIKELY (tmp_error != NULL)) {
-    g_propagate_error (error, tmp_error);
-
-    return;
-  }
+  retro_propagate_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);
-
-    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_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);
+    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);
 
-      return;
-    }
+    retro_propagate_if_error (error, tmp_error);
 
-    retro_game_info_free (game_info);
-    g_free (path);
-    g_object_unref (file);
+    retro_core_replace_disk_image_index (self, index, game_info, &tmp_error);
+    retro_propagate_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_propagate_if_error (error, tmp_error);
 }
 
 static gboolean
@@ -969,12 +927,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);
@@ -989,45 +947,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);
-
-      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);
+  game_info = fullpath ?
+    retro_game_info_new (path) :
+    retro_game_info_new_with_data (path, &tmp_error);
+  retro_propagate_if_error (error, tmp_error);
 
+  if (!retro_core_load_game (self, game_info))
     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);
+
+  if (self->disk_control_callback == NULL)
+    return;
+
+  retro_core_load_discs (self, &tmp_error);
+  retro_propagate_if_error (error, tmp_error);
 }
 
 void retro_core_set_environment_interface (RetroCore *self);
@@ -1343,7 +1275,7 @@ retro_core_boot (RetroCore  *self,
                  GError    **error)
 {
   RetroInit init;
-  GError *tmp_error = NULL;
+  g_autoptr (RetroError) tmp_error = NULL;
 
   g_return_if_fail (RETRO_IS_CORE (self));
 
@@ -1355,11 +1287,7 @@ retro_core_boot (RetroCore  *self,
   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_propagate_if_error (error, tmp_error);
 }
 
 /**
@@ -1382,7 +1310,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);
 }
 
 /**
@@ -1401,7 +1329,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);
@@ -1412,25 +1340,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_propagate_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_propagate_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_propagate_if_error (error, tmp_error);
 }
 
 void
@@ -1591,7 +1507,7 @@ retro_core_iteration (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;
@@ -1645,8 +1561,6 @@ retro_core_iteration (RetroCore *self)
   if (!success) {
     g_critical ("Couldn't run ahead: serialization unexpectedly failed.");
 
-    g_free (data);
-
     return;
   }
 
@@ -1660,8 +1574,6 @@ retro_core_iteration (RetroCore *self)
                 G_GSIZE_FORMAT", expected %"G_GSIZE_FORMAT" or less.",
                 new_size, size);
 
-    g_free (data);
-
     return;
   }
 
@@ -1671,12 +1583,8 @@ retro_core_iteration (RetroCore *self)
   if (!success) {
     g_critical ("Couldn't run ahead: deserialization unexpectedly failed.");
 
-    g_free (data);
-
     return;
   }
-
-  g_free (data);
 }
 
 /**
@@ -1727,35 +1635,26 @@ retro_core_save_state (RetroCore    *self,
   serialize_size = retro_module_get_serialize_size (self->module);
   size = serialize_size ();
 
-  if (size <= 0) {
-    g_set_error (error,
-                 RETRO_CORE_ERROR,
-                 RETRO_CORE_ERROR_SERIALIZATION_NOT_SUPPORTED,
-                 "Couldn't serialize the internal state: serialization not supported.");
-
-    return;
-  }
+  retro_throw_if_fail (error, size > 0,
+                       RETRO_CORE_ERROR,
+                       RETRO_CORE_ERROR_SERIALIZATION_NOT_SUPPORTED,
+                       "Couldn't serialize the internal state: serialization not supported.");
 
   serialize = retro_module_get_serialize (self->module);
   data = g_new0 (guint8, size);
 
   success = serialize (data, size);
 
-  if (!success) {
-    g_set_error (error,
-                 RETRO_CORE_ERROR,
-                 RETRO_CORE_ERROR_COULDNT_SERIALIZE,
-                 "Couldn't serialize the internal state: serialization failed.");
-
-    return;
-  }
+  retro_throw_if_fail (error, success,
+                       RETRO_CORE_ERROR,
+                       RETRO_CORE_ERROR_COULDNT_SERIALIZE,
+                       "Couldn't serialize the internal state: serialization failed.");
 
   g_file_set_contents (filename, (gchar *) data, size, &tmp_error);
-  if (G_UNLIKELY (tmp_error != NULL))
-    g_set_error (error,
-                 RETRO_CORE_ERROR,
-                 RETRO_CORE_ERROR_COULDNT_ACCESS_FILE,
-                 "Couldn't serialize the internal state: %s", tmp_error->message);
+  retro_throw_if_fail (error, tmp_error == NULL,
+                       RETRO_CORE_ERROR,
+                       RETRO_CORE_ERROR_COULDNT_ACCESS_FILE,
+                       "Couldn't serialize the internal state: %s", tmp_error->message);
 }
 
 /**
@@ -1782,26 +1681,18 @@ retro_core_load_state (RetroCore    *self,
   g_return_if_fail (filename != NULL);
 
   g_file_get_contents (filename, &data, &data_size, &tmp_error);
-  if (G_UNLIKELY (tmp_error != NULL)) {
-    g_set_error (error,
-                 RETRO_CORE_ERROR,
-                 RETRO_CORE_ERROR_COULDNT_ACCESS_FILE,
-                 "Couldn't deserialize the internal state: %s", tmp_error->message);
-
-    return;
-  }
+  retro_throw_if_fail (error, tmp_error == NULL,
+                       RETRO_CORE_ERROR,
+                       RETRO_CORE_ERROR_COULDNT_ACCESS_FILE,
+                       "Couldn't deserialize the internal state: %s", tmp_error->message);
 
   serialize_size = retro_module_get_serialize_size (self->module);
   expected_size = serialize_size ();
 
-  if (expected_size == 0) {
-    g_set_error (error,
-                 RETRO_CORE_ERROR,
-                 RETRO_CORE_ERROR_SERIALIZATION_NOT_SUPPORTED,
-                 "Couldn't deserialize the internal state: serialization not supported.");
-
-    return;
-  }
+  retro_throw_if_fail (error, expected_size != 0,
+                       RETRO_CORE_ERROR,
+                       RETRO_CORE_ERROR_SERIALIZATION_NOT_SUPPORTED,
+                       "Couldn't deserialize the internal state: serialization not supported.");
 
   if (data_size != expected_size)
     g_critical ("%s expects %"G_GSIZE_FORMAT" bytes for its internal state, but %"
@@ -1813,12 +1704,10 @@ retro_core_load_state (RetroCore    *self,
   unserialize = retro_module_get_unserialize (self->module);
   success = unserialize ((guint8 *) data, data_size);
 
-  if (!success) {
-    g_set_error (error,
-                 RETRO_CORE_ERROR,
-                 RETRO_CORE_ERROR_COULDNT_DESERIALIZE,
-                 "Couldn't deserialize the internal state: deserialization failed.");
-  }
+  retro_throw_if_fail (error, success,
+                       RETRO_CORE_ERROR,
+                       RETRO_CORE_ERROR_COULDNT_DESERIALIZE,
+                       "Couldn't deserialize the internal state: deserialization failed.");
 }
 
 /**
@@ -1873,11 +1762,10 @@ retro_core_save_memory (RetroCore        *self,
   size = get_mem_size (memory_type);
 
   g_file_set_contents (filename, data, size, &tmp_error);
-  if (G_UNLIKELY (tmp_error != NULL))
-    g_set_error (error,
-                 RETRO_CORE_ERROR,
-                 RETRO_CORE_ERROR_COULDNT_ACCESS_FILE,
-                 "Couldn't save the memory state: %s", tmp_error->message);
+  retro_throw_if_fail (error, tmp_error == NULL,
+                       RETRO_CORE_ERROR,
+                       RETRO_CORE_ERROR_COULDNT_ACCESS_FILE,
+                       "Couldn't save the memory state: %s", tmp_error->message);
 }
 
 /**
@@ -1901,7 +1789,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]