[glib/wip/gproperty: 1093/1118] gobject: Add override_property_default()
- From: Emmanuele Bassi <ebassi src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [glib/wip/gproperty: 1093/1118] gobject: Add override_property_default()
- Date: Fri, 19 Oct 2012 16:59:07 +0000 (UTC)
commit 40c81398c8a07ae10df839a3f7621604d3de74e8
Author: Emmanuele Bassi <ebassi linux intel com>
Date: Tue May 17 17:26:38 2011 +0100
gobject: Add override_property_default()
For object properties defined using the GProperty API we can easily
override the default value from sub-classes; GObject should have a
convenience method that does all the automagic heavy lifting so that the
developer can just provide the property name and the new default value
inside the class initialization function.
https://bugzilla.gnome.org/show_bug.cgi?id=648526
gobject/gobject.c | 123 +++++++++++++++++++++++++++++++++++++++++++++++++++++
gobject/gobject.h | 7 +++
2 files changed, 130 insertions(+), 0 deletions(-)
---
diff --git a/gobject/gobject.c b/gobject/gobject.c
index 6ad952a..8c29458 100644
--- a/gobject/gobject.c
+++ b/gobject/gobject.c
@@ -909,6 +909,129 @@ g_object_class_list_properties (GObjectClass *class,
return pspecs;
}
+static inline void
+override_property_default_value (GObjectClass *oclass,
+ GProperty *property,
+ const GValue *value)
+{
+ g_property_set_default_value (property, oclass, value);
+}
+
+/**
+ * g_object_class_override_property_default_value:
+ * @oclass: a #GObjectClass
+ * @property_name: the name of the property to override
+ * @value: a valid #GValue containing the new default value
+ *
+ * Overrides the default value of @property_name for @oclass with
+ * a new value.
+ *
+ * See g_object_class_override_property_default() for more information.
+ *
+ * This function is meant for language bindings.
+ *
+ * Since: 2.32
+ */
+void
+g_object_class_override_property_default_value (GObjectClass *oclass,
+ const gchar *property_name,
+ const GValue *default_value)
+{
+ GParamSpec *pspec;
+
+ pspec = g_object_class_find_property (oclass, property_name);
+ if (pspec == NULL)
+ {
+ g_critical ("The class '%s' does not have a property named '%s'",
+ g_type_name (G_OBJECT_CLASS_TYPE (oclass)),
+ property_name);
+ return;
+ }
+
+ if (!G_IS_PROPERTY (pspec))
+ {
+ g_critical ("The property '%s' of class '%s' is of type '%s' and "
+ "not a GProperty. Overriding the default is allowed "
+ "only on properties defined using the GProperty API.",
+ G_PARAM_SPEC (pspec)->name,
+ g_type_name (G_OBJECT_CLASS_TYPE (oclass)),
+ g_type_name (G_PARAM_SPEC_TYPE (pspec)));
+ return;
+ }
+
+ override_property_default_value (oclass, G_PROPERTY (pspec), default_value);
+}
+
+/**
+ * g_object_class_override_property_default:
+ * @oclass: a #GObjectClass
+ * @property_name: the name of the property to override
+ * @...: the new default value of the property
+ *
+ * Sets the default value of @property_name for the given #GObject
+ * class.
+ *
+ * This function can only be used with properties installed using
+ * the #GProperty API.
+ *
+ * This function is the equivalent of:
+ *
+ * |[
+ * GParamSpec *pspec = g_object_class_find_property (oclass, property_name);
+ *
+ * g_property_set_default (G_PROPERTY (pspec), oclass, new_value);
+ * ]|
+ *
+ * Since: 2.32
+ */
+void
+g_object_class_override_property_default (GObjectClass *oclass,
+ const gchar *property_name,
+ ...)
+{
+ GParamSpec *pspec;
+ GValue value = { 0, };
+ va_list args;
+ gchar *error = NULL;
+
+ pspec = g_object_class_find_property (oclass, property_name);
+ if (pspec == NULL)
+ {
+ g_critical ("The class '%s' does not have a property named '%s'",
+ g_type_name (G_OBJECT_CLASS_TYPE (oclass)),
+ property_name);
+ return;
+ }
+
+ if (!G_IS_PROPERTY (pspec))
+ {
+ g_critical ("The property '%s' of class '%s' is of type '%s' and "
+ "not a GProperty. Overriding the default is allowed "
+ "only on properties defined using the GProperty API.",
+ G_PARAM_SPEC (pspec)->name,
+ g_type_name (G_OBJECT_CLASS_TYPE (oclass)),
+ g_type_name (G_PARAM_SPEC_TYPE (pspec)));
+ return;
+ }
+
+ va_start (args, property_name);
+
+ G_VALUE_COLLECT_INIT (&value, G_PARAM_SPEC_VALUE_TYPE (pspec), args, 0, &error);
+ if (error != NULL)
+ {
+ g_critical (G_STRLOC ": %s", error);
+ g_free (error);
+ va_end (args);
+ return;
+ }
+
+ override_property_default_value (oclass, G_PROPERTY (pspec), &value);
+
+ g_value_unset (&value);
+
+ va_end (args);
+}
+
/**
* g_object_interface_list_properties:
* @g_iface: any interface vtable for the interface, or the default
diff --git a/gobject/gobject.h b/gobject/gobject.h
index f63e4d1..773de51 100644
--- a/gobject/gobject.h
+++ b/gobject/gobject.h
@@ -402,6 +402,13 @@ GParamSpec* g_object_interface_find_property (gpointer g_iface,
GParamSpec**g_object_interface_list_properties (gpointer g_iface,
guint *n_properties_p);
+void g_object_class_override_property_default (GObjectClass *oclass,
+ const gchar *property_name,
+ ...);
+void g_object_class_override_property_default_value (GObjectClass *oclass,
+ const gchar *property_name,
+ const GValue *default_value);
+
GType g_object_get_type (void) G_GNUC_CONST;
gpointer g_object_new (GType object_type,
const gchar *first_property_name,
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]