[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [PATCH v7 17/65] qom: add object_{class_}property_add_qapi_enum
- To: Marc-André Lureau <marcandre.lureau@xxxxxxxxxx>
- From: Markus Armbruster <armbru@xxxxxxxxxx>
- Date: Fri, 30 Oct 2026 11:02:00 +0100
- Authentication-results: eu.smtp.expurgate.cloud; dkim=pass header.s=mimecast20190719 header.d=redhat.com header.i="@redhat.com" header.h="From:Subject:Date:Message-ID:To:Cc:MIME-Version:Content-Type:Content-Transfer-Encoding:Resent-To:Resent-From:Resent-Message-ID:In-Reply-To:References"
- Cc: qemu-devel@xxxxxxxxxx, Michael Roth <michael.roth@xxxxxxx>, John Snow <jsnow@xxxxxxxxxx>, Peter Maydell <peter.maydell@xxxxxxxxxx>, Mauro Carvalho Chehab <mchehab+huawei@xxxxxxxxxx>, Pierrick Bouvier <pierrick.bouvier@xxxxxxxxxxxxxxxx>, Paolo Bonzini <pbonzini@xxxxxxxxxx>, Daniel P. Berrangé <berrange@xxxxxxxxxx>, Kostiantyn Kostiuk <kkostiuk@xxxxxxxxxx>, Eric Blake <eblake@xxxxxxxxxx>, David Hildenbrand <david@xxxxxxxxxx>, Igor Mammedov <imammedo@xxxxxxxxxx>, "Edgar E. Iglesias" <edgar.iglesias@xxxxxxxxx>, Jason Wang <jasowangio@xxxxxxxxx>, Marcelo Tosatti <mtosatti@xxxxxxxxxx>, kvm@xxxxxxxxxxxxxxx, Alberto Garcia <berto@xxxxxxxxxx>, Kevin Wolf <kwolf@xxxxxxxxxx>, Hanna Reitz <hreitz@xxxxxxxxxx>, Philippe Mathieu-Daudé <philmd@xxxxxxxxxxxxxxxx>, Zhao Liu <zhao1.liu@xxxxxxxxx>, Jonathan Cameron <jic23@xxxxxxxxxx>, Junjie Cao <junjie.cao@xxxxxxxxx>, Anisa Su <anisa.su@xxxxxxxxxxx>, Elena Ufimtseva <elena.ufimtseva@xxxxxxxxxx>, Jagannathan Raman <jag.raman@xxxxxxxxxx>, John Levon <john.levon@xxxxxxxxxxx>, Thanos Makatos <thanos.makatos@xxxxxxxxxxx>, Cédric Le Goater <clg@xxxxxxxxxx>, Cornelia Huck <cohuck@xxxxxxxxxx>, Eric Farman <farman@xxxxxxxxxxxxx>, Matthew Rosato <mjrosato@xxxxxxxxxxxxx>, Richard Henderson <richard.henderson@xxxxxxxxxx>, Ilya Leoshkevich <iii@xxxxxxxxxxxxx>, qemu-block@xxxxxxxxxx, linux-cxl@xxxxxxxxxxxxxxx, qemu-s390x@xxxxxxxxxx, "Michael S. Tsirkin" <mst@xxxxxxxxxx>, Sergio Lopez <slp@xxxxxxxxxx>, qemu-arm@xxxxxxxxxx, Palmer Dabbelt <palmer@xxxxxxxxxxx>, Alistair Francis <alistair.francis@xxxxxxx>, Weiwei Li <liwei1518@xxxxxxxxx>, Daniel Henrique Barboza <daniel.barboza@xxxxxxxxxxxxxxxx>, Liu Zhiwei <zhiwei_liu@xxxxxxxxxxxxxxxxx>, Chao Liu <chao.liu@xxxxxxxxxxxxxxxxxx>, qemu-riscv@xxxxxxxxxx, Song Gao <17746591750@xxxxxxx>, Bibo Mao <maobibo@xxxxxxxxxxx>, Xianglai Li <lixianglai@xxxxxxxxxxx>, Jiaxun Yang <jiaxun.yang@xxxxxxxxxxx>, Pedro Barbuda <pbarbuda@xxxxxxxxxxxxx>, Mohamed Mediouni <mohamed@xxxxxxxxxxxxxxxx>, Nicholas Piggin <npiggin@xxxxxxxxx>, Harsh Prateek Bora <harshpb@xxxxxxxxxxxxx>, Amit Machhiwal <amachhiw@xxxxxxxxxxxxx>, qemu-ppc@xxxxxxxxxx, Zhang Chen <zhangckid@xxxxxxxxx>, Li Zhijian <lizhijian@xxxxxxxxxxx>, Alistair Francis <alistair@xxxxxxxxxxxxx>, Alexander Graf <graf@xxxxxxxxxx>, "Gonglei (Arei)" <arei.gonglei@xxxxxxxxxx>, zhenwei pi <zhenwei.pi@xxxxxxxxx>, Ani Sinha <anisinha@xxxxxxxxxx>, Luc Michel <luc@xxxxxxxxxx>, Cédric Le Goater <clg@xxxxxxxx>, Jamin Lin <jamin_lin@xxxxxxxxxxxxxx>, Kane Chen <kane_chen@xxxxxxxxxxxxxx>, Samuel Tardieu <sam@xxxxxxxxxxx>, "Denis V. Lunev" <den@xxxxxxxxxx>, FangSheng Huang <FangSheng.Huang@xxxxxxx>, Tyrone Ting <kfting@xxxxxxxxxxx>, Hao Wu <wuhaotsh@xxxxxxxxxx>, Keith Busch <kbusch@xxxxxxxxxx>, Klaus Jensen <its@xxxxxxxxxxxxx>, Jesper Devantier <foss@xxxxxxxxxxx>, Aditya Gupta <adityag@xxxxxxxxxxxxx>, Glenn Miles <milesg@xxxxxxxxxxxxx>, Conor Dooley <conor@xxxxxxxxxx>, Sebastian Huber <sebastian.huber@xxxxxxxxxxxxxxxxxx>, Halil Pasic <pasic@xxxxxxxxxxxxx>, Christian Borntraeger <borntraeger@xxxxxxxxxxxxx>, Jason Herne <jjherne@xxxxxxxxxxxxx>, Titus Rwantare <titusr@xxxxxxxxxx>, Stefano Stabellini <sstabellini@xxxxxxxxxx>, Anthony PERARD <anthony@xxxxxxxxxxxxxx>, Peter Xu <peterx@xxxxxxxxxx>, Chinmay Rath <rathc@xxxxxxxxxxxxx>, Hendrik Brueckner <brueckner@xxxxxxxxxxxxx>, xen-devel@xxxxxxxxxxxxxxxxxxxx, Roman Bolshakov <rbolshakov@xxxxxxx>, Phil Dennis-Jordan <phil@xxxxxxxxxxxxx>, Fam Zheng <fam@xxxxxxxxxx>, Farhan Ali <alifm@xxxxxxxxxxxxx>, Alex Williamson <alex@xxxxxxxxxxx>, Tao Tang <tangtao1634@xxxxxxxxxxxxxx>, Stefan Berger <stefanb@xxxxxxxxxxxxxxxxxx>, Fabiano Rosas <farosas@xxxxxxx>, Mark Cave-Ayland <mark.cave-ayland@xxxxxxxxxxxx>, Artyom Tarasenko <atar4qemu@xxxxxxxxx>, Yoshinori Sato <yoshinori.sato@xxxxxxxxx>, Jiri Pirko <jiri@xxxxxxxxxxx>, Eric Auger <eric.auger@xxxxxxxxxx>, Laurent Vivier <lvivier@xxxxxxxxxx>
- Delivery-date: Mon, 02 Nov 2026 05:50:37 +0000
- List-id: Xen developer discussion <xen-devel.lists.xenproject.org>
- Resent-date: Sun, 01 Nov 2026 21:11:04 +0100
- Resent-from: Markus Armbruster <armbru@xxxxxxxxxx>
- Resent-message-id: <87ik2g1fdz.fsf@xxxxxxxxxxxx>
- Resent-to: pasic@xxxxxxxxxxxxx, 17746591750@xxxxxxx, rathc@xxxxxxxxxxxxx, alistair@xxxxxxxxxxxxx, stefanb@xxxxxxxxxxxxxxxxxx, graf@xxxxxxxxxx, xen-devel@xxxxxxxxxxxxxxxxxxxx, FangSheng.Huang@xxxxxxx, michael.roth@xxxxxxx, jamin_lin@xxxxxxxxxxxxxx, kane_chen@xxxxxxxxxxxxxx, palmer@xxxxxxxxxxx, rbolshakov@xxxxxxx, foss@xxxxxxxxxxx, sebastian.huber@xxxxxxxxxxxxxxxxxx, fam@xxxxxxxxxx, jiaxun.yang@xxxxxxxxxxx, lizhijian@xxxxxxxxxxx, atar4qemu@xxxxxxxxx, edgar.iglesias@xxxxxxxxx, jasowangio@xxxxxxxxx, liwei1518@xxxxxxxxx, npiggin@xxxxxxxxx, zhangckid@xxxxxxxxx, titusr@xxxxxxxxxx, wuhaotsh@xxxxxxxxxx, arei.gonglei@xxxxxxxxxx, berto@xxxxxxxxxx, mark.cave-ayland@xxxxxxxxxxxx, junjie.cao@xxxxxxxxx, zhao1.liu@xxxxxxxxx, its@xxxxxxxxxxxxx, clg@xxxxxxxx, conor@xxxxxxxxxx, david@xxxxxxxxxx, jic23@xxxxxxxxxx, kbusch@xxxxxxxxxx, mchehab+huawei@xxxxxxxxxx, sstabellini@xxxxxxxxxx, peter.maydell@xxxxxxxxxx, richard.henderson@xxxxxxxxxx, zhiwei_liu@xxxxxxxxxxxxxxxxx, zhenwei.pi@xxxxxxxxx, adityag@xxxxxxxxxxxxx, alifm@xxxxxxxxxxxxx, amachhiw@xxxxxxxxxxxxx, borntraeger@xxxxxxxxxxxxx, brueckner@xxxxxxxxxxxxx, farman@xxxxxxxxxxxxx, harshpb@xxxxxxxxxxxxx, iii@xxxxxxxxxxxxx, jjherne@xxxxxxxxxxxxx, milesg@xxxxxxxxxxxxx, mjrosato@xxxxxxxxxxxxx, luc@xxxxxxxxxx, lixianglai@xxxxxxxxxxx, maobibo@xxxxxxxxxxx, pbarbuda@xxxxxxxxxxxxx, yoshinori.sato@xxxxxxxxx, qemu-arm@xxxxxxxxxx, qemu-block@xxxxxxxxxx, qemu-devel@xxxxxxxxxx, qemu-ppc@xxxxxxxxxx, qemu-riscv@xxxxxxxxxx, qemu-s390x@xxxxxxxxxx, john.levon@xxxxxxxxxxx, thanos.makatos@xxxxxxxxxxx, kfting@xxxxxxxxxxx, den@xxxxxxxxxx, elena.ufimtseva@xxxxxxxxxx, jag.raman@xxxxxxxxxx, daniel.barboza@xxxxxxxxxxxxxxxx, philmd@xxxxxxxxxxxxxxxx, pierrick.bouvier@xxxxxxxxxxxxxxxx, phil@xxxxxxxxxxxxx, tangtao1634@xxxxxxxxxxxxxx, chao.liu@xxxxxxxxxxxxxxxxxx, jiri@xxxxxxxxxxx, sam@xxxxxxxxxxx, anisa.su@xxxxxxxxxxx, alex@xxxxxxxxxxx, farosas@xxxxxxx, mohamed@xxxxxxxxxxxxxxxx, kvm@xxxxxxxxxxxxxxx, linux-cxl@xxxxxxxxxxxxxxx, alistair.francis@xxxxxxx, anthony@xxxxxxxxxxxxxx
Marc-André Lureau <marcandre.lureau@xxxxxxxxxx> writes:
> Signed-off-by: Markus Armbruster <armbru@xxxxxxxxxx>
> Signed-off-by: Marc-André Lureau <marcandre.lureau@xxxxxxxxxx>
> ---
> include/qom/object.h | 74 ++++++++++++++++++++++++++++++++++++++++++
> qom/object.c | 90
> +++++++++++++++++++++++++++++++++++++++++++++++++---
> 2 files changed, 159 insertions(+), 5 deletions(-)
>
> diff --git a/include/qom/object.h b/include/qom/object.h
> index eb61d8842b80..e04be4caed57 100644
> --- a/include/qom/object.h
> +++ b/include/qom/object.h
> @@ -2027,6 +2027,80 @@ ObjectProperty
> *object_class_property_add_enum(ObjectClass *klass,
> int (*get)(Object *, Error **),
> void (*set)(Object *, int, Error **));
>
> +/**
> + * struct QapiEnumProp: Descriptor for a property backed by a QAPI enum type
> + *
> + * Binds a QOM object property to a QAPI enum, providing automatic
> + * string<->int conversion through QAPI visitors and optional
> + * default-value initialization during object instance init.
> + *
> + * Use the QAPI_ENUM_PROP() macro to construct instances inline.
> + *
> + * @name: property name exposed on the QOM object
> + * @description: human-readable description (shown in ``-device help``, etc.)
> + * @default_value: initial enum value applied via @set during instance init,
> + * or -1 (the QAPI_ENUM_PROP default) to skip initialization
> + * @qapi_type: pointer to the generated QAPITypeInfo for the enum
> + * (provides the string<->int lookup table)
> + * @get: getter that returns the current enum value as int, or -1 on error
> + * @set: setter that receives the enum value as int; %NULL for read-only
> props
> + */
> +typedef struct QapiEnumProp {
> + const char *name;
> + const char *description;
> + const int default_value;
> + const QAPITypeInfo *qapi_type;
> + int (*get)(Object *, Error **);
> + void (*set)(Object *, int, Error **);
> +} QapiEnumProp;
> +
> +#define QAPI_ENUM_PROP(...) ({ \
> + static const QapiEnumProp _prop = { \
> + .default_value = -1, __VA_ARGS__ \
> + }; \
> + &_prop; })
This is a cute and reasonably well-known macro trick for optional
keyword arguments in C.
The example [*] below specifies all arguments. What happens when we
omit any? They default to zero via default initializaion, except for
.default_value, which defaults to -1.
Trap for developers: some arguments aren't actually optional! If you
forget to specify @name or @qapi_type, the code compiles, but crashes at
run time.
Not a fan.
> +
> +/**
> + * object_property_add_qapi_enum:
> + * @obj: the object to add a property to
> + * @prop: property descriptor
> + *
> + * Add an enum property with QAPI type association.
> + *
> + * Returns: The newly added property on success, or %NULL on failure.
> + */
> +ObjectProperty *
> +object_property_add_qapi_enum(Object *obj, const QapiEnumProp *prop);
> +
> +/**
> + * object_class_property_add_qapi_enum:
> + * @klass: the class object to add a property to
> + * @prop: property descriptor
> + *
> + * Add an enum property with QAPI type association.
> + *
> + * Use the QAPI_ENUM_PROP() macro to construct the property descriptor
> + * inline. If .default_value is not set, the property is not initialized
> + * (default_value is -1). Otherwise, the setter is called with
> + * default_value during object instance init.
> + *
> + * Example::
[*]
> + *
> + * object_class_property_add_qapi_enum(oc, QAPI_ENUM_PROP(
> + * .name = "policy",
> + * .description = "Set the NUMA policy",
> + * .default_value = HOST_MEM_POLICY_DEFAULT,
> + * .qapi_type = &HostMemPolicy_type_info,
> + * .get = my_get_policy,
> + * .set = my_set_policy,
> + * ));
> + *
> + * Returns: The newly added property on success, or %NULL on failure.
> + */
> +ObjectProperty *
> +object_class_property_add_qapi_enum(ObjectClass *klass,
> + const QapiEnumProp *prop);
> +
> /**
> * object_property_add_qapi:
> * @obj: the object to add a property to
[...]
|