Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 174F2CCF2E5 for ; Mon, 19 Oct 2026 14:25:44 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xIoFw-0005EV-21; Mon, 19 Oct 2026 10:22:28 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xIoDJ-0002CY-9l for qemu-devel@nongnu.org; Mon, 19 Oct 2026 10:19:46 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xIoDG-0005Yn-3E for qemu-devel@nongnu.org; Mon, 19 Oct 2026 10:19:44 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1792419581; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=L3wwzFlW8okAB1Tl0pcWPb0OQMZiiVQsns1cWQJiT6A=; b=eDPzSP3CApV6t2wJZVlfa38KGSDArde7OOTTrVx+aW/PIim9l5oR09TTWEoDqkY1ox1wQj fBBA6g8RZhjotW+gcz5cpsOyMmN3/WcnbT1dA5Ei6PaAgW9XJkL9cOeqAxxXoNHxFD8HFg YEm4FyaVdXyB8OblpLLnmYhaoOboNEc= Received: from mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-563-yo8KD5nWN9-koab463wgBA-1; Mon, 19 Oct 2026 14:19:39 +0000 X-MC-Unique: yo8KD5nWN9-koab463wgBA-1 X-Mimecast-MFC-AGG-ID: yo8KD5nWN9-koab463wgBA_1792419578 Received: from mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.93]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 73ABD180076C; Mon, 19 Oct 2026 14:19:38 +0000 (UTC) Received: from blackfin.pond.sub.org (unknown [10.44.22.5]) by mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id E5AA518005BA; Mon, 19 Oct 2026 14:19:37 +0000 (UTC) Received: by blackfin.pond.sub.org (Postfix, from userid 1000) id B74CA21E6895; Mon, 19 Oct 2026 16:19:29 +0200 (CEST) From: Markus Armbruster To: qemu-devel@nongnu.org Cc: marcandre.lureau@redhat.com, pbonzini@redhat.com, berrange@redhat.com, michael.roth@amd.com, pierrick.bouvier@oss.qualcomm.com, philmd@oss.qualcomm.com, kkostiuk@redhat.com, alistair.francis@wdc.com, amachhiw@linux.ibm.com Subject: [PATCH v6 17/65] qom: add object_{class_}property_add_qapi_enum Date: Mon, 19 Oct 2026 16:18:41 +0200 Message-ID: <20261019141929.1594337-18-armbru@redhat.com> In-Reply-To: <20261019141929.1594337-1-armbru@redhat.com> References: <20261019141929.1594337-1-armbru@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.93 Received-SPF: pass client-ip=170.10.129.124; envelope-from=armbru@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -23 X-Spam_score: -2.4 X-Spam_bar: -- X-Spam_report: (-2.4 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.329, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org From: Marc-André Lureau Signed-off-by: Marc-André Lureau Signed-off-by: Markus Armbruster --- include/qom/object.h | 74 ++++++++++++++++++++++++++++++++++++ qom/object.c | 89 +++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 158 insertions(+), 5 deletions(-) diff --git a/include/qom/object.h b/include/qom/object.h index 365d819d35..ce9375f37a 100644 --- a/include/qom/object.h +++ b/include/qom/object.h @@ -2006,6 +2006,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; }) + +/** + * 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 diff --git a/qom/object.c b/qom/object.c index bebbfc4355..c1aa2b96fc 100644 --- a/qom/object.c +++ b/qom/object.c @@ -1219,7 +1219,7 @@ static void object_class_foreach_tramp(gpointer key, gpointer value, return; } - if (data->implements_type && + if (data->implements_type && !object_class_dynamic_cast(k, data->implements_type)) { return; } @@ -1786,7 +1786,6 @@ int object_property_get_enum(Object *obj, const char *name, char *str; int ret; ObjectProperty *prop = object_property_find_err(obj, name, errp); - EnumProperty *enumprop; if (prop == NULL) { return -1; @@ -1799,14 +1798,17 @@ int object_property_get_enum(Object *obj, const char *name, return -1; } - enumprop = prop->opaque; - str = object_property_get_str(obj, name, errp); if (!str) { return -1; } - ret = qapi_enum_parse(enumprop->lookup, str, -1, errp); + if (prop->qapi_type) { + ret = qapi_enum_parse(prop->qapi_type->lookup, str, -1, errp); + } else { + EnumProperty *enumprop = prop->opaque; + ret = qapi_enum_parse(enumprop->lookup, str, -1, errp); + } g_free(str); return ret; @@ -2609,6 +2611,83 @@ object_class_property_add_enum(ObjectClass *klass, const char *name, prop); } +static void get_qapi_enum(Object *obj, Visitor *v, const char *name, + void *opaque, Error **errp) +{ + const QapiEnumProp *prop = opaque; + int value; + Error *err = NULL; + + value = prop->get(obj, &err); + if (err) { + error_propagate(errp, err); + return; + } + + visit_type_enum(v, name, &value, prop->qapi_type->lookup, errp); +} + +static void set_qapi_enum(Object *obj, Visitor *v, const char *name, + void *opaque, Error **errp) +{ + const QapiEnumProp *prop = opaque; + int value; + + if (!visit_type_enum(v, name, &value, prop->qapi_type->lookup, errp)) { + return; + } + prop->set(obj, value, errp); +} + +static void init_qapi_enum(Object *obj, ObjectProperty *prop) +{ + const QapiEnumProp *e = prop->opaque; + + if (e->set && e->default_value >= 0) { + e->set(obj, e->default_value, &error_abort); + } +} + +ObjectProperty * +object_property_add_qapi_enum(Object *obj, const QapiEnumProp *e) +{ + ObjectProperty *prop; + + assert(e && e->qapi_type && e->qapi_type->lookup); + + prop = object_property_add_qapi(obj, e->name, e->qapi_type, + e->get ? get_qapi_enum : NULL, + e->set ? set_qapi_enum : NULL, + NULL, + (void *)e); + prop->description = g_strdup(e->description); + if (e->default_value >= 0) { + prop->init = init_qapi_enum; + } + + return prop; +} + +ObjectProperty * +object_class_property_add_qapi_enum(ObjectClass *klass, const QapiEnumProp *e) +{ + ObjectProperty *prop; + + assert(e && e->qapi_type && e->qapi_type->lookup); + + prop = object_class_property_add_qapi(klass, e->name, e->qapi_type, + e->get ? get_qapi_enum : NULL, + e->set ? set_qapi_enum : NULL, + NULL, + (void *)e); + prop->description = g_strdup(e->description); + if (e->default_value >= 0) { + prop->init = init_qapi_enum; + } + + return prop; +} + ObjectProperty * object_property_add_qapi(Object *obj, const char *name, const QAPITypeInfo *qapi_type, -- 2.55.0