[glib/wip/gproperty-2: 24/26] property: Remove G_DECLARE_PROPERTIES macro



commit 60ce7b604d317db845fac797ad6b41938f326a0e
Author: Emmanuele Bassi <ebassi gnome org>
Date:   Mon Jun 17 16:44:35 2013 +0100

    property: Remove G_DECLARE_PROPERTIES macro
    
    Make property definition use an allocated GParamSpec array instead of a
    statically allocated one. The GParamSpec array and its length is
    declared inside G_DEFINE_TYPE_EXTENDED; the array is accessed by the
    generated setter and getter functions, as well as the
    G_DEFINE_PROPERTY and G_DEFINE_PROPERTIES macros.

 gobject/gproperty.h      |  192 ++++++++++++----------------------------------
 gobject/gtype.h          |   12 ++-
 gobject/tests/property.c |   13 +---
 3 files changed, 56 insertions(+), 161 deletions(-)
---
diff --git a/gobject/gproperty.h b/gobject/gproperty.h
index d6416b3..0856036 100644
--- a/gobject/gproperty.h
+++ b/gobject/gproperty.h
@@ -493,128 +493,6 @@ void            g_property_init_default        (GProperty *property,
   g_property_set_prerequisite (g_property, type);
 
 /**
- * G_DECLARE_PROPERTY:
- * @T_N: the name of type type, in CamelCase
- * @name: the name of the property, with '-' replaced by '_'
- *
- * Declares the property @name of type @T_N.
- *
- * Note that this macro can only be used inside the G_DECLARE_PROPERTIES() macro.
- *
- * Since: 2.38
- */
-#define G_DECLARE_PROPERTY(T_N, name) \
-  PROP_##T_N##_##name,
-
-/**
- * G_DECLARE_PROPERTIES:
- * @T_N: the name of the type, in CamelCase
- * @t_n: the name of the type, in lower case, with spaces replaced by underscores
- * @_C_: a list of G_DECLARE_PROPERTY macros, one for each property
- *
- * Declares a list of properties for the type @T_N.
- *
- * |[
- *   G_DECLARE_PROPERTIES (GtkGadget, gtk_gadget,
- *                         G_DECLARE_PROPERTY (GtkGadget, width),
- *                         G_DECLARE_PROPERTY (GtkGadget, height))
- * ]|
- * expands to
- * |[
- *   enum {
- *     PROP_GtkGadget_0,
- *     PROP_GtkGadget_width,
- *     PROP_GtkGadget_height,
- *     PROP_GtkGadget_LAST
- *   };
- *
- *   static GProperty *gtk_gadget_properties[PROP_GtkGadget_LAST];
- *
- *   static GParamSpec *
- *   gtk_gadget_find_property (GtkGadget   *self,
- *                             const gchar *name)
- *   {
- *     const gchar *iname = g_intern_string (name);
- *     gint i;
- *
- *     for (i = 1; i < PROP_GtkGadget_LAST; i++)
- *       {
- *         GParamSpec *pspec = (GParamSpec *) gtk_gadget_properties[i];
- *         if (pspec != NULL && pspec->name == iname)
- *           return pspec;
- *       }
- *
- *     return g_object_class_find_property (G_OBJECT_GET_CLASS (self), name);
- *   }
- *
- *   static void
- *   gtk_gadget_notify_property (GtkGadget   *self,
- *                               const gchar *name)
- *   {
- *     GParamSpec *pspec = gtk_gadget_find_property (self, name);
- *
- *     if (pspec != NULL)
- *       g_object_notify_by_pspec (G_OBJECT (self), name);
- *   }
- *
- *   static void
- *   gtk_gadget_class_install_properties (GtkGadgetClass *g_class)
- *   {
- *     g_object_class_install_properties (G_OBJECT_CLASS (g_class),
- *                                        PROP_GtkGadget_LAST,
- *                                        (GParamSpec **) gtk_gadget_properties);
- *   }
- * ]|
- *
- * This macro should be used alongside G_DEFINE_PROPERTIES().
- *
- * Since: 2.38
- */
-#define G_DECLARE_PROPERTIES(T_N, t_n, _C_) \
-enum { \
-  PROP_##T_N##_0, \
-  _C_ \
-  PROP_##T_N##_LAST \
-}; \
-\
-static GProperty *t_n##_properties[PROP_##T_N##_LAST] = { NULL, }; \
-\
-static inline GParamSpec * \
-t_n##_find_property (T_N        *self, \
-                     const char *name) \
-{ \
-  const char *iname = g_intern_string (name); \
-  gint i; \
-\
-  for (i = PROP_##T_N##_0 + 1; i < PROP_##T_N##_LAST; i++) \
-    { \
-      GParamSpec *pspec = (GParamSpec *) t_n##_properties[i]; \
-      if (pspec != NULL && pspec->name == iname) \
-        return pspec; \
-    } \
-\
-  return g_object_class_find_property (G_OBJECT_GET_CLASS (self), name); \
-} \
-\
-static inline void \
-t_n##_notify_property (T_N        *self, \
-                       const char *name) \
-{ \
-  GParamSpec *pspec = t_n##_find_property (self, name); \
-\
-  if (G_LIKELY (pspec != NULL)) \
-    g_object_notify_by_pspec (G_OBJECT (self), pspec); \
-} \
-\
-static inline void \
-t_n##_class_install_properties (T_N##Class *g_class) \
-{ \
-  g_object_class_install_properties (G_OBJECT_CLASS (g_class), \
-                                     PROP_##T_N##_LAST, \
-                                     (GParamSpec **) t_n##_properties); \
-}
-
-/**
  * G_DEFINE_PROPERTIES:
  * @T_N: the name of the type, in CamelCase
  * @t_n: the name of the type, in lower case, with spaces replaced by underscores
@@ -623,15 +501,24 @@ t_n##_class_install_properties (T_N##Class *g_class) \
  *
  * Defines a list of properties and installs them on the class.
  *
- * This macro should be used alongside the G_DECLARE_PROPERTIES() macro.
+ * Note that this macro can only be used with types defined using
+ * G_DEFINE_TYPE_* macros, as it depends on variables defined by
+ * those macros.
  *
  * Since: 2.38
  */
 #define G_DEFINE_PROPERTIES(T_N, t_n, g_class, _C_) \
   { \
+    GPtrArray *g_define_properties = g_ptr_array_new (); \
+    g_ptr_array_add (g_define_properties, NULL); \
+\
     _C_ \
 \
-    t_n##_class_install_properties (g_class); \
+    t_n##_properties_len = g_define_properties->len; \
+    t_n##_properties = (GParamSpec **) g_ptr_array_free (g_define_properties, FALSE); \
+    g_object_class_install_properties (G_OBJECT_CLASS (g_class), \
+                                       t_n##_properties_len, \
+                                       t_n##_properties); \
   }
 
 #define _G_DEFINE_DIRECT_PROPERTY_EXTENDED_BEGIN(T_N, t_n, c_type, name, flags) \
@@ -649,7 +536,7 @@ t_n##_class_install_properties (T_N##Class *g_class) \
                                setterFunc, \
                                getterFunc);
 #define _G_DEFINE_PROPERTY_EXTENDED_END(T_N, t_n, name) \
-  t_n##_properties[PROP_##T_N##_##name] = g_property; \
+  g_ptr_array_add (g_define_properties, g_property); \
 }
 
 /**
@@ -690,9 +577,8 @@ t_n##_class_install_properties (T_N##Class *g_class) \
  *   }
  * ]|
  *
- * This macro should only be used with G_DEFINE_PROPERTIES() and
- * G_DECLARE_PROPERTIES(), as it depends on variable names defined
- * by those two macros.
+ * This macro should only be used with G_DEFINE_PROPERTIES() as it depends
+ * on variables and functions defined by that macro.
  *
  * Since: 2.38
  */
