serviceList.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #include "./main.h"
  2. #include "./serviceList.h"
  3. #include "./ifc_omservice.h"
  4. OmServiceList::OmServiceList()
  5. : ref(1), cursor(0)
  6. {
  7. }
  8. OmServiceList::~OmServiceList()
  9. {
  10. size_t index = list.size();
  11. while(index--)
  12. {
  13. list[index]->Release();
  14. }
  15. }
  16. HRESULT OmServiceList::CreateInstance(OmServiceList **instance)
  17. {
  18. if (NULL == instance) return E_POINTER;
  19. *instance = new OmServiceList();
  20. if (NULL == *instance) return E_OUTOFMEMORY;
  21. return S_OK;
  22. }
  23. size_t OmServiceList::AddRef()
  24. {
  25. return InterlockedIncrement((LONG*)&ref);
  26. }
  27. size_t OmServiceList::Release()
  28. {
  29. if (0 == ref)
  30. return ref;
  31. LONG r = InterlockedDecrement((LONG*)&ref);
  32. if (0 == r)
  33. delete(this);
  34. return r;
  35. }
  36. int OmServiceList::QueryInterface(GUID interface_guid, void **object)
  37. {
  38. if (NULL == object) return E_POINTER;
  39. if (IsEqualIID(interface_guid, IFC_OmServiceEnum))
  40. *object = static_cast<ifc_omserviceenum*>(this);
  41. else
  42. {
  43. *object = NULL;
  44. return E_NOINTERFACE;
  45. }
  46. if (NULL == *object)
  47. return E_UNEXPECTED;
  48. AddRef();
  49. return S_OK;
  50. }
  51. HRESULT OmServiceList::Next(unsigned long listSize, ifc_omservice **elementList, unsigned long *elementCount)
  52. {
  53. if (NULL == elementList || 0 == listSize) return E_INVALIDARG;
  54. size_t size = list.size();
  55. if (cursor >= size)
  56. {
  57. if (NULL != elementCount) *elementCount = 0;
  58. return S_FALSE;
  59. }
  60. size_t available = size - cursor;
  61. size_t count = ((available > listSize) ? listSize : available);
  62. ifc_omservice **source = &list.at(0) + cursor;
  63. CopyMemory(elementList, source, count * sizeof(ifc_omservice*));
  64. for(size_t i = 0; i < count; i++)
  65. {
  66. elementList[i]->AddRef();
  67. }
  68. cursor += count;
  69. if (NULL != elementCount)
  70. *elementCount = (ULONG)count;
  71. return (count == listSize) ? S_OK : S_FALSE;
  72. }
  73. HRESULT OmServiceList::Reset(void)
  74. {
  75. cursor = 0;
  76. return S_OK;
  77. }
  78. HRESULT OmServiceList::Skip(unsigned long elementCount)
  79. {
  80. size_t size = list.size();
  81. cursor += elementCount;
  82. if (cursor > size)
  83. cursor = size;
  84. return (cursor < size) ? S_OK : S_FALSE;
  85. }
  86. HRESULT OmServiceList::Add(ifc_omservice *service)
  87. {
  88. if(NULL == service) return E_INVALIDARG;
  89. list.push_back(service);
  90. service->AddRef();
  91. return S_OK;
  92. }
  93. HRESULT OmServiceList::Remove(size_t index)
  94. {
  95. if (index >= list.size())
  96. return E_FAIL;
  97. ifc_omservice *temp = list[index];
  98. list.erase(list.begin() + index);
  99. temp->Release();
  100. return S_OK;
  101. }
  102. HRESULT OmServiceList::Clear()
  103. {
  104. size_t index = list.size();
  105. while(index--)
  106. {
  107. list[index]->Release();
  108. }
  109. list.clear();
  110. return S_OK;
  111. }
  112. #define CBCLASS OmServiceList
  113. START_DISPATCH;
  114. CB(ADDREF, AddRef)
  115. CB(RELEASE, Release)
  116. CB(QUERYINTERFACE, QueryInterface)
  117. CB(API_NEXT, Next)
  118. CB(API_RESET, Reset)
  119. CB(API_SKIP, Skip)
  120. END_DISPATCH;
  121. #undef CBCLASS