foreach.h 1014 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #ifndef _FOREACH_H
  2. #define _FOREACH_H
  3. #include "memblock.h"
  4. #include "ptrlist.h"
  5. // foreach stuff
  6. /* use like this:
  7. PtrList<blah> list;
  8. foreach(list)
  9. list.getfor()->booga();
  10. something(list.getfor());
  11. endfor
  12. */
  13. // foreach stuff
  14. class __foreach
  15. {
  16. public:
  17. __foreach(const PtrListRoot &ptrlist, int reverse=FALSE);
  18. __foreach(const PtrListRoot *ptrlist, int reverse=FALSE);
  19. int done() const;
  20. void *next(int advance = TRUE);
  21. void *getPtr() const;
  22. int getPos() const;
  23. private:
  24. MemBlock<void *> list;
  25. int pos;
  26. };
  27. #define foreach(x) \
  28. { \
  29. void *__fe_void; \
  30. __foreach ___f(x); \
  31. for (__fe_void = ___f.getPtr(); !___f.done(); __fe_void = ___f.next()) {
  32. #define getfor() castFor(__fe_void)
  33. #define endfor \
  34. } \
  35. }
  36. #define foreach_reverse(x) \
  37. { \
  38. void *__fe_void; \
  39. __foreach ___f(x, TRUE); \
  40. for (__fe_void = ___f.getPtr(); !___f.done(); __fe_void = ___f.next()) {
  41. #define getfor() castFor(__fe_void)
  42. #define endfor \
  43. } \
  44. }
  45. #define foreach_index (___f.getPos())
  46. #endif