[PATCH v2 06/21] qom/object: add helpers to set/get/clear property flags

Zhao Liu posted 21 patches 14 hours ago
Maintainers: Pierrick Bouvier <pierrick.bouvier@linaro.org>, Paolo Bonzini <pbonzini@redhat.com>, "Daniel P. Berrangé" <berrange@redhat.com>, Eduardo Habkost <eduardo@habkost.net>, "Michael S. Tsirkin" <mst@redhat.com>, Marcel Apfelbaum <marcel.apfelbaum@gmail.com>, Richard Henderson <richard.henderson@linaro.org>, Zhao Liu <zhao1.liu@intel.com>
[PATCH v2 06/21] qom/object: add helpers to set/get/clear property flags
Posted by Zhao Liu 14 hours ago
Introduce four helper functions to operate object property flags:
object_property_set_flags(), object_property_get_flags(),
object_property_check_flags() and object_property_clear_flags().

Among them, object_property_check_flags() is a complement to
object_property_get_flags(), because sometimes it's needed to check
whether certain flags are set without returning all flags.

This encapsulates and separates the flag management logic, avoiding
direct access to ObjectProperty internals in call sites.

Suggested-by: Igor Mammedov <imammedo@redhat.com>
Signed-off-by: Zhao Liu <zhao1.liu@intel.com>
---
 include/qom/object.h | 64 ++++++++++++++++++++++++++++++++++++++++++++
 qom/object.c         | 63 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 127 insertions(+)

diff --git a/include/qom/object.h b/include/qom/object.h
index 30c9f20b1d18..856b12e7289c 100644
--- a/include/qom/object.h
+++ b/include/qom/object.h
@@ -1086,6 +1086,70 @@ Object *object_ref(void *obj);
  */
 void object_unref(void *obj);
 
+/**
+ * object_property_set_flags:
+ * @obj: the object.
+ * @name: the name of the property.
+ * @flags: the flags to be set for the property.
+ * @errp: pointer to error object.
+ *
+ * Set the @flags to the property. Existing flags are preserved.
+ *
+ * Returns: %true on success, %false on failure.
+ */
+bool object_property_set_flags(Object *obj, const char *name,
+                               ObjectPropertyFlags flags,
+                               Error **errp);
+
+/**
+ * object_property_get_flags:
+ * @obj: the object.
+ * @name: the name of the property.
+ * @flags: pointer to a location to store the retrieved flags. Must not be
+ *  NULL.
+ * @errp: pointer to error object.
+ *
+ * Get the current flags of the specified property.
+ *
+ * Returns: %true on success, %false on failure.
+ */
+bool object_property_get_flags(Object *obj, const char *name,
+                               ObjectPropertyFlags *flags,
+                               Error **errp);
+
+/**
+ * object_property_check_flags:
+ * @obj: the object.
+ * @name: the name of the property.
+ * @flags: the flags to check for on the property.
+ * @errp: pointer to error object.
+ *
+ * Check whether the specified property has all bits in @flags set.
+ * This is useful for detecting if a property has been explicitly set
+ * by the user (e.g. with %OBJ_PROP_FLAG_USER_SET).
+ *
+ * Returns: 1 if all @flags are set, 0 if not all @flags are set,
+ * or -1 on failure (property not found).
+ */
+int object_property_check_flags(Object *obj, const char *name,
+                                ObjectPropertyFlags flags,
+                                Error **errp);
+
+/**
+ * object_property_clear_flags:
+ * @obj: the object.
+ * @name: the name of the property.
+ * @flags: the flags to be removed from the property.
+ * @errp: pointer to error object
+ *
+ * Clear the @flags from the property. Other flags remain unchanged.
+ *
+ * Returns: %true on success, %false on failure.
+ */
+bool object_property_clear_flags(Object *obj, const char *name,
+                                 ObjectPropertyFlags flags,
+                                 Error **errp);
+
 /**
  * object_property_add_full:
  * @obj: the object to add a property to
diff --git a/qom/object.c b/qom/object.c
index c1a1e5ff3fbe..49ef99a299b6 100644
--- a/qom/object.c
+++ b/qom/object.c
@@ -1233,6 +1233,69 @@ void object_unref(void *objptr)
     }
 }
 
+bool object_property_set_flags(Object *obj, const char *name,
+                               ObjectPropertyFlags flags,
+                               Error **errp)
+{
+    ObjectProperty *prop = object_property_find_err(obj, name, errp);
+    if (!prop) {
+        return false;
+    }
+
+    prop->flags |= flags;
+    return true;
+}
+
+bool object_property_get_flags(Object *obj, const char *name,
+                               ObjectPropertyFlags *flags,
+                               Error **errp)
+{
+    ObjectProperty *prop;
+
+    if (!flags) {
+        error_setg(errp, "invalid argument: flags is NULL");
+        return false;
+    }
+
+    prop = object_property_find_err(obj, name, errp);
+    if (!prop) {
+        return false;
+    }
+
+    *flags = prop->flags;
+    return true;
+}
+
+int object_property_check_flags(Object *obj, const char *name,
+                                ObjectPropertyFlags flags,
+                                Error **errp)
+{
+    ObjectPropertyFlags prop_flags;
+
+    if (!object_property_get_flags(obj, name, &prop_flags, errp)) {
+        return -1;
+    }
+
+    if ((prop_flags & flags) == flags) {
+        return 1;
+    }
+
+    return 0;
+}
+
+bool object_property_clear_flags(Object *obj, const char *name,
+                                 ObjectPropertyFlags flags,
+                                 Error **errp)
+{
+    ObjectProperty *prop = object_property_find_err(obj, name, errp);
+    if (!prop) {
+        return false;
+    }
+
+    prop->flags &= ~flags;
+    return true;
+}
+
 static inline void object_property_flags_init(ObjectProperty *prop,
                                               ObjectPropertyFlags flags)
 {
-- 
2.34.1