00001
00002 #ifndef BGLIBS__GENERIC_QUEUE__H__
00003 #define BGLIBS__GENERIC_QUEUE__H__
00004
00005 #include <adt/common.h>
00006
00021 struct gqueue_node
00022 {
00024 struct gqueue_node* next;
00026 char data[0];
00027 };
00028
00030 struct gqueue
00031 {
00033 struct gqueue_node* head;
00035 struct gqueue_node* tail;
00037 unsigned count;
00038 };
00039
00040 int gqueue_push(struct gqueue* d, unsigned datasize, const void* data,
00041 adt_copy_fn* fn);
00042 void* gqueue_top(const struct gqueue* q);
00043 void gqueue_pop(struct gqueue* q, adt_free_fn* fn);
00044
00046 #define GQUEUE_DECL(PREFIX,TYPE) \
00047 extern int PREFIX##_push(struct gqueue* q, TYPE const* data); \
00048 extern TYPE* PREFIX##_top(struct gqueue* q); \
00049 extern void PREFIX##_pop(struct gqueue* q);
00050
00052 #define GQUEUE_PUSH_DEFN(PREFIX,TYPE,COPY) \
00053 int PREFIX##_push(struct gqueue* q, TYPE const* data) { \
00054 return gqueue_push(q, sizeof *data, data, (adt_copy_fn*)COPY); \
00055 }
00056
00058 #define GQUEUE_TOP_DEFN(PREFIX,TYPE) \
00059 TYPE* PREFIX##_top(struct gqueue* q) { \
00060 return (q->head == 0) ? 0 : (TYPE*)q->head->data; \
00061 }
00062
00064 #define GQUEUE_POP_DEFN(PREFIX,FREE) \
00065 void PREFIX##_pop(struct gqueue* q) { \
00066 gqueue_pop(q, (adt_free_fn*)(FREE)); \
00067 }
00068
00072 #define GQUEUE_DEFN(PREFIX,TYPE,COPY,FREE) \
00073 GQUEUE_PUSH_DEFN(PREFIX,TYPE,COPY) \
00074 GQUEUE_TOP_DEFN(PREFIX,TYPE) \
00075 GQUEUE_POP_DEFN(PREFIX,FREE)
00076
00079 #endif