depend.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. #include "precomp_wasabi_bfc.h"
  2. #include "depend.h"
  3. #include "platform/guid.h"
  4. DependentViewerI::DependentViewerI() : viewed_items(NULL) { }
  5. DependentViewerI::DependentViewerI(const DependentViewerI &dep) :
  6. viewed_items(NULL)
  7. {
  8. // watch their items now
  9. if (dep.viewed_items != NULL)
  10. {
  11. for (int i = 0; i < dep.viewed_items->getNumItems(); i++)
  12. viewer_addViewItem(dep.viewed_items->enumItem(i));
  13. }
  14. }
  15. DependentViewerI::~DependentViewerI()
  16. {
  17. if (viewed_items != NULL)
  18. {
  19. // tell viewed items to detach from us
  20. foreach(viewed_items)
  21. viewed_items->getfor()->dependent_regViewer(this, FALSE);
  22. endfor
  23. #if 0
  24. for (int i = 0; i < viewed_items->getNumItems(); i++)
  25. {
  26. ifc_dependent *item = viewed_items->enumItem(i);
  27. item->dependent_regViewer(this, FALSE);
  28. }
  29. #endif
  30. viewed_items->removeAll();
  31. delete viewed_items;
  32. }
  33. }
  34. DependentViewerI &DependentViewerI::operator =(const DependentViewerI &dep)
  35. {
  36. if (this != &dep)
  37. {
  38. viewer_delAllItems();
  39. // watch their items now
  40. if (dep.viewed_items != NULL)
  41. {
  42. for (int i = 0; i < dep.viewed_items->getNumItems(); i++)
  43. viewer_addViewItem(dep.viewed_items->enumItem(i));
  44. }
  45. }
  46. return *this;
  47. }
  48. void DependentViewerI::viewer_addViewItem(ifc_dependent *item)
  49. {
  50. ASSERT(item != NULL);
  51. if (viewed_items == NULL) viewed_items = new DependentList(8);
  52. if (viewed_items->haveItem(item)) return ;
  53. viewed_items->addItem(item);
  54. item->dependent_regViewer(this, TRUE);
  55. }
  56. void DependentViewerI::viewer_delViewItem(ifc_dependent *item)
  57. {
  58. if (item == NULL) return ;
  59. if (viewed_items == NULL || !viewed_items->haveItem(item)) return ;
  60. item->dependent_regViewer(this, FALSE);
  61. viewed_items->removeItem(item);
  62. if (viewed_items->getNumItems() == 0)
  63. {
  64. delete viewed_items; viewed_items = NULL;
  65. }
  66. }
  67. void DependentViewerI::viewer_delAllItems()
  68. {
  69. while (viewed_items != NULL)
  70. {
  71. ifc_dependent *item = viewer_enumViewItem(0);
  72. if (item == NULL) break;
  73. viewer_delViewItem(item);
  74. }
  75. delete viewed_items;
  76. viewed_items = NULL;
  77. }
  78. void DependentViewerI::viewer_delAllOfClass(const GUID *guid)
  79. {
  80. if (viewed_items == NULL) return ;
  81. foreach(viewed_items)
  82. if (viewed_items->getfor()->dependent_getInterface(guid))
  83. viewer_delViewItem(viewed_items->getfor());
  84. endfor
  85. }
  86. ifc_dependent *DependentViewerI::viewer_enumViewItem(int which)
  87. {
  88. if (viewed_items == NULL) return NULL;
  89. return viewed_items->enumItem(which);
  90. }
  91. int DependentViewerI::viewer_getNumItems()
  92. {
  93. if (viewed_items == NULL) return 0;
  94. return viewed_items->getNumItems();
  95. }
  96. int DependentViewerI::viewer_haveItem(ifc_dependent *item)
  97. {
  98. if (viewed_items == NULL) return 0;
  99. return viewed_items->haveItem(item);
  100. }
  101. int DependentViewerI::dependentViewer_callback(ifc_dependent *item, const GUID *classguid, int cb, intptr_t param1, intptr_t param2, void *ptr, size_t ptrlen)
  102. {
  103. if (viewed_items == NULL || !viewed_items->haveItem(item)) return 0;
  104. switch (cb)
  105. {
  106. case DependentCB::DEPCB_NOP: return 1;
  107. case DependentCB::DEPCB_DELETED:
  108. viewed_items->removeItem(item);
  109. return viewer_onItemDeleted(item);
  110. case DependentCB::DEPCB_EVENT:
  111. return viewer_onEvent(item, classguid, (int)param1, param2, ptr, ptrlen);
  112. break;
  113. }
  114. return 0;
  115. }
  116. DependentI::DependentI(const GUID *classguid) : viewers(NULL), class_guid(classguid ? *classguid : INVALID_GUID)
  117. {}
  118. DependentI::DependentI(const DependentI &dep) : viewers(NULL), class_guid(dep.class_guid) { }
  119. DependentI::~DependentI()
  120. {
  121. if (viewers != NULL)
  122. {
  123. foreach(viewers)
  124. viewers->getfor()->dependentViewer_callback(this, &class_guid, DependentCB::DEPCB_DELETED);
  125. endfor
  126. delete viewers;
  127. }
  128. }
  129. DependentI& DependentI::operator =(const ifc_dependent &dep)
  130. {
  131. // placeholder code... nobody asked to watch us
  132. if (this != &dep)
  133. {
  134. viewers = NULL;
  135. }
  136. return *this;
  137. }
  138. void DependentI::dependent_regViewer(api_dependentviewer *viewer, int add)
  139. {
  140. if (viewer == NULL) return ;
  141. if (add)
  142. {
  143. if (viewers == NULL) viewers = new ViewerList(8);
  144. if (viewers->haveItem(viewer)) return ;
  145. viewers->addItem(viewer);
  146. viewer->dependentViewer_callback(this, &class_guid, DependentCB::DEPCB_NOP);
  147. }
  148. else
  149. { // delete
  150. if (viewers == NULL) return ;
  151. viewers->removeItem(viewer);
  152. if (viewers->getNumItems() == 0)
  153. {
  154. delete viewers; viewers = NULL;
  155. }
  156. }
  157. dependent_onRegViewer(viewer, add);
  158. }
  159. void DependentI::dependent_sendEvent(const GUID *classguid, int event, intptr_t param, void *ptr, size_t ptrlen, api_dependentviewer *viewer)
  160. {
  161. if (classguid == NULL) classguid = &class_guid; // use default
  162. ASSERT(*classguid != INVALID_GUID);
  163. sendViewerCallbacks(classguid, DependentCB::DEPCB_EVENT, event, param, ptr, ptrlen, viewer);
  164. }
  165. void DependentI::sendViewerCallbacks(const GUID *classguid, int msg, intptr_t param1, intptr_t param2, void *ptr, size_t ptrlen, api_dependentviewer *viewer)
  166. {
  167. if (viewers == NULL) return ;
  168. ASSERT(classguid != NULL);
  169. foreach(viewers)
  170. if (viewer == NULL || viewer == viewers->getfor())
  171. viewers->getfor()->dependentViewer_callback(this, classguid, msg, param1, param2, ptr, ptrlen);
  172. endfor
  173. }
  174. #ifdef CBCLASS
  175. #undef CBCLASS
  176. #endif
  177. #define CBCLASS DependentViewerI
  178. START_DISPATCH;
  179. CB(DEPENDENTVIEWER_CALLBACK, dependentViewer_callback);
  180. END_DISPATCH;
  181. #undef CBCLASS
  182. #define CBCLASS DependentI
  183. START_DISPATCH;
  184. VCB(API_DEPENDENT_REGVIEWER, dependent_regViewer);
  185. CB(API_DEPENDENT_GETINTERFACE, dependent_getInterface);
  186. END_DISPATCH;
  187. #undef CBCLASS