19e7dac7cSPeter Lieven /* 29e7dac7cSPeter Lieven * QAPI util functions 39e7dac7cSPeter Lieven * 49e7dac7cSPeter Lieven * Copyright Fujitsu, Inc. 2014 59e7dac7cSPeter Lieven * 69e7dac7cSPeter Lieven * This work is licensed under the terms of the GNU LGPL, version 2.1 or later. 79e7dac7cSPeter Lieven * See the COPYING.LIB file in the top-level directory. 89e7dac7cSPeter Lieven * 99e7dac7cSPeter Lieven */ 109e7dac7cSPeter Lieven 119e7dac7cSPeter Lieven #ifndef QAPI_UTIL_H 129e7dac7cSPeter Lieven #define QAPI_UTIL_H 139e7dac7cSPeter Lieven 14*aa237044SMarkus Armbruster /* QEnumLookup flags */ 15*aa237044SMarkus Armbruster #define QAPI_ENUM_DEPRECATED 1 16*aa237044SMarkus Armbruster 17f7abe0ecSMarc-André Lureau typedef struct QEnumLookup { 18f7abe0ecSMarc-André Lureau const char *const *array; 19*aa237044SMarkus Armbruster const unsigned char *const flags; 20*aa237044SMarkus Armbruster const int size; 21f7abe0ecSMarc-André Lureau } QEnumLookup; 22f7abe0ecSMarc-André Lureau 23f7abe0ecSMarc-André Lureau const char *qapi_enum_lookup(const QEnumLookup *lookup, int val); 24f7abe0ecSMarc-André Lureau int qapi_enum_parse(const QEnumLookup *lookup, const char *buf, 2506c60b6cSMarkus Armbruster int def, Error **errp); 26372bcb25SPaolo Bonzini bool qapi_bool_parse(const char *name, const char *value, bool *obj, 27372bcb25SPaolo Bonzini Error **errp); 289e7dac7cSPeter Lieven 29069b64e3SMarkus Armbruster int parse_qapi_name(const char *name, bool complete); 30069b64e3SMarkus Armbruster 319812e712SEric Blake /* 329812e712SEric Blake * For any GenericList @list, insert @element at the front. 339812e712SEric Blake * 349812e712SEric Blake * Note that this macro evaluates @element exactly once, so it is safe 359812e712SEric Blake * to have side-effects with that argument. 369812e712SEric Blake */ 379812e712SEric Blake #define QAPI_LIST_PREPEND(list, element) do { \ 389812e712SEric Blake typeof(list) _tmp = g_malloc(sizeof(*(list))); \ 399812e712SEric Blake _tmp->value = (element); \ 409812e712SEric Blake _tmp->next = (list); \ 419812e712SEric Blake (list) = _tmp; \ 429812e712SEric Blake } while (0) 439812e712SEric Blake 44dc13f40cSEric Blake /* 45dc13f40cSEric Blake * For any pointer to a GenericList @tail (usually the 'next' member of a 46dc13f40cSEric Blake * list element), insert @element at the back and update the tail. 47dc13f40cSEric Blake * 48dc13f40cSEric Blake * Note that this macro evaluates @element exactly once, so it is safe 49dc13f40cSEric Blake * to have side-effects with that argument. 50dc13f40cSEric Blake */ 51dc13f40cSEric Blake #define QAPI_LIST_APPEND(tail, element) do { \ 52dc13f40cSEric Blake *(tail) = g_malloc0(sizeof(**(tail))); \ 53dc13f40cSEric Blake (*(tail))->value = (element); \ 54dc13f40cSEric Blake (tail) = &(*(tail))->next; \ 55dc13f40cSEric Blake } while (0) 56dc13f40cSEric Blake 579e7dac7cSPeter Lieven #endif 58