From: Eric Blake <eblake@redhat.com>
To: qemu-devel@nongnu.org
Cc: kwolf@redhat.com, armbru@redhat.com, lcapitulino@redhat.com
Subject: [Qemu-devel] [PATCH v2 2/3] qobject: Add a special null QObject
Date: Wed, 29 Apr 2015 15:35:05 -0600 [thread overview]
Message-ID: <1430343306-6152-3-git-send-email-eblake@redhat.com> (raw)
In-Reply-To: <1430343306-6152-1-git-send-email-eblake@redhat.com>
From: Markus Armbruster <armbru@redhat.com>
I'm going to fix the JSON parser to recognize null. The obvious
representation of JSON null as (QObject *)NULL doesn't work, because
the parser already uses it as an error value. Perhaps we should
change it to free NULL for null, but that's more than I can do right
now. Create a special null QObject instead.
The existing QDict, QList, and QString all represent something that
is a pointer in C and could therefore be associated with NULL. But
right now, all three of these sub-types are always non-null once
created, so the new null sentinel object is intentionally unrelated
to them.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Signed-off-by: Eric Blake <eblake@redhat.com>
---
include/qapi/qmp/qobject.h | 11 ++++++++++-
qobject/Makefile.objs | 2 +-
qobject/qjson.c | 3 +++
qobject/qnull.c | 29 +++++++++++++++++++++++++++++
4 files changed, 43 insertions(+), 2 deletions(-)
create mode 100644 qobject/qnull.c
diff --git a/include/qapi/qmp/qobject.h b/include/qapi/qmp/qobject.h
index 0991296..84b2d9f 100644
--- a/include/qapi/qmp/qobject.h
+++ b/include/qapi/qmp/qobject.h
@@ -3,7 +3,7 @@
*
* Based on ideas by Avi Kivity <avi@redhat.com>
*
- * Copyright (C) 2009 Red Hat Inc.
+ * Copyright (C) 2009, 2015 Red Hat Inc.
*
* Authors:
* Luiz Capitulino <lcapitulino@redhat.com>
@@ -37,6 +37,7 @@
typedef enum {
QTYPE_NONE, /* sentinel value, no QObject has this type code */
+ QTYPE_QNULL,
QTYPE_QINT,
QTYPE_QSTRING,
QTYPE_QDICT,
@@ -110,4 +111,12 @@ static inline qtype_code qobject_type(const QObject *obj)
return obj->type->code;
}
+extern QObject qnull_;
+
+static inline QObject *qnull(void)
+{
+ qobject_incref(&qnull_);
+ return &qnull_;
+}
+
#endif /* QOBJECT_H */
diff --git a/qobject/Makefile.objs b/qobject/Makefile.objs
index c9ff59c..f7595f5 100644
--- a/qobject/Makefile.objs
+++ b/qobject/Makefile.objs
@@ -1,3 +1,3 @@
-util-obj-y = qint.o qstring.o qdict.o qlist.o qfloat.o qbool.o
+util-obj-y = qnull.o qint.o qstring.o qdict.o qlist.o qfloat.o qbool.o
util-obj-y += qjson.o json-lexer.o json-streamer.o json-parser.o
util-obj-y += qerror.o
diff --git a/qobject/qjson.c b/qobject/qjson.c
index f2857c1..846733d 100644
--- a/qobject/qjson.c
+++ b/qobject/qjson.c
@@ -127,6 +127,9 @@ static void to_json_list_iter(QObject *obj, void *opaque)
static void to_json(const QObject *obj, QString *str, int pretty, int indent)
{
switch (qobject_type(obj)) {
+ case QTYPE_QNULL:
+ qstring_append(str, "null");
+ break;
case QTYPE_QINT: {
QInt *val = qobject_to_qint(obj);
char buffer[1024];
diff --git a/qobject/qnull.c b/qobject/qnull.c
new file mode 100644
index 0000000..9873e26
--- /dev/null
+++ b/qobject/qnull.c
@@ -0,0 +1,29 @@
+/*
+ * QNull
+ *
+ * Copyright (C) 2015 Red Hat, Inc.
+ *
+ * Authors:
+ * Markus Armbruster <armbru@redhat.com>
+ *
+ * This work is licensed under the terms of the GNU LGPL, version 2.1
+ * or later. See the COPYING.LIB file in the top-level directory.
+ */
+
+#include "qemu-common.h"
+#include "qapi/qmp/qobject.h"
+
+static void qnull_destroy_obj(QObject *obj)
+{
+ assert(0);
+}
+
+static const QType qnull_type = {
+ .code = QTYPE_QNULL,
+ .destroy = qnull_destroy_obj,
+};
+
+QObject qnull_ = {
+ .type = &qnull_type,
+ .refcnt = 1,
+};
--
2.1.0
next prev parent reply other threads:[~2015-04-29 21:35 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-04-29 21:35 [Qemu-devel] [PATCH v2 0/3] parse 'null' literal in QMP Eric Blake
2015-04-29 21:35 ` [Qemu-devel] [PATCH v2 1/3] qobject: Clean up around qtype_code Eric Blake
2015-04-29 21:35 ` Eric Blake [this message]
2015-04-30 12:16 ` [Qemu-devel] [PATCH v2 2/3] qobject: Add a special null QObject Markus Armbruster
2015-04-29 21:35 ` [Qemu-devel] [PATCH v2 3/3] json-parser: Accept 'null' in QMP Eric Blake
2015-04-30 12:21 ` [Qemu-devel] [PATCH v2 0/3] parse 'null' literal " Markus Armbruster
2015-04-30 12:33 ` Luiz Capitulino
2015-05-01 17:16 ` Luiz Capitulino
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=1430343306-6152-3-git-send-email-eblake@redhat.com \
--to=eblake@redhat.com \
--cc=armbru@redhat.com \
--cc=kwolf@redhat.com \
--cc=lcapitulino@redhat.com \
--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).