[retro-gtk/emit-iterated-cleanup: 10/10] core: Auto emit 'iterated' on cleanup



commit b923180c878db2716d8f31a361e17711a5159c95
Author: Adrien Plazas <kekun plazas laposte net>
Date:   Thu Jan 30 20:31:04 2020 +0100

    core: Auto emit 'iterated' on cleanup
    
    Automatically emit the 'iterated' signal at the end of
    retro_core_iteration() thanks to a cleanup attribute. This makes the
    code simpler and safer by ensuring we emit it when needed and no more.

 retro-runner/retro-core.c | 23 +++++++++--------------
 1 file changed, 9 insertions(+), 14 deletions(-)
---
diff --git a/retro-runner/retro-core.c b/retro-runner/retro-core.c
index 5affa8d..f695e88 100644
--- a/retro-runner/retro-core.c
+++ b/retro-runner/retro-core.c
@@ -1571,6 +1571,13 @@ retro_core_reset (RetroCore *self)
   reset ();
 }
 
+static inline void
+emit_iterated (RetroCore **self)
+{
+  if (*self)
+    g_signal_emit (*self, signals[SIG_ITERATED_SIGNAL], 0);
+}
+
 /**
  * retro_core_iteration:
  * @self: a #RetroCore
@@ -1588,17 +1595,17 @@ retro_core_iteration (RetroCore *self)
   gsize size;
   gsize new_size;
   gboolean success;
+  RetroCore *iterated __attribute__((cleanup(emit_iterated))) = NULL;
 
   g_return_if_fail (RETRO_IS_CORE (self));
 
+  iterated = self;
   run = retro_module_get_run (self->module);
 
   if (self->runahead == 0) {
     self->run_remaining = 0;
     run ();
 
-    g_signal_emit (self, signals[SIG_ITERATED_SIGNAL], 0);
-
     return;
   }
 
@@ -1611,8 +1618,6 @@ retro_core_iteration (RetroCore *self)
 
     g_critical ("Couldn't run ahead: serialization not supported.");
 
-    g_signal_emit (self, signals[SIG_ITERATED_SIGNAL], 0);
-
     return;
   }
 
@@ -1628,8 +1633,6 @@ retro_core_iteration (RetroCore *self)
                 G_GSIZE_FORMAT", expected %"G_GSIZE_FORMAT" or less.",
                 new_size, size);
 
-    g_signal_emit (self, signals[SIG_ITERATED_SIGNAL], 0);
-
     return;
   }
 
@@ -1644,8 +1647,6 @@ retro_core_iteration (RetroCore *self)
 
     g_free (data);
 
-    g_signal_emit (self, signals[SIG_ITERATED_SIGNAL], 0);
-
     return;
   }
 
@@ -1661,8 +1662,6 @@ retro_core_iteration (RetroCore *self)
 
     g_free (data);
 
-    g_signal_emit (self, signals[SIG_ITERATED_SIGNAL], 0);
-
     return;
   }
 
@@ -1674,14 +1673,10 @@ retro_core_iteration (RetroCore *self)
 
     g_free (data);
 
-    g_signal_emit (self, signals[SIG_ITERATED_SIGNAL], 0);
-
     return;
   }
 
   g_free (data);
-
-  g_signal_emit (self, signals[SIG_ITERATED_SIGNAL], 0);
 }
 
 /**


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