serviceEventMngr.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #include "main.h"
  2. #include "./service.h"
  3. #include "./ifc_omserviceevent.h"
  4. HRESULT OmService::RegisterEventHandler(ifc_omserviceevent *handler)
  5. {
  6. if (NULL == handler)
  7. return E_POINTER;
  8. HRESULT hr;
  9. EnterCriticalSection(&eventLock);
  10. size_t index = eventList.size();
  11. while (index--)
  12. {
  13. if (handler == eventList[index])
  14. {
  15. break;
  16. }
  17. }
  18. if (((size_t)-1) == index)
  19. {
  20. eventList.push_back(handler);
  21. handler->AddRef();
  22. hr = S_OK;
  23. }
  24. else
  25. {
  26. hr = E_UNEXPECTED;
  27. }
  28. LeaveCriticalSection(&eventLock);
  29. return hr;
  30. }
  31. HRESULT OmService::UnregisterEventHandler(ifc_omserviceevent *handler)
  32. {
  33. if (NULL == handler)
  34. return E_POINTER;
  35. HRESULT hr;
  36. EnterCriticalSection(&eventLock);
  37. size_t index = eventList.size();
  38. while (index--)
  39. {
  40. if (handler == eventList[index])
  41. {
  42. break;
  43. }
  44. }
  45. if (((size_t)-1) != index)
  46. {
  47. ifc_omserviceevent *h = eventList[index];
  48. eventList.erase(eventList.begin() + index);
  49. h->Release();
  50. hr = S_OK;
  51. }
  52. else
  53. {
  54. hr = S_FALSE;
  55. }
  56. LeaveCriticalSection(&eventLock);
  57. return hr;
  58. }
  59. void OmService::UnregisterAllEventHandlers()
  60. {
  61. EnterCriticalSection(&eventLock);
  62. size_t index = eventList.size();
  63. while(index--)
  64. {
  65. ifc_omserviceevent *handler = eventList[index];
  66. if (NULL != handler) handler->Release();
  67. }
  68. eventList.clear();
  69. LeaveCriticalSection(&eventLock);
  70. }
  71. HRESULT OmService::Signal_ServiceChange(unsigned int modifiedFlags)
  72. {
  73. EnterCriticalSection(&eventLock);
  74. size_t index = eventList.size();
  75. while (index--)
  76. {
  77. ifc_omserviceevent *handler = eventList[index];
  78. if (NULL != handler)
  79. handler->ServiceChange(this, modifiedFlags);
  80. }
  81. LeaveCriticalSection(&eventLock);
  82. return S_OK;
  83. }
  84. HRESULT OmService::Signal_CommandStateChange(const GUID *commandGroup, UINT commandId)
  85. {
  86. EnterCriticalSection(&eventLock);
  87. size_t index = eventList.size();
  88. while (index--)
  89. {
  90. ifc_omserviceevent *handler = eventList[index];
  91. if (NULL != handler)
  92. handler->CommandStateChange(this, commandGroup, commandId);
  93. }
  94. LeaveCriticalSection(&eventLock);
  95. return S_OK;
  96. }