From patchwork Thu Feb 2 16:45:31 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 139174 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 67013104792 for ; Fri, 3 Feb 2012 04:12:36 +1100 (EST) Received: from localhost ([::1]:51597 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RszoA-00065A-Cr for incoming@patchwork.ozlabs.org; Thu, 02 Feb 2012 11:46:50 -0500 Received: from eggs.gnu.org ([140.186.70.92]:35267) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RsznY-0004X0-UY for qemu-devel@nongnu.org; Thu, 02 Feb 2012 11:46:17 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1RsznU-0002Zx-MQ for qemu-devel@nongnu.org; Thu, 02 Feb 2012 11:46:12 -0500 Received: from mail-pz0-f45.google.com ([209.85.210.45]:35764) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RsznU-0002WX-Bl for qemu-devel@nongnu.org; Thu, 02 Feb 2012 11:46:08 -0500 Received: by mail-pz0-f45.google.com with SMTP id p14so2506378dad.4 for ; Thu, 02 Feb 2012 08:46:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=sender:from:to:subject:date:message-id:x-mailer:in-reply-to :references; bh=87vpRt2py3hQqI9PxScnIQdnS0/4bU8ogw6kLPkz2Pk=; b=PEhz1bBl7MORgDgS/+E6SAf+bbKU/YvC93KNWHaAEg4ipCeeOn0hY1uQTxtkJbBVEC FljRGQHLlXrrToTKCUk8yE58pOMfrfQrBTZjm+6M++Wry30ZAaTuueF129ZKXnrt350A wHC/cWQ8uiXDN6np1ZYlEV3dlbcPj+rwl2+4k= Received: by 10.68.132.66 with SMTP id os2mr9047267pbb.50.1328201167196; Thu, 02 Feb 2012 08:46:07 -0800 (PST) Received: from yakj.usersys.redhat.com (93-34-182-16.ip50.fastwebnet.it. [93.34.182.16]) by mx.google.com with ESMTPS id x4sm6756321pbx.16.2012.02.02.08.46.04 (version=TLSv1/SSLv3 cipher=OTHER); Thu, 02 Feb 2012 08:46:06 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Thu, 2 Feb 2012 17:45:31 +0100 Message-Id: <1328201142-26145-6-git-send-email-pbonzini@redhat.com> X-Mailer: git-send-email 1.7.7.6 In-Reply-To: <1328201142-26145-1-git-send-email-pbonzini@redhat.com> References: <1328201142-26145-1-git-send-email-pbonzini@redhat.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 209.85.210.45 Subject: [Qemu-devel] [PATCH 05/16] qom: add property get/set wrappers for C types 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 Add more wrappers that create a QObject and free it around a property set, and that convert a QObject to a C type for a property get. Signed-off-by: Paolo Bonzini --- include/qemu/object.h | 70 ++++++++++++++++++++++++++++++ qom/object.c | 115 +++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 176 insertions(+), 9 deletions(-) diff --git a/include/qemu/object.h b/include/qemu/object.h index 71090f2..1dcaea2 100644 --- a/include/qemu/object.h +++ b/include/qemu/object.h @@ -554,6 +554,76 @@ struct QObject *object_property_get_qobject(Object *obj, const char *name, struct Error **errp); /** + * object_property_set_str: + * @value: the value to be written to the property + * @name: the name of the property + * @errp: returns an error if this function fails + * + * Writes a string value to a property. + */ +void object_property_set_str(Object *obj, const char *value, + const char *name, struct Error **errp); + +/** + * object_property_get_str: + * @obj: the object + * @name: the name of the property + * @errp: returns an error if this function fails + * + * Returns: the value of the property, converted to a C string, or NULL if + * an error occurs (including when the property value is not a string). + * The caller should free the string. + */ +char *object_property_get_str(Object *obj, const char *name, + struct Error **errp); + +/** + * object_property_set_bool: + * @value: the value to be written to the property + * @name: the name of the property + * @errp: returns an error if this function fails + * + * Writes a bool value to a property. + */ +void object_property_set_bool(Object *obj, bool value, + const char *name, struct Error **errp); + +/** + * object_property_get_bool: + * @obj: the object + * @name: the name of the property + * @errp: returns an error if this function fails + * + * Returns: the value of the property, converted to a boolean, or NULL if + * an error occurs (including when the property value is not a bool). + */ +bool object_property_get_bool(Object *obj, const char *name, + struct Error **errp); + +/** + * object_property_set_int: + * @value: the value to be written to the property + * @name: the name of the property + * @errp: returns an error if this function fails + * + * Writes an integer value to a property. + */ +void object_property_set_int(Object *obj, int64_t value, + const char *name, struct Error **errp); + +/** + * object_property_get_int: + * @obj: the object + * @name: the name of the property + * @errp: returns an error if this function fails + * + * Returns: the value of the property, converted to an integer, or NULL if + * an error occurs (including when the property value is not an integer). + */ +int64_t object_property_get_int(Object *obj, const char *name, + struct Error **errp); + +/** * object_property_set: * @obj: the object * @v: the visitor that will be used to write the property value. This should diff --git a/qom/object.c b/qom/object.c index 13c8bec..f1a1261 100644 --- a/qom/object.c +++ b/qom/object.c @@ -15,6 +15,10 @@ #include "qapi/qapi-visit-core.h" #include "qapi/qmp-input-visitor.h" #include "qapi/qmp-output-visitor.h" +#include "qobject.h" +#include "qbool.h" +#include "qint.h" +#include "qstring.h" #define MAX_INTERFACES 32 @@ -675,6 +679,99 @@ QObject *object_property_get_qobject(Object *obj, const char *name, return ret; } +void object_property_set_str(Object *obj, const char *value, + const char *name, Error **errp) +{ + QString *qstr = qstring_from_str(value); + object_property_set_qobject(obj, QOBJECT(qstr), name, errp); + + QDECREF(qstr); +} + +char *object_property_get_str(Object *obj, const char *name, + Error **errp) +{ + QObject *ret = object_property_get_qobject(obj, name, errp); + QString *qstring; + char *retval; + + if (!ret) { + return NULL; + } + qstring = qobject_to_qstring(ret); + if (!qstring) { + error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "string"); + retval = NULL; + } else { + retval = g_strdup(qstring_get_str(qstring)); + } + + QDECREF(qstring); + return retval; +} + +void object_property_set_bool(Object *obj, bool value, + const char *name, Error **errp) +{ + QBool *qbool = qbool_from_int(value); + object_property_set_qobject(obj, QOBJECT(qbool), name, errp); + + QDECREF(qbool); +} + +bool object_property_get_bool(Object *obj, const char *name, + Error **errp) +{ + QObject *ret = object_property_get_qobject(obj, name, errp); + QBool *qbool; + bool retval; + + if (!ret) { + return false; + } + qbool = qobject_to_qbool(ret); + if (!qbool) { + error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "boolean"); + retval = false; + } else { + retval = qbool_get_int(qbool); + } + + QDECREF(qbool); + return retval; +} + +void object_property_set_int(Object *obj, int64_t value, + const char *name, Error **errp) +{ + QInt *qint = qint_from_int(value); + object_property_set_qobject(obj, QOBJECT(qint), name, errp); + + QDECREF(qint); +} + +int64_t object_property_get_int(Object *obj, const char *name, + Error **errp) +{ + QObject *ret = object_property_get_qobject(obj, name, errp); + QInt *qint; + int64_t retval; + + if (!ret) { + return -1; + } + qint = qobject_to_qint(ret); + if (!qint) { + error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "int"); + retval = -1; + } else { + retval = qint_get_int(qint); + } + + QDECREF(qint); + return retval; +} + const char *object_property_get_type(Object *obj, const char *name, Error **errp) { ObjectProperty *prop = object_property_find(obj, name); @@ -957,8 +1054,8 @@ typedef struct StringProperty void (*set)(Object *, const char *, Error **); } StringProperty; -static void object_property_get_str(Object *obj, Visitor *v, void *opaque, - const char *name, Error **errp) +static void property_get_str(Object *obj, Visitor *v, void *opaque, + const char *name, Error **errp) { StringProperty *prop = opaque; char *value; @@ -970,8 +1067,8 @@ static void object_property_get_str(Object *obj, Visitor *v, void *opaque, } } -static void object_property_set_str(Object *obj, Visitor *v, void *opaque, - const char *name, Error **errp) +static void property_set_str(Object *obj, Visitor *v, void *opaque, + const char *name, Error **errp) { StringProperty *prop = opaque; char *value; @@ -987,8 +1084,8 @@ static void object_property_set_str(Object *obj, Visitor *v, void *opaque, g_free(value); } -static void object_property_release_str(Object *obj, const char *name, - void *opaque) +static void property_release_str(Object *obj, const char *name, + void *opaque) { StringProperty *prop = opaque; g_free(prop); @@ -1005,8 +1102,8 @@ void object_property_add_str(Object *obj, const char *name, prop->set = set; object_property_add(obj, name, "string", - get ? object_property_get_str : NULL, - set ? object_property_set_str : NULL, - object_property_release_str, + get ? property_get_str : NULL, + set ? property_set_str : NULL, + property_release_str, prop, errp); }