@@ -823,7 +709,19 @@ t_n##_class_install_properties (T_N##Class *g_class) \
   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE_TYPE (self, t_n##_get_type ()), (f_t) 0); \
 \
   { \
-    g_property = (GProperty *) t_n##_find_property (self, #f_n); \
+    const char *pname = g_property_canonicalize_name (#f_n); \
+    int i; \
+\
+    for (i = 1; i < t_n##_properties_len; i++) \
+      { \
+        GParamSpec *pspec = t_n##_properties[i]; \
+        if (pspec != NULL && pspec->name == pname) \
+          { \
+            g_property = (GProperty *) pspec; \
+            break; \
+          } \
+      } \
+\
     if (G_UNLIKELY (g_property == NULL)) \
       { \
         g_critical (G_STRLOC ": No property " #f_n " found for class %s", \
@@ -854,7 +752,19 @@ t_n##_class_install_properties (T_N##Class *g_class) \
   g_return_if_fail (G_TYPE_CHECK_INSTANCE_TYPE (self, t_n##_get_type ())); \
 \
   { \
-    g_property = (GProperty *) t_n##_find_property (self, #f_n); \
+    const char *pname = g_property_canonicalize_name (#f_n); \
+    int i; \
+\
+    for (i = 1; i < t_n##_properties_len; i++) \
+      { \
+        GParamSpec *pspec = t_n##_properties[i]; \
+        if (pspec != NULL && pspec->name == pname) \
+          { \
+            g_property = (GProperty *) pspec; \
+            break; \
+          } \
+      } \
+\
     if (G_UNLIKELY (g_property == NULL)) \
       { \
         g_critical (G_STRLOC ": No property " #f_n " found for class %s", G_OBJECT_TYPE_NAME (self)); \
@@ -946,8 +856,7 @@ _G_DECLARE_PROPERTY_GETTER (T_n, t_n, f_t, f_n);
  * updated to a new value.
  *
  * Note that this macro should be used with types defined using G_DEFINE_TYPE_*
- * macros and properties declared using the G_DECLARE_PROPERTIES() macro, as it
- * depends on variables and functions defined by those macros.
+ * macros, as it depends on variables and functions defined by those macros.
  *
  * Since: 2.38
  */
@@ -980,8 +889,7 @@ _G_DEFINE_PROPERTY_SETTER_END
  * This macro should only be used in C source files.
  *
  * Note that this macro should be used with types defined using G_DEFINE_TYPE_*
- * macros and properties declared using the G_DECLARE_PROPERTIES() macro, as it
- * depends on variables and functions defined by those macros.
+ * macros, as it depends on variables and functions defined by those macros.
  *
  * Since: 2.38
  */
@@ -1009,8 +917,7 @@ _G_DEFINE_PROPERTY_GETTER_END
  * This macro should only be used in C source files.
  *
  * Note that this macro should be used with types defined using G_DEFINE_TYPE_*
- * macros and properties declared using the G_DECLARE_PROPERTIES() macro, as it
- * depends on variables and functions defined by those macros.
+ * macros, as it depends on variables and functions defined by those macros.
  *
  * Since: 2.38
  */
@@ -1033,8 +940,7 @@ _G_DEFINE_PROPERTY_GETTER_END
  * class @TypeName. This macro should only be used in C source files.
  *
  * Note that this macro should be used with types defined using G_DEFINE_TYPE_*
- * macros and properties declared using the G_DECLARE_PROPERTIES() macro, as it
- * depends on variables and functions defined by those macros.
+ * macros, as it depends on variables and functions defined by those macros.
  *
  * See also: %G_DEFINE_PROPERTY_SET_WITH_CODE
  *
@@ -1055,8 +961,7 @@ _G_DEFINE_PROPERTY_GETTER_END
  * class @TypeName. This macro should only be used in C source files.
  *
  * Note that this macro should be used with types defined using G_DEFINE_TYPE_*
- * macros and properties declared using the G_DECLARE_PROPERTIES() macro, as it
- * depends on variables and functions defined by those macros.
+ * macros, as it depends on variables and functions defined by those macros.
  *
  * See also %G_DEFINE_PROPERTY_GET_WITH_CODE.
  *
@@ -1077,8 +982,7 @@ _G_DEFINE_PROPERTY_GETTER_END
  * class @TypeName. This macro should only be used in C source files.
  *
  * Note that this macro should be used with types defined using G_DEFINE_TYPE_*
- * macros and properties declared using the G_DECLARE_PROPERTIES() macro, as it
- * depends on variables and functions defined by those macros.
+ * macros, as it depends on variables and functions defined by those macros.
  *
  * See also %G_DEFINE_PROPERTY_COMPUTED_GET_WITH_CODE.
  *
@@ -1147,8 +1051,8 @@ _G_DEFINE_PROPERTY_GETTER_END
  * variants.
  *
  * Note that this macro should only be used with types defined using
- * G_DEFINE_TYPE_* macros and properties declared using G_DECLARE_PROPERTIES()
- * macro, as it depends on variable and functions defined by those macros.
+ * G_DEFINE_TYPE_* macros, as it depends on variable and functions defined
+ * by those macros.
  *
  * Since: 2.38
  */
diff --git a/gobject/gtype.h b/gobject/gtype.h
index d06af03..78dd6a4 100644
--- a/gobject/gtype.h
+++ b/gobject/gtype.h
@@ -1601,11 +1601,13 @@ guint     g_type_get_type_registration_serial (void);
 
 #define _G_DEFINE_TYPE_EXTENDED_BEGIN(TypeName, type_name, TYPE_PARENT, flags) \
 \
-static void     type_name##_init              (TypeName        *self); \
-static void     type_name##_class_init        (TypeName##Class *klass); \
-static gpointer type_name##_parent_class = NULL; \
-static gint     TypeName##_private_offset; \
-static void     type_name##_class_intern_init (gpointer klass) \
+static void         type_name##_init              (TypeName        *self); \
+static void         type_name##_class_init        (TypeName##Class *klass); \
+static gpointer     type_name##_parent_class = NULL; \
+static gint         TypeName##_private_offset; \
+static GParamSpec **type_name##_properties G_GNUC_UNUSED; \
+static guint        type_name##_properties_len G_GNUC_UNUSED; \
+static void         type_name##_class_intern_init (gpointer klass) \
 { \
   type_name##_parent_class = g_type_class_peek_parent (klass); \
   if (TypeName##_private_offset != 0) \
diff --git a/gobject/tests/property.c b/gobject/tests/property.c
index 048f3fc..b63e77b 100644
--- a/gobject/tests/property.c
+++ b/gobject/tests/property.c
@@ -68,17 +68,6 @@ test_enum_get_type (void)
 
 G_DEFINE_TYPE_WITH_PRIVATE (TestObject, test_object, G_TYPE_OBJECT)
 
-G_DECLARE_PROPERTIES (TestObject, test_object,
-                      G_DECLARE_PROPERTY (TestObject, integer_val)
-                      G_DECLARE_PROPERTY (TestObject, double_val)
-                      G_DECLARE_PROPERTY (TestObject, string_val)
-                      G_DECLARE_PROPERTY (TestObject, bool_val)
-                      G_DECLARE_PROPERTY (TestObject, enum_val)
-                      G_DECLARE_PROPERTY (TestObject, enum_val_set)
-                      G_DECLARE_PROPERTY (TestObject, with_default)
-                      G_DECLARE_PROPERTY (TestObject, width)
-                      G_DECLARE_PROPERTY (TestObject, height))
-
 static void
 test_object_finalize (GObject *gobject)
 {
@@ -180,7 +169,7 @@ G_DEFINE_PROPERTY_GET_SET (TestObject, test_object, gboolean, bool_val)
 G_DEFINE_PROPERTY_GET_SET (TestObject, test_object, float, width)
 G_DEFINE_PROPERTY_GET_SET (TestObject, test_object, float, height)
 G_DEFINE_PROPERTY_GET_SET (TestObject, test_object, TestEnum, enum_val)
-G_DEFINE_PROPERTY_GET (TestObject, test_object, gboolean, enum_val_set)
+G_DEFINE_PROPERTY_INDIRECT_GET (TestObject, test_object, gboolean, enum_val_set)
 
 /* test units start here */
 


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