Similar to object_property_try_add(), factor out
object_class_property_try_add().
This allows adding more arguments to the core implementation without
changing the signature of object_class_property_add(), avoiding the
need to modify the extensive number of callers distributed throughout
the code tree.
While at it, add documentation for these functions.
Signed-off-by: Zhao Liu <zhao1.liu@intel.com>
---
include/qom/object.h | 56 ++++++++++++++++++++++++++++++++++++++++++++
qom/object.c | 34 ++++++++++++++++++++-------
2 files changed, 82 insertions(+), 8 deletions(-)
diff --git a/include/qom/object.h b/include/qom/object.h
index 05706d4d7e3a..060db136988b 100644
--- a/include/qom/object.h
+++ b/include/qom/object.h
@@ -1147,6 +1147,62 @@ ObjectProperty *object_property_add(Object *obj, const char *name,
void object_property_del(Object *obj, const char *name);
+/**
+ * object_class_property_try_add:
+ * @klass: the object class to add a property to
+ * @name: the name of the property. This can contain any character except for
+ * a forward slash. In general, you should use hyphens '-' instead of
+ * underscores '_' when naming properties.
+ * @type: the type name of the property. This namespace is pretty loosely
+ * defined. Sub namespaces are constructed by using a prefix and then
+ * to angle brackets. For instance, the type 'virtio-net-pci' in the
+ * 'link' namespace would be 'link<virtio-net-pci>'.
+ * @get: The getter to be called to read a property. If this is NULL, then
+ * the property cannot be read.
+ * @set: the setter to be called to write a property. If this is NULL,
+ * then the property cannot be written.
+ * @release: called when the property is removed from the object. This is
+ * meant to allow a property to free its opaque upon object
+ * destruction. This may be NULL.
+ * @opaque: an opaque pointer to pass to the callbacks for the property
+ * @errp: pointer to error object
+ *
+ * Returns: The #ObjectProperty; this can be used to set the @resolve
+ * callback for child and link properties.
+ */
+ObjectProperty *object_class_property_try_add(ObjectClass *klass, const char *name,
+ const char *type,
+ ObjectPropertyAccessor *get,
+ ObjectPropertyAccessor *set,
+ ObjectPropertyRelease *release,
+ void *opaque, Error **errp);
+
+
+/**
+ * object_class_property_add:
+ * Same as object_class_property_try_add() with @errp hardcoded to
+ * &error_abort.
+ *
+ * @klass: the object class to add a property to
+ * @name: the name of the property. This can contain any character except for
+ * a forward slash. In general, you should use hyphens '-' instead of
+ * underscores '_' when naming properties.
+ * @type: the type name of the property. This namespace is pretty loosely
+ * defined. Sub namespaces are constructed by using a prefix and then
+ * to angle brackets. For instance, the type 'virtio-net-pci' in the
+ * 'link' namespace would be 'link<virtio-net-pci>'.
+ * @get: The getter to be called to read a property. If this is NULL, then
+ * the property cannot be read.
+ * @set: the setter to be called to write a property. If this is NULL,
+ * then the property cannot be written.
+ * @release: called when the property is removed from the object. This is
+ * meant to allow a property to free its opaque upon object
+ * destruction. This may be NULL.
+ * @opaque: an opaque pointer to pass to the callbacks for the property
+ *
+ * Returns: The #ObjectProperty; this can be used to set the @resolve
+ * callback for child and link properties.
+ */
ObjectProperty *object_class_property_add(ObjectClass *klass, const char *name,
const char *type,
ObjectPropertyAccessor *get,
diff --git a/qom/object.c b/qom/object.c
index f5801f4624c8..f101b48154d1 100644
--- a/qom/object.c
+++ b/qom/object.c
@@ -1310,17 +1310,22 @@ object_property_add(Object *obj, const char *name, const char *type,
}
ObjectProperty *
-object_class_property_add(ObjectClass *klass,
- const char *name,
- const char *type,
- ObjectPropertyAccessor *get,
- ObjectPropertyAccessor *set,
- ObjectPropertyRelease *release,
- void *opaque)
+object_class_property_try_add(ObjectClass *klass,
+ const char *name,
+ const char *type,
+ ObjectPropertyAccessor *get,
+ ObjectPropertyAccessor *set,
+ ObjectPropertyRelease *release,
+ void *opaque, Error **errp)
{
ObjectProperty *prop;
- assert(!object_class_property_find(klass, name));
+ if (object_class_property_find(klass, name) != NULL) {
+ error_setg(errp, "attempt to add duplicate property '%s'"
+ " to object class (type '%s')",
+ name, object_class_get_name(klass));
+ return NULL;
+ }
prop = g_malloc0(sizeof(*prop));
@@ -1337,6 +1342,19 @@ object_class_property_add(ObjectClass *klass,
return prop;
}
+ObjectProperty *
+object_class_property_add(ObjectClass *klass,
+ const char *name,
+ const char *type,
+ ObjectPropertyAccessor *get,
+ ObjectPropertyAccessor *set,
+ ObjectPropertyRelease *release,
+ void *opaque)
+{
+ return object_class_property_try_add(klass, name, type, get, set, release,
+ opaque, &error_abort);
+}
+
ObjectProperty *object_property_find(Object *obj, const char *name)
{
ObjectProperty *prop;
--
2.34.1