qlit.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * QLit literal qobject
  3. *
  4. * Copyright IBM, Corp. 2009
  5. * Copyright (c) 2013, 2015, 2017 Red Hat Inc.
  6. *
  7. * Authors:
  8. * Anthony Liguori <aliguori@us.ibm.com>
  9. * Markus Armbruster <armbru@redhat.com>
  10. * Marc-André Lureau <marcandre.lureau@redhat.com>
  11. *
  12. * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
  13. * See the COPYING.LIB file in the top-level directory.
  14. */
  15. #include "qemu/osdep.h"
  16. #include "qobject/qlit.h"
  17. #include "qobject/qbool.h"
  18. #include "qobject/qlist.h"
  19. #include "qobject/qnum.h"
  20. #include "qobject/qdict.h"
  21. #include "qobject/qstring.h"
  22. #include "qobject/qnull.h"
  23. static bool qlit_equal_qdict(const QLitObject *lhs, const QDict *qdict)
  24. {
  25. int i;
  26. for (i = 0; lhs->value.qdict[i].key; i++) {
  27. QObject *obj = qdict_get(qdict, lhs->value.qdict[i].key);
  28. if (!qlit_equal_qobject(&lhs->value.qdict[i].value, obj)) {
  29. return false;
  30. }
  31. }
  32. /* Note: the literal qdict must not contain duplicates, this is
  33. * considered a programming error and it isn't checked here. */
  34. if (qdict_size(qdict) != i) {
  35. return false;
  36. }
  37. return true;
  38. }
  39. static bool qlit_equal_qlist(const QLitObject *lhs, const QList *qlist)
  40. {
  41. QListEntry *e;
  42. int i = 0;
  43. QLIST_FOREACH_ENTRY(qlist, e) {
  44. QObject *obj = qlist_entry_obj(e);
  45. if (!qlit_equal_qobject(&lhs->value.qlist[i], obj)) {
  46. return false;
  47. }
  48. i++;
  49. }
  50. return !e && lhs->value.qlist[i].type == QTYPE_NONE;
  51. }
  52. bool qlit_equal_qobject(const QLitObject *lhs, const QObject *rhs)
  53. {
  54. if (!rhs || lhs->type != qobject_type(rhs)) {
  55. return false;
  56. }
  57. switch (lhs->type) {
  58. case QTYPE_QBOOL:
  59. return lhs->value.qbool == qbool_get_bool(qobject_to(QBool, rhs));
  60. case QTYPE_QNUM:
  61. return lhs->value.qnum == qnum_get_int(qobject_to(QNum, rhs));
  62. case QTYPE_QSTRING:
  63. return (strcmp(lhs->value.qstr,
  64. qstring_get_str(qobject_to(QString, rhs))) == 0);
  65. case QTYPE_QDICT:
  66. return qlit_equal_qdict(lhs, qobject_to(QDict, rhs));
  67. case QTYPE_QLIST:
  68. return qlit_equal_qlist(lhs, qobject_to(QList, rhs));
  69. case QTYPE_QNULL:
  70. return true;
  71. default:
  72. break;
  73. }
  74. return false;
  75. }
  76. QObject *qobject_from_qlit(const QLitObject *qlit)
  77. {
  78. switch (qlit->type) {
  79. case QTYPE_QNULL:
  80. return QOBJECT(qnull());
  81. case QTYPE_QNUM:
  82. return QOBJECT(qnum_from_int(qlit->value.qnum));
  83. case QTYPE_QSTRING:
  84. return QOBJECT(qstring_from_str(qlit->value.qstr));
  85. case QTYPE_QDICT: {
  86. QDict *qdict = qdict_new();
  87. QLitDictEntry *e;
  88. for (e = qlit->value.qdict; e->key; e++) {
  89. qdict_put_obj(qdict, e->key, qobject_from_qlit(&e->value));
  90. }
  91. return QOBJECT(qdict);
  92. }
  93. case QTYPE_QLIST: {
  94. QList *qlist = qlist_new();
  95. QLitObject *e;
  96. for (e = qlit->value.qlist; e->type != QTYPE_NONE; e++) {
  97. qlist_append_obj(qlist, qobject_from_qlit(e));
  98. }
  99. return QOBJECT(qlist);
  100. }
  101. case QTYPE_QBOOL:
  102. return QOBJECT(qbool_from_bool(qlit->value.qbool));
  103. default:
  104. g_assert_not_reached();
  105. }
  106. return NULL;
  107. }