diff options
Diffstat (limited to 'qobject')
-rw-r--r-- | qobject/json-parser.c | 13 | ||||
-rw-r--r-- | qobject/qbool.c | 15 | ||||
-rw-r--r-- | qobject/qdict.c | 65 | ||||
-rw-r--r-- | qobject/qjson.c | 10 | ||||
-rw-r--r-- | qobject/qlist.c | 17 | ||||
-rw-r--r-- | qobject/qlit.c | 47 | ||||
-rw-r--r-- | qobject/qnum.c | 17 | ||||
-rw-r--r-- | qobject/qstring.c | 38 |
8 files changed, 114 insertions, 108 deletions
diff --git a/qobject/json-parser.c b/qobject/json-parser.c index b724562415..769b960c9f 100644 --- a/qobject/json-parser.c +++ b/qobject/json-parser.c @@ -276,7 +276,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); @@ -285,8 +286,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(QString, parse_value(ctxt, ap)); + if (!key) { parse_error(ctxt, peek, "key is not a string in object"); goto out; } @@ -308,14 +309,14 @@ static int parse_pair(JSONParserContext *ctxt, QDict *dict, va_list *ap) goto out; } - qdict_put_obj(dict, qstring_get_str(qobject_to_qstring(key)), 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/qbool.c b/qobject/qbool.c index e5a7a53879..b58249925c 100644 --- a/qobject/qbool.c +++ b/qobject/qbool.c @@ -40,22 +40,11 @@ bool qbool_get_bool(const QBool *qb) } /** - * qobject_to_qbool(): Convert a QObject into a QBool - */ -QBool *qobject_to_qbool(const QObject *obj) -{ - if (!obj || qobject_type(obj) != QTYPE_QBOOL) { - return NULL; - } - return container_of(obj, QBool, base); -} - -/** * qbool_is_equal(): Test whether the two QBools are equal */ bool qbool_is_equal(const QObject *x, const QObject *y) { - return qobject_to_qbool(x)->value == qobject_to_qbool(y)->value; + return qobject_to(QBool, x)->value == qobject_to(QBool, y)->value; } /** @@ -65,5 +54,5 @@ bool qbool_is_equal(const QObject *x, const QObject *y) void qbool_destroy_obj(QObject *obj) { assert(obj != NULL); - g_free(qobject_to_qbool(obj)); + g_free(qobject_to(QBool, obj)); } diff --git a/qobject/qdict.c b/qobject/qdict.c index 229b8c840b..d1997a0d8a 100644 --- a/qobject/qdict.c +++ b/qobject/qdict.c @@ -38,17 +38,6 @@ QDict *qdict_new(void) } /** - * qobject_to_qdict(): Convert a QObject into a QDict - */ -QDict *qobject_to_qdict(const QObject *obj) -{ - if (!obj || qobject_type(obj) != QTYPE_QDICT) { - return NULL; - } - return container_of(obj, QDict, base); -} - -/** * tdb_hash(): based on the hash agorithm from gdbm, via tdb * (from module-init-tools) */ @@ -206,7 +195,7 @@ size_t qdict_size(const QDict *qdict) */ double qdict_get_double(const QDict *qdict, const char *key) { - return qnum_get_double(qobject_to_qnum(qdict_get(qdict, key))); + return qnum_get_double(qobject_to(QNum, qdict_get(qdict, key))); } /** @@ -219,7 +208,7 @@ double qdict_get_double(const QDict *qdict, const char *key) */ int64_t qdict_get_int(const QDict *qdict, const char *key) { - return qnum_get_int(qobject_to_qnum(qdict_get(qdict, key))); + return qnum_get_int(qobject_to(QNum, qdict_get(qdict, key))); } /** @@ -232,7 +221,7 @@ int64_t qdict_get_int(const QDict *qdict, const char *key) */ bool qdict_get_bool(const QDict *qdict, const char *key) { - return qbool_get_bool(qobject_to_qbool(qdict_get(qdict, key))); + return qbool_get_bool(qobject_to(QBool, qdict_get(qdict, key))); } /** @@ -240,7 +229,7 @@ bool qdict_get_bool(const QDict *qdict, const char *key) */ QList *qdict_get_qlist(const QDict *qdict, const char *key) { - return qobject_to_qlist(qdict_get(qdict, key)); + return qobject_to(QList, qdict_get(qdict, key)); } /** @@ -248,7 +237,7 @@ QList *qdict_get_qlist(const QDict *qdict, const char *key) */ QDict *qdict_get_qdict(const QDict *qdict, const char *key) { - return qobject_to_qdict(qdict_get(qdict, key)); + return qobject_to(QDict, qdict_get(qdict, key)); } /** @@ -262,7 +251,7 @@ QDict *qdict_get_qdict(const QDict *qdict, const char *key) */ const char *qdict_get_str(const QDict *qdict, const char *key) { - return qstring_get_str(qobject_to_qstring(qdict_get(qdict, key))); + return qstring_get_str(qobject_to(QString, qdict_get(qdict, key))); } /** @@ -275,7 +264,7 @@ 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) { - QNum *qnum = qobject_to_qnum(qdict_get(qdict, key)); + QNum *qnum = qobject_to(QNum, qdict_get(qdict, key)); int64_t val; if (!qnum || !qnum_get_try_int(qnum, &val)) { @@ -294,7 +283,7 @@ int64_t qdict_get_try_int(const QDict *qdict, const char *key, */ bool qdict_get_try_bool(const QDict *qdict, const char *key, bool def_value) { - QBool *qbool = qobject_to_qbool(qdict_get(qdict, key)); + QBool *qbool = qobject_to(QBool, qdict_get(qdict, key)); return qbool ? qbool_get_bool(qbool) : def_value; } @@ -309,7 +298,7 @@ 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) { - QString *qstr = qobject_to_qstring(qdict_get(qdict, key)); + QString *qstr = qobject_to(QString, qdict_get(qdict, key)); return qstr ? qstring_get_str(qstr) : NULL; } @@ -432,8 +421,8 @@ void qdict_del(QDict *qdict, const char *key) */ bool qdict_is_equal(const QObject *x, const QObject *y) { - const QDict *dict_x = qobject_to_qdict(x); - const QDict *dict_y = qobject_to_qdict(y); + const QDict *dict_x = qobject_to(QDict, x); + const QDict *dict_y = qobject_to(QDict, y); const QDictEntry *e; if (qdict_size(dict_x) != qdict_size(dict_y)) { @@ -461,7 +450,7 @@ void qdict_destroy_obj(QObject *obj) QDict *qdict; assert(obj != NULL); - qdict = qobject_to_qdict(obj); + qdict = qobject_to(QDict, obj); for (i = 0; i < QDICT_BUCKET_MAX; i++) { QDictEntry *entry = QLIST_FIRST(&qdict->table[i]); @@ -532,9 +521,9 @@ static void qdict_flatten_qlist(QList *qlist, QDict *target, const char *prefix) new_key = g_strdup_printf("%s.%i", prefix, i); if (qobject_type(value) == QTYPE_QDICT) { - qdict_flatten_qdict(qobject_to_qdict(value), target, new_key); + qdict_flatten_qdict(qobject_to(QDict, value), target, new_key); } else if (qobject_type(value) == QTYPE_QLIST) { - qdict_flatten_qlist(qobject_to_qlist(value), target, new_key); + qdict_flatten_qlist(qobject_to(QList, value), target, new_key); } else { /* All other types are moved to the target unchanged. */ qobject_incref(value); @@ -568,11 +557,11 @@ static void qdict_flatten_qdict(QDict *qdict, QDict *target, const char *prefix) if (qobject_type(value) == QTYPE_QDICT) { /* Entries of QDicts are processed recursively, the QDict object * itself disappears. */ - qdict_flatten_qdict(qobject_to_qdict(value), target, + qdict_flatten_qdict(qobject_to(QDict, value), target, new_key ? new_key : entry->key); delete = true; } else if (qobject_type(value) == QTYPE_QLIST) { - qdict_flatten_qlist(qobject_to_qlist(value), target, + qdict_flatten_qlist(qobject_to(QList, value), target, new_key ? new_key : entry->key); delete = true; } else if (prefix) { @@ -893,18 +882,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(QDict, child); + 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_qdict(child), 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", @@ -924,9 +915,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_qdict(ent->value), errp); + QDict *dict = qobject_to(QDict, ent->value); + if (dict) { + child = qdict_crumple(dict, errp); if (!child) { goto error; } @@ -961,7 +952,7 @@ QObject *qdict_crumple(const QDict *src, Error **errp) } qobject_incref(child); - qlist_append_obj(qobject_to_qlist(dst), child); + qlist_append_obj(qobject_to(QList, dst), child); } QDECREF(multi_level); multi_level = NULL; diff --git a/qobject/qjson.c b/qobject/qjson.c index e1ce75651c..655d38adf1 100644 --- a/qobject/qjson.c +++ b/qobject/qjson.c @@ -137,14 +137,14 @@ static void to_json(const QObject *obj, QString *str, int pretty, int indent) qstring_append(str, "null"); break; case QTYPE_QNUM: { - QNum *val = qobject_to_qnum(obj); + QNum *val = qobject_to(QNum, obj); char *buffer = qnum_to_string(val); qstring_append(str, buffer); g_free(buffer); break; } case QTYPE_QSTRING: { - QString *val = qobject_to_qstring(obj); + QString *val = qobject_to(QString, obj); const char *ptr; int cp; char buf[16]; @@ -201,7 +201,7 @@ static void to_json(const QObject *obj, QString *str, int pretty, int indent) } case QTYPE_QDICT: { ToJsonIterState s; - QDict *val = qobject_to_qdict(obj); + QDict *val = qobject_to(QDict, obj); s.count = 0; s.str = str; @@ -220,7 +220,7 @@ static void to_json(const QObject *obj, QString *str, int pretty, int indent) } case QTYPE_QLIST: { ToJsonIterState s; - QList *val = qobject_to_qlist(obj); + QList *val = qobject_to(QList, obj); s.count = 0; s.str = str; @@ -238,7 +238,7 @@ static void to_json(const QObject *obj, QString *str, int pretty, int indent) break; } case QTYPE_QBOOL: { - QBool *val = qobject_to_qbool(obj); + QBool *val = qobject_to(QBool, obj); if (qbool_get_bool(val)) { qstring_append(str, "true"); diff --git a/qobject/qlist.c b/qobject/qlist.c index 613a95c12b..954fe98375 100644 --- a/qobject/qlist.c +++ b/qobject/qlist.c @@ -152,17 +152,6 @@ size_t qlist_size(const QList *qlist) } /** - * qobject_to_qlist(): Convert a QObject into a QList - */ -QList *qobject_to_qlist(const QObject *obj) -{ - if (!obj || qobject_type(obj) != QTYPE_QLIST) { - return NULL; - } - return container_of(obj, QList, base); -} - -/** * qlist_is_equal(): Test whether the two QLists are equal * * In order to be considered equal, the respective two objects at each @@ -173,8 +162,8 @@ QList *qobject_to_qlist(const QObject *obj) */ bool qlist_is_equal(const QObject *x, const QObject *y) { - const QList *list_x = qobject_to_qlist(x); - const QList *list_y = qobject_to_qlist(y); + const QList *list_x = qobject_to(QList, x); + const QList *list_y = qobject_to(QList, y); const QListEntry *entry_x, *entry_y; entry_x = qlist_first(list_x); @@ -203,7 +192,7 @@ void qlist_destroy_obj(QObject *obj) QListEntry *entry, *next_entry; assert(obj != NULL); - qlist = qobject_to_qlist(obj); + qlist = qobject_to(QList, obj); QTAILQ_FOREACH_SAFE(entry, &qlist->head, next, next_entry) { QTAILQ_REMOVE(&qlist->head, entry, next); diff --git a/qobject/qlit.c b/qobject/qlit.c index 948e0b860c..be8332136c 100644 --- a/qobject/qlit.c +++ b/qobject/qlit.c @@ -21,6 +21,7 @@ #include "qapi/qmp/qnum.h" #include "qapi/qmp/qdict.h" #include "qapi/qmp/qstring.h" +#include "qapi/qmp/qnull.h" static bool qlit_equal_qdict(const QLitObject *lhs, const QDict *qdict) { @@ -68,16 +69,16 @@ bool qlit_equal_qobject(const QLitObject *lhs, const QObject *rhs) switch (lhs->type) { case QTYPE_QBOOL: - return lhs->value.qbool == qbool_get_bool(qobject_to_qbool(rhs)); + return lhs->value.qbool == qbool_get_bool(qobject_to(QBool, rhs)); case QTYPE_QNUM: - return lhs->value.qnum == qnum_get_int(qobject_to_qnum(rhs)); + return lhs->value.qnum == qnum_get_int(qobject_to(QNum, rhs)); case QTYPE_QSTRING: return (strcmp(lhs->value.qstr, - qstring_get_str(qobject_to_qstring(rhs))) == 0); + qstring_get_str(qobject_to(QString, rhs))) == 0); case QTYPE_QDICT: - return qlit_equal_qdict(lhs, qobject_to_qdict(rhs)); + return qlit_equal_qdict(lhs, qobject_to(QDict, rhs)); case QTYPE_QLIST: - return qlit_equal_qlist(lhs, qobject_to_qlist(rhs)); + return qlit_equal_qlist(lhs, qobject_to(QList, rhs)); case QTYPE_QNULL: return true; default: @@ -86,3 +87,39 @@ bool qlit_equal_qobject(const QLitObject *lhs, const QObject *rhs) return false; } + +QObject *qobject_from_qlit(const QLitObject *qlit) +{ + switch (qlit->type) { + case QTYPE_QNULL: + return QOBJECT(qnull()); + case QTYPE_QNUM: + return QOBJECT(qnum_from_int(qlit->value.qnum)); + case QTYPE_QSTRING: + return QOBJECT(qstring_from_str(qlit->value.qstr)); + case QTYPE_QDICT: { + QDict *qdict = qdict_new(); + QLitDictEntry *e; + + for (e = qlit->value.qdict; e->key; e++) { + qdict_put_obj(qdict, e->key, qobject_from_qlit(&e->value)); + } + return QOBJECT(qdict); + } + case QTYPE_QLIST: { + QList *qlist = qlist_new(); + QLitObject *e; + + for (e = qlit->value.qlist; e->type != QTYPE_NONE; e++) { + qlist_append_obj(qlist, qobject_from_qlit(e)); + } + return QOBJECT(qlist); + } + case QTYPE_QBOOL: + return QOBJECT(qbool_from_bool(qlit->value.qbool)); + default: + assert(0); + } + + return NULL; +} diff --git a/qobject/qnum.c b/qobject/qnum.c index 60c395c1bc..1501c82832 100644 --- a/qobject/qnum.c +++ b/qobject/qnum.c @@ -200,17 +200,6 @@ char *qnum_to_string(QNum *qn) } /** - * qobject_to_qnum(): Convert a QObject into a QNum - */ -QNum *qobject_to_qnum(const QObject *obj) -{ - if (!obj || qobject_type(obj) != QTYPE_QNUM) { - return NULL; - } - return container_of(obj, QNum, base); -} - -/** * qnum_is_equal(): Test whether the two QNums are equal * * Negative integers are never considered equal to unsigned integers, @@ -221,8 +210,8 @@ QNum *qobject_to_qnum(const QObject *obj) */ bool qnum_is_equal(const QObject *x, const QObject *y) { - QNum *num_x = qobject_to_qnum(x); - QNum *num_y = qobject_to_qnum(y); + QNum *num_x = qobject_to(QNum, x); + QNum *num_y = qobject_to(QNum, y); switch (num_x->kind) { case QNUM_I64: @@ -271,5 +260,5 @@ bool qnum_is_equal(const QObject *x, const QObject *y) void qnum_destroy_obj(QObject *obj) { assert(obj != NULL); - g_free(qobject_to_qnum(obj)); + g_free(qobject_to(QNum, obj)); } diff --git a/qobject/qstring.c b/qobject/qstring.c index 05b4bbc2d6..afca54b47a 100644 --- a/qobject/qstring.c +++ b/qobject/qstring.c @@ -106,17 +106,6 @@ void qstring_append_chr(QString *qstring, int c) } /** - * qobject_to_qstring(): Convert a QObject to a QString - */ -QString *qobject_to_qstring(const QObject *obj) -{ - if (!obj || qobject_type(obj) != QTYPE_QSTRING) { - return NULL; - } - return container_of(obj, QString, base); -} - -/** * qstring_get_str(): Return a pointer to the stored string * * NOTE: Should be used with caution, if the object is deallocated @@ -128,12 +117,33 @@ const char *qstring_get_str(const QString *qstring) } /** + * qstring_get_try_str(): Return a pointer to the stored string + * + * NOTE: will return NULL if qstring is not provided. + */ +const char *qstring_get_try_str(const QString *qstring) +{ + return qstring ? qstring_get_str(qstring) : NULL; +} + +/** + * qobject_get_try_str(): Return a pointer to the corresponding string + * + * NOTE: the string will only be returned if the object is valid, and + * its type is QString, otherwise NULL is returned. + */ +const char *qobject_get_try_str(const QObject *qstring) +{ + return qstring_get_try_str(qobject_to(QString, qstring)); +} + +/** * qstring_is_equal(): Test whether the two QStrings are equal */ bool qstring_is_equal(const QObject *x, const QObject *y) { - return !strcmp(qobject_to_qstring(x)->string, - qobject_to_qstring(y)->string); + return !strcmp(qobject_to(QString, x)->string, + qobject_to(QString, y)->string); } /** @@ -145,7 +155,7 @@ void qstring_destroy_obj(QObject *obj) QString *qs; assert(obj != NULL); - qs = qobject_to_qstring(obj); + qs = qobject_to(QString, obj); g_free(qs->string); g_free(qs); } |