xref: /openbmc/qemu/include/qapi/qmp/qlist.h (revision 80adf54e)
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 "qemu/queue.h"
19 
20 typedef struct QListEntry {
21     QObject *value;
22     QTAILQ_ENTRY(QListEntry) next;
23 } QListEntry;
24 
25 typedef struct QList {
26     QObject base;
27     QTAILQ_HEAD(,QListEntry) head;
28 } QList;
29 
30 #define qlist_append(qlist, obj) \
31         qlist_append_obj(qlist, QOBJECT(obj))
32 
33 /* Helpers for int, bool, and string */
34 #define qlist_append_int(qlist, value) \
35         qlist_append(qlist, qnum_from_int(value))
36 #define qlist_append_bool(qlist, value) \
37         qlist_append(qlist, qbool_from_bool(value))
38 #define qlist_append_str(qlist, value) \
39         qlist_append(qlist, qstring_from_str(value))
40 
41 #define QLIST_FOREACH_ENTRY(qlist, var)             \
42         for ((var) = ((qlist)->head.tqh_first);     \
43             (var);                                  \
44             (var) = ((var)->next.tqe_next))
45 
46 static inline QObject *qlist_entry_obj(const QListEntry *entry)
47 {
48     return entry->value;
49 }
50 
51 QList *qlist_new(void);
52 QList *qlist_copy(QList *src);
53 void qlist_append_obj(QList *qlist, QObject *obj);
54 void qlist_iter(const QList *qlist,
55                 void (*iter)(QObject *obj, void *opaque), void *opaque);
56 QObject *qlist_pop(QList *qlist);
57 QObject *qlist_peek(QList *qlist);
58 int qlist_empty(const QList *qlist);
59 size_t qlist_size(const QList *qlist);
60 QList *qobject_to_qlist(const QObject *obj);
61 void qlist_destroy_obj(QObject *obj);
62 
63 static inline const QListEntry *qlist_first(const QList *qlist)
64 {
65     return QTAILQ_FIRST(&qlist->head);
66 }
67 
68 static inline const QListEntry *qlist_next(const QListEntry *entry)
69 {
70     return QTAILQ_NEXT(entry, next);
71 }
72 
73 #endif /* QLIST_H */
74