xref: /openbmc/qemu/include/qapi/qmp/qlist.h (revision f764718d)
1 /*
2  * QList Module
3  *
4  * Copyright (C) 2009 Red Hat Inc.
5  *
6  * Authors:
7  *  Luiz Capitulino <lcapitulino@redhat.com>
8  *
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.
11  */
12 
13 #ifndef QLIST_H
14 #define QLIST_H
15 
16 #include "qapi/qmp/qobject.h"
17 #include "qapi/qmp/qnum.h"
18 #include "qapi/qmp/qnull.h"
19 #include "qemu/queue.h"
20 
21 typedef struct QListEntry {
22     QObject *value;
23     QTAILQ_ENTRY(QListEntry) next;
24 } QListEntry;
25 
26 typedef struct QList {
27     QObject base;
28     QTAILQ_HEAD(,QListEntry) head;
29 } QList;
30 
31 #define qlist_append(qlist, obj) \
32         qlist_append_obj(qlist, QOBJECT(obj))
33 
34 /* Helpers for int, bool, and string */
35 #define qlist_append_int(qlist, value) \
36         qlist_append(qlist, qnum_from_int(value))
37 #define qlist_append_bool(qlist, value) \
38         qlist_append(qlist, qbool_from_bool(value))
39 #define qlist_append_str(qlist, value) \
40         qlist_append(qlist, qstring_from_str(value))
41 #define qlist_append_null(qlist) \
42         qlist_append(qlist, qnull())
43 
44 #define QLIST_FOREACH_ENTRY(qlist, var)             \
45         for ((var) = ((qlist)->head.tqh_first);     \
46             (var);                                  \
47             (var) = ((var)->next.tqe_next))
48 
49 static inline QObject *qlist_entry_obj(const QListEntry *entry)
50 {
51     return entry->value;
52 }
53 
54 QList *qlist_new(void);
55 QList *qlist_copy(QList *src);
56 void qlist_append_obj(QList *qlist, QObject *obj);
57 void qlist_iter(const QList *qlist,
58                 void (*iter)(QObject *obj, void *opaque), void *opaque);
59 QObject *qlist_pop(QList *qlist);
60 QObject *qlist_peek(QList *qlist);
61 int qlist_empty(const QList *qlist);
62 size_t qlist_size(const QList *qlist);
63 QList *qobject_to_qlist(const QObject *obj);
64 bool qlist_is_equal(const QObject *x, const QObject *y);
65 void qlist_destroy_obj(QObject *obj);
66 
67 static inline const QListEntry *qlist_first(const QList *qlist)
68 {
69     return QTAILQ_FIRST(&qlist->head);
70 }
71 
72 static inline const QListEntry *qlist_next(const QListEntry *entry)
73 {
74     return QTAILQ_NEXT(entry, next);
75 }
76 
77 #endif /* QLIST_H */
78