diff mbox series

[v2,32/44] qom: Add allow_set callback to ObjectProperty

Message ID 20201104160021.2342108-33-ehabkost@redhat.com
State New
Headers show
Series Make qdev static property API usable by any QOM type | expand

Commit Message

Eduardo Habkost Nov. 4, 2020, 4 p.m. UTC
Add a ObjectProperty.allow_set callback, that can be set by QOM
property registration functions.

Note that this doesn't replace the check callback at
object*_property_add_link() (yet), because currently the link
property check callback needs to get the property value as
argument (despite this not being necessary in most cases).

Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
---
Changes v1 -> v2:
* Redone patch on top of changes in previous patches in the
  series
* Provide prop_allow_set_always() and prop_allow_set_never()
  helpers
---
Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: "Daniel P. Berrangé" <berrange@redhat.com>
Cc: Eduardo Habkost <ehabkost@redhat.com>
Cc: qemu-devel@nongnu.org
---
 include/qom/object.h | 38 ++++++++++++++++++++++++++++++++++++++
 qom/object.c         | 16 ++++++++++++++++
 2 files changed, 54 insertions(+)
diff mbox series

Patch

diff --git a/include/qom/object.h b/include/qom/object.h
index d378f13a11..2ab124b8f0 100644
--- a/include/qom/object.h
+++ b/include/qom/object.h
@@ -86,6 +86,43 @@  typedef void (ObjectPropertyRelease)(Object *obj,
  */
 typedef void (ObjectPropertyInit)(Object *obj, ObjectProperty *prop);
 
+/**
+ * typedef ObjectPropertyAllowSet:
+ * @obj: the object that owns the property
+ * @prop: the property being set
+ * @errp: pointer to error information
+ *
+ * Called when a property is being set.
+ *
+ * If return value is false, it will prevent the property from
+ * being changed.  Error information should be filled in @errp
+ * if return vlaue is false.
+ */
+typedef bool (ObjectPropertyAllowSet)(Object *obj, ObjectProperty *prop,
+                                      Error **errp);
+
+/**
+ * prop_allow_set_always:
+ * @obj: the object that owns the property
+ * @prop: the property being set
+ * @errp: pointer to error information
+ *
+ * ObjectPropertyAllowSet implementation that always allow the
+ * property to be set.
+ */
+bool prop_allow_set_always(Object *obj, ObjectProperty *prop, Error **errp);
+
+/**
+ * prop_allow_set_never:
+ * @obj: the object that owns the property
+ * @prop: the property being set
+ * @errp: pointer to error information
+ *
+ * ObjectPropertyAllowSet implementation that never allows the
+ * property to be set.
+ */
+bool prop_allow_set_never(Object *obj, ObjectProperty *prop, Error **errp);
+
 struct ObjectProperty
 {
     char *name;
@@ -96,6 +133,7 @@  struct ObjectProperty
     ObjectPropertyResolve *resolve;
     ObjectPropertyRelease *release;
     ObjectPropertyInit *init;
+    ObjectPropertyAllowSet *allow_set;
     void *opaque;
     QObject *defval;
 };
diff --git a/qom/object.c b/qom/object.c
index 1065355233..7c11bcd3b1 100644
--- a/qom/object.c
+++ b/qom/object.c
@@ -1381,6 +1381,18 @@  bool object_property_get(Object *obj, const char *name, Visitor *v,
     return !err;
 }
 
+bool prop_allow_set_always(Object *obj, ObjectProperty *prop, Error **errp)
+{
+    return true;
+}
+
+bool prop_allow_set_never(Object *obj, ObjectProperty *prop, Error **errp)
+{
+    error_setg(errp, "Property '%s.%s' can't be set",
+               object_get_typename(obj), prop->name);
+    return false;
+}
+
 bool object_property_set(Object *obj, const char *name, Visitor *v,
                          Error **errp)
 {
@@ -1395,6 +1407,10 @@  bool object_property_set(Object *obj, const char *name, Visitor *v,
         error_setg(errp, QERR_PERMISSION_DENIED);
         return false;
     }
+    if (prop->allow_set && !prop->allow_set(obj, prop, errp)) {
+        return false;
+    }
+
     prop->set(obj, v, name, prop->opaque, &err);
     error_propagate(errp, err);
     return !err;