[glib/wip/gproperty-2: 47/57] gobject: Initialize GProperties at instance creation



commit b47e898e5c48ede611f23722dec437a62420ca80
Author: Emmanuele Bassi <ebassi gnome org>
Date:   Tue Jun 11 14:01:04 2013 +0100

    gobject: Initialize GProperties at instance creation
    
    Using their default value. This removes the need to manually calling
    g_property_init_default(), which means we can make it private.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=648526

 gobject/gobject.c        |   31 +++++++++++++++++++++++++++++--
 gobject/gproperty.c      |    6 +++---
 gobject/gproperty.h      |   12 ++++++------
 gobject/tests/property.c |   18 +++++++++++++-----
 4 files changed, 51 insertions(+), 16 deletions(-)
---
diff --git a/gobject/gobject.c b/gobject/gobject.c
index e923e15..74ba167 100644
--- a/gobject/gobject.c
+++ b/gobject/gobject.c
@@ -563,7 +563,7 @@ g_object_class_install_property (GObjectClass *class,
   install_property_internal (G_OBJECT_CLASS_TYPE (class), property_id, pspec);
 
   if (G_IS_PROPERTY (pspec))
-    _g_property_set_installed ((GProperty *) pspec, class, G_OBJECT_CLASS_TYPE (class));
+    g_property_set_installed ((GProperty *) pspec, class, G_OBJECT_CLASS_TYPE (class));
 
   if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
     class->construct_properties = g_slist_append (class->construct_properties, pspec);
@@ -686,7 +686,7 @@ g_object_class_install_properties (GObjectClass  *oclass,
       install_property_internal (oclass_type, i, pspec);
 
       if (G_IS_PROPERTY (pspec))
-        _g_property_set_installed ((GProperty *) pspec, oclass, G_OBJECT_CLASS_TYPE (oclass));
+        g_property_set_installed ((GProperty *) pspec, oclass, G_OBJECT_CLASS_TYPE (oclass));
 
       if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
         oclass->construct_properties = g_slist_append (oclass->construct_properties, pspec);
@@ -1711,6 +1711,20 @@ g_object_new_with_custom_constructor (GObjectClass          *class,
 
   if (CLASS_HAS_PROPS (class))
     {
+      GParamSpec **pspecs;
+      guint n_pspecs, i;
+
+      /* initialize all properties that have a default */
+      pspecs = g_object_class_list_properties (class, &n_pspecs);
+
+      for (i = 0; i < n_pspecs; i++)
+        {
+          if (G_IS_PROPERTY (pspecs[i]))
+            g_property_init_default ((GProperty *) pspecs[i], object);
+        }
+
+      g_free (pspecs);
+
       /* If this object was newly_constructed then g_object_init()
        * froze the queue.  We need to freeze it here in order to get
        * the handle so that we can thaw it below (otherwise it will
@@ -1767,6 +1781,19 @@ g_object_new_internal (GObjectClass          *class,
   if (CLASS_HAS_PROPS (class))
     {
       GSList *node;
+      GParamSpec **pspecs;
+      guint n_pspecs, i;
+
+      /* initialize all properties that have a default */
+      pspecs = g_object_class_list_properties (class, &n_pspecs);
+
+      for (i = 0; i < n_pspecs; i++)
+        {
+          if (G_IS_PROPERTY (pspecs[i]))
+            g_property_init_default ((GProperty *) pspecs[i], object);
+        }
+
+      g_free (pspecs);
 
       /* This will have been setup in g_object_init() */
       nqueue = g_datalist_id_get_data (&object->qdata, quark_notify_queue);
diff --git a/gobject/gproperty.c b/gobject/gproperty.c
index da31d8d..21cc777 100644
--- a/gobject/gproperty.c
+++ b/gobject/gproperty.c
@@ -2605,9 +2605,9 @@ g_pointer_property_get_value (GProperty *property,
  * the property.
  */
 void
-_g_property_set_installed (GProperty *property,
-                           gpointer   g_class,
-                           GType      class_gtype)
+g_property_set_installed (GProperty *property,
+                          gpointer   g_class,
+                          GType      class_gtype)
 {
   if (property->field_offset >= 0)
     {
diff --git a/gobject/gproperty.h b/gobject/gproperty.h
index e632c26..ee6a3ea 100644
--- a/gobject/gproperty.h
+++ b/gobject/gproperty.h
@@ -141,9 +141,6 @@ void            g_property_override_default             (GProperty    *property,
                                                          GType         gtype,
                                                          ...);
 GLIB_AVAILABLE_IN_2_38
-void            g_property_init_default                 (GProperty    *property,
-                                                         gpointer      gobject);
-GLIB_AVAILABLE_IN_2_38
 void            g_property_set_prerequisite             (GProperty    *property,
                                                          ...);
 
@@ -428,9 +425,12 @@ GParamSpec *    g_pointer_property_new  (const gchar         *name,
 
 
 /* private API */
-void            _g_property_set_installed       (GProperty           *property,
-                                                 gpointer             g_class,
-                                                 GType                class_gtype);
+void            g_property_set_installed       (GProperty *property,
+                                                gpointer   g_class,
+                                                GType      class_gtype);
+void            g_property_init_default        (GProperty *property,
+                                                gpointer   object);
+
 
 G_END_DECLS
 
diff --git a/gobject/tests/property.c b/gobject/tests/property.c
index 27cbf71..bf3b02b 100644
--- a/gobject/tests/property.c
+++ b/gobject/tests/property.c
@@ -109,10 +109,21 @@ test_object_set_enum_val (gpointer obj,
 }
 
 static void
+test_object_constructed (GObject *gobject)
+{
+  TestObject *self = (TestObject *) gobject;
+  TestObjectPrivate *priv = test_object_get_instance_private (self);
+
+  g_assert (priv->enum_val == TEST_ENUM_UNSET);
+  g_assert (!priv->enum_val_set);
+}
+
+static void
 test_object_class_init (TestObjectClass *klass)
 {
   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
 
+  gobject_class->constructed = test_object_constructed;
   gobject_class->finalize = test_object_finalize;
 
   test_object_properties[PROP_INTEGER_VAL] =
@@ -147,6 +158,8 @@ test_object_class_init (TestObjectClass *klass)
                          NULL);
   g_property_set_prerequisite ((GProperty *) test_object_properties[PROP_ENUM_VAL],
                                test_enum_get_type ());
+  g_property_set_default ((GProperty *) test_object_properties[PROP_ENUM_VAL],
+                          TEST_ENUM_UNSET);
 
   test_object_properties[PROP_WITH_DEFAULT] =
     g_uint8_property_new ("with-default",
@@ -162,11 +175,6 @@ test_object_class_init (TestObjectClass *klass)
 static void
 test_object_init (TestObject *self)
 {
-  TestObjectPrivate *priv = test_object_get_private (self);
-
-  priv->enum_val = TEST_ENUM_UNSET;
-
-  g_property_init_default ((GProperty *) test_object_properties[PROP_WITH_DEFAULT], self);
 }
 
 static void


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