diff mbox

[v3,06/13] qobject: Add helper macros for common scalar insertions

Message ID 20170405194741.18956-7-eblake@redhat.com (mailing list archive)
State New, archived
Headers show

Commit Message

Eric Blake April 5, 2017, 7:47 p.m. UTC
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>
---
 include/qapi/qmp/qdict.h         |  8 ++++++++
 include/qapi/qmp/qlist.h         |  8 ++++++++
 scripts/coccinelle/qobject.cocci | 22 ++++++++++++++++++++++
 3 files changed, 38 insertions(+)

Comments

Markus Armbruster April 11, 2017, 5:06 p.m. UTC | #1
Eric Blake <eblake@redhat.com> writes:

> 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>
> ---
>  include/qapi/qmp/qdict.h         |  8 ++++++++
>  include/qapi/qmp/qlist.h         |  8 ++++++++
>  scripts/coccinelle/qobject.cocci | 22 ++++++++++++++++++++++
>  3 files changed, 38 insertions(+)
>
> diff --git a/include/qapi/qmp/qdict.h b/include/qapi/qmp/qdict.h
> index fe9a4c5..9d9f9a3 100644
> --- a/include/qapi/qmp/qdict.h
> +++ b/include/qapi/qmp/qdict.h
> @@ -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 const char*. */

"char *." looks awkward.  What about

   /* Helpers for int, bool, and const char * */

or

   /* Helpers for int, bool, string */

or no comment?

> +#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);
> diff --git a/include/qapi/qmp/qlist.h b/include/qapi/qmp/qlist.h
> index a84117e..659325a 100644
> --- a/include/qapi/qmp/qlist.h
> +++ b/include/qapi/qmp/qlist.h
> @@ -29,6 +29,14 @@ typedef struct QList {
>  #define qlist_append(qlist, obj) \
>          qlist_append_obj(qlist, QOBJECT(obj))
>
> +/* Helpers for int, bool, and const char*. */

Likewise.

> +#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);                                  \
> diff --git a/scripts/coccinelle/qobject.cocci b/scripts/coccinelle/qobject.cocci
> index aa899e2..97703a4 100644
> --- a/scripts/coccinelle/qobject.cocci
> +++ b/scripts/coccinelle/qobject.cocci
> @@ -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);
> +)
Eric Blake April 11, 2017, 5:43 p.m. UTC | #2
On 04/11/2017 12:06 PM, Markus Armbruster wrote:
> Eric Blake <eblake@redhat.com> writes:
> 
>> 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>
>> ---
>>  include/qapi/qmp/qdict.h         |  8 ++++++++
>>  include/qapi/qmp/qlist.h         |  8 ++++++++
>>  scripts/coccinelle/qobject.cocci | 22 ++++++++++++++++++++++
>>  3 files changed, 38 insertions(+)
>>
>> diff --git a/include/qapi/qmp/qdict.h b/include/qapi/qmp/qdict.h
>> index fe9a4c5..9d9f9a3 100644
>> --- a/include/qapi/qmp/qdict.h
>> +++ b/include/qapi/qmp/qdict.h
>> @@ -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 const char*. */
> 
> "char *." looks awkward.  What about
> 
>    /* Helpers for int, bool, and const char * */
> 
> or
> 
>    /* Helpers for int, bool, string */

That does look nicer.
diff mbox

Patch

diff --git a/include/qapi/qmp/qdict.h b/include/qapi/qmp/qdict.h
index fe9a4c5..9d9f9a3 100644
--- a/include/qapi/qmp/qdict.h
+++ b/include/qapi/qmp/qdict.h
@@ -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 const char*. */
+#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);
diff --git a/include/qapi/qmp/qlist.h b/include/qapi/qmp/qlist.h
index a84117e..659325a 100644
--- a/include/qapi/qmp/qlist.h
+++ b/include/qapi/qmp/qlist.h
@@ -29,6 +29,14 @@  typedef struct QList {
 #define qlist_append(qlist, obj) \
         qlist_append_obj(qlist, QOBJECT(obj))

+/* Helpers for int, bool, and const char*. */
+#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);                                  \
diff --git a/scripts/coccinelle/qobject.cocci b/scripts/coccinelle/qobject.cocci
index aa899e2..97703a4 100644
--- a/scripts/coccinelle/qobject.cocci
+++ b/scripts/coccinelle/qobject.cocci
@@ -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);
+)