[glib/wip/gproperty-2: 19/26] gobject: Initialize GProperties at instance init time



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

    gobject: Initialize GProperties at instance init time
    
    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        |   21 +++++++++++++++++++--
 gobject/gproperty.c      |    6 +++---
 gobject/gproperty.h      |   12 ++++++------
 gobject/tests/property.c |   13 ++++---------
 4 files changed, 32 insertions(+), 20 deletions(-)
---
diff --git a/gobject/gobject.c b/gobject/gobject.c
index e923e15..2dd8c9e 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);
@@ -978,6 +978,23 @@ g_object_init (GObject             *object,
       G_UNLOCK (construction_mutex);
     }
 
+  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);
+    }
+
 #ifdef G_ENABLE_DEBUG
   IF_DEBUG (OBJECTS)
     {
diff --git a/gobject/gproperty.c b/gobject/gproperty.c
index e06c7fa..324fa10 100644
--- a/gobject/gproperty.c
+++ b/gobject/gproperty.c
@@ -2741,9 +2741,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 deed674..3bf5861 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,
                                                          GType         gtype);
 
@@ -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 7fc2b47..bf50d78 100644
--- a/gobject/tests/property.c
+++ b/gobject/tests/property.c
@@ -83,8 +83,7 @@ static GParamSpec *test_object_properties[LAST_PROP] = { NULL, };
 static void
 test_object_finalize (GObject *gobject)
 {
-  TestObjectPrivate *priv =
-    g_type_instance_get_private ((GTypeInstance *) gobject, test_object_get_type ());
+  TestObjectPrivate *priv = test_object_get_private ((TestObject *) gobject);
 
   g_free (priv->str_val);
 
@@ -98,8 +97,7 @@ static gboolean
 test_object_set_enum_val (gpointer obj,
                           gint     val)
 {
-  TestObjectPrivate *priv =
-    g_type_instance_get_private (obj, test_object_get_type ());
+  TestObjectPrivate *priv = test_object_get_private (obj);
 
   if (priv->enum_val == val)
     return FALSE;
@@ -149,6 +147,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",
@@ -164,11 +164,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]