[glib/wip/gproperty-2: 7/7] gproperty: Add macros for defining properties



commit 5631c2641b4760821d289b42192b7beb3f7ec67f
Author: Emmanuele Bassi <ebassi gnome org>
Date:   Wed Apr 24 17:41:15 2013 -0400

    gproperty: Add macros for defining properties
    
    Alongside the macros for defining accessors we may want to use simple
    macros for defining properties to install on a GObject class.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=648526

 gobject/gproperty.h      |  408 ++++++++++++++++++++++++++++++++++++++++++----
 gobject/tests/property.c |  133 +++++++--------
 2 files changed, 436 insertions(+), 105 deletions(-)
---
diff --git a/gobject/gproperty.h b/gobject/gproperty.h
index fafdc06..0ba4c7d 100644
--- a/gobject/gproperty.h
+++ b/gobject/gproperty.h
@@ -431,6 +431,341 @@ void            g_property_set_installed       (GProperty *property,
 void            g_property_init_default        (GProperty *property,
                                                 gpointer   object);
 
+/* property generation */
+
+/**
+ * G_PROPERTY_DESCRIBE:
+ * @p_nick: a human readable, translatable name for the property
+ * @p_blurb: a human readable, translatable description for the property
+ *
+ * Sets the property nick and blurb using two static strings.
+ *
+ * This macro can only be called inside %G_DEFINE_PROPERTY_EXTENDED.
+ *
+ * Since: 2.38
+ */
+#define G_PROPERTY_DESCRIBE(p_nick, p_blurb) \
+  { \
+    GParamSpec *__g_param_spec = (GParamSpec *) g_property; \
+    g_param_spec_set_static_nick (__g_param_spec, p_nick); \
+    g_param_spec_set_static_blurb (__g_param_spec, p_blurb); \
+  }
+
+/**
+ * G_PROPERTY_DEFAULT:
+ * @p_val: the default value of the property
+ *
+ * Sets the default value for the property.
+ *
+ * This macro can only be called inside %G_DEFINE_PROPERTY_EXTENDED.
+ *
+ * Since: 2.38
+ */
+#define G_PROPERTY_DEFAULT(p_val) \
+  g_property_set_default (g_property, p_val);
+
+/**
+ * G_PROPERTY_RANGE:
+ * @p_min: the minimum value of the valid range for the property
+ * @p_max: the maximum value of the valid range for the property
+ *
+ * Sets the range of valid values for the property.
+ *
+ * This macro can only be called inside %G_DEFINE_PROPERTY_EXTENDED.
+ *
+ * Since: 2.38
+ */
+#define G_PROPERTY_RANGE(p_min, p_max) \
+  g_property_set_range (g_property, p_min, p_max);
+
+/**
+ * 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 (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 NULL;
+ *   }
+ *
+ *   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 (const char *name) \
+{ \
+  gint n_props = G_N_ELEMENTS (t_n##_properties), i; \
+  const char *iname = g_intern_string (name); \
+\
+  for (i = PROP_##T_N##_0 + 1; i < n_props; i++) \
+    { \
+      GParamSpec *pspec = (GParamSpec *) t_n##_properties[i]; \
+\
+      if (pspec != NULL && pspec->name == iname) \
+        return pspec; \
+    } \
+\
+  return NULL; \
+} \
+\
+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
+ * @g_class: a pointer to the class structure of the type
+ * @_C_: a list of G_DEFINE_PROPERTY_EXTENDED calls
+ *
+ * Defines a list of properties and installs them on the class.
+ *
+ * This macro should be used alongside the G_DECLARE_PROPERTIES() macro.
+ *
+ * Since: 2.38
+ */
+#define G_DEFINE_PROPERTIES(T_N, t_n, g_class, _C_) \
+  { \
+    _C_ \
+\
+    t_n##_class_install_properties (g_class); \
+  }
+
+#define _G_DEFINE_DIRECT_PROPERTY_EXTENDED_BEGIN(T_N, t_n, c_type, name, setterFunc, getterFunc, flags) \
+{ \
+  GProperty *g_property = (GProperty *) \
+    g_##c_type##_property_new (#name, flags, \
+                               G_STRUCT_OFFSET (T_N##Private, name), \
+                               setterFunc, \
+                               getterFunc);
+#define _G_DEFINE_PROPERTY_EXTENDED_BEGIN(T_N, t_n, c_type, name, offset, setterFunc, getterFunc, flags) \
+{ \
+  GProperty *g_property = (GProperty *) \
+    g_##c_type##_property_new (#name, flags, \
+                               offset, \
+                               setterFunc, \
+                               getterFunc);
+#define _G_DEFINE_PROPERTY_EXTENDED_END(T_N, t_n, name) \
+  t_n##_properties[PROP_##T_N##_##name] = g_property; \
+}
+
+/**
+ * G_DEFINE_PROPERTY_EXTENDED:
+ * @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_type: the C type of the property, in lower case, minus the "g" if the type
+ *   is defined by GLib; for instance "int" for "gint", "uint8" for "guint8",
+ *   "boolean" for "gboolean"; strings stored in a gchar* are defined as "string"
+ * @name: the name of the property, in lower case, with '-' replaced by '_'
+ * @offset: the offset of the field in the structure that stores the property, or 0
+ * @setterFunc: the explicit setter function, or %NULL for direct access
+ * @getterFunc: the explicit getter function, or %NULL for direct access
+ * @flags: #GPropertyFlags for the property
+ * @_C_: custom code to be called after the property has been defined; the
+ *   GProperty instance is available under the "g_property" variable
+ *
+ * The most generic property definition macro.
+ *
+ * |[
+ *   G_DEFINE_PROPERTY_EXTENDED (GtkGadget, gtk_gadget,
+ *                               int,
+ *                               width,
+ *                               G_STRUCT_OFFSET (GtkGadgetPrivate, width),
+ *                               NULL, NULL,
+ *                               G_PROPERTY_READWRITE,
+ *                               G_PROPERTY_RANGE (0, G_MAXINT))
+ * ]|
+ * expands to
+ * |[
+ *   {
+ *     GProperty *g_property =
+ *       g_int_property_new ("width", G_PROPERTY_READWRITE,
+ *                           G_STRUCT_OFFSET (GtkGadgetPrivate, width),
+ *                           NULL, NULL);
+ *     g_property_set_range (g_property, 0, G_MAXINT);
+ *     gtk_gadget_properties[PROP_GtkGadget_width] = g_property;
+ *   }
+ * ]|
+ *
+ * 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.
+ *
+ * Since: 2.38
+ */
+#define G_DEFINE_PROPERTY_EXTENDED(T_N, t_n, c_type, name, offset, setterFunc, getterFunc, flags, _C_) \
+  _G_DEFINE_PROPERTY_EXTENDED_BEGIN (T_N, t_n, c_type, name, offset, setterFunc, getterFunc, flags) \
+  { _C_; } \
+  _G_DEFINE_PROPERTY_EXTENDED_END (T_N, t_n, name)
+
+/**
+ * G_DEFINE_PROPERTY_WITH_CODE:
+ * @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_type: the C type of the property, in lower case, minus the "g" if the type
+ *   is defined by GLib; for instance "int" for "gint", "uint8" for "guint8",
+ *   "boolean" for "gboolean"; strings stored in a gchar* are defined as "string"
+ * @name: the name of the property, in lower case, with '-' replaced by '_'; the
+ *   name of the property must exist as a member of the per-instance private data
+ *   structure of the type name
+ * @flags: #GPropertyFlags for the property
+ * @_C_: custom code to be called after the property has been defined; the
+ *   GProperty instance is available under the "g_property" variable
+ *
+ * A variant of G_DEFINE_PROPERTY_EXTENDED() that only allows properties
+ * with direct access, stored on the per-instance private data structure
+ * for the given type.
+ *
+ * Since: 2.38
+ */
+#define G_DEFINE_PROPERTY_WITH_CODE(T_N, t_n, c_type, name, flags, _C_) \
+  _G_DEFINE_DIRECT_PROPERTY_EXTENDED_BEGIN (T_N, t_n, c_type, name, NULL, NULL, flags) \
+  { _C_; } \
+  _G_DEFINE_PROPERTY_EXTENDED_END (T_N, t_n, name)
+
+/**
+ * G_DEFINE_PROPERTY_WITH_DEFAULT:
+ * @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_type: the C type of the property, in lower case, minus the "g" if the type
+ *   is defined by GLib; for instance "int" for "gint", "uint8" for "guint8",
+ *   "boolean" for "gboolean"; strings stored in a gchar* are defined as "string"
+ * @name: the name of the property, in lower case, with '-' replaced by '_'; the
+ *   name of the property must exist as a member of the per-instance private data
+ *   structure of the type name
+ * @flags: #GPropertyFlags for the property
+ * @defVal: the default value of the property
+ *
+ * A convenience macro for defining a direct access property with a default
+ * value.
+ *
+ * See G_DEFINE_PROPERTY_WITH_CODE() and G_PROPERTY_DEFAULT().
+ *
+ * Since: 2.38
+ */
+#define G_DEFINE_PROPERTY_WITH_DEFAULT(T_N, t_n, c_type, name, flags, defVal) \
+  _G_DEFINE_DIRECT_PROPERTY_EXTENDED_BEGIN (T_N, t_n, c_type, name, NULL, NULL, flags) \
+  G_PROPERTY_DEFAULT (defVal) \
+  _G_DEFINE_PROPERTY_EXTENDED_END (T_N, t_n, name)
+
+/**
+ * G_DEFINE_PROPERTY_WITH_RANGE:
+ * @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_type: the C type of the property, in lower case, minus the "g" if the type
+ *   is defined by GLib; for instance "int" for "gint", "uint8" for "guint8",
+ *   "boolean" for "gboolean"; strings stored in a gchar* are defined as "string"
+ * @name: the name of the property, in lower case, with '-' replaced by '_'; the
+ *   name of the property must exist as a member of the per-instance private data
+ *   structure of the type name
+ * @flags: #GPropertyFlags for the property
+ * @minVal: the minimum value of the property
+ * @maxVal: the maximum value of the property
+ *
+ * A convenience macro for defining a direct access property with a range
+ * of valid values.
+ *
+ * See G_DEFINE_PROPERTY_WITH_CODE() and G_PROPERTY_RANGE().
+ *
+ * Since: 2.38
+ */
+#define G_DEFINE_PROPERTY_WITH_RANGE(T_N, t_n, c_type, name, flags, minVal, maxVal) \
+  _G_DEFINE_DIRECT_PROPERTY_EXTENDED_BEGIN (T_N, t_n, c_type, name, NULL, NULL, flags) \
+  G_PROPERTY_RANGE (minVal, maxVal) \
+  _G_DEFINE_PROPERTY_EXTENDED_END (T_N, t_n, name)
+
+/**
+ * G_DEFINE_PROPERTY:
+ * @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_type: the C type of the property, in lower case, minus the "g" if the type
+ *   is defined by GLib; for instance "int" for "gint", "uint8" for "guint8",
+ *   "boolean" for "gboolean"; strings stored in a gchar* are defined as "string"
+ * @name: the name of the property, in lower case, with '-' replaced by '_'; the
+ *   name of the property must exist as a member of the per-instance private data
+ *   structure of the type name
+ * @flags: #GPropertyFlags for the property
+ * @minVal: the minimum value of the property
+ * @maxVal: the maximum value of the property
+ *
+ * A convenience macro for defining a direct access property.
+ *
+ * See G_DEFINE_PROPERTY_WITH_CODE() and G_DEFINE_PROPERTY_EXTENDED() if
+ * you need more flexibility.
+ *
+ * Since: 2.38
+ */
+#define G_DEFINE_PROPERTY(T_N, t_n, c_type, name, flags) \
+  _G_DEFINE_DIRECT_PROPERTY_EXTENDED_BEGIN (T_N, t_n, c_type, name, NULL, NULL, flags) \
+  _G_DEFINE_PROPERTY_EXTENDED_END (T_N, t_n, name)
+
 /* accessors generation */
 #define _G_DECLARE_PROPERTY_GETTER(T_n, t_n, f_t, f_n)  f_t t_n##_get_##f_n (T_n *self)
 
@@ -441,7 +776,7 @@ void            g_property_init_default        (GProperty *property,
 \
   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE_TYPE (self, t_n##_get_type ()), (f_t) 0); \
 \
-  priv = g_type_instance_get_private ((GTypeInstance *) self, t_n##_get_type ()); \
+  priv = t_n##_get_private (self); \
   retval = priv->f_n; \
 \
   { /* custom code follows */
@@ -465,18 +800,6 @@ void            g_property_init_default        (GProperty *property,
       } \
   } \
 \
-  if (!G_IS_PROPERTY (g_property)) \
-    { \
-      g_critical (G_STRLOC ": Property " #f_n " is not a GProperty"); \
-      return (f_t) 0; \
-    } \
-\
-  if (!g_property_is_readable (g_property)) \
-    { \
-       g_critical (G_STRLOC ": The property " #f_n " is not readable"); \
-       return (f_t) 0; \
-    } \
-\
   if (!g_property_get (g_property, self, &retval)) \
     { \
       g_property_get_default (g_property, self, &retval); \
@@ -509,18 +832,6 @@ void            g_property_init_default        (GProperty *property,
       } \
   } \
 \
-  if (!G_IS_PROPERTY (g_property)) \
-    { \
-      g_critical (G_STRLOC ": Property " #f_n " is not a GProperty"); \
-      return; \
-    } \
-\
-  if (!g_property_is_writable (g_property)) \
-    { \
-       g_critical (G_STRLOC ": The property " #f_n " is not writable"); \
-       return; \
-    } \
-\
   if (!g_property_set (g_property, self, value)) \
     return; \
 \
@@ -588,9 +899,8 @@ _G_DECLARE_PROPERTY_GETTER (T_n, t_n, f_t, f_n);
  * @_C_: C code that should be called after the property has been set
  *
  * Defines the setter function for a @field_name property in the
- * class @TypeName, with the possibility of calling custom code.
- *
- * This macro should only be used in C source files.
+ * class @TypeName, with the possibility of calling custom code, for
+ * instance:
  *
  * |[
  * G_DEFINE_PROPERTY_SET_WITH_CODE (ClutterActor, clutter_actor,
@@ -631,10 +941,16 @@ _G_DEFINE_PROPERTY_SETTER_END
  * class @T_n, with the possibility of calling custom code.
  *
  * This macro will directly access the field on the private
- * data structure.
+ * data structure, and should only be used if the property
+ * has been defined to use an offset instead of an explicit
+ * getter. Use G_DEFINE_PROPERTY_COMPUTED_GET_WITH_CODE() if
+ * you have an internal getter function.
  *
  * 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, as it depends on variables and functions defined by those macros.
+ *
  * Since: 2.38
  */
 #define G_DEFINE_PROPERTY_GET_WITH_CODE(T_n, t_n, f_t, f_n, _C_) \
@@ -660,6 +976,9 @@ _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, as it depends on variables and functions defined by those macros.
+ *
  * Since: 2.38
  */
 #define G_DEFINE_PROPERTY_INDIRECT_GET_WITH_CODE(T_n, t_n, f_t, f_n, _C_) \
@@ -680,6 +999,9 @@ _G_DEFINE_PROPERTY_GETTER_END
  * Defines the setter function for a @field_name property in the
  * 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, as it depends on variables and functions defined by those macros.
+ *
  * See also: %G_DEFINE_PROPERTY_SET_WITH_CODE
  *
  * Since: 2.38
@@ -698,6 +1020,9 @@ _G_DEFINE_PROPERTY_GETTER_END
  * Defines the getter function for a @field_name property in the
  * 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, as it depends on variables and functions defined by those macros.
+ *
  * See also %G_DEFINE_PROPERTY_GET_WITH_CODE.
  *
  * Since: 2.38
@@ -716,6 +1041,9 @@ _G_DEFINE_PROPERTY_GETTER_END
  * Defines the getter function for a @field_name property in the
  * 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, as it depends on variables and functions defined by those macros.
+ *
  * See also %G_DEFINE_PROPERTY_COMPUTED_GET_WITH_CODE.
  *
  * Since: 2.38
@@ -750,22 +1078,26 @@ _G_DEFINE_PROPERTY_GETTER_END
  *
  *   g_return_if_fail (G_TYPE_CHECK_INSTANCE_TYPE (self, clutter_actor_get_type ()));
  *
- *   priv = self->priv;
- *
- *   if (priv->margin_top == value)
- *     return;
+ *   priv = clutter_actor_get_private (self);
  *
- *   priv->value = value;
+ *   if (priv->margin_top != value)
+ *     {
+ *       priv->value = value;
  *
- *   g_object_notify (G_OBJECT (self), "margin-top");
+ *       g_object_notify (G_OBJECT (self), "margin-top");
+ *     }
  * }
  *
  * int
  * clutter_actor_get_margin_top (ClutterActor *self)
  * {
+ *   ClutterActorPrivate *priv;
+ *
  *   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE_TYPE (self, clutter_actor_get_type ()), 0);
  *
- *   return self->priv->margin_top;
+ *   priv = clutter_actor_get_private (self);
+ *
+ *   return priv->margin_top;
  * }
  * ]|
  *
@@ -778,6 +1110,10 @@ _G_DEFINE_PROPERTY_GETTER_END
  * %G_DEFINE_PROPERTY_GET_WITH_CODE and %G_DEFINE_PROPERTY_SET_WITH_CODE
  * variants.
  *
+ * Note that this macro should only be used with types defined using
+ * G_DEFINE_TYPE_* macros, as it depends on variable and functions defined
+ * by those macros.
+ *
  * Since: 2.38
  */
 #define G_DEFINE_PROPERTY_GET_SET(T_n, t_n, f_t, f_n)      \
diff --git a/gobject/tests/property.c b/gobject/tests/property.c
index ccb534a..3a5ec2f 100644
--- a/gobject/tests/property.c
+++ b/gobject/tests/property.c
@@ -27,7 +27,7 @@ struct _TestObjectPrivate
 
   double double_val;
 
-  char *str_val;
+  char *string_val;
 
   gboolean bool_val;
 
@@ -35,6 +35,9 @@ struct _TestObjectPrivate
   guint enum_val_set : 1;
 
   guint8 with_default;
+
+  float width;
+  float height;
 };
 
 GType test_enum_get_type (void);
@@ -64,28 +67,22 @@ test_enum_get_type (void)
 
 G_DEFINE_TYPE_WITH_PRIVATE (TestObject, test_object, G_TYPE_OBJECT)
 
-enum
-{
-  PROP_0,
-
-  PROP_INTEGER_VAL,
-  PROP_DOUBLE_VAL,
-  PROP_STRING_VAL,
-  PROP_BOOL_VAL,
-  PROP_ENUM_VAL,
-  PROP_WITH_DEFAULT,
-
-  LAST_PROP
-};
-
-static GParamSpec *test_object_properties[LAST_PROP] = { NULL, };
+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, with_default)
+                      G_DECLARE_PROPERTY (TestObject, width)
+                      G_DECLARE_PROPERTY (TestObject, height))
 
 static void
 test_object_finalize (GObject *gobject)
 {
   TestObjectPrivate *priv = test_object_get_private ((TestObject *) gobject);
 
-  g_free (priv->str_val);
+  g_free (priv->string_val);
 
   if (priv->enum_val != TEST_ENUM_UNSET)
     g_assert (priv->enum_val_set);
@@ -115,50 +112,47 @@ test_object_class_init (TestObjectClass *klass)
 
   gobject_class->finalize = test_object_finalize;
 
-  test_object_properties[PROP_INTEGER_VAL] =
-    g_int_property_new ("integer-val",
-                        G_PROPERTY_READWRITE,
-                        G_STRUCT_OFFSET (TestObjectPrivate, integer_val),
-                        NULL, NULL);
-
-  test_object_properties[PROP_DOUBLE_VAL] =
-    g_double_property_new ("double-val",
-                           G_PROPERTY_READWRITE,
-                           G_STRUCT_OFFSET (TestObjectPrivate, double_val),
-                           NULL, NULL);
-
-  test_object_properties[PROP_STRING_VAL] =
-    g_string_property_new ("string-val",
-                           G_PROPERTY_READWRITE | G_PROPERTY_COPY_SET,
-                           G_STRUCT_OFFSET (TestObjectPrivate, str_val),
-                           NULL, NULL);
-
-  test_object_properties[PROP_BOOL_VAL] =
-    g_boolean_property_new ("bool-val",
-                            G_PROPERTY_READWRITE,
-                            G_STRUCT_OFFSET (TestObjectPrivate, bool_val),
-                            NULL, NULL);
-
-  test_object_properties[PROP_ENUM_VAL] =
-    g_enum_property_new ("enum-val",
-                         G_PROPERTY_READWRITE,
-                         G_STRUCT_OFFSET (TestObjectPrivate, enum_val),
-                         test_object_set_enum_val,
-                         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",
-                          G_PROPERTY_READWRITE,
-                          G_STRUCT_OFFSET (TestObjectPrivate, with_default),
-                          NULL,
-                          NULL);
-  g_property_set_default ((GProperty *) test_object_properties[PROP_WITH_DEFAULT], 255);
-
-  g_object_class_install_properties (gobject_class, LAST_PROP, test_object_properties);
+  G_DEFINE_PROPERTIES (TestObject, test_object, klass,
+                       G_DEFINE_PROPERTY (TestObject, test_object,
+                                          int,
+                                          integer_val,
+                                          G_PROPERTY_READWRITE)
+                       G_DEFINE_PROPERTY (TestObject, test_object,
+                                          double,
+                                          double_val,
+                                          G_PROPERTY_READWRITE)
+                       G_DEFINE_PROPERTY (TestObject, test_object,
+                                          string,
+                                          string_val,
+                                          G_PROPERTY_READWRITE | G_PROPERTY_COPY_SET)
+                       G_DEFINE_PROPERTY (TestObject, test_object,
+                                          boolean,
+                                          bool_val,
+                                          G_PROPERTY_READWRITE)
+                       G_DEFINE_PROPERTY (TestObject, test_object,
+                                          float,
+                                          width,
+                                          G_PROPERTY_READWRITE)
+                       G_DEFINE_PROPERTY (TestObject, test_object,
+                                          float,
+                                          height,
+                                          G_PROPERTY_READWRITE)
+                       G_DEFINE_PROPERTY_EXTENDED (TestObject, test_object,
+                                                   enum,
+                                                   enum_val,
+                                                   G_STRUCT_OFFSET (TestObjectPrivate, enum_val),
+                                                   test_object_set_enum_val,
+                                                   NULL,
+                                                   G_PROPERTY_READWRITE,
+                                                   G_PROPERTY_DEFAULT (TEST_ENUM_UNSET)
+                                                   g_property_set_prerequisite (g_property, 
test_enum_get_type ());)
+                       G_DEFINE_PROPERTY_WITH_CODE (TestObject, test_object,
+                                                    uint8,
+                                                    with_default,
+                                                    G_PROPERTY_READWRITE,
+                                                    G_PROPERTY_DEFAULT (255)
+                                                    G_PROPERTY_DESCRIBE ("With Default",
+                                                                         "A property with a default value")))
 }
 
 static void
@@ -167,8 +161,12 @@ test_object_init (TestObject *self)
 }
 
 G_DECLARE_PROPERTY_GET_SET (TestObject, test_object, gboolean, bool_val)
+G_DECLARE_PROPERTY_GET_SET (TestObject, test_object, float, width)
+G_DECLARE_PROPERTY_GET_SET (TestObject, test_object, float, height)
 
 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)
 
 /* test units start here */
 
@@ -191,12 +189,11 @@ gproperty_construct (void)
                                   "double-val", 3.14159,
                                   NULL);
 
-  TestObjectPrivate *priv =
-    g_type_instance_get_private ((GTypeInstance *) obj, test_object_get_type ());
+  TestObjectPrivate *priv = test_object_get_private (obj);
 
   g_assert_cmpint (priv->integer_val, ==, 42);
   g_assert (priv->bool_val);
-  g_assert_cmpstr (priv->str_val, ==, "Hello, world");
+  g_assert_cmpstr (priv->string_val, ==, "Hello, world");
   g_assert_cmpfloat ((float) priv->double_val, ==, 3.14159f);
 
   g_object_unref (obj);
@@ -206,23 +203,21 @@ static void
 gproperty_object_set (void)
 {
   TestObject *obj = g_object_new (test_object_get_type (), NULL);
-
-  TestObjectPrivate *priv =
-    g_type_instance_get_private ((GTypeInstance *) obj, test_object_get_type ());
+  TestObjectPrivate *priv = test_object_get_private (obj);
 
   gboolean did_emit_notify = FALSE;
 
   g_signal_connect (obj, "notify::string-val", G_CALLBACK (check_notify_emission), &did_emit_notify);
 
   g_object_set (obj, "string-val", "Hello!", NULL);
-  g_assert_cmpstr (priv->str_val, ==, "Hello!");
+  g_assert_cmpstr (priv->string_val, ==, "Hello!");
 
   g_assert (did_emit_notify);
 
   did_emit_notify = FALSE;
 
   g_object_set (obj, "string-val", "Hello!", NULL);
-  g_assert_cmpstr (priv->str_val, ==, "Hello!");
+  g_assert_cmpstr (priv->string_val, ==, "Hello!");
 
   g_assert (!did_emit_notify);
 


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