2
0

qapi-visit-core.c 7.9 KB

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