diff mbox series

[V4,2/5] qapi: QAPI_LIST_LENGTH

Message ID 1705099758-211963-3-git-send-email-steven.sistare@oracle.com (mailing list archive)
State New, archived
Headers show
Series string list functions | expand

Commit Message

Steven Sistare Jan. 12, 2024, 10:49 p.m. UTC
Signed-off-by: Steve Sistare <steven.sistare@oracle.com>
Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>
---
 include/qapi/util.h | 13 +++++++++++++
 1 file changed, 13 insertions(+)

Comments

Markus Armbruster Feb. 21, 2024, 1:29 p.m. UTC | #1
Steve Sistare <steven.sistare@oracle.com> writes:

> Signed-off-by: Steve Sistare <steven.sistare@oracle.com>
> Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>
> ---
>  include/qapi/util.h | 13 +++++++++++++
>  1 file changed, 13 insertions(+)
>
> diff --git a/include/qapi/util.h b/include/qapi/util.h
> index 81a2b13..e1b8b1d 100644
> --- a/include/qapi/util.h
> +++ b/include/qapi/util.h
> @@ -56,4 +56,17 @@ int parse_qapi_name(const char *name, bool complete);
>      (tail) = &(*(tail))->next; \
>  } while (0)
>  
> +/*
> + * For any GenericList @list, return its length.
> + */
> +#define QAPI_LIST_LENGTH(list) \
> +    ({ \
> +        int len = 0; \

size_t

> +        typeof(list) elem; \

Name this @tail, please.

> +        for (elem = list; elem != NULL; elem = elem->next) { \
> +            len++; \
> +        } \
> +        len; \
> +    })
> +
>  #endif

This is a macro instead of a function so users don't have to cast their
FooList * to GenericList *.

The only user outside tests is strv_from_strList().  I'd be tempted to
open-code it there and call it a day.  Or do you have more users in
mind?

If we keep the macro, please align the backslashes like this:

   #define QAPI_LIST_LENGTH(list)                                  \
       ({                                                          \
           int len = 0;                                            \
           typeof(list) elem;                                      \
           for (elem = list; elem != NULL; elem = elem->next) {    \
               len++;                                              \
           }                                                       \
           len;                                                    \
       })
Steven Sistare Feb. 21, 2024, 5:01 p.m. UTC | #2
On 2/21/2024 8:29 AM, Markus Armbruster wrote:
> Steve Sistare <steven.sistare@oracle.com> writes:
> 
>> Signed-off-by: Steve Sistare <steven.sistare@oracle.com>
>> Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>
>> ---
>>  include/qapi/util.h | 13 +++++++++++++
>>  1 file changed, 13 insertions(+)
>>
>> diff --git a/include/qapi/util.h b/include/qapi/util.h
>> index 81a2b13..e1b8b1d 100644
>> --- a/include/qapi/util.h
>> +++ b/include/qapi/util.h
>> @@ -56,4 +56,17 @@ int parse_qapi_name(const char *name, bool complete);
>>      (tail) = &(*(tail))->next; \
>>  } while (0)
>>  
>> +/*
>> + * For any GenericList @list, return its length.
>> + */
>> +#define QAPI_LIST_LENGTH(list) \
>> +    ({ \
>> +        int len = 0; \
> 
> size_t

ok.

>> +        typeof(list) elem; \
> 
> Name this @tail, please.

ok.

>> +        for (elem = list; elem != NULL; elem = elem->next) { \
>> +            len++; \
>> +        } \
>> +        len; \
>> +    })
>> +
>>  #endif
> 
> This is a macro instead of a function so users don't have to cast their
> FooList * to GenericList *.
> 
> The only user outside tests is strv_from_strList().  I'd be tempted to
> open-code it there and call it a day.  Or do you have more users in
> mind?

That's the only use.  If I make it private, I would still define it as
a static subroutine in util/strList.c, because it simplifies strv_from_strList.
IMO providing a public list length function or macro is pretty basic, but
I am not wedded to it.  Your call.

- Steve

> If we keep the macro, please align the backslashes like this:
> 
>    #define QAPI_LIST_LENGTH(list)                                  \
>        ({                                                          \
>            int len = 0;                                            \
>            typeof(list) elem;                                      \
>            for (elem = list; elem != NULL; elem = elem->next) {    \
>                len++;                                              \
>            }                                                       \
>            len;                                                    \
>        })
>
diff mbox series

Patch

diff --git a/include/qapi/util.h b/include/qapi/util.h
index 81a2b13..e1b8b1d 100644
--- a/include/qapi/util.h
+++ b/include/qapi/util.h
@@ -56,4 +56,17 @@  int parse_qapi_name(const char *name, bool complete);
     (tail) = &(*(tail))->next; \
 } while (0)
 
+/*
+ * For any GenericList @list, return its length.
+ */
+#define QAPI_LIST_LENGTH(list) \
+    ({ \
+        int len = 0; \
+        typeof(list) elem; \
+        for (elem = list; elem != NULL; elem = elem->next) { \
+            len++; \
+        } \
+        len; \
+    })
+
 #endif