qapi-visit-core.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*
  2. * Core Definitions for QAPI Visitor Classes
  3. *
  4. * Copyright IBM, Corp. 2011
  5. *
  6. * Authors:
  7. * Anthony Liguori <aliguori@us.ibm.com>
  8. *
  9. * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
  10. * See the COPYING.LIB file in the top-level directory.
  11. *
  12. */
  13. #include "qapi/qapi-visit-core.h"
  14. #include "qapi/qapi-visit-impl.h"
  15. void visit_start_handle(Visitor *v, void **obj, const char *kind,
  16. const char *name, Error **errp)
  17. {
  18. if (!error_is_set(errp) && v->start_handle) {
  19. v->start_handle(v, obj, kind, name, errp);
  20. }
  21. }
  22. void visit_end_handle(Visitor *v, Error **errp)
  23. {
  24. if (!error_is_set(errp) && v->end_handle) {
  25. v->end_handle(v, errp);
  26. }
  27. }
  28. void visit_start_struct(Visitor *v, void **obj, const char *kind,
  29. const char *name, size_t size, Error **errp)
  30. {
  31. if (!error_is_set(errp)) {
  32. v->start_struct(v, obj, kind, name, size, errp);
  33. }
  34. }
  35. void visit_end_struct(Visitor *v, Error **errp)
  36. {
  37. assert(!error_is_set(errp));
  38. v->end_struct(v, errp);
  39. }
  40. void visit_start_list(Visitor *v, const char *name, Error **errp)
  41. {
  42. if (!error_is_set(errp)) {
  43. v->start_list(v, name, errp);
  44. }
  45. }
  46. GenericList *visit_next_list(Visitor *v, GenericList **list, Error **errp)
  47. {
  48. if (!error_is_set(errp)) {
  49. return v->next_list(v, list, errp);
  50. }
  51. return 0;
  52. }
  53. void visit_end_list(Visitor *v, Error **errp)
  54. {
  55. assert(!error_is_set(errp));
  56. v->end_list(v, errp);
  57. }
  58. void visit_start_optional(Visitor *v, bool *present, const char *name,
  59. Error **errp)
  60. {
  61. if (!error_is_set(errp) && v->start_optional) {
  62. v->start_optional(v, present, name, errp);
  63. }
  64. }
  65. void visit_end_optional(Visitor *v, Error **errp)
  66. {
  67. if (!error_is_set(errp) && v->end_optional) {
  68. v->end_optional(v, errp);
  69. }
  70. }
  71. void visit_type_enum(Visitor *v, int *obj, const char *strings[],
  72. const char *kind, const char *name, Error **errp)
  73. {
  74. if (!error_is_set(errp)) {
  75. v->type_enum(v, obj, strings, kind, name, errp);
  76. }
  77. }
  78. void visit_type_int(Visitor *v, int64_t *obj, const char *name, Error **errp)
  79. {
  80. if (!error_is_set(errp)) {
  81. v->type_int(v, obj, name, errp);
  82. }
  83. }
  84. void visit_type_uint8(Visitor *v, uint8_t *obj, const char *name, Error **errp)
  85. {
  86. int64_t value;
  87. if (!error_is_set(errp)) {
  88. if (v->type_uint8) {
  89. v->type_uint8(v, obj, name, errp);
  90. } else {
  91. value = *obj;
  92. v->type_int(v, &value, name, errp);
  93. if (value < 0 || value > UINT8_MAX) {
  94. error_set(errp, QERR_INVALID_PARAMETER_VALUE, name ? name : "null",
  95. "uint8_t");
  96. return;
  97. }
  98. *obj = value;
  99. }
  100. }
  101. }
  102. void visit_type_uint16(Visitor *v, uint16_t *obj, const char *name, Error **errp)
  103. {
  104. int64_t value;
  105. if (!error_is_set(errp)) {
  106. if (v->type_uint16) {
  107. v->type_uint16(v, obj, name, errp);
  108. } else {
  109. value = *obj;
  110. v->type_int(v, &value, name, errp);
  111. if (value < 0 || value > UINT16_MAX) {
  112. error_set(errp, QERR_INVALID_PARAMETER_VALUE, name ? name : "null",
  113. "uint16_t");
  114. return;
  115. }
  116. *obj = value;
  117. }
  118. }
  119. }
  120. void visit_type_uint32(Visitor *v, uint32_t *obj, const char *name, Error **errp)
  121. {
  122. int64_t value;
  123. if (!error_is_set(errp)) {
  124. if (v->type_uint32) {
  125. v->type_uint32(v, obj, name, errp);
  126. } else {
  127. value = *obj;
  128. v->type_int(v, &value, name, errp);
  129. if (value < 0 || value > UINT32_MAX) {
  130. error_set(errp, QERR_INVALID_PARAMETER_VALUE, name ? name : "null",
  131. "uint32_t");
  132. return;
  133. }
  134. *obj = value;
  135. }
  136. }
  137. }
  138. void visit_type_uint64(Visitor *v, uint64_t *obj, const char *name, Error **errp)
  139. {
  140. int64_t value;
  141. if (!error_is_set(errp)) {
  142. if (v->type_uint64) {
  143. v->type_uint64(v, obj, name, errp);
  144. } else {
  145. value = *obj;
  146. v->type_int(v, &value, name, errp);
  147. *obj = value;
  148. }
  149. }
  150. }
  151. void visit_type_int8(Visitor *v, int8_t *obj, const char *name, Error **errp)
  152. {
  153. int64_t value;
  154. if (!error_is_set(errp)) {
  155. if (v->type_int8) {
  156. v->type_int8(v, obj, name, errp);
  157. } else {
  158. value = *obj;
  159. v->type_int(v, &value, name, errp);
  160. if (value < INT8_MIN || value > INT8_MAX) {
  161. error_set(errp, QERR_INVALID_PARAMETER_VALUE, name ? name : "null",
  162. "int8_t");
  163. return;
  164. }
  165. *obj = value;
  166. }
  167. }
  168. }
  169. void visit_type_int16(Visitor *v, int16_t *obj, const char *name, Error **errp)
  170. {
  171. int64_t value;
  172. if (!error_is_set(errp)) {
  173. if (v->type_int16) {
  174. v->type_int16(v, obj, name, errp);
  175. } else {
  176. value = *obj;
  177. v->type_int(v, &value, name, errp);
  178. if (value < INT16_MIN || value > INT16_MAX) {
  179. error_set(errp, QERR_INVALID_PARAMETER_VALUE, name ? name : "null",
  180. "int16_t");
  181. return;
  182. }
  183. *obj = value;
  184. }
  185. }
  186. }
  187. void visit_type_int32(Visitor *v, int32_t *obj, const char *name, Error **errp)
  188. {
  189. int64_t value;
  190. if (!error_is_set(errp)) {
  191. if (v->type_int32) {
  192. v->type_int32(v, obj, name, errp);
  193. } else {
  194. value = *obj;
  195. v->type_int(v, &value, name, errp);
  196. if (value < INT32_MIN || value > INT32_MAX) {
  197. error_set(errp, QERR_INVALID_PARAMETER_VALUE, name ? name : "null",
  198. "int32_t");
  199. return;
  200. }
  201. *obj = value;
  202. }
  203. }
  204. }
  205. void visit_type_int64(Visitor *v, int64_t *obj, const char *name, Error **errp)
  206. {
  207. if (!error_is_set(errp)) {
  208. if (v->type_int64) {
  209. v->type_int64(v, obj, name, errp);
  210. } else {
  211. v->type_int(v, obj, name, errp);
  212. }
  213. }
  214. }
  215. void visit_type_size(Visitor *v, uint64_t *obj, const char *name, Error **errp)
  216. {
  217. if (!error_is_set(errp)) {
  218. (v->type_size ? v->type_size : v->type_uint64)(v, obj, name, errp);
  219. }
  220. }
  221. void visit_type_bool(Visitor *v, bool *obj, const char *name, Error **errp)
  222. {
  223. if (!error_is_set(errp)) {
  224. v->type_bool(v, obj, name, errp);
  225. }
  226. }
  227. void visit_type_str(Visitor *v, char **obj, const char *name, Error **errp)
  228. {
  229. if (!error_is_set(errp)) {
  230. v->type_str(v, obj, name, errp);
  231. }
  232. }
  233. void visit_type_number(Visitor *v, double *obj, const char *name, Error **errp)
  234. {
  235. if (!error_is_set(errp)) {
  236. v->type_number(v, obj, name, errp);
  237. }
  238. }
  239. void output_type_enum(Visitor *v, int *obj, const char *strings[],
  240. const char *kind, const char *name,
  241. Error **errp)
  242. {
  243. int i = 0;
  244. int value = *obj;
  245. char *enum_str;
  246. assert(strings);
  247. while (strings[i++] != NULL);
  248. if (value < 0 || value >= i - 1) {
  249. error_set(errp, QERR_INVALID_PARAMETER, name ? name : "null");
  250. return;
  251. }
  252. enum_str = (char *)strings[value];
  253. visit_type_str(v, &enum_str, name, errp);
  254. }
  255. void input_type_enum(Visitor *v, int *obj, const char *strings[],
  256. const char *kind, const char *name,
  257. Error **errp)
  258. {
  259. int64_t value = 0;
  260. char *enum_str;
  261. assert(strings);
  262. visit_type_str(v, &enum_str, name, errp);
  263. if (error_is_set(errp)) {
  264. return;
  265. }
  266. while (strings[value] != NULL) {
  267. if (strcmp(strings[value], enum_str) == 0) {
  268. break;
  269. }
  270. value++;
  271. }
  272. if (strings[value] == NULL) {
  273. error_set(errp, QERR_INVALID_PARAMETER, enum_str);
  274. g_free(enum_str);
  275. return;
  276. }
  277. g_free(enum_str);
  278. *obj = value;
  279. }