From: Max Reitz <mreitz@redhat.com>
To: qemu-block@nongnu.org
Cc: qemu-devel@nongnu.org, Max Reitz <mreitz@redhat.com>,
Markus Armbruster <armbru@redhat.com>,
Eric Blake <eblake@redhat.com>, Alberto Garcia <berto@igalia.com>,
Kevin Wolf <kwolf@redhat.com>
Subject: [Qemu-devel] [PATCH v2 4/6] qapi: Make more of qobject_to()
Date: Sat, 20 Jan 2018 16:44:10 +0100 [thread overview]
Message-ID: <20180120154412.9990-5-mreitz@redhat.com> (raw)
In-Reply-To: <20180120154412.9990-1-mreitz@redhat.com>
This patch reworks some places which use either qobject_type() checks
plus qobject_to(), where the latter alone is sufficient, or NULL checks
plus qobject_type() checks where we can simply do a qobject_to() != NULL
check.
Signed-off-by: Max Reitz <mreitz@redhat.com>
---
qapi/qobject-input-visitor.c | 4 ++--
qobject/json-parser.c | 13 +++++++------
qobject/qdict.c | 20 +++++++++++---------
3 files changed, 20 insertions(+), 17 deletions(-)
diff --git a/qapi/qobject-input-visitor.c b/qapi/qobject-input-visitor.c
index 45e6160b21..fa6b92d9f6 100644
--- a/qapi/qobject-input-visitor.c
+++ b/qapi/qobject-input-visitor.c
@@ -334,7 +334,7 @@ static GenericList *qobject_input_next_list(Visitor *v, GenericList *tail,
QObjectInputVisitor *qiv = to_qiv(v);
StackObject *tos = QSLIST_FIRST(&qiv->stack);
- assert(tos && tos->obj && qobject_type(tos->obj) == QTYPE_QLIST);
+ assert(tos && qobject_to(tos->obj, QList));
if (!tos->entry) {
return NULL;
@@ -348,7 +348,7 @@ static void qobject_input_check_list(Visitor *v, Error **errp)
QObjectInputVisitor *qiv = to_qiv(v);
StackObject *tos = QSLIST_FIRST(&qiv->stack);
- assert(tos && tos->obj && qobject_type(tos->obj) == QTYPE_QLIST);
+ assert(tos && qobject_to(tos->obj, QList));
if (tos->entry) {
error_setg(errp, "Only %u list elements expected in %s",
diff --git a/qobject/json-parser.c b/qobject/json-parser.c
index 885039e94b..c4d1f0d8de 100644
--- a/qobject/json-parser.c
+++ b/qobject/json-parser.c
@@ -271,7 +271,8 @@ static void parser_context_free(JSONParserContext *ctxt)
*/
static int parse_pair(JSONParserContext *ctxt, QDict *dict, va_list *ap)
{
- QObject *key = NULL, *value;
+ QObject *value;
+ QString *key = NULL;
JSONToken *peek, *token;
peek = parser_context_peek_token(ctxt);
@@ -280,8 +281,8 @@ static int parse_pair(JSONParserContext *ctxt, QDict *dict, va_list *ap)
goto out;
}
- key = parse_value(ctxt, ap);
- if (!key || qobject_type(key) != QTYPE_QSTRING) {
+ key = qobject_to(parse_value(ctxt, ap), QString);
+ if (!key) {
parse_error(ctxt, peek, "key is not a string in object");
goto out;
}
@@ -303,14 +304,14 @@ static int parse_pair(JSONParserContext *ctxt, QDict *dict, va_list *ap)
goto out;
}
- qdict_put_obj(dict, qstring_get_str(qobject_to(key, QString)), value);
+ qdict_put_obj(dict, qstring_get_str(key), value);
- qobject_decref(key);
+ QDECREF(key);
return 0;
out:
- qobject_decref(key);
+ QDECREF(key);
return -1;
}
diff --git a/qobject/qdict.c b/qobject/qdict.c
index ca84455883..3e97c2d3bc 100644
--- a/qobject/qdict.c
+++ b/qobject/qdict.c
@@ -861,18 +861,20 @@ QObject *qdict_crumple(const QDict *src, Error **errp)
child = qdict_get(two_level, prefix);
if (suffix) {
- if (child) {
- if (qobject_type(child) != QTYPE_QDICT) {
+ QDict *child_dict = qobject_to(child, QDict);
+ if (!child_dict) {
+ if (child) {
error_setg(errp, "Key %s prefix is already set as a scalar",
prefix);
goto error;
}
- } else {
- child = QOBJECT(qdict_new());
- qdict_put_obj(two_level, prefix, child);
+
+ child_dict = qdict_new();
+ qdict_put_obj(two_level, prefix, QOBJECT(child_dict));
}
+
qobject_incref(ent->value);
- qdict_put_obj(qobject_to(child, QDict), suffix, ent->value);
+ qdict_put_obj(child_dict, suffix, ent->value);
} else {
if (child) {
error_setg(errp, "Key %s prefix is already set as a dict",
@@ -892,9 +894,9 @@ QObject *qdict_crumple(const QDict *src, Error **errp)
multi_level = qdict_new();
for (ent = qdict_first(two_level); ent != NULL;
ent = qdict_next(two_level, ent)) {
-
- if (qobject_type(ent->value) == QTYPE_QDICT) {
- child = qdict_crumple(qobject_to(ent->value, QDict), errp);
+ QDict *dict = qobject_to(ent->value, QDict);
+ if (dict) {
+ child = qdict_crumple(dict, errp);
if (!child) {
goto error;
}
--
2.14.3
next prev parent reply other threads:[~2018-01-20 15:44 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-01-20 15:44 [Qemu-devel] [PATCH v2 0/6] block: Handle null backing link Max Reitz
2018-01-20 15:44 ` [Qemu-devel] [PATCH v2 1/6] qapi: Add qobject_to() Max Reitz
2018-01-22 15:33 ` Eric Blake
2018-01-22 16:53 ` Max Reitz
2018-01-31 16:02 ` Alberto Garcia
2018-01-20 15:44 ` [Qemu-devel] [PATCH v2 2/6] qapi: Replace qobject_to_X(o) by qobject_to(o, X) Max Reitz
2018-01-31 16:05 ` Alberto Garcia
2018-01-31 16:11 ` Eric Blake
2018-01-31 16:14 ` Eric Blake
2018-01-20 15:44 ` [Qemu-devel] [PATCH v2 3/6] qapi: Remove qobject_to_X() functions Max Reitz
2018-01-31 16:05 ` Alberto Garcia
2018-01-20 15:44 ` Max Reitz [this message]
2018-01-31 16:16 ` [Qemu-devel] [PATCH v2 4/6] qapi: Make more of qobject_to() Alberto Garcia
2018-01-20 15:44 ` [Qemu-devel] [PATCH v2 5/6] block: Handle null backing link Max Reitz
2018-02-01 14:17 ` Alberto Garcia
2018-01-20 15:44 ` [Qemu-devel] [PATCH v2 6/6] block: Deprecate "backing": "" Max Reitz
2018-01-31 16:22 ` Eric Blake
2018-02-01 14:18 ` Alberto Garcia
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20180120154412.9990-5-mreitz@redhat.com \
--to=mreitz@redhat.com \
--cc=armbru@redhat.com \
--cc=berto@igalia.com \
--cc=eblake@redhat.com \
--cc=kwolf@redhat.com \
--cc=qemu-block@nongnu.org \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).