qobject-output-visitor.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * Core Definitions for QAPI/QMP Command Registry
  3. *
  4. * Copyright (C) 2012-2016 Red Hat, Inc.
  5. * Copyright IBM, Corp. 2011
  6. *
  7. * Authors:
  8. * Anthony Liguori <aliguori@us.ibm.com>
  9. *
  10. * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
  11. * See the COPYING.LIB file in the top-level directory.
  12. *
  13. */
  14. #include "qemu/osdep.h"
  15. #include "qapi/qobject-output-visitor.h"
  16. #include "qapi/visitor-impl.h"
  17. #include "qemu/queue.h"
  18. #include "qapi/qmp/qbool.h"
  19. #include "qapi/qmp/qdict.h"
  20. #include "qapi/qmp/qlist.h"
  21. #include "qapi/qmp/qnull.h"
  22. #include "qapi/qmp/qnum.h"
  23. #include "qapi/qmp/qstring.h"
  24. typedef struct QStackEntry {
  25. QObject *value;
  26. void *qapi; /* sanity check that caller uses same pointer */
  27. QSLIST_ENTRY(QStackEntry) node;
  28. } QStackEntry;
  29. struct QObjectOutputVisitor {
  30. Visitor visitor;
  31. QSLIST_HEAD(, QStackEntry) stack; /* Stack of unfinished containers */
  32. QObject *root; /* Root of the output visit */
  33. QObject **result; /* User's storage location for result */
  34. };
  35. #define qobject_output_add(qov, name, value) \
  36. qobject_output_add_obj(qov, name, QOBJECT(value))
  37. #define qobject_output_push(qov, value, qapi) \
  38. qobject_output_push_obj(qov, QOBJECT(value), qapi)
  39. static QObjectOutputVisitor *to_qov(Visitor *v)
  40. {
  41. return container_of(v, QObjectOutputVisitor, visitor);
  42. }
  43. /* Push @value onto the stack of current QObjects being built */
  44. static void qobject_output_push_obj(QObjectOutputVisitor *qov, QObject *value,
  45. void *qapi)
  46. {
  47. QStackEntry *e = g_malloc0(sizeof(*e));
  48. assert(qov->root);
  49. assert(value);
  50. e->value = value;
  51. e->qapi = qapi;
  52. QSLIST_INSERT_HEAD(&qov->stack, e, node);
  53. }
  54. /* Pop a value off the stack of QObjects being built, and return it. */
  55. static QObject *qobject_output_pop(QObjectOutputVisitor *qov, void *qapi)
  56. {
  57. QStackEntry *e = QSLIST_FIRST(&qov->stack);
  58. QObject *value;
  59. assert(e);
  60. assert(e->qapi == qapi);
  61. QSLIST_REMOVE_HEAD(&qov->stack, node);
  62. value = e->value;
  63. assert(value);
  64. g_free(e);
  65. return value;
  66. }
  67. /* Add @value to the current QObject being built.
  68. * If the stack is visiting a dictionary or list, @value is now owned
  69. * by that container. Otherwise, @value is now the root. */
  70. static void qobject_output_add_obj(QObjectOutputVisitor *qov, const char *name,
  71. QObject *value)
  72. {
  73. QStackEntry *e = QSLIST_FIRST(&qov->stack);
  74. QObject *cur = e ? e->value : NULL;
  75. if (!cur) {
  76. /* Don't allow reuse of visitor on more than one root */
  77. assert(!qov->root);
  78. qov->root = value;
  79. } else {
  80. switch (qobject_type(cur)) {
  81. case QTYPE_QDICT:
  82. assert(name);
  83. qdict_put_obj(qobject_to(QDict, cur), name, value);
  84. break;
  85. case QTYPE_QLIST:
  86. assert(!name);
  87. qlist_append_obj(qobject_to(QList, cur), value);
  88. break;
  89. default:
  90. g_assert_not_reached();
  91. }
  92. }
  93. }
  94. static void qobject_output_start_struct(Visitor *v, const char *name,
  95. void **obj, size_t unused, Error **errp)
  96. {
  97. QObjectOutputVisitor *qov = to_qov(v);
  98. QDict *dict = qdict_new();
  99. qobject_output_add(qov, name, dict);
  100. qobject_output_push(qov, dict, obj);
  101. }
  102. static void qobject_output_end_struct(Visitor *v, void **obj)
  103. {
  104. QObjectOutputVisitor *qov = to_qov(v);
  105. QObject *value = qobject_output_pop(qov, obj);
  106. assert(qobject_type(value) == QTYPE_QDICT);
  107. }
  108. static void qobject_output_start_list(Visitor *v, const char *name,
  109. GenericList **listp, size_t size,
  110. Error **errp)
  111. {
  112. QObjectOutputVisitor *qov = to_qov(v);
  113. QList *list = qlist_new();
  114. qobject_output_add(qov, name, list);
  115. qobject_output_push(qov, list, listp);
  116. }
  117. static GenericList *qobject_output_next_list(Visitor *v, GenericList *tail,
  118. size_t size)
  119. {
  120. return tail->next;
  121. }
  122. static void qobject_output_end_list(Visitor *v, void **obj)
  123. {
  124. QObjectOutputVisitor *qov = to_qov(v);
  125. QObject *value = qobject_output_pop(qov, obj);
  126. assert(qobject_type(value) == QTYPE_QLIST);
  127. }
  128. static void qobject_output_type_int64(Visitor *v, const char *name,
  129. int64_t *obj, Error **errp)
  130. {
  131. QObjectOutputVisitor *qov = to_qov(v);
  132. qobject_output_add(qov, name, qnum_from_int(*obj));
  133. }
  134. static void qobject_output_type_uint64(Visitor *v, const char *name,
  135. uint64_t *obj, Error **errp)
  136. {
  137. QObjectOutputVisitor *qov = to_qov(v);
  138. qobject_output_add(qov, name, qnum_from_uint(*obj));
  139. }
  140. static void qobject_output_type_bool(Visitor *v, const char *name, bool *obj,
  141. Error **errp)
  142. {
  143. QObjectOutputVisitor *qov = to_qov(v);
  144. qobject_output_add(qov, name, qbool_from_bool(*obj));
  145. }
  146. static void qobject_output_type_str(Visitor *v, const char *name, char **obj,
  147. Error **errp)
  148. {
  149. QObjectOutputVisitor *qov = to_qov(v);
  150. if (*obj) {
  151. qobject_output_add(qov, name, qstring_from_str(*obj));
  152. } else {
  153. qobject_output_add(qov, name, qstring_from_str(""));
  154. }
  155. }
  156. static void qobject_output_type_number(Visitor *v, const char *name,
  157. double *obj, Error **errp)
  158. {
  159. QObjectOutputVisitor *qov = to_qov(v);
  160. qobject_output_add(qov, name, qnum_from_double(*obj));
  161. }
  162. static void qobject_output_type_any(Visitor *v, const char *name,
  163. QObject **obj, Error **errp)
  164. {
  165. QObjectOutputVisitor *qov = to_qov(v);
  166. qobject_output_add_obj(qov, name, qobject_ref(*obj));
  167. }
  168. static void qobject_output_type_null(Visitor *v, const char *name,
  169. QNull **obj, Error **errp)
  170. {
  171. QObjectOutputVisitor *qov = to_qov(v);
  172. qobject_output_add(qov, name, qnull());
  173. }
  174. /* Finish building, and return the root object.
  175. * The root object is never null. The caller becomes the object's
  176. * owner, and should use qobject_unref() when done with it. */
  177. static void qobject_output_complete(Visitor *v, void *opaque)
  178. {
  179. QObjectOutputVisitor *qov = to_qov(v);
  180. /* A visit must have occurred, with each start paired with end. */
  181. assert(qov->root && QSLIST_EMPTY(&qov->stack));
  182. assert(opaque == qov->result);
  183. *qov->result = qobject_ref(qov->root);
  184. qov->result = NULL;
  185. }
  186. static void qobject_output_free(Visitor *v)
  187. {
  188. QObjectOutputVisitor *qov = to_qov(v);
  189. QStackEntry *e;
  190. while (!QSLIST_EMPTY(&qov->stack)) {
  191. e = QSLIST_FIRST(&qov->stack);
  192. QSLIST_REMOVE_HEAD(&qov->stack, node);
  193. g_free(e);
  194. }
  195. qobject_unref(qov->root);
  196. g_free(qov);
  197. }
  198. Visitor *qobject_output_visitor_new(QObject **result)
  199. {
  200. QObjectOutputVisitor *v;
  201. v = g_malloc0(sizeof(*v));
  202. v->visitor.type = VISITOR_OUTPUT;
  203. v->visitor.start_struct = qobject_output_start_struct;
  204. v->visitor.end_struct = qobject_output_end_struct;
  205. v->visitor.start_list = qobject_output_start_list;
  206. v->visitor.next_list = qobject_output_next_list;
  207. v->visitor.end_list = qobject_output_end_list;
  208. v->visitor.type_int64 = qobject_output_type_int64;
  209. v->visitor.type_uint64 = qobject_output_type_uint64;
  210. v->visitor.type_bool = qobject_output_type_bool;
  211. v->visitor.type_str = qobject_output_type_str;
  212. v->visitor.type_number = qobject_output_type_number;
  213. v->visitor.type_any = qobject_output_type_any;
  214. v->visitor.type_null = qobject_output_type_null;
  215. v->visitor.complete = qobject_output_complete;
  216. v->visitor.free = qobject_output_free;
  217. *result = NULL;
  218. v->result = result;
  219. return &v->visitor;
  220. }