From mboxrd@z Thu Jan 1 00:00:00 1970 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 24B97C5DF7D for ; Tue, 18 Aug 2026 11:17:10 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1wwHoF-0007QS-Sx; Tue, 18 Aug 2026 07:16:47 -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 1wwHoF-0007Q3-03 for qemu-devel@nongnu.org; Tue, 18 Aug 2026 07:16:47 -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 1wwHoD-0005TG-A0 for qemu-devel@nongnu.org; Tue, 18 Aug 2026 07:16:46 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1787051804; 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=geAxzwWzCqnvRTHj+zaFdvrGdVNHEUNtRmAOO42h0qQ=; b=hvHpzkLMx0dZt9a6dQ/02SY5xOohyxxdCZ/DqBn4DYszL9ZtNRSrpAiXunKKAvnLPrSGm3 dykoEYaTPJf01EQKsUOsQ2r7DD+2zIQtsPRHOCaidKHSLxs9eLYkZh1a+ofY8RiFJ1WA2t f4k1Jt/Ns1Oe5SsE83cfeq8NMLfiDVI= Received: from mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-537-3r-5S9fNM0OTSZw55Hz85Q-1; Tue, 18 Aug 2026 07:16:41 -0400 X-MC-Unique: 3r-5S9fNM0OTSZw55Hz85Q-1 X-Mimecast-MFC-AGG-ID: 3r-5S9fNM0OTSZw55Hz85Q_1787051800 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (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-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 1F3071954B07; Tue, 18 Aug 2026 11:16:40 +0000 (UTC) Received: from localhost (headnet05.pony-001.prod.iad2.dc.redhat.com [10.2.32.117]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id A591630002E9; Tue, 18 Aug 2026 11:16:34 +0000 (UTC) From: =?utf-8?q?Marc-Andr=C3=A9_Lureau?= Date: Tue, 18 Aug 2026 15:10:39 +0400 Subject: [PATCH v3 12/74] qom: add object_{class_}property_add_qapi_enum MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit Message-Id: <20260818-qom-qapi-v3-12-24b8bbbe3d86@redhat.com> References: <20260818-qom-qapi-v3-0-24b8bbbe3d86@redhat.com> In-Reply-To: <20260818-qom-qapi-v3-0-24b8bbbe3d86@redhat.com> To: qemu-devel@nongnu.org Cc: Markus Armbruster , Paolo Bonzini , =?utf-8?q?Daniel_P=2E_Berrang=C3=A9?= , =?utf-8?q?Marc-Andr=C3=A9_Lureau?= , Michael Roth , Pierrick Bouvier , =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= X-Developer-Signature: v=1; a=openpgp-sha256; l=7253; i=marcandre.lureau@redhat.com; h=from:subject:message-id; bh=wcgqrsFY697MpF1aOWpauni1B+DwPa5waKS53/E14F0=; b=owEBbQKS/ZANAwAKAdro4Ql1lpzlAcsmYgBqhD23jE9eua/C0OUUDPPKpmlAzud/FvIv0Mqqp 7X6d04/UfeJAjMEAAEKAB0WIQSHqb2TP4fGBtJ29i3a6OEJdZac5QUCaoQ9twAKCRDa6OEJdZac 5d2KEACrNihkF+FeD9O8S93BmE9SVKYjpD595VmhQ2J4VW2jmPU/G+gBcmabmus0CXj+ycyW2MV 7inInz+/n9tTvwg1U6LBulmt5rByUy0P75qlH+B8LC83k3NanY1dl4brOd7t27m4Bc/XIBGDq1q yV67tS5OLykH7BWc3cCX2dlaw6AK3QN6wbqjr1W9QNHjECuTua90eA7f6k7I6BJ53fCWRlmZTb+ 7B74OwSvv/iInapvpWxyT31mXy9LNxyp4EKsOM/7ZSvYDNldafvKBuVnwajC2xKM5U0HP5y7Z6f AnkQ7IozMeyN2ZSDh4AARuy19669zSO5P2N6Crdr6173O3EAO5c0mbATgcmCecJuVp6hIJxdZzf XWdtYShmSm7Dz770pPC0LMWb2PSP4wXiV/PJSE/iYA6etJu8vOmaJJmXERF8sveOzvmKbLyxR6b +Z4jOZ5cTeVW/jI2wECaErg3EMKgza1hb6asR4N1FHpmM80+DIl/mQ5D6af81MYkE4/UjdUheMN Y+nXp0lJETVwC/wnhAvowY7f3ngGFcOLpUkVG2KxD2BMgVSeWbX1GlvpHW4zcNgQ5JT4bKUiUM5 ljVLsxL0DB3REiiwJ0L6G1maOU/gnUm0Aghy+sCNO44SmYGdRWSWLtJ8upcMULyrKoMiDEivaf4 +gIROxSy1qtbvpA== X-Developer-Key: i=marcandre.lureau@redhat.com; a=openpgp; fpr=87A9BD933F87C606D276F62DDAE8E10975969CE5 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 Received-SPF: pass client-ip=170.10.129.124; envelope-from=marcandre.lureau@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: 9 X-Spam_score: 0.9 X-Spam_bar: / X-Spam_report: (0.9 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.343, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=0.001, RCVD_IN_SBL_CSS=3.335, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=no 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 Signed-off-by: Marc-André Lureau --- include/qom/object.h | 64 +++++++++++++++++++++++++++++++++++++ qom/object.c | 89 +++++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 148 insertions(+), 5 deletions(-) diff --git a/include/qom/object.h b/include/qom/object.h index 9684db935d23..a03f4bf8f7cf 100644 --- a/include/qom/object.h +++ b/include/qom/object.h @@ -1990,6 +1990,70 @@ ObjectProperty *object_class_property_add_enum(ObjectClass *klass, int (*get)(Object *, Error **), void (*set)(Object *, int, Error **)); +/** + * struct QapiEnumProp - Descriptor for a QOM 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. + * + * 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_property_add_qapi_enum(Object *obj, const QapiEnumProp *prop); + +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 1bfb6b933339..f5568b9f9790 100644 --- a/qom/object.c +++ b/qom/object.c @@ -1206,7 +1206,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; } @@ -1772,7 +1772,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; @@ -1785,14 +1784,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; @@ -2595,6 +2597,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.543.g5ebe2ebe4ea8