This removes the last remaining DeviceState-specific line of code
inside qdev property registration code, and will allow us to make
static properties a core QOM feature.

Signed-off-by: Eduardo Habkost <ehabk...@redhat.com>
---
Cc: Paolo Bonzini <pbonz...@redhat.com>
Cc: "Daniel P. Berrangé" <berra...@redhat.com>
Cc: Eduardo Habkost <ehabk...@redhat.com>
Cc: qemu-devel@nongnu.org
---
 hw/core/qdev-prop-internal.h |  4 +++-
 include/hw/qdev-core.h       | 10 ++++++++++
 include/hw/qdev-properties.h | 10 ++++++++--
 hw/core/qdev-properties.c    | 36 ++++++++++++++++++++++--------------
 4 files changed, 43 insertions(+), 17 deletions(-)

diff --git a/hw/core/qdev-prop-internal.h b/hw/core/qdev-prop-internal.h
index 858f844504..49bf557fd5 100644
--- a/hw/core/qdev-prop-internal.h
+++ b/hw/core/qdev-prop-internal.h
@@ -31,11 +31,13 @@ void qdev_propinfo_get_size32(Object *obj, Visitor *v, 
const char *name,
  * object_property_add_static: Add a static property to an object instance
  * @obj: object instance
  * @prop: property definition
+ * @allow_set: optional check function
  *
  * This function should not be used in new code.  Please add class properties
  * instead, using object_class_add_static_props().
  */
 ObjectProperty *
-object_property_add_static(Object *obj, Property *prop);
+object_property_add_static(Object *obj, Property *prop,
+                           ObjectPropertyAllowSet allow_set);
 
 #endif
diff --git a/include/hw/qdev-core.h b/include/hw/qdev-core.h
index aea49355fa..e69d19d06b 100644
--- a/include/hw/qdev-core.h
+++ b/include/hw/qdev-core.h
@@ -765,6 +765,16 @@ void qdev_machine_init(void);
  */
 void device_legacy_reset(DeviceState *dev);
 
+/**
+ * device_class_set_props: Add class properties from #Property array
+ *
+ * @dc: Device class
+ * @props: array of device properties, terminated by DEFINE_PROP_END_OF_LIST
+ *
+ * Add class properties from the array at @props.
+ * Properties added using this function will be settable only
+ * before the device is realized.
+ */
 void device_class_set_props(DeviceClass *dc, Property *props);
 
 /**
diff --git a/include/hw/qdev-properties.h b/include/hw/qdev-properties.h
index c94301c349..0acc92ae2b 100644
--- a/include/hw/qdev-properties.h
+++ b/include/hw/qdev-properties.h
@@ -279,25 +279,31 @@ extern const PropertyInfo qdev_prop_pcie_link_width;
  * object_class_property_add_static: Add a static property to object class
  * @oc: object class
  * @prop: property definition
+ * @allow_set: optional check function
  *
  * Add a property to an object class based on the property definition
  * at @prop.
  *
+ * If @allow_set is NULL, the property will always be allowed to be set.
+ *
  * The property definition at @prop should be defined using the
  * ``DEFINE_PROP`` family of macros.  *@prop must exist for the
  * life time of @oc.
  */
 ObjectProperty *
-object_class_property_add_static(ObjectClass *oc, Property *prop);
+object_class_property_add_static(ObjectClass *oc, Property *prop,
+                                 ObjectPropertyAllowSet allow_set);
 
 /**
  * object_class_add_static_props: Add multiple static properties to object 
class
  * @oc: object class
  * @props: property definition array, terminated by DEFINED_PROP_END_OF_LIST()
+ * @allow_set: optional check function
  *
  * Add properties from @props using object_class_property_add_static()
  */
-void object_class_add_static_props(ObjectClass *oc, Property *props);
+void object_class_add_static_props(ObjectClass *oc, Property *props,
+                                   ObjectPropertyAllowSet allow_set);
 
 /*
  * Set properties between creation and realization.
diff --git a/hw/core/qdev-properties.c b/hw/core/qdev-properties.c
index 4fec9cb73b..4cb8baa6a0 100644
--- a/hw/core/qdev-properties.c
+++ b/hw/core/qdev-properties.c
@@ -26,13 +26,13 @@ void qdev_prop_set_after_realize(DeviceState *dev, const 
char *name,
 }
 
 /* returns: true if property is allowed to be set, false otherwise */
-static bool qdev_prop_allow_set(Object *obj, const char *name,
+static bool qdev_prop_allow_set(Object *obj, ObjectProperty *op,
                                 Error **errp)
 {
     DeviceState *dev = DEVICE(obj);
 
     if (dev->realized) {
-        qdev_prop_set_after_realize(dev, name, errp);
+        qdev_prop_set_after_realize(dev, op->name, errp);
         return false;
     }
     return true;
@@ -80,10 +80,6 @@ static void static_prop_set(Object *obj, Visitor *v, const 
char *name,
 {
     Property *prop = opaque;
 
-    if (!qdev_prop_allow_set(obj, name, errp)) {
-        return;
-    }
-
     return prop->info->set(obj, v, name, opaque, errp);
 }
 
@@ -631,7 +627,9 @@ static void object_property_add_array_element(Object *obj,
                                               Property *array_len_prop,
                                               ArrayElementProperty *prop)
 {
-    ObjectProperty *op = object_property_add_static(obj, &prop->prop);
+    ObjectProperty *array_op = object_property_find(obj, array_len_prop->name);
+    ObjectProperty *op = object_property_add_static(obj, &prop->prop,
+                                                    array_op->allow_set);
 
     assert((void *)prop == (void *)&prop->prop);
     prop->release = op->release;
@@ -894,9 +892,13 @@ const PropertyInfo qdev_prop_size = {
 
 static ObjectProperty *create_link_property(ObjectClass *oc, Property *prop)
 {
+    /*
+     * NOTE: object_property_allow_set_link is unconditional, but
+     *       ObjectProperty.allow_set may be set for the property too.
+     */
     return object_class_property_add_link(oc, prop->name, prop->link_type,
                                           prop->offset,
-                                          
qdev_prop_allow_set_link_before_realize,
+                                          object_property_allow_set_link,
                                           OBJ_PROP_LINK_STRONG);
 }
 
@@ -906,7 +908,8 @@ const PropertyInfo qdev_prop_link = {
 };
 
 ObjectProperty *
-object_property_add_static(Object *obj, Property *prop)
+object_property_add_static(Object *obj, Property *prop,
+                           ObjectPropertyAllowSet allow_set)
 {
     ObjectProperty *op;
 
@@ -928,11 +931,13 @@ object_property_add_static(Object *obj, Property *prop)
         }
     }
 
+    op->allow_set = allow_set;
     return op;
 }
 
 ObjectProperty *
-object_class_property_add_static(ObjectClass *oc, Property *prop)
+object_class_property_add_static(ObjectClass *oc, Property *prop,
+                                 ObjectPropertyAllowSet allow_set)
 {
     ObjectProperty *op;
 
@@ -953,21 +958,24 @@ object_class_property_add_static(ObjectClass *oc, 
Property *prop)
         object_class_property_set_description(oc, prop->name,
                                             prop->info->description);
     }
+
+    op->allow_set = allow_set;
     return op;
 }
 
-void object_class_add_static_props(ObjectClass *oc, Property *props)
+void object_class_add_static_props(ObjectClass *oc, Property *props,
+                                   ObjectPropertyAllowSet allow_set)
 {
     Property *prop;
 
     for (prop = props; prop && prop->name; prop++) {
-        object_class_property_add_static(oc, prop);
+        object_class_property_add_static(oc, prop, allow_set);
     }
 }
 
 void qdev_property_add_static(DeviceState *dev, Property *prop)
 {
-    object_property_add_static(OBJECT(dev), prop);
+    object_property_add_static(OBJECT(dev), prop, qdev_prop_allow_set);
 }
 
 /**
@@ -1027,7 +1035,7 @@ void device_class_set_props(DeviceClass *dc, Property 
*props)
 {
     dc->props_ = props;
     qdev_class_add_legacy_properties(dc, props);
-    object_class_add_static_props(OBJECT_CLASS(dc), props);
+    object_class_add_static_props(OBJECT_CLASS(dc), props, 
qdev_prop_allow_set);
 }
 
 void qdev_alias_all_properties(DeviceState *target, Object *source)
-- 
2.28.0


Reply via email to