stack.h 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. //PORTABLE
  2. #ifndef _STACK_H
  3. #define _STACK_H
  4. #include <bfc/common.h>
  5. #include <bfc/wasabi_std.h>
  6. // a self-growing stack. note that it never shrinks (for now)
  7. class StackBase {
  8. protected:
  9. StackBase();
  10. ~StackBase();
  11. int push(void *item, int sizeofT, int increment);
  12. int peek();
  13. int peekAt(void *ptr, int n, int sizeofT);
  14. int getRef(void **ptr, int n, int sizeofT);
  15. void *top(int sizeofT);
  16. int pop(void *ptr, int sizeofT);
  17. int isempty();
  18. void purge();
  19. private:
  20. int nslots, cur;
  21. char *stack;
  22. };
  23. template<class T>
  24. class Stack : public StackBase {
  25. public:
  26. int push(T item, int increment=-1) { return StackBase::push(&item, sizeof(T), increment); }
  27. using StackBase::peek;
  28. T top() { return *static_cast<T*>(StackBase::top(sizeof(T))); }
  29. int peekAt(T *ptr = NULL, int n = 0) { return StackBase::peekAt(ptr, n, sizeof(T)); }
  30. int getRef(T **ptr = NULL, int n = 0) { if (!ptr) return peek(); return StackBase::getRef((void **)&(*ptr), n, sizeof(T)); }
  31. int pop(T *ptr = NULL) { return StackBase::pop(ptr, sizeof(T)); }
  32. using StackBase::isempty;
  33. using StackBase::purge;
  34. };
  35. #endif