gmain: add g_clear_handle_id API

It's a very common pattern to see code that looks like this in
dispose() or finalize() implementations:

if (priv->source_id > 0)
  {
    g_source_remove (priv->source_id);
    priv->source_id = 0;
  }

This API allows to accomplish the same goal with a single line:

g_clear_handle_id (&priv->source_id, (GClearHandleFunc) g_source_remove);

Thanks to Emmanuele Bassi <ebassi@gnome.org> for making the patch
generic.

https://bugzilla.gnome.org/show_bug.cgi?id=788489
This commit is contained in:
Cosimo Cecchi
2017-09-30 21:09:37 -07:00
committed by Cosimo Cecchi
parent 44d6052584
commit 5ebd8f6e88
4 changed files with 94 additions and 0 deletions

View File

@@ -478,6 +478,31 @@ test_desktop_special_dir (void)
g_assert (dir2 != NULL);
}
static gboolean
source_test (gpointer data)
{
g_assert_not_reached ();
return G_SOURCE_REMOVE;
}
static void
test_clear_source (void)
{
guint id;
id = g_idle_add (source_test, NULL);
g_assert_cmpuint (id, >, 0);
g_clear_handle_id (&id, g_source_remove);
g_assert_cmpuint (id, ==, 0);
id = g_timeout_add (100, source_test, NULL);
g_assert_cmpuint (id, >, 0);
g_clear_handle_id (&id, g_source_remove);
g_assert_cmpuint (id, ==, 0);
}
static void
test_clear_pointer (void)
{
@@ -632,6 +657,7 @@ main (int argc,
g_test_add_func ("/utils/specialdir/desktop", test_desktop_special_dir);
g_test_add_func ("/utils/clear-pointer", test_clear_pointer);
g_test_add_func ("/utils/take-pointer", test_take_pointer);
g_test_add_func ("/utils/clear-source", test_clear_source);
g_test_add_func ("/utils/misc-mem", test_misc_mem);
g_test_add_func ("/utils/nullify", test_nullify);
g_test_add_func ("/utils/atexit", test_atexit);