diff mbox series

[v3,16/19] qom: Make PropertyInfo.set_default_value optional

Message ID 20201123194818.2773508-17-ehabkost@redhat.com
State New
Headers show
Series qom: Use qlit to represent property defaults | expand

Commit Message

Eduardo Habkost Nov. 23, 2020, 7:48 p.m. UTC
If .set_default_value is not set, call
object_property_set_default().  This will let us delete most of
the .set_default_value functions later.

Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
---
This is a new patch in v3 of this series.

In v2 of the series, equivalent functionality was implemented by
"qom: Use qlit to represent property defaults".
---
 include/qom/field-property.h |  3 +++
 qom/field-property.c         | 12 ++++++++++--
 2 files changed, 13 insertions(+), 2 deletions(-)
diff mbox series

Patch

diff --git a/include/qom/field-property.h b/include/qom/field-property.h
index 951cec2fb0..00b83ee9ba 100644
--- a/include/qom/field-property.h
+++ b/include/qom/field-property.h
@@ -53,6 +53,9 @@  struct PropertyInfo {
      * @set_default_value: Callback for initializing the default value
      *
      * @defval is a weak reference.
+     *
+     * Optional.  If not set and Property.defval is not QTYPE_NONE,
+     * object_property_set_default() will be called.
      */
     void (*set_default_value)(ObjectProperty *op, const Property *prop,
                               const QObject *defval);
diff --git a/qom/field-property.c b/qom/field-property.c
index 593ffb53e9..d21ff98862 100644
--- a/qom/field-property.c
+++ b/qom/field-property.c
@@ -72,8 +72,16 @@  static void field_prop_set_default_value(ObjectProperty *op,
     }
 
     defval = qobject_from_qlit(&prop->defval);
-    assert(prop->info->set_default_value);
-    prop->info->set_default_value(op, prop, defval);
+    if (prop->info->set_default_value) {
+        /* .set_default_value() gets a weak reference */
+        prop->info->set_default_value(op, prop, defval);
+    } else {
+        /*
+         * object_property_set_default() takes ownership,
+         * so qobject_ref() is needed.
+         */
+        object_property_set_default(op, qobject_ref(defval));
+    }
     qobject_unref(defval);
 }