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