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



commit 51862bd8f51a97b4385f473bb58f726e87ba1a86
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 | 24 ++++++++++--------------
 1 file changed, 10 insertions(+), 14 deletions(-)
---
diff --git a/retro-runner/retro-core.c b/retro-runner/retro-core.c
index c96ba19..7c1cfa0 100644
--- a/retro-runner/retro-core.c
+++ b/retro-runner/retro-core.c
@@ -1659,6 +1659,13 @@ retro_core_reset (RetroCore *self)
   retro_core_pop_cb_data ();
 }
 
+static inline void
+emit_iterated (RetroCore **self)
+{
+  if (*self)
+    g_signal_emit (*self, signals[SIG_ITERATED_SIGNAL], 0);
+}
+
 /**
  * retro_core_iteration:
  * @self: a #RetroCore
@@ -1676,9 +1683,12 @@ 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) {
@@ -1687,8 +1697,6 @@ retro_core_iteration (RetroCore *self)
     run ();
     retro_core_pop_cb_data ();
 
-    g_signal_emit (self, signals[SIG_ITERATED_SIGNAL], 0);
-
     return;
   }
 
@@ -1706,8 +1714,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;
   }
 
@@ -1726,8 +1732,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;
   }
 
@@ -1744,8 +1748,6 @@ retro_core_iteration (RetroCore *self)
 
     g_free (data);
 
-    g_signal_emit (self, signals[SIG_ITERATED_SIGNAL], 0);
-
     return;
   }
 
@@ -1763,8 +1765,6 @@ retro_core_iteration (RetroCore *self)
 
     g_free (data);
 
-    g_signal_emit (self, signals[SIG_ITERATED_SIGNAL], 0);
-
     return;
   }
 
@@ -1778,14 +1778,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]