xref: /qemu/include/qapi/util.h (revision 9c707525)
1 /*
2  * QAPI util functions
3  *
4  * Copyright Fujitsu, Inc. 2014
5  *
6  * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
7  * See the COPYING.LIB file in the top-level directory.
8  *
9  */
10 
11 #ifndef QAPI_UTIL_H
12 #define QAPI_UTIL_H
13 
14 typedef enum {
15     QAPI_DEPRECATED,
16     QAPI_UNSTABLE,
17 } QapiSpecialFeature;
18 
19 typedef struct QEnumLookup {
20     const char *const *array;
21     const unsigned char *const special_features;
22     const int size;
23 } QEnumLookup;
24 
25 const char *qapi_enum_lookup(const QEnumLookup *lookup, int val);
26 int qapi_enum_parse(const QEnumLookup *lookup, const char *buf,
27                     int def, Error **errp);
28 bool qapi_bool_parse(const char *name, const char *value, bool *obj,
29                      Error **errp);
30 
31 int parse_qapi_name(const char *name, bool complete);
32 
33 /*
34  * For any GenericList @list, insert @element at the front.
35  *
36  * Note that this macro evaluates @element exactly once, so it is safe
37  * to have side-effects with that argument.
38  */
39 #define QAPI_LIST_PREPEND(list, element) do { \
40     typeof(list) _tmp = g_malloc(sizeof(*(list))); \
41     _tmp->value = (element); \
42     _tmp->next = (list); \
43     (list) = _tmp; \
44 } while (0)
45 
46 /*
47  * For any pointer to a GenericList @tail (usually the 'next' member of a
48  * list element), insert @element at the back and update the tail.
49  *
50  * Note that this macro evaluates @element exactly once, so it is safe
51  * to have side-effects with that argument.
52  */
53 #define QAPI_LIST_APPEND(tail, element) do { \
54     *(tail) = g_malloc0(sizeof(**(tail))); \
55     (*(tail))->value = (element); \
56     (tail) = &(*(tail))->next; \
57 } while (0)
58 
59 /*
60  * For any GenericList @list, return its length.
61  */
62 #define QAPI_LIST_LENGTH(list)                                      \
63     ({                                                              \
64         size_t _len = 0;                                            \
65         typeof(list) _tail;                                         \
66         for (_tail = list; _tail != NULL; _tail = _tail->next) {    \
67             _len++;                                                 \
68         }                                                           \
69         _len;                                                       \
70     })
71 
72 #endif
73