reentryfilter.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #ifndef __REENTRYFILTER_H
  2. #define __REENTRYFILTER_H
  3. #include <bfc/tlist.h>
  4. class ReentryFilterObject {
  5. public:
  6. ReentryFilterObject() {}
  7. virtual ~ReentryFilterObject() {}
  8. void enterScope(int scope) {
  9. scopes.addItem(scope);
  10. }
  11. int isInScope(int scope) {
  12. for (int i=0;i<scopes.getNumItems();i++)
  13. if (scopes.enumItem(i) == scope) return 1;
  14. return 0;
  15. }
  16. void leaveScope(int scope) {
  17. for (int i=0;i<scopes.getNumItems();i++)
  18. if (scopes.enumItem(i) == scope) {
  19. scopes.delByPos(i);
  20. return;
  21. }
  22. }
  23. private:
  24. TList<int> scopes;
  25. };
  26. class ReentryFilter {
  27. public:
  28. ReentryFilter() : reentering(0), thisscope(-1), filter(NULL) {} // call setFilterObject, enterScope and leaveScope manually
  29. ReentryFilter(ReentryFilterObject *filterobj, intptr_t scope) {
  30. setFilterObject(filterobj);
  31. enterScope(scope);
  32. }
  33. void setFilterObject(ReentryFilterObject *filterobj) {
  34. filter = filterobj;
  35. }
  36. void enterScope(intptr_t scope) {
  37. ASSERT(filter != NULL);
  38. if (filter->isInScope(scope)) {
  39. reentering = 1;
  40. } else {
  41. filter->enterScope(scope);
  42. reentering = 0;
  43. }
  44. thisscope = scope;
  45. }
  46. void leaveScope() {
  47. ASSERT(filter != NULL);
  48. filter->leaveScope(thisscope);
  49. }
  50. virtual ~ReentryFilter() {
  51. if (reentering) return;
  52. leaveScope();
  53. }
  54. int mustLeave() { return reentering; }
  55. private:
  56. intptr_t thisscope;
  57. int reentering;
  58. ReentryFilterObject *filter;
  59. };
  60. #endif