@samitouri / QOSamiQemu / commits / 57df0759c8

qom/object.c: add object_class_property_add_bool_ptr()

This adds a class property that references a bool within the object instance and is intended to be used as a replacement for object_class_property_add_bool() where possible. Since bool is a macro then it is necessary to implement property_class_get_bool_ptr() and property_class_set_bool_ptr() manually instead of using the existing OBJECT_CLASS_PROPERTY_SCALAR_GETTER() and OBJECT_CLASS_PROPERTY_SCALAR_SETTER() macros. Signed-off-by: Mark Cave-Ayland <mark.caveayland@nutanix.com> Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com> Message-Id: <20260825134320.1039012-11-mark.caveayland@nutanix.com>

Mark Cave-Ayland committed Aug 25, 2026 at 14:41 UTC 57df0759c83c491281554724fdf77c4ba793a523
2 files changed +61
include/qom/object.h
+18
@@ -2017,6 +2017,24 @@ typedef enum {
2017 OBJ_PROP_FLAG_READWRITE = (OBJ_PROP_FLAG_READ | OBJ_PROP_FLAG_WRITE),
2018 } ObjectPropertyFlags;
2019
2020 +/**
2021 + * object_class_property_add_bool_ptr:
2022 + * @klass: the object class to add a property to
2023 + * @name: the name of the property
2024 + * @offset: the offset from the object instance where the bool value is
2025 + * stored
2026 + * @flags: bitwise-or'd ObjectPropertyFlags
2027 + *
2028 + * Add an boolean property in memory. This function will add a
2029 + * property of type 'bool'.
2030 + *
2031 + * Returns: The newly added property on success, or %NULL on failure.
2032 + */
2033 +ObjectProperty *object_class_property_add_bool_ptr(ObjectClass *klass,
2034 + const char *name,
2035 + ptrdiff_t offset,
2036 + ObjectPropertyFlags flags);
2037 +
2038 /**
2039 * object_property_add_uint8_ptr:
2040 * @obj: the object to add a property to
qom/object.c
+43
@@ -2750,6 +2750,49 @@ DEFINE_OBJECT_CLASS_PROPERTY_SCALAR_METHODS(uint64)
2750 #undef DEFINE_OBJECT_CLASS_PROPERTY_SCALAR_METHODS
2751
2752
2753 +static void property_class_get_bool_ptr(Object *obj, Visitor *v,
2754 + const char *name,
2755 + void *opaque, Error **errp)
2756 +{
2757 + bool value = *(bool *)object_class_prop_ptr(obj, (ptrdiff_t)opaque);
2758 +
2759 + visit_type_bool(v, name, &value, errp);
2760 +}
2761 +
2762 +static void property_class_set_bool_ptr(Object *obj, Visitor *v,
2763 + const char *name,
2764 + void *opaque, Error **errp)
2765 +{
2766 + bool *field = (bool *)object_class_prop_ptr(obj, (ptrdiff_t)opaque);
2767 + bool value;
2768 +
2769 + if (!visit_type_bool(v, name, &value, errp)) {
2770 + return;
2771 + }
2772 +
2773 + *field = value;
2774 +}
2775 +
2776 +ObjectProperty *
2777 +object_class_property_add_bool_ptr(ObjectClass *klass, const char *name,
2778 + ptrdiff_t offset,
2779 + ObjectPropertyFlags flags)
2780 +{
2781 + ObjectPropertyAccessor *getter = NULL;
2782 + ObjectPropertyAccessor *setter = NULL;
2783 +
2784 + if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2785 + getter = property_class_get_bool_ptr;
2786 + }
2787 +
2788 + if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2789 + setter = property_class_set_bool_ptr;
2790 + }
2791 +
2792 + return object_class_property_add(klass, name, "bool",
2793 + getter, setter, NULL, (void *)offset);
2794 +}
2795 +
2796 ObjectProperty *
2797 object_property_add_uint8_ptr(Object *obj, const char *name,
2798 const uint8_t *v,