4 * Copyright (C) 2009 Red Hat Inc.
7 * Luiz Capitulino <lcapitulino@redhat.com>
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.
13 #include "qemu/osdep.h"
14 #include "qapi/qmp/qbool.h"
15 #include "qapi/qmp/qlist.h"
16 #include "qapi/qmp/qnull.h"
17 #include "qapi/qmp/qnum.h"
18 #include "qapi/qmp/qstring.h"
19 #include "qemu/queue.h"
20 #include "qobject-internal.h"
23 * qlist_new(): Create a new QList
25 * Return strong reference.
27 QList
*qlist_new(void)
31 qlist
= g_malloc(sizeof(*qlist
));
32 qobject_init(QOBJECT(qlist
), QTYPE_QLIST
);
33 QTAILQ_INIT(&qlist
->head
);
38 QList
*qlist_copy(QList
*src
)
40 QList
*dst
= qlist_new();
44 QLIST_FOREACH_ENTRY(src
, entry
) {
45 elt
= qlist_entry_obj(entry
);
47 qlist_append_obj(dst
, elt
);
53 * qlist_append_obj(): Append an QObject into QList
55 * NOTE: ownership of 'value' is transferred to the QList
57 void qlist_append_obj(QList
*qlist
, QObject
*value
)
61 entry
= g_malloc(sizeof(*entry
));
64 QTAILQ_INSERT_TAIL(&qlist
->head
, entry
, next
);
67 void qlist_append_int(QList
*qlist
, int64_t value
)
69 qlist_append(qlist
, qnum_from_int(value
));
72 void qlist_append_bool(QList
*qlist
, bool value
)
74 qlist_append(qlist
, qbool_from_bool(value
));
77 void qlist_append_str(QList
*qlist
, const char *value
)
79 qlist_append(qlist
, qstring_from_str(value
));
82 void qlist_append_null(QList
*qlist
)
84 qlist_append(qlist
, qnull());
87 QObject
*qlist_pop(QList
*qlist
)
92 if (qlist
== NULL
|| QTAILQ_EMPTY(&qlist
->head
)) {
96 entry
= QTAILQ_FIRST(&qlist
->head
);
97 QTAILQ_REMOVE(&qlist
->head
, entry
, next
);
105 QObject
*qlist_peek(QList
*qlist
)
109 if (qlist
== NULL
|| QTAILQ_EMPTY(&qlist
->head
)) {
113 entry
= QTAILQ_FIRST(&qlist
->head
);
118 int qlist_empty(const QList
*qlist
)
120 return QTAILQ_EMPTY(&qlist
->head
);
123 size_t qlist_size(const QList
*qlist
)
128 QLIST_FOREACH_ENTRY(qlist
, entry
) {
135 * qlist_is_equal(): Test whether the two QLists are equal
137 * In order to be considered equal, the respective two objects at each
138 * index of the two lists have to compare equal (regarding
139 * qobject_is_equal()), and both lists have to have the same number of
141 * That means both lists have to contain equal objects in equal order.
143 bool qlist_is_equal(const QObject
*x
, const QObject
*y
)
145 const QList
*list_x
= qobject_to(QList
, x
);
146 const QList
*list_y
= qobject_to(QList
, y
);
147 const QListEntry
*entry_x
, *entry_y
;
149 entry_x
= qlist_first(list_x
);
150 entry_y
= qlist_first(list_y
);
152 while (entry_x
&& entry_y
) {
153 if (!qobject_is_equal(qlist_entry_obj(entry_x
),
154 qlist_entry_obj(entry_y
)))
159 entry_x
= qlist_next(entry_x
);
160 entry_y
= qlist_next(entry_y
);
163 return !entry_x
&& !entry_y
;
167 * qlist_destroy_obj(): Free all the memory allocated by a QList
169 void qlist_destroy_obj(QObject
*obj
)
172 QListEntry
*entry
, *next_entry
;
175 qlist
= qobject_to(QList
, obj
);
177 QTAILQ_FOREACH_SAFE(entry
, &qlist
->head
, next
, next_entry
) {
178 QTAILQ_REMOVE(&qlist
->head
, entry
, next
);
179 qobject_unref(entry
->value
);
186 void qlist_unref(QList
*q
)