From 01b2ffcedd94ad7b42bc870e4c6936c87ad03429 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Date: Wed, 7 Jun 2017 20:35:58 +0400 Subject: qapi: merge QInt and QFloat in QNum MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We would like to use a same QObject type to represent numbers, whether they are int, uint, or floats. Getters will allow some compatibility between the various types if the number fits other representations. Add a few more tests while at it. Signed-off-by: Marc-André Lureau Message-Id: <20170607163635.17635-7-marcandre.lureau@redhat.com> Reviewed-by: Markus Armbruster [parse_stats_intervals() simplified a bit, comment in test_visitor_in_int_overflow() tidied up, suppress bogus warnings] Signed-off-by: Markus Armbruster --- qobject/qdict.c | 37 +++++++++++++++---------------------- 1 file changed, 15 insertions(+), 22 deletions(-) (limited to 'qobject/qdict.c') diff --git a/qobject/qdict.c b/qobject/qdict.c index 88e2ecd658..576018e531 100644 --- a/qobject/qdict.c +++ b/qobject/qdict.c @@ -11,8 +11,7 @@ */ #include "qemu/osdep.h" -#include "qapi/qmp/qint.h" -#include "qapi/qmp/qfloat.h" +#include "qapi/qmp/qnum.h" #include "qapi/qmp/qdict.h" #include "qapi/qmp/qbool.h" #include "qapi/qmp/qstring.h" @@ -180,37 +179,26 @@ size_t qdict_size(const QDict *qdict) /** * qdict_get_double(): Get an number mapped by 'key' * - * This function assumes that 'key' exists and it stores a - * QFloat or QInt object. + * This function assumes that 'key' exists and it stores a QNum. * * Return number mapped by 'key'. */ double qdict_get_double(const QDict *qdict, const char *key) { - QObject *obj = qdict_get(qdict, key); - - assert(obj); - switch (qobject_type(obj)) { - case QTYPE_QFLOAT: - return qfloat_get_double(qobject_to_qfloat(obj)); - case QTYPE_QINT: - return qint_get_int(qobject_to_qint(obj)); - default: - abort(); - } + return qnum_get_double(qobject_to_qnum(qdict_get(qdict, key))); } /** * qdict_get_int(): Get an integer mapped by 'key' * * This function assumes that 'key' exists and it stores a - * QInt object. + * QNum representable as int. * * Return integer mapped by 'key'. */ int64_t qdict_get_int(const QDict *qdict, const char *key) { - return qint_get_int(qobject_to_qint(qdict_get(qdict, key))); + return qnum_get_int(qobject_to_qnum(qdict_get(qdict, key))); } /** @@ -259,16 +247,21 @@ const char *qdict_get_str(const QDict *qdict, const char *key) /** * qdict_get_try_int(): Try to get integer mapped by 'key' * - * Return integer mapped by 'key', if it is not present in - * the dictionary or if the stored object is not of QInt type - * 'def_value' will be returned. + * Return integer mapped by 'key', if it is not present in the + * dictionary or if the stored object is not a QNum representing an + * integer, 'def_value' will be returned. */ int64_t qdict_get_try_int(const QDict *qdict, const char *key, int64_t def_value) { - QInt *qint = qobject_to_qint(qdict_get(qdict, key)); + QNum *qnum = qobject_to_qnum(qdict_get(qdict, key)); + int64_t val; + + if (!qnum || !qnum_get_try_int(qnum, &val)) { + return def_value; + } - return qint ? qint_get_int(qint) : def_value; + return val; } /** -- cgit v1.2.3