2
0

qobject-output-visitor.c 7.7 KB

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