bglibs: gstack.h Source File

- Main Page
- Modules
- Data Structures
- Files
- Directories
- Related Pages
- Examples

# gstack.h

    00001 /* $Id: gstack.h 712 2006-07-27 22:32:39Z bruce $ */
    00002 #ifndef BGLIBS__GENERIC_STACK__H__
    00003 #define BGLIBS__GENERIC_STACK__H__
    00004
    00005 #include <adt/common.h>
    00006
    00021 struct gstack_node
    00022 {
    00024   struct gstack_node* next;
    00026   char data[0];
    00027 };
    00028
    00030 struct gstack
    00031 {
    00033   struct gstack_node* head;
    00035   unsigned count;
    00036 };
    00037
    00038 int gstack_push(struct gstack* d, unsigned datasize, const void* data,
    00039                 adt_copy_fn* fn);
    00040 void* gstack_top(const struct gstack* s);
    00041 void gstack_pop(struct gstack* s, adt_free_fn* fn);
    00042
    00044 #define GSTACK_DECL(PREFIX,TYPE) \
    00045 extern int PREFIX##_push(struct gstack* s, TYPE const* data); \
    00046 extern TYPE* PREFIX##_top(struct gstack* s); \
    00047 extern void PREFIX##_pop(struct gstack* s);
    00048
    00050 #define GSTACK_PUSH_DEFN(PREFIX,TYPE,COPY) \
    00051 int PREFIX##_push(struct gstack* s, TYPE const* data) { \
    00052   return gstack_push(s, sizeof *data, data, (adt_copy_fn*)COPY); \
    00053 }
    00054
    00056 #define GSTACK_TOP_DEFN(PREFIX,TYPE) \
    00057 TYPE* PREFIX##_top(struct gstack* s) { \
    00058   return (s->head == 0) ? 0 : (TYPE*)s->head->data; \
    00059 }
    00060
    00062 #define GSTACK_POP_DEFN(PREFIX,FREE) \
    00063 void PREFIX##_pop(struct gstack* s) { \
    00064   gstack_pop(s, (adt_free_fn*)(FREE)); \
    00065 }
    00066
    00070 #define GSTACK_DEFN(PREFIX,TYPE,COPY,FREE) \
    00071 GSTACK_PUSH_DEFN(PREFIX,TYPE,COPY) \
    00072 GSTACK_TOP_DEFN(PREFIX,TYPE) \
    00073 GSTACK_POP_DEFN(PREFIX,FREE)
    00074
    00077 #endif