From c980546e8bca6f1c0c340634a4c3640e14fd1228 Mon Sep 17 00:00:00 2001
From: Cyrille Bagard <nocbos@gmail.com>
Date: Fri, 7 Dec 2018 21:40:32 +0100
Subject: Created signals for format symbol handling.

---
 src/format/format-int.h |  5 +++++
 src/format/format.c     | 35 +++++++++++++++++++++++++++++------
 2 files changed, 34 insertions(+), 6 deletions(-)

diff --git a/src/format/format-int.h b/src/format/format-int.h
index 6a3be81..eb1d0d2 100644
--- a/src/format/format-int.h
+++ b/src/format/format-int.h
@@ -119,6 +119,11 @@ struct _GBinFormatClass
 
     format_complete_analysis_fc complete;   /* Terminaison d'analyse       */
 
+    /* Signaux */
+
+    void (* symbol_added) (GBinFormat *, GBinSymbol *);
+    void (* symbol_removed) (GBinFormat *, GBinSymbol *);
+
 };
 
 
diff --git a/src/format/format.c b/src/format/format.c
index 05e4597..b43f78f 100644
--- a/src/format/format.c
+++ b/src/format/format.c
@@ -93,6 +93,22 @@ static void g_binary_format_class_init(GBinFormatClass *klass)
     object->dispose = (GObjectFinalizeFunc/* ! */)g_binary_format_dispose;
     object->finalize = (GObjectFinalizeFunc)g_binary_format_finalize;
 
+    g_signal_new("symbol-added",
+                 G_TYPE_BIN_FORMAT,
+                 G_SIGNAL_RUN_LAST,
+                 G_STRUCT_OFFSET(GBinFormatClass, symbol_added),
+                 NULL, NULL,
+                 g_cclosure_marshal_VOID__OBJECT,
+                 G_TYPE_NONE, 1, G_TYPE_OBJECT);
+
+    g_signal_new("symbol-removed",
+                 G_TYPE_BIN_FORMAT,
+                 G_SIGNAL_RUN_LAST,
+                 G_STRUCT_OFFSET(GBinFormatClass, symbol_removed),
+                 NULL, NULL,
+                 g_cclosure_marshal_VOID__OBJECT,
+                 G_TYPE_NONE, 1, G_TYPE_OBJECT);
+
 }
 
 
@@ -835,6 +851,9 @@ bool g_binary_format_add_symbol(GBinFormat *format, GBinSymbol *symbol)
 
     g_binary_format_lock_unlock_symbols_wr(format, false);
 
+    if (result)
+        g_signal_emit_by_name(format, "symbol-added", symbol);
+
     return result;
 
 }
@@ -855,20 +874,17 @@ bool g_binary_format_add_symbol(GBinFormat *format, GBinSymbol *symbol)
 
 static void _g_binary_format_remove_symbol(GBinFormat *format, size_t index)
 {
-    /**
-     * TODO : envoyer un signal pour avertir les opérandes concernées.
-     */
-
     assert(g_atomic_int_get(&format->sym_locked) == 1);
 
     assert(index < format->sym_count);
 
+    g_object_unref(G_OBJECT(format->symbols[index]));
+
     if ((index + 1) < format->sym_count)
         memmove(&format->symbols[index], &format->symbols[index + 1],
                 (format->sym_count - index - 1) * sizeof(GBinSymbol *));
 
-    format->symbols = (GBinSymbol **)realloc(format->symbols,
-                                             --format->sym_count * sizeof(GBinSymbol *));
+    format->symbols = realloc(format->symbols, --format->sym_count * sizeof(GBinSymbol *));
 
 }
 
@@ -891,6 +907,8 @@ void g_binary_format_remove_symbol(GBinFormat *format, GBinSymbol *symbol)
     bool found;                             /* Jeton de présence           */
     size_t index;                           /* Indice du point de retrait  */
 
+    g_object_ref(G_OBJECT(symbol));
+
     g_binary_format_lock_unlock_symbols_wr(format, true);
 
     found = bsearch_index(&symbol, format->symbols, format->sym_count,
@@ -901,6 +919,11 @@ void g_binary_format_remove_symbol(GBinFormat *format, GBinSymbol *symbol)
 
     g_binary_format_lock_unlock_symbols_wr(format, false);
 
+    if (found)
+        g_signal_emit_by_name(format, "symbol-removed", symbol);
+
+    g_object_unref(G_OBJECT(symbol));
+
 }
 
 
-- 
cgit v0.11.2-87-g4458