1
0

depview.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #ifndef _DEPVIEW_H
  2. #define _DEPVIEW_H
  3. #include <bfc/depend.h>
  4. #include <map>
  5. // this handles classes that use the getDependencyPtr() method instead of a
  6. // public cast
  7. template <class VT>
  8. class NOVTABLE DependentViewerTPtr : private DependentViewerI
  9. {
  10. protected:
  11. DependentViewerTPtr(VT *first = NULL)
  12. {
  13. _addViewItem(first);
  14. }
  15. void viewer_addViewItem(VT *item) { _addViewItem(item); }
  16. void viewer_delViewItem(VT *item) { _delViewItem(item); }
  17. using DependentViewerI::viewer_delAllItems;
  18. VT *viewer_enumViewItem(int which)
  19. {
  20. return lookup(DependentViewerI::viewer_enumViewItem(which));
  21. }
  22. using DependentViewerI::viewer_getNumItems;
  23. int viewer_haveItem(VT *item)
  24. {
  25. return DependentViewerI::viewer_haveItem(item->getDependencyPtr());
  26. }
  27. // item you were looking at is gone: WARNING: pointer no longer valid!
  28. virtual int viewer_onItemDeleted(VT *item) { return 1; }
  29. // item you are looking at issuing an event
  30. virtual int viewer_onEvent(VT *item, int event, intptr_t param2, void *ptr, size_t ptrlen) { return 1; }
  31. protected:
  32. virtual int viewer_onItemDeleted(ifc_dependent *item)
  33. {
  34. return viewer_onItemDeleted(lookup(item));
  35. }
  36. virtual int viewer_onEvent(ifc_dependent *item, const GUID *classguid, int event, intptr_t param, void *ptr, size_t ptrlen)
  37. {
  38. if (*classguid != *VT::depend_getClassGuid()) return 0; // filter namespace
  39. return viewer_onEvent(lookup(item), event, param, ptr, ptrlen);
  40. }
  41. private:
  42. VT *lookup(ifc_dependent *d)
  43. {
  44. if (d == NULL)
  45. return NULL;
  46. VT *vt = NULL;
  47. //int r = ptrmap.getItem(d, &vt);
  48. auto it = ptrmap.find(d);
  49. if (it != ptrmap.end())
  50. {
  51. vt = it->second;
  52. }
  53. //ASSERT(r);
  54. ASSERT(vt->getDependencyPtr() == d);
  55. return vt;
  56. }
  57. void _addViewItem(VT *item)
  58. {
  59. if (item == NULL)
  60. return ;
  61. ifc_dependent *d = item->getDependencyPtr();
  62. if (d == NULL)
  63. return ;
  64. ptrmap.insert({ d, item });
  65. DependentViewerI::viewer_addViewItem(d);
  66. }
  67. void _delViewItem(VT *item)
  68. {
  69. if (item == NULL) return ;
  70. ifc_dependent *dep = NULL;
  71. //ptrmap.reverseGetItem(item, &dep);
  72. //int r = ptrmap.reverseDelItem(item);
  73. for (auto &ptr : ptrmap)
  74. {
  75. if (ptr.second == item)
  76. {
  77. dep = ptr.first;
  78. ptrmap.erase(dep);
  79. break;
  80. }
  81. }
  82. //ASSERT(r);
  83. DependentViewerI::viewer_delViewItem(dep);
  84. }
  85. std::map<ifc_dependent *, VT *> ptrmap;
  86. };
  87. #endif