From patchwork Mon Dec 12 20:29:42 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anthony Liguori X-Patchwork-Id: 130888 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [140.186.70.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 2B2D01007D3 for ; Tue, 13 Dec 2011 08:59:15 +1100 (EST) Received: from localhost ([::1]:46236 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RaDtr-0003tZ-QM for incoming@patchwork.ozlabs.org; Mon, 12 Dec 2011 16:59:07 -0500 Received: from eggs.gnu.org ([140.186.70.92]:45678) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RaCWl-0000lJ-Iv for qemu-devel@nongnu.org; Mon, 12 Dec 2011 15:31:12 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1RaCWk-0002S9-Gf for qemu-devel@nongnu.org; Mon, 12 Dec 2011 15:31:11 -0500 Received: from cpe-70-123-132-139.austin.res.rr.com ([70.123.132.139]:49881 helo=localhost6.localdomain6) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RaCWk-0002S1-AK for qemu-devel@nongnu.org; Mon, 12 Dec 2011 15:31:10 -0500 Received: from localhost6.localdomain6 (localhost.localdomain [127.0.0.1]) by localhost6.localdomain6 (8.14.4/8.14.4/Debian-2ubuntu1) with ESMTP id pBCKUx4i000864; Mon, 12 Dec 2011 14:30:59 -0600 Received: (from anthony@localhost) by localhost6.localdomain6 (8.14.4/8.14.4/Submit) id pBCKUvSG000863; Mon, 12 Dec 2011 14:30:57 -0600 From: Anthony Liguori To: qemu-devel@nongnu.org Date: Mon, 12 Dec 2011 14:29:42 -0600 Message-Id: <1323721784-704-19-git-send-email-aliguori@us.ibm.com> X-Mailer: git-send-email 1.7.4.1 In-Reply-To: <1323721784-704-1-git-send-email-aliguori@us.ibm.com> References: <1323721784-704-1-git-send-email-aliguori@us.ibm.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 70.123.132.139 Cc: Kevin Wolf , Peter Maydell , Anthony Liguori , Stefan Hajnoczi , Jan Kiszka , Markus Armbruster , Luiz Capitulino Subject: [Qemu-devel] [PATCH v3 18/20] qom: add string property type X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Signed-off-by: Anthony Liguori --- hw/qdev.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ hw/qdev.h | 22 ++++++++++++++++++++++ 2 files changed, 81 insertions(+), 0 deletions(-) diff --git a/hw/qdev.c b/hw/qdev.c index 4004860..0fc20fc 100644 --- a/hw/qdev.c +++ b/hw/qdev.c @@ -1449,3 +1449,62 @@ DeviceState *qdev_resolve_path(const char *path, bool *ambiguous) return dev; } +typedef struct StringProperty +{ + char *(*get)(DeviceState *, Error **); + void (*set)(DeviceState *, const char *, Error **); +} StringProperty; + +static void qdev_property_get_str(DeviceState *dev, Visitor *v, void *opaque, + const char *name, Error **errp) +{ + StringProperty *prop = opaque; + char *value; + + value = prop->get(dev, errp); + if (value) { + visit_type_str(v, &value, name, errp); + g_free(value); + } +} + +static void qdev_property_set_str(DeviceState *dev, Visitor *v, void *opaque, + const char *name, Error **errp) +{ + StringProperty *prop = opaque; + char *value; + Error *local_err = NULL; + + visit_type_str(v, &value, name, &local_err); + if (local_err) { + error_propagate(errp, local_err); + return; + } + + prop->set(dev, value, errp); + g_free(value); +} + +static void qdev_property_release_str(DeviceState *dev, const char *name, + void *opaque) +{ + StringProperty *prop = opaque; + g_free(prop); +} + +void qdev_property_add_str(DeviceState *dev, const char *name, + char *(*get)(DeviceState *, Error **), + void (*set)(DeviceState *, const char *, Error **), + Error **errp) +{ + StringProperty *prop = g_malloc0(sizeof(*prop)); + + prop->get = get; + prop->set = set; + + qdev_property_add(dev, name, "string", + get ? qdev_property_get_str : NULL, + set ? qdev_property_set_str : NULL, + qdev_property_release_str, + prop, errp); +} diff --git a/hw/qdev.h b/hw/qdev.h index fdab848..9faf2ee 100644 --- a/hw/qdev.h +++ b/hw/qdev.h @@ -574,4 +574,26 @@ void qdev_property_add_link(DeviceState *dev, const char *name, const char *type, DeviceState **child, Error **errp); +/** + * @qdev_property_add_str + * + * Add a string property using getters/setters. This function will add a + * property of type 'string'. + * + * @dev - the device to add a property to + * + * @name - the name of the property + * + * @get - the getter or NULL if the property is write-only. This function must + * return a string to be freed by @g_free(). + * + * @set - the setter or NULL if the property is read-only + * + * @errp - if an error occurs, a pointer to an area to store the error + */ +void qdev_property_add_str(DeviceState *dev, const char *name, + char *(*get)(DeviceState *, Error **), + void (*set)(DeviceState *, const char *, Error **), + Error **errp); + #endif