xref: /openbmc/qemu/include/qapi/util.h (revision 9bafe07b)
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 /* QEnumLookup flags */
15 #define QAPI_ENUM_DEPRECATED 1
16 
17 typedef struct QEnumLookup {
18     const char *const *array;
19     const unsigned char *const flags;
20     const int size;
21 } QEnumLookup;
22 
23 const char *qapi_enum_lookup(const QEnumLookup *lookup, int val);
24 int qapi_enum_parse(const QEnumLookup *lookup, const char *buf,
25                     int def, Error **errp);
26 bool qapi_bool_parse(const char *name, const char *value, bool *obj,
27                      Error **errp);
28 
29 int parse_qapi_name(const char *name, bool complete);
30 
31 /*
32  * For any GenericList @list, insert @element at the front.
33  *
34  * Note that this macro evaluates @element exactly once, so it is safe
35  * to have side-effects with that argument.
36  */
37 #define QAPI_LIST_PREPEND(list, element) do { \
38     typeof(list) _tmp = g_malloc(sizeof(*(list))); \
39     _tmp->value = (element); \
40     _tmp->next = (list); \
41     (list) = _tmp; \
42 } while (0)
43 
44 /*
45  * For any pointer to a GenericList @tail (usually the 'next' member of a
46  * list element), insert @element at the back and update the tail.
47  *
48  * Note that this macro evaluates @element exactly once, so it is safe
49  * to have side-effects with that argument.
50  */
51 #define QAPI_LIST_APPEND(tail, element) do { \
52     *(tail) = g_malloc0(sizeof(**(tail))); \
53     (*(tail))->value = (element); \
54     (tail) = &(*(tail))->next; \
55 } while (0)
56 
57 #endif
58