[libmanette] Use g_autoptr where possible



commit f92413784514e100b80a6b5e3f353afa27be7cdb
Author: Adrien Plazas <kekun plazas laposte net>
Date:   Sun Nov 22 13:07:46 2020 +0100

    Use g_autoptr where possible

 src/manette-device.c        | 24 +++++++-----------------
 src/manette-event-mapping.c | 23 +++++++++--------------
 2 files changed, 16 insertions(+), 31 deletions(-)
---
diff --git a/src/manette-device.c b/src/manette-device.c
index e5e18d2..672b162 100644
--- a/src/manette-device.c
+++ b/src/manette-device.c
@@ -513,8 +513,8 @@ ManetteDevice *
 manette_device_new (const gchar  *filename,
                     GError      **error)
 {
-  ManetteDevice *self = NULL;
-  GIOChannel *channel;
+  g_autoptr (ManetteDevice) self = NULL;
+  g_autoptr (GIOChannel) channel = NULL;
   gint buttons_number;
   gint axes_number;
   guint i;
@@ -531,7 +531,6 @@ manette_device_new (const gchar  *filename,
                  "Unable to open “%s”: %s",
                  filename,
                  strerror (errno));
-    g_object_unref (self);
 
     return NULL;
   }
@@ -544,7 +543,6 @@ manette_device_new (const gchar  *filename,
                  "Evdev is unable to open “%s”: %s",
                  filename,
                  strerror (errno));
-    g_object_unref (self);
 
     return NULL;
   }
@@ -555,7 +553,6 @@ manette_device_new (const gchar  *filename,
                  G_FILE_ERROR_NXIO,
                  "“%s” is not a game controller.",
                  filename);
-    g_object_unref (self);
 
     return NULL;
   }
@@ -600,9 +597,7 @@ manette_device_new (const gchar  *filename,
     }
   }
 
-  g_io_channel_unref (channel);
-
-  return self;
+  return g_steal_pointer (&self);
 }
 
 /**
@@ -763,17 +758,14 @@ gboolean
 manette_device_has_user_mapping (ManetteDevice *self)
 {
   const gchar *guid;
-  ManetteMappingManager *mapping_manager;
-  gboolean has_user_mapping;
+  g_autoptr (ManetteMappingManager) mapping_manager = NULL;
 
   g_return_val_if_fail (MANETTE_IS_DEVICE (self), FALSE);
 
   guid = manette_device_get_guid (self);
   mapping_manager = manette_mapping_manager_new ();
-  has_user_mapping = manette_mapping_manager_has_user_mapping (mapping_manager, guid);
-  g_object_unref (mapping_manager);
 
-  return has_user_mapping;
+  return manette_mapping_manager_has_user_mapping (mapping_manager, guid);
 }
 
 /**
@@ -789,7 +781,7 @@ manette_device_save_user_mapping (ManetteDevice *self,
 {
   const gchar *guid;
   const gchar *name;
-  ManetteMappingManager *mapping_manager;
+  g_autoptr (ManetteMappingManager) mapping_manager = NULL;
 
   g_return_if_fail (MANETTE_IS_DEVICE (self));
   g_return_if_fail (mapping_string != NULL);
@@ -801,7 +793,6 @@ manette_device_save_user_mapping (ManetteDevice *self,
                                         guid,
                                         name,
                                         mapping_string);
-  g_object_unref (mapping_manager);
 }
 
 /**
@@ -814,14 +805,13 @@ void
 manette_device_remove_user_mapping (ManetteDevice *self)
 {
   const gchar *guid;
-  ManetteMappingManager *mapping_manager;
+  g_autoptr (ManetteMappingManager) mapping_manager = NULL;
 
   g_return_if_fail (MANETTE_IS_DEVICE (self));
 
   guid = manette_device_get_guid (self);
   mapping_manager = manette_mapping_manager_new ();
   manette_mapping_manager_delete_mapping (mapping_manager, guid);
-  g_object_unref (mapping_manager);
 }
 
 /**
diff --git a/src/manette-event-mapping.c b/src/manette-event-mapping.c
index 312aefb..c9dfbfb 100644
--- a/src/manette-event-mapping.c
+++ b/src/manette-event-mapping.c
@@ -28,7 +28,6 @@ map_button_event (ManetteMapping     *mapping,
   const ManetteMappingBinding * const *bindings;
   const ManetteMappingBinding * binding;
   GSList *mapped_events = NULL;
-  ManetteEvent *mapped_event;
   gboolean pressed;
 
   bindings = manette_mapping_get_bindings (mapping,
@@ -38,9 +37,9 @@ map_button_event (ManetteMapping     *mapping,
     return NULL;
 
   for (; *bindings != NULL; bindings++) {
-    binding = *bindings;
+    g_autoptr (ManetteEvent) mapped_event = manette_event_copy ((ManetteEvent *) event);
 
-    mapped_event = manette_event_copy ((ManetteEvent *) event);
+    binding = *bindings;
 
     pressed = event->type == MANETTE_EVENT_BUTTON_PRESS;
 
@@ -72,12 +71,10 @@ map_button_event (ManetteMapping     *mapping,
 
       break;
     default:
-      manette_event_free (mapped_event);
-
       continue;
     }
 
-    mapped_events = g_slist_append (mapped_events, mapped_event);
+    mapped_events = g_slist_append (mapped_events, g_steal_pointer (&mapped_event));
   }
 
   return mapped_events;
@@ -90,7 +87,6 @@ map_absolute_event (ManetteMapping       *mapping,
   const ManetteMappingBinding * const *bindings;
   const ManetteMappingBinding * binding;
   GSList *mapped_events = NULL;
-  ManetteEvent *mapped_event;
   gdouble absolute_value;
   gboolean pressed;
 
@@ -101,6 +97,8 @@ map_absolute_event (ManetteMapping       *mapping,
     return NULL;
 
   for (; *bindings != NULL; bindings++) {
+    g_autoptr (ManetteEvent) mapped_event = NULL;
+
     binding = *bindings;
 
     if (binding->source.range == MANETTE_MAPPING_RANGE_NEGATIVE &&
@@ -151,12 +149,10 @@ map_absolute_event (ManetteMapping       *mapping,
 
       break;
     default:
-      manette_event_free (mapped_event);
-
       continue;
     }
 
-    mapped_events = g_slist_append (mapped_events, mapped_event);
+    mapped_events = g_slist_append (mapped_events, g_steal_pointer (&mapped_event));
   }
 
   return mapped_events;
@@ -169,7 +165,6 @@ map_hat_event (ManetteMapping  *mapping,
   const ManetteMappingBinding * const *bindings;
   const ManetteMappingBinding * binding;
   GSList *mapped_events = NULL;
-  ManetteEvent *mapped_event;
   gboolean pressed;
 
   bindings = manette_mapping_get_bindings (mapping,
@@ -179,6 +174,8 @@ map_hat_event (ManetteMapping  *mapping,
     return NULL;
 
   for (; *bindings != NULL; bindings++) {
+    g_autoptr (ManetteEvent) mapped_event = NULL;
+
     binding = *bindings;
 
     if (binding->source.range == MANETTE_MAPPING_RANGE_NEGATIVE &&
@@ -207,12 +204,10 @@ map_hat_event (ManetteMapping  *mapping,
 
       break;
     default:
-      manette_event_free (mapped_event);
-
       continue;
     }
 
-    mapped_events = g_slist_append (mapped_events, mapped_event);
+    mapped_events = g_slist_append (mapped_events, g_steal_pointer (&mapped_event));
   }
 
   return mapped_events;


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