2
0

qapi-visit-core.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. if (!error_is_set(errp)) {
  38. v->end_struct(v, errp);
  39. }
  40. }
  41. void visit_start_list(Visitor *v, const char *name, Error **errp)
  42. {
  43. if (!error_is_set(errp)) {
  44. v->start_list(v, name, errp);
  45. }
  46. }
  47. GenericList *visit_next_list(Visitor *v, GenericList **list, Error **errp)
  48. {
  49. if (!error_is_set(errp)) {
  50. return v->next_list(v, list, errp);
  51. }
  52. return 0;
  53. }
  54. void visit_end_list(Visitor *v, Error **errp)
  55. {
  56. if (!error_is_set(errp)) {
  57. v->end_list(v, errp);
  58. }
  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_bool(Visitor *v, bool *obj, const char *name, Error **errp)
  87. {
  88. if (!error_is_set(errp)) {
  89. v->type_bool(v, obj, name, errp);
  90. }
  91. }
  92. void visit_type_str(Visitor *v, char **obj, const char *name, Error **errp)
  93. {
  94. if (!error_is_set(errp)) {
  95. v->type_str(v, obj, name, errp);
  96. }
  97. }
  98. void visit_type_number(Visitor *v, double *obj, const char *name, Error **errp)
  99. {
  100. if (!error_is_set(errp)) {
  101. v->type_number(v, obj, name, errp);
  102. }
  103. }
  104. void output_type_enum(Visitor *v, int *obj, const char *strings[],
  105. const char *kind, const char *name,
  106. Error **errp)
  107. {
  108. int i = 0;
  109. int value = *obj;
  110. char *enum_str;
  111. assert(strings);
  112. while (strings[i++] != NULL);
  113. if (value < 0 || value >= i - 1) {
  114. error_set(errp, QERR_INVALID_PARAMETER, name ? name : "null");
  115. return;
  116. }
  117. enum_str = (char *)strings[value];
  118. visit_type_str(v, &enum_str, name, errp);
  119. }
  120. void input_type_enum(Visitor *v, int *obj, const char *strings[],
  121. const char *kind, const char *name,
  122. Error **errp)
  123. {
  124. int64_t value = 0;
  125. char *enum_str;
  126. assert(strings);
  127. visit_type_str(v, &enum_str, name, errp);
  128. if (error_is_set(errp)) {
  129. return;
  130. }
  131. while (strings[value] != NULL) {
  132. if (strcmp(strings[value], enum_str) == 0) {
  133. break;
  134. }
  135. value++;
  136. }
  137. if (strings[value] == NULL) {
  138. error_set(errp, QERR_INVALID_PARAMETER, name ? name : "null");
  139. g_free(enum_str);
  140. return;
  141. }
  142. g_free(enum_str);
  143. *obj = value;
  144. }