2
0

qobject-output-visitor.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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/compat-policy.h"
  16. #include "qapi/qobject-output-visitor.h"
  17. #include "qapi/visitor-impl.h"
  18. #include "qemu/queue.h"
  19. #include "qobject/qbool.h"
  20. #include "qobject/qdict.h"
  21. #include "qobject/qlist.h"
  22. #include "qobject/qnull.h"
  23. #include "qobject/qnum.h"
  24. #include "qobject/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 bool 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. return true;
  103. }
  104. static void qobject_output_end_struct(Visitor *v, void **obj)
  105. {
  106. QObjectOutputVisitor *qov = to_qov(v);
  107. QObject *value = qobject_output_pop(qov, obj);
  108. assert(qobject_type(value) == QTYPE_QDICT);
  109. }
  110. static bool qobject_output_start_list(Visitor *v, const char *name,
  111. GenericList **listp, size_t size,
  112. Error **errp)
  113. {
  114. QObjectOutputVisitor *qov = to_qov(v);
  115. QList *list = qlist_new();
  116. qobject_output_add(qov, name, list);
  117. qobject_output_push(qov, list, listp);
  118. return true;
  119. }
  120. static GenericList *qobject_output_next_list(Visitor *v, GenericList *tail,
  121. size_t size)
  122. {
  123. return tail->next;
  124. }
  125. static void qobject_output_end_list(Visitor *v, void **obj)
  126. {
  127. QObjectOutputVisitor *qov = to_qov(v);
  128. QObject *value = qobject_output_pop(qov, obj);
  129. assert(qobject_type(value) == QTYPE_QLIST);
  130. }
  131. static bool qobject_output_type_int64(Visitor *v, const char *name,
  132. int64_t *obj, Error **errp)
  133. {
  134. QObjectOutputVisitor *qov = to_qov(v);
  135. qobject_output_add(qov, name, qnum_from_int(*obj));
  136. return true;
  137. }
  138. static bool qobject_output_type_uint64(Visitor *v, const char *name,
  139. uint64_t *obj, Error **errp)
  140. {
  141. QObjectOutputVisitor *qov = to_qov(v);
  142. qobject_output_add(qov, name, qnum_from_uint(*obj));
  143. return true;
  144. }
  145. static bool qobject_output_type_bool(Visitor *v, const char *name, bool *obj,
  146. Error **errp)
  147. {
  148. QObjectOutputVisitor *qov = to_qov(v);
  149. qobject_output_add(qov, name, qbool_from_bool(*obj));
  150. return true;
  151. }
  152. static bool qobject_output_type_str(Visitor *v, const char *name, char **obj,
  153. Error **errp)
  154. {
  155. QObjectOutputVisitor *qov = to_qov(v);
  156. if (*obj) {
  157. qobject_output_add(qov, name, qstring_from_str(*obj));
  158. } else {
  159. qobject_output_add(qov, name, qstring_from_str(""));
  160. }
  161. return true;
  162. }
  163. static bool qobject_output_type_number(Visitor *v, const char *name,
  164. double *obj, Error **errp)
  165. {
  166. QObjectOutputVisitor *qov = to_qov(v);
  167. qobject_output_add(qov, name, qnum_from_double(*obj));
  168. return true;
  169. }
  170. static bool qobject_output_type_any(Visitor *v, const char *name,
  171. QObject **obj, Error **errp)
  172. {
  173. QObjectOutputVisitor *qov = to_qov(v);
  174. qobject_output_add_obj(qov, name, qobject_ref(*obj));
  175. return true;
  176. }
  177. static bool qobject_output_type_null(Visitor *v, const char *name,
  178. QNull **obj, Error **errp)
  179. {
  180. QObjectOutputVisitor *qov = to_qov(v);
  181. qobject_output_add(qov, name, qnull());
  182. return true;
  183. }
  184. static bool qobject_output_policy_skip(Visitor *v, const char *name,
  185. uint64_t features)
  186. {
  187. CompatPolicy *pol = &v->compat_policy;
  188. return ((features & 1u << QAPI_DEPRECATED)
  189. && pol->deprecated_output == COMPAT_POLICY_OUTPUT_HIDE)
  190. || ((features & 1u << QAPI_UNSTABLE)
  191. && pol->unstable_output == COMPAT_POLICY_OUTPUT_HIDE);
  192. }
  193. /* Finish building, and return the root object.
  194. * The root object is never null. The caller becomes the object's
  195. * owner, and should use qobject_unref() when done with it. */
  196. static void qobject_output_complete(Visitor *v, void *opaque)
  197. {
  198. QObjectOutputVisitor *qov = to_qov(v);
  199. /* A visit must have occurred, with each start paired with end. */
  200. assert(qov->root && QSLIST_EMPTY(&qov->stack));
  201. assert(opaque == qov->result);
  202. *qov->result = qobject_ref(qov->root);
  203. qov->result = NULL;
  204. }
  205. static void qobject_output_free(Visitor *v)
  206. {
  207. QObjectOutputVisitor *qov = to_qov(v);
  208. QStackEntry *e;
  209. while (!QSLIST_EMPTY(&qov->stack)) {
  210. e = QSLIST_FIRST(&qov->stack);
  211. QSLIST_REMOVE_HEAD(&qov->stack, node);
  212. g_free(e);
  213. }
  214. qobject_unref(qov->root);
  215. g_free(qov);
  216. }
  217. Visitor *qobject_output_visitor_new(QObject **result)
  218. {
  219. QObjectOutputVisitor *v;
  220. v = g_malloc0(sizeof(*v));
  221. v->visitor.type = VISITOR_OUTPUT;
  222. v->visitor.start_struct = qobject_output_start_struct;
  223. v->visitor.end_struct = qobject_output_end_struct;
  224. v->visitor.start_list = qobject_output_start_list;
  225. v->visitor.next_list = qobject_output_next_list;
  226. v->visitor.end_list = qobject_output_end_list;
  227. v->visitor.type_int64 = qobject_output_type_int64;
  228. v->visitor.type_uint64 = qobject_output_type_uint64;
  229. v->visitor.type_bool = qobject_output_type_bool;
  230. v->visitor.type_str = qobject_output_type_str;
  231. v->visitor.type_number = qobject_output_type_number;
  232. v->visitor.type_any = qobject_output_type_any;
  233. v->visitor.type_null = qobject_output_type_null;
  234. v->visitor.policy_skip = qobject_output_policy_skip;
  235. v->visitor.complete = qobject_output_complete;
  236. v->visitor.free = qobject_output_free;
  237. *result = NULL;
  238. v->result = result;
  239. return &v->visitor;
  240. }