Commit 481b002c authored by Markus Armbruster's avatar Markus Armbruster Committed by Luiz Capitulino
Browse files

qobject: Add a special null QObject



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: default avatarMarkus Armbruster <armbru@redhat.com>
Signed-off-by: default avatarEric Blake <eblake@redhat.com>
Signed-off-by: default avatarLuiz Capitulino <lcapitulino@redhat.com>
parent a7c31816
Loading
Loading
Loading
Loading
+10 −1
Original line number Diff line number Diff line
@@ -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 */
+1 −1
Original line number Diff line number Diff line
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
+3 −0
Original line number Diff line number Diff line
@@ -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];

qobject/qnull.c

0 → 100644
+29 −0
Original line number Diff line number Diff line
/*
 * 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,
};