@@ -61,6 +61,8 @@ QDict *qdict_get_qdict(const QDict *qdict, const char *key);
const char *qdict_get_str(const QDict *qdict, const char *key);
int64_t qdict_get_try_int(const QDict *qdict, const char *key,
int64_t def_value);
+uint64_t qdict_get_try_uint(const QDict *qdict, const char *key,
+ Error **errp);
bool qdict_get_try_bool(const QDict *qdict, const char *key, bool def_value);
const char *qdict_get_try_str(const QDict *qdict, const char *key);
@@ -12,6 +12,7 @@
#include "qemu/osdep.h"
#include "qapi/qmp/qint.h"
+#include "qapi/qmp/quint.h"
#include "qapi/qmp/qfloat.h"
#include "qapi/qmp/qdict.h"
#include "qapi/qmp/qbool.h"
@@ -181,7 +182,7 @@ 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.
+ * QFloat, QInt or QUInt object.
*
* Return number mapped by 'key'.
*/
@@ -195,6 +196,8 @@ double qdict_get_double(const QDict *qdict, const char *key)
return qfloat_get_double(qobject_to_qfloat(obj));
case QTYPE_QINT:
return qint_get_int(qobject_to_qint(obj));
+ case QTYPE_QUINT:
+ return quint_get_uint(qobject_to_quint(obj));
default:
abort();
}
@@ -272,6 +275,38 @@ int64_t qdict_get_try_int(const QDict *qdict, const char *key,
}
/**
+ * qdict_get_try_uint(): Try to get unsigned mapped by 'key'
+ *
+ * Return integer mapped by 'key', if it is not present in
+ * the dictionary or with negative value, returns 0 and set error.
+ */
+uint64_t qdict_get_try_uint(const QDict *qdict, const char *key,
+ Error **errp)
+{
+ QUInt *quint = qobject_to_quint(qdict_get(qdict, key));
+ QInt *qint;
+ int val;
+
+ if (quint) {
+ return quint_get_uint(quint);
+ }
+
+ qint = qobject_to_qint(qdict_get(qdict, key));
+ if (!qint) {
+ error_setg(errp, "Missing key or type mismatch");
+ return 0;
+ }
+
+ val = qint_get_int(qint);
+ if (val < 0) {
+ error_setg(errp, "Invalid value, unsigned int expected");
+ return 0;
+ }
+
+ return val;
+}
+
+/**
* qdict_get_try_bool(): Try to get a bool mapped by 'key'
*
* Return bool mapped by 'key', if it is not present in the
@@ -12,6 +12,7 @@
#include "qemu/osdep.h"
#include "qapi/qmp/qint.h"
+#include "qapi/qmp/quint.h"
#include "qapi/qmp/qdict.h"
#include "qapi/qmp/qstring.h"
#include "qapi/error.h"
@@ -121,6 +122,35 @@ static void qdict_get_try_int_test(void)
QDECREF(tests_dict);
}
+static void qdict_get_try_uint_test(void)
+{
+ uint64_t ret;
+ const int ivalue = 100;
+ const uint64_t uvalue = G_MAXUINT64;
+ const char *key = "uint";
+ QDict *tests_dict = qdict_new();
+ Error *err = NULL;
+
+ qdict_put(tests_dict, key, qint_from_int(ivalue));
+ /* try_uint will work with int types too */
+ ret = qdict_get_try_uint(tests_dict, key, &err);
+ g_assert_cmpint(ret, ==, ivalue);
+ g_assert(!err);
+
+ qdict_put(tests_dict, key, quint_from_uint(uvalue));
+ ret = qdict_get_try_uint(tests_dict, key, &err);
+ g_assert_cmpint(ret, ==, uvalue);
+ g_assert(!err);
+
+ qdict_put(tests_dict, key, qint_from_int(-1));
+ /* try_uint will fail with negative values */
+ ret = qdict_get_try_uint(tests_dict, key, &err);
+ g_assert(err);
+ error_free(err);
+
+ QDECREF(tests_dict);
+}
+
static void qdict_get_str_test(void)
{
const char *p;
@@ -854,6 +884,7 @@ int main(int argc, char **argv)
g_test_add_func("/public/get", qdict_get_test);
g_test_add_func("/public/get_int", qdict_get_int_test);
g_test_add_func("/public/get_try_int", qdict_get_try_int_test);
+ g_test_add_func("/public/get_try_uint", qdict_get_try_uint_test);
g_test_add_func("/public/get_str", qdict_get_str_test);
g_test_add_func("/public/get_try_str", qdict_get_try_str_test);
g_test_add_func("/public/defaults", qdict_defaults_test);
@@ -959,6 +959,12 @@ static void qemu_opts_from_qdict_1(const char *key, QObject *obj, void *opaque)
assert(n < sizeof(buf));
value = buf;
break;
+ case QTYPE_QUINT:
+ n = snprintf(buf, sizeof(buf), "%" PRIu64,
+ quint_get_uint(qobject_to_quint(obj)));
+ assert(n < sizeof(buf));
+ value = buf;
+ break;
case QTYPE_QFLOAT:
n = snprintf(buf, sizeof(buf), "%.17g",
qfloat_get_double(qobject_to_qfloat(obj)));
Since a number may be parsed as an integer if it fits, learn to deal with this situtation and fallback on QInt if QUint failed. There is no need for exact type getters in qemu yet. Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com> --- include/qapi/qmp/qdict.h | 2 ++ qobject/qdict.c | 37 ++++++++++++++++++++++++++++++++++++- tests/check-qdict.c | 31 +++++++++++++++++++++++++++++++ util/qemu-option.c | 6 ++++++ 4 files changed, 75 insertions(+), 1 deletion(-)