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



commit 6661d8d1ab25b9c259ef7306506d101df09ccf00
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.

 gobject/gproperty.h      |   88 ++++++++++++++++++++++++++++++++++++++++++++++
 gobject/tests/property.c |   29 +++++++++++----
 2 files changed, 110 insertions(+), 7 deletions(-)
---
diff --git a/gobject/gproperty.h b/gobject/gproperty.h
index f8ee156..9359eeb 100644
--- a/gobject/gproperty.h
+++ b/gobject/gproperty.h
@@ -455,6 +455,94 @@ void            _g_property_set_installed       (GProperty           *property,
                                                  gpointer             g_class,
                                                  GType                class_gtype);
 
+/* 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_DEFINE_PROPERTY_EXTENDED:
+ * @T_n: the type name of the class installing the property, in camel case
+ * @p_type: the type of the property
+ * @p_name: the name of the field in the private structure that stores the property
+ * @p_flags: #GPropertyFlags for the property
+ * @p_setter: the optional setter function, or %NULL for direct setters
+ * @p_getter: the optional getter function, or %NULL for direct getters
+ * @_C_: custom code to be inserted after the property has been defined; the
+ *   #GProperty instance is stored in a variabled called g_property
+ *
+ * Defines a new #GProperty using the provided arguments.
+ *
+ * Since: 2.38
+ */
+#define G_DEFINE_PROPERTY_EXTENDED(T_n, p_type, p_name, p_flags, p_setter, p_getter, _C_) \
+  { \
+    GProperty *g_property; \
+\
+    g_property = (GProperty *) \
+      g_ ##p_type## _property_new (#p_name, p_flags, \
+                                   G_STRUCT_OFFSET (T_n ## Private, p_name), \
+                                   p_setter, p_getter); \
+\
+    /* custom code follows */ { _C_; } \
+  }
+
+/**
+ * G_DEFINE_PROPERTY:
+ * @T_n: the type of the class installing the property, in camel case
+ * @p_type: the type of the property
+ * @p_name: the name of the field in the @T_n private structure that
+ *   stores the property
+ * @p_flags: #GPropertyFlags
+ *
+ * Defines a new #GProperty.
+ *
+ * Since: 2.38
+ */
+#define G_DEFINE_PROPERTY(T_n, p_type, p_name, p_flags) \
+  g_ ##p_type## _property_new (#p_name, p_flags, G_STRUCT_OFFSET (T_n ## Private, p_name), NULL, NULL)
 
 /* 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)
diff --git a/gobject/tests/property.c b/gobject/tests/property.c
index 7ef330f..79e8a56 100644
--- a/gobject/tests/property.c
+++ b/gobject/tests/property.c
@@ -35,6 +35,9 @@ struct _TestObjectPrivate
   guint enum_val_set : 1;
 
   guint8 with_default;
+
+  float width;
+  float height;
 };
 
 GType test_enum_get_type (void);
@@ -74,6 +77,8 @@ enum
   PROP_BOOL_VAL,
   PROP_ENUM_VAL,
   PROP_WITH_DEFAULT,
+  PROP_WIDTH,
+  PROP_HEIGHT,
 
   LAST_PROP
 };
@@ -152,13 +157,19 @@ test_object_class_init (TestObjectClass *klass)
   g_property_set_prerequisite ((GProperty *) test_object_properties[PROP_ENUM_VAL],
                                test_enum_get_type ());
 
-  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_DEFINE_PROPERTY_EXTENDED (TestObject,
+                              uint8, with_default,
+                              G_PROPERTY_READWRITE,
+                              NULL, NULL,
+                              G_PROPERTY_DEFAULT (255)
+                              G_PROPERTY_DESCRIBE ("With Default", "A property with a default value")
+                              test_object_properties[PROP_WITH_DEFAULT] = G_PARAM_SPEC (g_property); );
+
+  test_object_properties[PROP_WIDTH] =
+    G_DEFINE_PROPERTY (TestObject, float, width, G_PROPERTY_READWRITE);
+
+  test_object_properties[PROP_HEIGHT] =
+    G_DEFINE_PROPERTY (TestObject, float, height, G_PROPERTY_READWRITE);
 
   g_object_class_install_properties (gobject_class, LAST_PROP, test_object_properties);
 }
@@ -175,8 +186,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 */
 


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