[glib/more-static-names] Add g_source_set_static_name_by_id




commit 3fac01437e9f9e10c33492d00282ac40545f7b83
Author: Matthias Clasen <mclasen redhat com>
Date:   Mon Jul 26 16:47:20 2021 -0400

    Add g_source_set_static_name_by_id
    
    After adding g_source_set_static_name, I realized that the
    majority of GSources in the GTK sources still rely on
    g_source_set_name_by_id, and open-coding this in a million
    places is no fun.

 docs/reference/glib/glib-sections.txt |  1 +
 glib/gmain.c                          | 45 ++++++++++++++++++++++++++++-------
 glib/gmain.h                          |  3 +++
 3 files changed, 40 insertions(+), 9 deletions(-)
---
diff --git a/docs/reference/glib/glib-sections.txt b/docs/reference/glib/glib-sections.txt
index 8fdf065a1..8601148f1 100644
--- a/docs/reference/glib/glib-sections.txt
+++ b/docs/reference/glib/glib-sections.txt
@@ -918,6 +918,7 @@ g_source_get_name
 g_source_set_name
 g_source_set_static_name
 g_source_set_name_by_id
+g_source_set_static_name_by_id
 g_source_get_context
 g_source_set_callback
 GSourceFunc
diff --git a/glib/gmain.c b/glib/gmain.c
index 6cba3bd53..09fcf159a 100644
--- a/glib/gmain.c
+++ b/glib/gmain.c
@@ -2097,6 +2097,22 @@ g_source_set_name_full (GSource    *source,
     UNLOCK_CONTEXT (context);
 }
 
+static void
+g_source_set_name_by_id_full (guint       tag,
+                              const char *name,
+                              gboolean    is_static)
+{
+  GSource *source;
+
+  g_return_if_fail (tag > 0);
+
+  source = g_main_context_find_source_by_id (NULL, tag);
+  if (source == NULL)
+    return;
+
+  g_source_set_name_full (source, name, is_static);
+}
+
 /**
  * g_source_set_name:
  * @source: a #GSource
@@ -2190,23 +2206,34 @@ g_source_get_name (GSource *source)
  * been reissued, leading to the operation being performed against the
  * wrong source.
  *
+ * Also see g_source_set_static_name_by_id().
+ *
  * Since: 2.26
  **/
 void
 g_source_set_name_by_id (guint           tag,
                          const char     *name)
 {
-  GSource *source;
-
-  g_return_if_fail (tag > 0);
-
-  source = g_main_context_find_source_by_id (NULL, tag);
-  if (source == NULL)
-    return;
-
-  g_source_set_name (source, name);
+  g_source_set_name_by_id_full (tag, name, FALSE);
 }
 
+/**
+ * g_source_set_static_name_by_id:
+ * @tag: a #GSource ID
+ * @name: debug name for the source
+ *
+ * A variant of g_source_set_name_by_id() that does not
+ * duplicate the @name, and can only be used with
+ * string literals.
+ *
+ * Since: 2.70
+ */
+void
+g_source_set_static_name_by_id (guint       tag,
+                                const char *name)
+{
+  g_source_set_name_by_id_full (tag, name, TRUE);
+}
 
 /**
  * g_source_ref:
diff --git a/glib/gmain.h b/glib/gmain.h
index 8e15f3da4..fc8a60b5f 100644
--- a/glib/gmain.h
+++ b/glib/gmain.h
@@ -613,6 +613,9 @@ const char *         g_source_get_name       (GSource        *source);
 GLIB_AVAILABLE_IN_ALL
 void                 g_source_set_name_by_id (guint           tag,
                                               const char     *name);
+GLIB_AVAILABLE_IN_2_70
+void                 g_source_set_static_name_by_id (guint           tag,
+                                                     const char     *name);
 
 GLIB_AVAILABLE_IN_2_36
 void                 g_source_set_ready_time (GSource        *source,


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