Skip to content

Commit

Permalink
qobject: Add helper macros for common scalar insertions
Browse files Browse the repository at this point in the history
Rather than making lots of callers wrap a scalar in a QInt, QString,
or QBool, provide helper macros that do the wrapping automatically.

Update the Coccinelle script to make mass conversions easy, although
the conversion itself will be done as a separate patches to ease
review and backport efforts.

Signed-off-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Markus Armbruster <armbru@redhat.com>
Message-Id: <20170427215821.19397-6-eblake@redhat.com>
Signed-off-by: Markus Armbruster <armbru@redhat.com>
(cherry picked from commit a92c215)
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
  • Loading branch information
ebblake authored and mdroth committed Jul 31, 2017
1 parent 3f308bf commit 396474a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
8 changes: 8 additions & 0 deletions include/qapi/qmp/qdict.h
Expand Up @@ -52,6 +52,14 @@ void qdict_destroy_obj(QObject *obj);
#define qdict_put(qdict, key, obj) \
qdict_put_obj(qdict, key, QOBJECT(obj))

/* Helpers for int, bool, and string */
#define qdict_put_int(qdict, key, value) \
qdict_put(qdict, key, qint_from_int(value))
#define qdict_put_bool(qdict, key, value) \
qdict_put(qdict, key, qbool_from_bool(value))
#define qdict_put_str(qdict, key, value) \
qdict_put(qdict, key, qstring_from_str(value))

/* High level helpers */
double qdict_get_double(const QDict *qdict, const char *key);
int64_t qdict_get_int(const QDict *qdict, const char *key);
Expand Down
8 changes: 8 additions & 0 deletions include/qapi/qmp/qlist.h
Expand Up @@ -29,6 +29,14 @@ typedef struct QList {
#define qlist_append(qlist, obj) \
qlist_append_obj(qlist, QOBJECT(obj))

/* Helpers for int, bool, and string */
#define qlist_append_int(qlist, value) \
qlist_append(qlist, qint_from_int(value))
#define qlist_append_bool(qlist, value) \
qlist_append(qlist, qbool_from_bool(value))
#define qlist_append_str(qlist, value) \
qlist_append(qlist, qstring_from_str(value))

#define QLIST_FOREACH_ENTRY(qlist, var) \
for ((var) = ((qlist)->head.tqh_first); \
(var); \
Expand Down
22 changes: 22 additions & 0 deletions scripts/coccinelle/qobject.cocci
Expand Up @@ -2,12 +2,34 @@
@@
expression Obj, Key, E;
@@
(
- qdict_put_obj(Obj, Key, QOBJECT(E));
+ qdict_put(Obj, Key, E);
|
- qdict_put(Obj, Key, qint_from_int(E));
+ qdict_put_int(Obj, Key, E);
|
- qdict_put(Obj, Key, qbool_from_bool(E));
+ qdict_put_bool(Obj, Key, E);
|
- qdict_put(Obj, Key, qstring_from_str(E));
+ qdict_put_str(Obj, Key, E);
)

// Use QList macros where they make sense
@@
expression Obj, E;
@@
(
- qlist_append_obj(Obj, QOBJECT(E));
+ qlist_append(Obj, E);
|
- qlist_append(Obj, qint_from_int(E));
+ qlist_append_int(Obj, E);
|
- qlist_append(Obj, qbool_from_bool(E));
+ qlist_append_bool(Obj, E);
|
- qlist_append(Obj, qstring_from_str(E));
+ qlist_append_str(Obj, E);
)

0 comments on commit 396474a

Please sign in to comment.