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



commit ceda38b69638180f60a2433eb8086cdd19dceba7
Author: Adrien Plazas <kekun plazas laposte net>
Date:   Thu Jan 30 07:46:52 2020 +0100

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

 retro-gtk/retro-option.c | 32 ++++++++++++--------------------
 1 file changed, 12 insertions(+), 20 deletions(-)
---
diff --git a/retro-gtk/retro-option.c b/retro-gtk/retro-option.c
index 488ff9b..051a172 100644
--- a/retro-gtk/retro-option.c
+++ b/retro-gtk/retro-option.c
@@ -2,6 +2,8 @@
 
 #include "retro-option-private.h"
 
+#include "retro-error-private.h"
+
 struct _RetroOption
 {
   GObject parent_instance;
@@ -180,39 +182,29 @@ retro_option_new (const gchar  *key,
 {
   RetroOption *self;
   gchar *description_separator;
-  gchar **values;
+  g_auto(GStrv) values = NULL;
 
   g_return_val_if_fail (key != NULL, NULL);
   g_return_val_if_fail (definition != NULL, NULL);
 
   description_separator = g_strstr_len (definition, -1, "; ");
-  if (G_UNLIKELY (description_separator == NULL)) {
-    g_set_error_literal (error,
-                         RETRO_OPTION_ERROR,
-                         RETRO_OPTION_ERROR_NO_DESCRIPTION_SEPARATOR,
-                         "Unexpected variable format: no description separator found.");
-
-    return NULL;
-  }
+  retro_throw_val_if_fail (error, description_separator != NULL, NULL,
+                           RETRO_OPTION_ERROR,
+                           RETRO_OPTION_ERROR_NO_DESCRIPTION_SEPARATOR,
+                           "Unexpected variable format: no description separator found.");
 
   values = g_strsplit (description_separator + 2, "|", 0);
-  if (G_UNLIKELY (*values == NULL)) {
-    g_strfreev (values);
-
-    g_set_error_literal (error,
-                         RETRO_OPTION_ERROR,
-                         RETRO_OPTION_ERROR_NO_VALUES,
-                         "Unexpected variable format: no values.");
-
-    return NULL;
-  }
+  retro_throw_val_if_fail (error, *values != NULL, NULL,
+                           RETRO_OPTION_ERROR,
+                           RETRO_OPTION_ERROR_NO_VALUES,
+                           "Unexpected variable format: no values.");
 
   self = g_object_new (RETRO_TYPE_OPTION, NULL);
 
   self->key = g_strdup (key);
   self->description = g_strndup (definition,
                                  description_separator - definition);
-  self->values = values;
+  self->values = g_steal_pointer (&values);
 
   return self;
 }


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