[glib/wip/chergert/add-gbindinggroup] bindinggroup: add GBindingGroup




commit 0fb3d190f543a5ad85922322e4cee9136014f9cc
Author: Christian Hergert <chergert redhat com>
Date:   Thu Aug 26 16:32:32 2021 -0700

    bindinggroup: add GBindingGroup
    
    Originally, GBindingGroup started with Builder as a way to simplify all
    of the third-degree object bindings necessary around Model-Controller
    objects such as TextBuffer/TextView.
    
    Over time, it has grown to be useful in a number of scenarios outside
    of Builder and has been copied into a number of projects such as GNOME
    Text Editor, GtkSourceView, libdazzle, and more.
    
    It makes sense at this point to unify on a single implementation and
    include that upstream in GObject directly alongside GBinding.
    
    ** Note:
    
    While this currently uses 2_70 as a target, it is expected that this
    would really target 2_72 after branching for GNOME 41 has happend.

 docs/reference/gobject/gobject-docs.xml     |   1 +
 docs/reference/gobject/gobject-sections.txt |  18 +
 glib/glib-object.h                          |   1 +
 gobject/gbindinggroup.c                     | 609 ++++++++++++++++++++++++++++
 gobject/gbindinggroup.h                     |  75 ++++
 gobject/meson.build                         |   2 +
 gobject/tests/bindinggroup.c                | 606 +++++++++++++++++++++++++++
 gobject/tests/meson.build                   |   1 +
 8 files changed, 1313 insertions(+)
---
diff --git a/docs/reference/gobject/gobject-docs.xml b/docs/reference/gobject/gobject-docs.xml
index e8e7c76d9..7deb9a548 100644
--- a/docs/reference/gobject/gobject-docs.xml
+++ b/docs/reference/gobject/gobject-docs.xml
@@ -84,6 +84,7 @@
       <xi:include href="xml/gclosure.xml" />
       <xi:include href="xml/value_arrays.xml" />
       <xi:include href="xml/gbinding.xml" />
+      <xi:include href="xml/gbindinggroup.xml" />
   </reference>
   <reference label="III">
     <title>Tools Reference</title>
diff --git a/docs/reference/gobject/gobject-sections.txt b/docs/reference/gobject/gobject-sections.txt
index 25ecd3aec..8b207ffd3 100644
--- a/docs/reference/gobject/gobject-sections.txt
+++ b/docs/reference/gobject/gobject-sections.txt
@@ -1006,3 +1006,21 @@ G_IS_BINDING
 g_binding_flags_get_type
 g_binding_get_type
 </SECTION>
+
+<SECTION>
+<FILE>gbindinggroup</FILE>
+GBindingGroup
+g_binding_group_new
+g_binding_group_get_source
+g_binding_group_set_source
+g_binding_group_bind
+g_binding_group_bind_full
+g_binding_group_bind_with_closures
+<SUBSECTION Standard>
+G_TYPE_BINDING_GROUP
+G_TYPE_BINDING_GROUP_CLASS
+G_BINDING_GROUP
+G_IS_BINDING_GROUP
+<SUBSECTION Private>
+g_binding_group_get_type
+</SECTION>
diff --git a/glib/glib-object.h b/glib/glib-object.h
index fa824f3bb..d259a3d39 100644
--- a/glib/glib-object.h
+++ b/glib/glib-object.h
@@ -20,6 +20,7 @@
 #define __GLIB_GOBJECT_H_INSIDE__
 
 #include <gobject/gbinding.h>
+#include <gobject/gbindinggroup.h>
 #include <gobject/gboxed.h>
 #include <gobject/genums.h>
 #include <gobject/glib-enumtypes.h>
