00001 #ifndef BGLIBS__GENERIC_STACK__H__ 00002 #define BGLIBS__GENERIC_STACK__H__ 00003 00004 #include <adt/common.h> 00005 00020 struct gstack_node 00021 { 00023 struct gstack_node* next; 00025 char data[0]; 00026 }; 00027 00029 struct gstack 00030 { 00032 struct gstack_node* head; 00034 unsigned count; 00035 }; 00036 00037 int gstack_push(struct gstack* d, unsigned datasize, const void* data, 00038 adt_copy_fn* fn); 00039 void* gstack_top(const struct gstack* s); 00040 void gstack_pop(struct gstack* s, adt_free_fn* fn); 00041 00043 #define GSTACK_DECL(PREFIX,TYPE) \ 00044 extern int PREFIX##_push(struct gstack* s, TYPE const* data); \ 00045 extern TYPE* PREFIX##_top(struct gstack* s); \ 00046 extern void PREFIX##_pop(struct gstack* s); 00047 00049 #define GSTACK_PUSH_DEFN(PREFIX,TYPE,COPY) \ 00050 int PREFIX##_push(struct gstack* s, TYPE const* data) { \ 00051 return gstack_push(s, sizeof *data, data, (adt_copy_fn*)COPY); \ 00052 } 00053 00055 #define GSTACK_TOP_DEFN(PREFIX,TYPE) \ 00056 TYPE* PREFIX##_top(struct gstack* s) { \ 00057 return (s->head == 0) ? 0 : (TYPE*)s->head->data; \ 00058 } 00059 00061 #define GSTACK_POP_DEFN(PREFIX,FREE) \ 00062 void PREFIX##_pop(struct gstack* s) { \ 00063 gstack_pop(s, (adt_free_fn*)(FREE)); \ 00064 } 00065 00069 #define GSTACK_DEFN(PREFIX,TYPE,COPY,FREE) \ 00070 GSTACK_PUSH_DEFN(PREFIX,TYPE,COPY) \ 00071 GSTACK_TOP_DEFN(PREFIX,TYPE) \ 00072 GSTACK_POP_DEFN(PREFIX,FREE) 00073 00076 #endif