From fb08dde0988f920fed93d07745790e7ebc12af32 Mon Sep 17 00:00:00 2001 From: Luiz Capitulino Date: Fri, 28 Aug 2009 15:27:07 -0300 Subject: Introduce QDict QDict is a high-level dictionary data type that can be used to store a collection of QObjects. A unique key is associated with only one QObject. The following functions are available: - qdict_new() Create a new QDict - qdict_put() Add a new 'key:object' pair - qdict_get() Get the QObject of a given key - qdict_del() Delete a 'key:object' pair - qdict_size() Return the size of the dictionary - qdict_haskey() Check if a given 'key' exists Some high-level helpers to operate on QStrings and QInts objects are also provided. Signed-off-by: Luiz Capitulino Signed-off-by: Anthony Liguori --- qdict.h | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 qdict.h (limited to 'qdict.h') diff --git a/qdict.h b/qdict.h new file mode 100644 index 0000000000..613d163209 --- /dev/null +++ b/qdict.h @@ -0,0 +1,42 @@ +#ifndef QDICT_H +#define QDICT_H + +#include "qobject.h" +#include "sys-queue.h" +#include + +#define QDICT_HASH_SIZE 512 + +typedef struct QDictEntry { + char *key; + QObject *value; + LIST_ENTRY(QDictEntry) next; +} QDictEntry; + +typedef struct QDict { + QObject_HEAD; + size_t size; + LIST_HEAD(,QDictEntry) table[QDICT_HASH_SIZE]; +} QDict; + +/* Object API */ +QDict *qdict_new(void); +size_t qdict_size(const QDict *qdict); +void qdict_put_obj(QDict *qdict, const char *key, QObject *value); +void qdict_del(QDict *qdict, const char *key); +int qdict_haskey(const QDict *qdict, const char *key); +QObject *qdict_get(const QDict *qdict, const char *key); +QDict *qobject_to_qdict(const QObject *obj); + +/* Helper to qdict_put_obj(), accepts any object */ +#define qdict_put(qdict, key, obj) \ + qdict_put_obj(qdict, key, QOBJECT(obj)) + +/* High level helpers */ +int64_t qdict_get_int(const QDict *qdict, const char *key); +const char *qdict_get_str(const QDict *qdict, const char *key); +int64_t qdict_get_try_int(const QDict *qdict, const char *key, + int64_t err_value); +const char *qdict_get_try_str(const QDict *qdict, const char *key); + +#endif /* QDICT_H */ -- cgit v1.2.1