diff --git a/gobject/gbindinggroup.c b/gobject/gbindinggroup.c
new file mode 100644
index 000000000..c99eb350b
--- /dev/null
+++ b/gobject/gbindinggroup.c
@@ -0,0 +1,609 @@
+/* gbindinggroup.c
+ *
+ * Copyright (C) 2015 Christian Hergert <christian hergert me>
+ * Copyright (C) 2015 Garrett Regier <garrettregier gmail com>
+ *
+ * This file is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of the
+ * License, or (at your option) any later version.
+ *
+ * This file is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "config.h"
+#include "glib.h"
+#include "glibintl.h"
+
+#include "gbindinggroup.h"
+#include "gparamspecs.h"
+
+/**
+ * SECTION:gbindinggroup:
+ * @title: GBindingGroup
+ * @short_description: Binding multiple properties as a group
+ * @include: glib-object.h
+ *
+ * The #GBindingGroup can be used to bind multiple properties
+ * from an object collectively.
+ *
+ * Use the various methods to bind properties from a single source
+ * object to multiple destination objects. Properties can be bound
+ * bidrectionally and are connected when the source object is set
+ * with g_binding_group_set_source().
+ *
+ * Since: 2.70
+ */
+
+struct _GBindingGroup
+{
+  GObject    parent_instance;
+  GObject   *source;
+  GPtrArray *lazy_bindings;
+};
+
+typedef struct _GBindingGroupClass
+{
+  GObjectClass parent_class;
+} GBindingGroupClass;
+
+typedef struct
+{
+  GBindingGroup *group;
+  const char         *source_property;
+  const char         *target_property;
+  GObject            *target;
+  GBinding           *binding;
+  gpointer            user_data;
+  GDestroyNotify      user_data_destroy;
+  gpointer            transform_to;
+  gpointer            transform_from;
+  GBindingFlags       binding_flags;
+  guint               using_closures : 1;
+} LazyBinding;
+
+G_DEFINE_TYPE (GBindingGroup, g_binding_group, G_TYPE_OBJECT)
+
+enum {
+  PROP_0,
+  PROP_SOURCE,
+  N_PROPS
+};
+
+static GParamSpec *properties [N_PROPS];
+
+/*#define DEBUG_BINDINGS 1*/
+
+static void
+g_binding_group_connect (GBindingGroup *self,
+                         LazyBinding   *lazy_binding)
+{
+  GBinding *binding;
+
+  g_assert (G_IS_BINDING_GROUP (self));
+  g_assert (self->source != NULL);
+  g_assert (lazy_binding != NULL);
+  g_assert (lazy_binding->binding == NULL);
+  g_assert (lazy_binding->target != NULL);
+  g_assert (lazy_binding->target_property != NULL);
+  g_assert (lazy_binding->source_property != NULL);
+
+#ifdef DEBUG_BINDINGS
+  {
+    GFlagsClass *flags_class;
+    g_autofree gchar *flags_str = NULL;
+
+    flags_class = g_type_class_ref (G_TYPE_BINDING_FLAGS);
+    flags_str = g_flags_to_string (flags_class, lazy_binding->binding_flags);
+
+    g_print ("Binding %s(%p):%s to %s(%p):%s (flags=%s)\n",
+             G_OBJECT_TYPE_NAME (self->source),
+             self->source,
+             lazy_binding->source_property,
+             G_OBJECT_TYPE_NAME (lazy_binding->target),
+             lazy_binding->target,
+             lazy_binding->target_property,
+             flags_str);
+
+    g_type_class_unref (flags_class);
+  }
+#endif
+
+  if (!lazy_binding->using_closures)
+    binding = g_object_bind_property_full (self->source,
+                                           lazy_binding->source_property,
+                                           lazy_binding->target,
+                                           lazy_binding->target_property,
+                                           lazy_binding->binding_flags,
+                                           lazy_binding->transform_to,
+                                           lazy_binding->transform_from,
+                                           lazy_binding->user_data,
+                                           NULL);
+  else
+    binding = g_object_bind_property_with_closures (self->source,
+                                                    lazy_binding->source_property,
+                                                    lazy_binding->target,
+                                                    lazy_binding->target_property,
+                                                    lazy_binding->binding_flags,
+                                                    lazy_binding->transform_to,
+                                                    lazy_binding->transform_from);
+
+  lazy_binding->binding = binding;
+}
+
+static void
+g_binding_group_disconnect (LazyBinding *lazy_binding)
+{
+  g_assert (lazy_binding != NULL);
+
+  if (lazy_binding->binding != NULL)
+    {
+      g_binding_unbind (lazy_binding->binding);
+      lazy_binding->binding = NULL;
+    }
+}
+
+static void
+g_binding_group__source_weak_notify (gpointer  data,
+                                     GObject  *where_object_was)
+{
+  GBindingGroup *self = data;
+  guint i;
+
+  g_assert (G_IS_BINDING_GROUP (self));
+
+  self->source = NULL;
+
+  for (i = 0; i < self->lazy_bindings->len; i++)
+    {
+      LazyBinding *lazy_binding;
+
+      lazy_binding = g_ptr_array_index (self->lazy_bindings, i);
+      lazy_binding->binding = NULL;
+    }
+}
+
+static void
+g_binding_group__target_weak_notify (gpointer  data,
+                                     GObject  *where_object_was)
+{
+  GBindingGroup *self = data;
+  guint i;
+
+  g_assert (G_IS_BINDING_GROUP (self));
+
+  for (i = 0; i < self->lazy_bindings->len; i++)
+    {
+      LazyBinding *lazy_binding;
+
+      lazy_binding = g_ptr_array_index (self->lazy_bindings, i);
+
+      if (lazy_binding->target == where_object_was)
+        {
+          lazy_binding->target = NULL;
+          lazy_binding->binding = NULL;
+
+          g_ptr_array_remove_index_fast (self->lazy_bindings, i);
+          break;
+        }
+    }
+}
+
+static void
+lazy_binding_free (gpointer data)
+{
+  LazyBinding *lazy_binding = data;
+
+  if (lazy_binding->target != NULL)
+    {
+      g_object_weak_unref (lazy_binding->target,
+                           g_binding_group__target_weak_notify,
+                           lazy_binding->group);
+      lazy_binding->target = NULL;
+    }
+
+  g_binding_group_disconnect (lazy_binding);
+
+  lazy_binding->group = NULL;
+  lazy_binding->source_property = NULL;
+  lazy_binding->target_property = NULL;
+
+  if (lazy_binding->user_data_destroy)
+    lazy_binding->user_data_destroy (lazy_binding->user_data);
+
+  if (lazy_binding->using_closures)
+    {
+      g_clear_pointer (&lazy_binding->transform_to, g_closure_unref);
+      g_clear_pointer (&lazy_binding->transform_from, g_closure_unref);
+    }
+
+  g_slice_free (LazyBinding, lazy_binding);
+}
+
+static void
+g_binding_group_dispose (GObject *object)
+{
+  GBindingGroup *self = (GBindingGroup *)object;
+
+  g_assert (G_IS_BINDING_GROUP (self));
+
+  if (self->source != NULL)
+    {
+      g_object_weak_unref (self->source,
+                           g_binding_group__source_weak_notify,
+                           self);
+      self->source = NULL;
+    }
+
+  if (self->lazy_bindings->len != 0)
+    g_ptr_array_remove_range (self->lazy_bindings, 0, self->lazy_bindings->len);
+
+  G_OBJECT_CLASS (g_binding_group_parent_class)->dispose (object);
+}
+
+static void
+g_binding_group_finalize (GObject *object)
+{
+  GBindingGroup *self = (GBindingGroup *)object;
+
+  g_assert (self->lazy_bindings != NULL);
+  g_assert (self->lazy_bindings->len == 0);
+
+  g_clear_pointer (&self->lazy_bindings, g_ptr_array_unref);
+
+  G_OBJECT_CLASS (g_binding_group_parent_class)->finalize (object);
+}
+
+static void
+g_binding_group_get_property (GObject    *object,
+                              guint       prop_id,
+                              GValue     *value,
+                              GParamSpec *pspec)
+{
+  GBindingGroup *self = G_BINDING_GROUP (object);
+
+  switch (prop_id)
+    {
+    case PROP_SOURCE:
+      g_value_set_object (value, g_binding_group_get_source (self));
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
+static void
+g_binding_group_set_property (GObject      *object,
+                              guint         prop_id,
+                              const GValue *value,
+                              GParamSpec   *pspec)
+{
+  GBindingGroup *self = G_BINDING_GROUP (object);
+
+  switch (prop_id)
+    {
+    case PROP_SOURCE:
+      g_binding_group_set_source (self, g_value_get_object (value));
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
+static void
+g_binding_group_class_init (GBindingGroupClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+  object_class->dispose = g_binding_group_dispose;
+  object_class->finalize = g_binding_group_finalize;
+  object_class->get_property = g_binding_group_get_property;
+  object_class->set_property = g_binding_group_set_property;
+
+  /**
+   * GBindingGroup:source:
+   *
+   * The source object used for binding properties.
+   */
+  properties [PROP_SOURCE] =
+    g_param_spec_object ("source",
+                         "Source",
+                         "The source GObject used for binding properties.",
+                         G_TYPE_OBJECT,
+                         (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+
+  g_object_class_install_properties (object_class, N_PROPS, properties);
+}
+
+static void
+g_binding_group_init (GBindingGroup *self)
+{
+  self->lazy_bindings = g_ptr_array_new_with_free_func (lazy_binding_free);
+}
+
+/**
+ * g_binding_group_new:
+ *
+ * Creates a new #GBindingGroup.
+ *
+ * Returns: a new #GBindingGroup
+ */
+GBindingGroup *
+g_binding_group_new (void)
+{
+  return g_object_new (G_TYPE_BINDING_GROUP, NULL);
+}
+
+/**
+ * g_binding_group_get_source:
+ * @self: the #GBindingGroup
+ *
+ * Gets the source object used for binding properties.
+ *
+ * Returns: (transfer none) (nullable): the source object.
+ */
+GObject *
+g_binding_group_get_source (GBindingGroup *self)
+{
+  g_return_val_if_fail (G_IS_BINDING_GROUP (self), NULL);
+
+  return self->source;
+}
+
+static gboolean
+g_binding_group_check_source (GBindingGroup *self,
+                              gpointer       source)
+{
+  guint i;
+
+  for (i = 0; i < self->lazy_bindings->len; i++)
+    {
+      LazyBinding *lazy_binding = g_ptr_array_index (self->lazy_bindings, i);
+
+      g_return_val_if_fail (g_object_class_find_property (G_OBJECT_GET_CLASS (source),
+                                                          lazy_binding->source_property) != NULL,
+                            FALSE);
+    }
+
+  return TRUE;
+}
+
+/**
+ * g_binding_group_set_source:
+ * @self: the #GBindingGroup
+ * @source: (type GObject) (nullable): the source #GObject
+ *
+ * Sets @source as the source object used for creating property
+ * bindings. If there is already a source object all bindings from it
+ * will be removed.
+ *
+ * Note: All properties that have been bound must exist on @source.
+ */
+void
+g_binding_group_set_source (GBindingGroup *self,
+                            gpointer       source)
+{
+  g_return_if_fail (G_IS_BINDING_GROUP (self));
+  g_return_if_fail (!source || G_IS_OBJECT (source));
+  g_return_if_fail (source != (gpointer)self);
+
+  if (source == (gpointer)self->source)
+    return;
+
+  if (self->source != NULL)
+    {
+      guint i;
+
+      g_object_weak_unref (self->source,
+                           g_binding_group__source_weak_notify,
+                           self);
+      self->source = NULL;
+
+      for (i = 0; i < self->lazy_bindings->len; i++)
+        {
+          LazyBinding *lazy_binding;
+
+          lazy_binding = g_ptr_array_index (self->lazy_bindings, i);
+          g_binding_group_disconnect (lazy_binding);
+        }
+    }
+
+  if (source != NULL && g_binding_group_check_source (self, source))
+    {
+      guint i;
+
+      self->source = source;
+      g_object_weak_ref (self->source,
+                         g_binding_group__source_weak_notify,
+                         self);
+
+      for (i = 0; i < self->lazy_bindings->len; i++)
+        {
+          LazyBinding *lazy_binding;
+
+          lazy_binding = g_ptr_array_index (self->lazy_bindings, i);
+          g_binding_group_connect (self, lazy_binding);
+        }
+    }
+
+  g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_SOURCE]);
+}
+
+static void
+g_binding_group_bind_helper (GBindingGroup  *self,
+                             const gchar    *source_property,
+                             gpointer        target,
+                             const gchar    *target_property,
+                             GBindingFlags   flags,
+                             gpointer        transform_to,
+                             gpointer        transform_from,
+                             gpointer        user_data,
+                             GDestroyNotify  user_data_destroy,
+                             gboolean        using_closures)
+{
+  LazyBinding *lazy_binding;
+
+  g_return_if_fail (G_IS_BINDING_GROUP (self));
+  g_return_if_fail (source_property != NULL);
+  g_return_if_fail (self->source == NULL ||
+                    g_object_class_find_property (G_OBJECT_GET_CLASS (self->source),
+                                                  source_property) != NULL);
+  g_return_if_fail (G_IS_OBJECT (target));
+  g_return_if_fail (target_property != NULL);
+  g_return_if_fail (g_object_class_find_property (G_OBJECT_GET_CLASS (target),
+                                                  target_property) != NULL);
+  g_return_if_fail (target != (gpointer)self ||
+                    strcmp (source_property, target_property) != 0);
+
+  lazy_binding = g_slice_new0 (LazyBinding);
+  lazy_binding->group = self;
+  lazy_binding->source_property = g_intern_string (source_property);
+  lazy_binding->target_property = g_intern_string (target_property);
+  lazy_binding->target = target;
+  lazy_binding->binding_flags = flags | G_BINDING_SYNC_CREATE;
+  lazy_binding->user_data = user_data;
+  lazy_binding->user_data_destroy = user_data_destroy;
+  lazy_binding->transform_to = transform_to;
+  lazy_binding->transform_from = transform_from;
+
+  if (using_closures)
+    {
+      lazy_binding->using_closures = TRUE;
+
+      if (transform_to != NULL)
+        g_closure_sink (g_closure_ref (transform_to));
+
+      if (transform_from != NULL)
+        g_closure_sink (g_closure_ref (transform_from));
+    }
+
+  g_object_weak_ref (target,
+                     g_binding_group__target_weak_notify,
+                     self);
+
+  g_ptr_array_add (self->lazy_bindings, lazy_binding);
+
+  if (self->source != NULL)
+    g_binding_group_connect (self, lazy_binding);
+}
+
+/**
+ * g_binding_group_bind:
+ * @self: the #GBindingGroup
+ * @source_property: the property on the source to bind
+ * @target: (type GObject): the target #GObject
+ * @target_property: the property on @target to bind
+ * @flags: the flags used to create the #GBinding
+ *
+ * Creates a binding between @source_property on the source object
+ * and @target_property on @target. Whenever the @source_property
+ * is changed the @target_property is updated using the same value.
+ * The binding flags #G_BINDING_SYNC_CREATE is automatically specified.
+ *
+ * See: g_object_bind_property().
+ */
+void
+g_binding_group_bind (GBindingGroup *self,
+                      const gchar   *source_property,
+                      gpointer       target,
+                      const gchar   *target_property,
+                      GBindingFlags  flags)
+{
+  g_binding_group_bind_full (self, source_property,
+                             target, target_property,
+                             flags,
+                             NULL, NULL,
+                             NULL, NULL);
+}
+
+/**
+ * g_binding_group_bind_full:
+ * @self: the #GBindingGroup
+ * @source_property: the property on the source to bind
+ * @target: (type GObject): the target #GObject
+ * @target_property: the property on @target to bind
+ * @flags: the flags used to create the #GBinding
+ * @transform_to: (scope notified) (nullable): the transformation function
+ *     from the source object to the @target, or %NULL to use the default
+ * @transform_from: (scope notified) (nullable): the transformation function
+ *     from the @target to the source object, or %NULL to use the default
+ * @user_data: custom data to be passed to the transformation
+ *             functions, or %NULL
+ * @user_data_destroy: function to be called when disposing the binding,
+ *     to free the resources used by the transformation functions
+ *
+ * Creates a binding between @source_property on the source object and
+ * @target_property on @target, allowing you to set the transformation
+ * functions to be used by the binding. The binding flags
+ * #G_BINDING_SYNC_CREATE is automatically specified.
+ *
+ * See: g_object_bind_property_full().
+ */
+void
+g_binding_group_bind_full (GBindingGroup         *self,
+                           const gchar           *source_property,
+                           gpointer               target,
+                           const gchar           *target_property,
+                           GBindingFlags          flags,
+                           GBindingTransformFunc  transform_to,
+                           GBindingTransformFunc  transform_from,
+                           gpointer               user_data,
+                           GDestroyNotify         user_data_destroy)
+{
+  g_binding_group_bind_helper (self, source_property,
+                               target, target_property,
+                               flags,
+                               transform_to, transform_from,
+                               user_data, user_data_destroy,
+                               FALSE);
+}
+
+/**
+ * g_binding_group_bind_with_closures: (rename-to g_binding_group_bind_full)
+ * @self: the #GBindingGroup
+ * @source_property: the property on the source to bind
+ * @target: (type GObject): the target #GObject
+ * @target_property: the property on @target to bind
+ * @flags: the flags used to create the #GBinding
+ * @transform_to: (nullable): a #GClosure wrapping the
+ *     transformation function from the source object to the @target,
+ *     or %NULL to use the default
+ * @transform_from: (nullable): a #GClosure wrapping the
+ *     transformation function from the @target to the source object,
+ *     or %NULL to use the default
+ *
+ * Creates a binding between @source_property on the source object and
+ * @target_property on @target, allowing you to set the transformation
+ * functions to be used by the binding. The binding flags
+ * #G_BINDING_SYNC_CREATE is automatically specified.
+ *
+ * This function is the language bindings friendly version of
+ * g_binding_group_bind_property_full(), using #GClosures
+ * instead of function pointers.
+ *
+ * See: g_object_bind_property_with_closures().
+ */
+void
+g_binding_group_bind_with_closures (GBindingGroup *self,
+                                    const gchar   *source_property,
+                                    gpointer       target,
+                                    const gchar   *target_property,
+                                    GBindingFlags  flags,
+                                    GClosure      *transform_to,
+                                    GClosure      *transform_from)
+{
+  g_binding_group_bind_helper (self, source_property,
+                               target, target_property,
+                               flags,
+                               transform_to, transform_from,
+                               NULL, NULL,
+                               TRUE);
+}
diff --git a/gobject/gbindinggroup.h b/gobject/gbindinggroup.h
new file mode 100644
index 000000000..2f9700d62
--- /dev/null
+++ b/gobject/gbindinggroup.h
@@ -0,0 +1,75 @@
+/* gbindinggroup.h
+ *
+ * Copyright (C) 2015 Christian Hergert <christian hergert me>
+ * Copyright (C) 2015 Garrett Regier <garrettregier gmail com>
+ *
+ * This file is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of the
+ * License, or (at your option) any later version.
+ *
+ * This file is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef __G_BINDING_GROUP_H__
+#define __G_BINDING_GROUP_H__
+
+#if !defined (__GLIB_GOBJECT_H_INSIDE__) && !defined (GOBJECT_COMPILATION)
+#error "Only <glib-object.h> can be included directly."
+#endif
+
+#include <glib.h>
+#include <gobject/gobject.h>
+#include <gobject/gbinding.h>
+
+G_BEGIN_DECLS
+
+#define G_BINDING_GROUP(obj)    (G_TYPE_CHECK_INSTANCE_CAST ((obj), G_TYPE_BINDING_GROUP, GBindingGroup))
+#define G_IS_BINDING_GROUP(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), G_TYPE_BINDING_GROUP))
+#define G_TYPE_BINDING_GROUP    (g_binding_group_get_type())
+
+typedef struct _GBindingGroup GBindingGroup;
+
+GLIB_AVAILABLE_IN_2_70
+GType          g_binding_group_get_type           (void) G_GNUC_CONST;
+GLIB_AVAILABLE_IN_2_70
+GBindingGroup *g_binding_group_new                (void);
+GLIB_AVAILABLE_IN_2_70
+GObject       *g_binding_group_get_source         (GBindingGroup         *self);
+GLIB_AVAILABLE_IN_2_70
+void           g_binding_group_set_source         (GBindingGroup         *self,
+                                                   gpointer               source);
+GLIB_AVAILABLE_IN_2_70
+void           g_binding_group_bind               (GBindingGroup         *self,
+                                                   const gchar           *source_property,
+                                                   gpointer               target,
+                                                   const gchar           *target_property,
+                                                   GBindingFlags          flags);
+GLIB_AVAILABLE_IN_2_70
+void           g_binding_group_bind_full          (GBindingGroup         *self,
+                                                   const gchar           *source_property,
+                                                   gpointer               target,
+                                                   const gchar           *target_property,
+                                                   GBindingFlags          flags,
+                                                   GBindingTransformFunc  transform_to,
+                                                   GBindingTransformFunc  transform_from,
+                                                   gpointer               user_data,
+                                                   GDestroyNotify         user_data_destroy);
+GLIB_AVAILABLE_IN_2_70
+void           g_binding_group_bind_with_closures (GBindingGroup         *self,
+                                                   const gchar           *source_property,
+                                                   gpointer               target,
+                                                   const gchar           *target_property,
+                                                   GBindingFlags          flags,
+                                                   GClosure              *transform_to,
+                                                   GClosure              *transform_from);
+
+G_END_DECLS
+
+#endif /* __G_BINDING_GROUP_H__ */
diff --git a/gobject/meson.build b/gobject/meson.build
index 536f15393..40443b54f 100644
--- a/gobject/meson.build
+++ b/gobject/meson.build
@@ -2,6 +2,7 @@ gobject_install_headers = files(
   'gobject-autocleanups.h',
   'glib-types.h',
   'gbinding.h',
+  'gbindinggroup.h',
   'gboxed.h',
   'gclosure.h',
   'genums.h',
@@ -25,6 +26,7 @@ install_headers(gobject_install_headers, subdir : 'glib-2.0/gobject')
 gobject_sources = files(
   'gatomicarray.c',
   'gbinding.c',
+  'gbindinggroup.c',
   'gboxed.c',
   'gclosure.c',
   'genums.c',
diff --git a/gobject/tests/bindinggroup.c b/gobject/tests/bindinggroup.c
new file mode 100644
index 000000000..dbba9cb6f
--- /dev/null
+++ b/gobject/tests/bindinggroup.c
@@ -0,0 +1,606 @@
+#include <glib-object.h>
+
+/* Copied from glib */
+typedef struct _BindingSource
+{
+  GObject parent_instance;
+
+  gint foo;
+  gint bar;
+  gdouble value;
+  gboolean toggle;
+} BindingSource;
+
+typedef struct _BindingSourceClass
+{
+  GObjectClass parent_class;
+} BindingSourceClass;
+
+enum
+{
+  PROP_SOURCE_0,
+
+  PROP_SOURCE_FOO,
+  PROP_SOURCE_BAR,
+  PROP_SOURCE_VALUE,
+  PROP_SOURCE_TOGGLE
+};
+
+static GType binding_source_get_type (void);
+G_DEFINE_TYPE (BindingSource, binding_source, G_TYPE_OBJECT);
+
+static void
+binding_source_set_property (GObject      *gobject,
+                             guint         prop_id,
+                             const GValue *value,
+                             GParamSpec   *pspec)
+{
+  BindingSource *source = (BindingSource *) gobject;
+
+  switch (prop_id)
+    {
+    case PROP_SOURCE_FOO:
+      source->foo = g_value_get_int (value);
+      break;
+
+    case PROP_SOURCE_BAR:
+      source->bar = g_value_get_int (value);
+      break;
+
+    case PROP_SOURCE_VALUE:
+      source->value = g_value_get_double (value);
+      break;
+
+    case PROP_SOURCE_TOGGLE:
+      source->toggle = g_value_get_boolean (value);
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
+    }
+}
+
+static void
+binding_source_get_property (GObject    *gobject,
+                             guint       prop_id,
+                             GValue     *value,
+                             GParamSpec *pspec)
+{
+  BindingSource *source = (BindingSource *) gobject;
+
+  switch (prop_id)
+    {
+    case PROP_SOURCE_FOO:
+      g_value_set_int (value, source->foo);
+      break;
+
+    case PROP_SOURCE_BAR:
+      g_value_set_int (value, source->bar);
+      break;
+
+    case PROP_SOURCE_VALUE:
+      g_value_set_double (value, source->value);
+      break;
+
+    case PROP_SOURCE_TOGGLE:
+      g_value_set_boolean (value, source->toggle);
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
+    }
+}
+
+static void
+binding_source_class_init (BindingSourceClass *klass)
+{
+  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
+
+  gobject_class->set_property = binding_source_set_property;
+  gobject_class->get_property = binding_source_get_property;
+
+  g_object_class_install_property (gobject_class, PROP_SOURCE_FOO,
+                                   g_param_spec_int ("foo", "Foo", "Foo",
+                                                     -1, 100,
+                                                     0,
+                                                     G_PARAM_READWRITE));
+  g_object_class_install_property (gobject_class, PROP_SOURCE_BAR,
+                                   g_param_spec_int ("bar", "Bar", "Bar",
+                                                     -1, 100,
+                                                     0,
+                                                     G_PARAM_READWRITE));
+  g_object_class_install_property (gobject_class, PROP_SOURCE_VALUE,
+                                   g_param_spec_double ("value", "Value", "Value",
+                                                        -100.0, 200.0,
+                                                        0.0,
+                                                        G_PARAM_READWRITE));
+  g_object_class_install_property (gobject_class, PROP_SOURCE_TOGGLE,
+                                   g_param_spec_boolean ("toggle", "Toggle", "Toggle",
+                                                         FALSE,
+                                                         G_PARAM_READWRITE));
+}
+
+static void
+binding_source_init (BindingSource *self)
+{
+}
+
+typedef struct _BindingTarget
+{
+  GObject parent_instance;
+
+  gint bar;
+  gdouble value;
+  gboolean toggle;
+} BindingTarget;
+
+typedef struct _BindingTargetClass
+{
+  GObjectClass parent_class;
+} BindingTargetClass;
+
+enum
+{
+  PROP_TARGET_0,
+
+  PROP_TARGET_BAR,
+  PROP_TARGET_VALUE,
+  PROP_TARGET_TOGGLE
+};
+
+static GType binding_target_get_type (void);
+G_DEFINE_TYPE (BindingTarget, binding_target, G_TYPE_OBJECT);
+
+static void
+binding_target_set_property (GObject      *gobject,
+                             guint         prop_id,
+                             const GValue *value,
+                             GParamSpec   *pspec)
+{
+  BindingTarget *target = (BindingTarget *) gobject;
+
+  switch (prop_id)
+    {
+    case PROP_TARGET_BAR:
+      target->bar = g_value_get_int (value);
+      break;
+
+    case PROP_TARGET_VALUE:
+      target->value = g_value_get_double (value);
+      break;
+
+    case PROP_TARGET_TOGGLE:
+      target->toggle = g_value_get_boolean (value);
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
+    }
+}
+
+static void
+binding_target_get_property (GObject    *gobject,
+                             guint       prop_id,
+                             GValue     *value,
+                             GParamSpec *pspec)
+{
+  BindingTarget *target = (BindingTarget *) gobject;
+
+  switch (prop_id)
+    {
+    case PROP_TARGET_BAR:
+      g_value_set_int (value, target->bar);
+      break;
+
+    case PROP_TARGET_VALUE:
+      g_value_set_double (value, target->value);
+      break;
+
+    case PROP_TARGET_TOGGLE:
+      g_value_set_boolean (value, target->toggle);
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
+    }
+}
+
+static void
+binding_target_class_init (BindingTargetClass *klass)
+{
+  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
+
+  gobject_class->set_property = binding_target_set_property;
+  gobject_class->get_property = binding_target_get_property;
+
+  g_object_class_install_property (gobject_class, PROP_TARGET_BAR,
+                                   g_param_spec_int ("bar", "Bar", "Bar",
+                                                     -1, 100,
+                                                     0,
+                                                     G_PARAM_READWRITE));
+  g_object_class_install_property (gobject_class, PROP_TARGET_VALUE,
+                                   g_param_spec_double ("value", "Value", "Value",
+                                                        -100.0, 200.0,
+                                                        0.0,
+                                                        G_PARAM_READWRITE));
+  g_object_class_install_property (gobject_class, PROP_TARGET_TOGGLE,
+                                   g_param_spec_boolean ("toggle", "Toggle", "Toggle",
+                                                         FALSE,
+                                                         G_PARAM_READWRITE));
+}
+
+static void
+binding_target_init (BindingTarget *self)
+{
+}
+
+static gboolean
+celsius_to_fahrenheit (GBinding     *binding,
+                       const GValue *from_value,
+                       GValue       *to_value,
+                       gpointer      user_data G_GNUC_UNUSED)
+{
+  gdouble celsius, fahrenheit;
+
+  g_assert_true (G_VALUE_HOLDS (from_value, G_TYPE_DOUBLE));
+  g_assert_true (G_VALUE_HOLDS (to_value, G_TYPE_DOUBLE));
+
+  celsius = g_value_get_double (from_value);
+  fahrenheit = (9 * celsius / 5) + 32.0;
+
+  if (g_test_verbose ())
+    g_printerr ("Converting %.2fC to %.2fF\n", celsius, fahrenheit);
+
+  g_value_set_double (to_value, fahrenheit);
+
+  return TRUE;
+}
+
+static gboolean
+fahrenheit_to_celsius (GBinding     *binding,
+                       const GValue *from_value,
+                       GValue       *to_value,
+                       gpointer      user_data G_GNUC_UNUSED)
+{
+  gdouble celsius, fahrenheit;
+
+  g_assert_true (G_VALUE_HOLDS (from_value, G_TYPE_DOUBLE));
+  g_assert_true (G_VALUE_HOLDS (to_value, G_TYPE_DOUBLE));
+
+  fahrenheit = g_value_get_double (from_value);
+  celsius = 5 * (fahrenheit - 32.0) / 9;
+
+  if (g_test_verbose ())
+    g_printerr ("Converting %.2fF to %.2fC\n", fahrenheit, celsius);
+
+  g_value_set_double (to_value, celsius);
+
+  return TRUE;
+}
+
+static void
+test_binding_group_invalid (void)
+{
+  GBindingGroup *group = g_binding_group_new ();
+  BindingSource *source = g_object_new (binding_source_get_type (), NULL);
+  BindingTarget *target = g_object_new (binding_target_get_type (), NULL);
+
+  /* Invalid Target Property */
+  g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
+                         "*find_property*target_property*!=*NULL*");
+  g_binding_group_bind (group, "value",
+                          target, "does-not-exist",
+                          G_BINDING_DEFAULT);
+  g_test_assert_expected_messages ();
+
+  g_binding_group_set_source (group, NULL);
+
+  /* Invalid Source Property */
+  g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
+                         "*find_property*source_property*!=*NULL*");
+  g_binding_group_set_source (group, source);
+  g_binding_group_bind (group, "does-not-exist",
+                          target, "value",
+                          G_BINDING_DEFAULT);
+  g_test_assert_expected_messages ();
+
+  g_binding_group_set_source (group, NULL);
+
+  /* Invalid Source */
+  g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
+                         "*find_property*->source_property*!=*NULL*");
+  g_binding_group_bind (group, "does-not-exist",
+                          target, "value",
+                          G_BINDING_DEFAULT);
+  g_binding_group_set_source (group, source);
+  g_test_assert_expected_messages ();
+
+  g_object_unref (target);
+  g_object_unref (source);
+  g_object_unref (group);
+}
+
+static void
+test_binding_group_default (void)
+{
+  gsize i, j;
+  GBindingGroup *group = g_binding_group_new ();
+  BindingSource *source = g_object_new (binding_source_get_type (), NULL);
+  BindingTarget *targets[5];
+
+  for (i = 0; i < G_N_ELEMENTS (targets); ++i)
+    {
+      targets[i] = g_object_new (binding_target_get_type (), NULL);
+      g_binding_group_bind (group, "foo",
+                              targets[i], "bar",
+                              G_BINDING_DEFAULT);
+    }
+
+  g_assert_null (g_binding_group_get_source (group));
+  g_binding_group_set_source (group, source);
+  g_assert_true (g_binding_group_get_source (group) == (GObject *)source);
+
+  for (i = 0; i < 2; ++i)
+    {
+      g_object_set (source, "foo", 42, NULL);
+      for (j = 0; j < G_N_ELEMENTS (targets); ++j)
+        g_assert_cmpint (source->foo, ==, targets[j]->bar);
+
+      g_object_set (targets[0], "bar", 47, NULL);
+      g_assert_cmpint (source->foo, !=, targets[0]->bar);
+
+      /* Check that we transition the source correctly */
+      g_binding_group_set_source (group, NULL);
+      g_assert_null (g_binding_group_get_source (group));
+      g_binding_group_set_source (group, source);
+      g_assert_true (g_binding_group_get_source (group) == (GObject *)source);
+    }
+
+  g_object_unref (group);
+
+  g_object_set (source, "foo", 0, NULL);
+  for (i = 0; i < G_N_ELEMENTS (targets); ++i)
+    g_assert_cmpint (source->foo, !=, targets[i]->bar);
+
+  g_object_unref (source);
+  for (i = 0; i < G_N_ELEMENTS (targets); ++i)
+    g_object_unref (targets[i]);
+}
+
+static void
+test_binding_group_bidirectional (void)
+{
+  gsize i, j;
+  GBindingGroup *group = g_binding_group_new ();
+  BindingSource *source = g_object_new (binding_source_get_type (), NULL);
+  BindingTarget *targets[5];
+
+  for (i = 0; i < G_N_ELEMENTS (targets); ++i)
+    {
+      targets[i] = g_object_new (binding_target_get_type (), NULL);
+      g_binding_group_bind (group, "value",
+                              targets[i], "value",
+                              G_BINDING_BIDIRECTIONAL);
+    }
+
+  g_assert_null (g_binding_group_get_source (group));
+  g_binding_group_set_source (group, source);
+  g_assert_true (g_binding_group_get_source (group) == (GObject *)source);
+
+  for (i = 0; i < 2; ++i)
+    {
+      g_object_set (source, "value", 42.0, NULL);
+      for (j = 0; j < G_N_ELEMENTS (targets); ++j)
+        g_assert_cmpfloat (source->value, ==, targets[j]->value);
+
+      g_object_set (targets[0], "value", 47.0, NULL);
+      g_assert_cmpfloat (source->value, ==, targets[0]->value);
+
+      /* Check that we transition the source correctly */
+      g_binding_group_set_source (group, NULL);
+      g_assert_null (g_binding_group_get_source (group));
+      g_binding_group_set_source (group, source);
+      g_assert_true (g_binding_group_get_source (group) == (GObject *)source);
+    }
+
+  g_object_unref (group);
+
+  g_object_set (targets[0], "value", 0.0, NULL);
+  g_assert_cmpfloat (source->value, !=, targets[0]->value);
+
+  g_object_unref (source);
+  for (i = 0; i < G_N_ELEMENTS (targets); ++i)
+    g_object_unref (targets[i]);
+}
+
+static void
+transform_destroy_notify (gpointer data)
+{
+  gboolean *transform_destroy_called = data;
+
+  *transform_destroy_called = TRUE;
+}
+
+static void
+test_binding_group_transform (void)
+{
+  gboolean transform_destroy_called = FALSE;
+  GBindingGroup *group = g_binding_group_new ();
+  BindingSource *source = g_object_new (binding_source_get_type (), NULL);
+  BindingTarget *target = g_object_new (binding_target_get_type (), NULL);
+
+  g_binding_group_set_source (group, source);
+  g_binding_group_bind_full (group, "value",
+                               target, "value",
+                               G_BINDING_BIDIRECTIONAL,
+                               celsius_to_fahrenheit,
+                               fahrenheit_to_celsius,
+                               &transform_destroy_called,
+                               transform_destroy_notify);
+
+  g_object_set (source, "value", 24.0, NULL);
+  g_assert_cmpfloat (target->value, ==, ((9 * 24.0 / 5) + 32.0));
+
+  g_object_set (target, "value", 69.0, NULL);
+  g_assert_cmpfloat (source->value, ==, (5 * (69.0 - 32.0) / 9));
+
+  /* The GDestroyNotify should only be called when the
+   * set is freed, not when the various GBindings are freed
+   */
+  g_binding_group_set_source (group, NULL);
+  g_assert_false (transform_destroy_called);
+
+  g_object_unref (group);
+  g_assert_true (transform_destroy_called);
+
+  g_object_unref (source);
+  g_object_unref (target);
+}
+
+static void
+test_binding_group_transform_closures (void)
+{
+  gboolean transform_destroy_called_1 = FALSE;
+  gboolean transform_destroy_called_2 = FALSE;
+  GBindingGroup *group = g_binding_group_new ();
+  BindingSource *source = g_object_new (binding_source_get_type (), NULL);
+  BindingTarget *target = g_object_new (binding_target_get_type (), NULL);
+  GClosure *c2f_closure, *f2c_closure;
+
+  c2f_closure = g_cclosure_new (G_CALLBACK (celsius_to_fahrenheit),
+                                &transform_destroy_called_1,
+                                (GClosureNotify) transform_destroy_notify);
+  f2c_closure = g_cclosure_new (G_CALLBACK (fahrenheit_to_celsius),
+                                &transform_destroy_called_2,
+                                (GClosureNotify) transform_destroy_notify);
+
+  g_binding_group_set_source (group, source);
+  g_binding_group_bind_with_closures (group, "value",
+                                        target, "value",
+                                        G_BINDING_BIDIRECTIONAL,
+                                        c2f_closure,
+                                        f2c_closure);
+
+  g_object_set (source, "value", 24.0, NULL);
+  g_assert_cmpfloat (target->value, ==, ((9 * 24.0 / 5) + 32.0));
+
+  g_object_set (target, "value", 69.0, NULL);
+  g_assert_cmpfloat (source->value, ==, (5 * (69.0 - 32.0) / 9));
+
+  /* The GClsoureNotify should only be called when the
+   * set is freed, not when the various GBindings are freed
+   */
+  g_binding_group_set_source (group, NULL);
+  g_assert_false (transform_destroy_called_1);
+  g_assert_false (transform_destroy_called_2);
+
+  g_object_unref (group);
+  g_assert_true (transform_destroy_called_1);
+  g_assert_true (transform_destroy_called_2);
+
+  g_object_unref (source);
+  g_object_unref (target);
+}
+
+static void
+test_binding_group_same_object (void)
+{
+  gsize i;
+  GBindingGroup *group = g_binding_group_new ();
+  BindingSource *source = g_object_new (binding_source_get_type (),
+                                        "foo", 100,
+                                        "bar", 50,
+                                        NULL);
+
+  g_binding_group_set_source (group, source);
+  g_binding_group_bind (group, "foo",
+                          source, "bar",
+                          G_BINDING_BIDIRECTIONAL);
+
+  for (i = 0; i < 2; ++i)
+    {
+      g_object_set (source, "foo", 10, NULL);
+      g_assert_cmpint (source->foo, ==, 10);
+      g_assert_cmpint (source->bar, ==, 10);
+
+      g_object_set (source, "bar", 30, NULL);
+      g_assert_cmpint (source->foo, ==, 30);
+      g_assert_cmpint (source->bar, ==, 30);
+
+      /* Check that it is possible both when initially
+       * adding the binding and when changing the source
+       */
+      g_binding_group_set_source (group, NULL);
+      g_binding_group_set_source (group, source);
+    }
+
+  g_object_unref (source);
+  g_object_unref (group);
+}
+
+static void
+test_binding_group_weak_ref_source (void)
+{
+  GBindingGroup *group = g_binding_group_new ();
+  BindingSource *source = g_object_new (binding_source_get_type (), NULL);
+  BindingTarget *target = g_object_new (binding_target_get_type (), NULL);
+
+  g_binding_group_set_source (group, source);
+  g_binding_group_bind (group, "value",
+                          target, "value",
+                          G_BINDING_BIDIRECTIONAL);
+
+  g_object_add_weak_pointer (G_OBJECT (source), (gpointer)&source);
+  g_assert_true (g_binding_group_get_source (group) == (GObject *)source);
+  g_object_unref (source);
+  g_assert_null (source);
+  g_assert_null (g_binding_group_get_source (group));
+
+  /* Hopefully this would explode if the binding was still alive */
+  g_object_set (target, "value", 0.0, NULL);
+
+  g_object_unref (target);
+  g_object_unref (group);
+}
+
+static void
+test_binding_group_weak_ref_target (void)
+{
+  GBindingGroup *group = g_binding_group_new ();
+  BindingSource *source = g_object_new (binding_source_get_type (), NULL);
+  BindingTarget *target = g_object_new (binding_target_get_type (), NULL);
+
+  g_binding_group_set_source (group, source);
+  g_binding_group_bind (group, "value",
+                          target, "value",
+                          G_BINDING_BIDIRECTIONAL);
+
+  g_object_set (source, "value", 47.0, NULL);
+  g_assert_cmpfloat (target->value, ==, 47.0);
+
+  g_object_add_weak_pointer (G_OBJECT (target), (gpointer)&target);
+  g_object_unref (target);
+  g_assert_null (target);
+
+  /* Hopefully this would explode if the binding was still alive */
+  g_object_set (source, "value", 0.0, NULL);
+
+  g_object_unref (source);
+  g_object_unref (group);
+}
+
+gint
+main (gint   argc,
+      gchar *argv[])
+{
+  g_test_init (&argc, &argv, NULL);
+  g_test_add_func ("/GObject/BindingGroup/invalid", test_binding_group_invalid);
+  g_test_add_func ("/GObject/BindingGroup/default", test_binding_group_default);
+  g_test_add_func ("/GObject/BindingGroup/bidirectional", test_binding_group_bidirectional);
+  g_test_add_func ("/GObject/BindingGroup/transform", test_binding_group_transform);
+  g_test_add_func ("/GObject/BindingGroup/transform-closures", test_binding_group_transform_closures);
+  g_test_add_func ("/GObject/BindingGroup/same-object", test_binding_group_same_object);
+  g_test_add_func ("/GObject/BindingGroup/weak-ref-source", test_binding_group_weak_ref_source);
+  g_test_add_func ("/GObject/BindingGroup/weak-ref-target", test_binding_group_weak_ref_target);
+  return g_test_run ();
+}
diff --git a/gobject/tests/meson.build b/gobject/tests/meson.build
index e97e1bc7e..867a9b5f0 100644
--- a/gobject/tests/meson.build
+++ b/gobject/tests/meson.build
@@ -34,6 +34,7 @@ gobject_tests = {
   'threadtests' : {},
   'dynamictests' : {},
   'binding' : {},
+  'bindinggroup' : {},
   'properties' : {},
   'reference' : {},
   'flags' : {},


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