[glib: 2/9] Add g_binding_dup_target() and g_binding_dup_source()




commit c8c829fa4248713baed0385007f9a3c6327243a6
Author: Sebastian Dröge <sebastian centricular com>
Date:   Mon Oct 19 13:55:12 2020 +0300

    Add g_binding_dup_target() and g_binding_dup_source()
    
    These new getters prevent the source/target from simply disappearing if
    they're finalized from another thread in the meantime.

 docs/reference/gobject/gobject-sections.txt |  2 ++
 gobject/gbinding.c                          | 54 +++++++++++++++++++++++++++++
 gobject/gbinding.h                          |  4 +++
 3 files changed, 60 insertions(+)
---
diff --git a/docs/reference/gobject/gobject-sections.txt b/docs/reference/gobject/gobject-sections.txt
index 36552b1b3..54fbccaf8 100644
--- a/docs/reference/gobject/gobject-sections.txt
+++ b/docs/reference/gobject/gobject-sections.txt
@@ -978,8 +978,10 @@ g_io_condition_get_type
 GBinding
 GBindingFlags
 g_binding_get_source
+g_binding_dup_source
 g_binding_get_source_property
 g_binding_get_target
+g_binding_dup_target
 g_binding_get_target_property
 g_binding_get_flags
 g_binding_unbind
diff --git a/gobject/gbinding.c b/gobject/gbinding.c
index 1448adb36..8451dc48e 100644
--- a/gobject/gbinding.c
+++ b/gobject/gbinding.c
@@ -793,6 +793,10 @@ g_binding_get_flags (GBinding *binding)
  * strong reference to the source. If the source is destroyed before the
  * binding then this function will return %NULL.
  *
+ * Use g_binding_dup_source() if the source or binding are used from different
+ * threads as otherwise the pointer returned from this function might become
+ * invalid if the source is finalized from another thread in the meantime.
+ *
  * Returns: (transfer none) (nullable): the source #GObject, or %NULL if the
  *     source does not exist any more.
  *
@@ -814,6 +818,29 @@ g_binding_get_source (GBinding *binding)
   return source;
 }
 
+/**
+ * g_binding_dup_source:
+ * @binding: a #GBinding
+ *
+ * Retrieves the #GObject instance used as the source of the binding.
+ *
+ * A #GBinding can outlive the source #GObject as the binding does not hold a
+ * strong reference to the source. If the source is destroyed before the
+ * binding then this function will return %NULL.
+ *
+ * Returns: (transfer full) (nullable): the source #GObject, or %NULL if the
+ *     source does not exist any more.
+ *
+ * Since: 2.68
+ */
+GObject *
+g_binding_dup_source (GBinding *binding)
+{
+  g_return_val_if_fail (G_IS_BINDING (binding), NULL);
+
+  return g_weak_ref_get (&binding->source);
+}
+
 /**
  * g_binding_get_target:
  * @binding: a #GBinding
@@ -824,6 +851,10 @@ g_binding_get_source (GBinding *binding)
  * strong reference to the target. If the target is destroyed before the
  * binding then this function will return %NULL.
  *
+ * Use g_binding_dup_target() if the target or binding are used from different
+ * threads as otherwise the pointer returned from this function might become
+ * invalid if the target is finalized from another thread in the meantime.
+ *
  * Returns: (transfer none) (nullable): the target #GObject, or %NULL if the
  *     target does not exist any more.
  *
@@ -845,6 +876,29 @@ g_binding_get_target (GBinding *binding)
   return target;
 }
 
+/**
+ * g_binding_dup_target:
+ * @binding: a #GBinding
+ *
+ * Retrieves the #GObject instance used as the target of the binding.
+ *
+ * A #GBinding can outlive the target #GObject as the binding does not hold a
+ * strong reference to the target. If the target is destroyed before the
+ * binding then this function will return %NULL.
+ *
+ * Returns: (transfer full) (nullable): the target #GObject, or %NULL if the
+ *     target does not exist any more.
+ *
+ * Since: 2.68
+ */
+GObject *
+g_binding_dup_target (GBinding *binding)
+{
+  g_return_val_if_fail (G_IS_BINDING (binding), NULL);
+
+  return g_weak_ref_get (&binding->target);
+}
+
 /**
  * g_binding_get_source_property:
  * @binding: a #GBinding
diff --git a/gobject/gbinding.h b/gobject/gbinding.h
index b4eb233b3..f269ad5a8 100644
--- a/gobject/gbinding.h
+++ b/gobject/gbinding.h
@@ -110,8 +110,12 @@ GLIB_AVAILABLE_IN_ALL
 GBindingFlags         g_binding_get_flags           (GBinding *binding);
 GLIB_AVAILABLE_IN_ALL
 GObject *             g_binding_get_source          (GBinding *binding);
+GLIB_AVAILABLE_IN_2_68
+GObject *             g_binding_dup_source          (GBinding *binding);
 GLIB_AVAILABLE_IN_ALL
 GObject *             g_binding_get_target          (GBinding *binding);
+GLIB_AVAILABLE_IN_2_68
+GObject *             g_binding_dup_target          (GBinding *binding);
 GLIB_AVAILABLE_IN_ALL
 const gchar *         g_binding_get_source_property (GBinding *binding);
 GLIB_AVAILABLE_IN_ALL


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