1
0

deviceSupportedCommandEnum.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #include "main.h"
  2. #include "./deviceSupportedCommandEnum.h"
  3. #include <new>
  4. DeviceSupportedCommandEnum::DeviceSupportedCommandEnum()
  5. : ref(1), commands(NULL), count(0), cursor(0)
  6. {
  7. }
  8. DeviceSupportedCommandEnum::~DeviceSupportedCommandEnum()
  9. {
  10. if (NULL != commands)
  11. {
  12. while(count--)
  13. commands[count]->Release();
  14. }
  15. }
  16. HRESULT DeviceSupportedCommandEnum::CreateInstance(ifc_devicesupportedcommand **commands, size_t count,
  17. DeviceSupportedCommandEnum **instance)
  18. {
  19. size_t index, size;
  20. void *storage;
  21. ifc_devicesupportedcommand *c;
  22. DeviceSupportedCommandEnum *enumerator;
  23. if (NULL == instance)
  24. return E_POINTER;
  25. *instance = NULL;
  26. size = sizeof(DeviceSupportedCommandEnum) + (sizeof(ifc_devicesupportedcommand**) * count);
  27. storage = calloc(1, size);
  28. if (NULL == storage)
  29. return E_OUTOFMEMORY;
  30. enumerator = new(storage) DeviceSupportedCommandEnum();
  31. if (NULL == enumerator)
  32. {
  33. free(storage);
  34. return E_FAIL;
  35. }
  36. enumerator->commands = (ifc_devicesupportedcommand**)(((BYTE*)enumerator) + sizeof(DeviceSupportedCommandEnum));
  37. for (index = 0; index < count; index++)
  38. {
  39. c = commands[index];
  40. if (NULL != c)
  41. {
  42. enumerator->commands[enumerator->count] = c;
  43. c->AddRef();
  44. enumerator->count++;
  45. }
  46. }
  47. *instance = enumerator;
  48. return S_OK;
  49. }
  50. size_t DeviceSupportedCommandEnum::AddRef()
  51. {
  52. return InterlockedIncrement((LONG*)&ref);
  53. }
  54. size_t DeviceSupportedCommandEnum::Release()
  55. {
  56. if (0 == ref)
  57. return ref;
  58. LONG r = InterlockedDecrement((LONG*)&ref);
  59. if (0 == r)
  60. delete(this);
  61. return r;
  62. }
  63. int DeviceSupportedCommandEnum::QueryInterface(GUID interface_guid, void **object)
  64. {
  65. if (NULL == object) return E_POINTER;
  66. if (IsEqualIID(interface_guid, IFC_DeviceSupportedCommandEnum))
  67. *object = static_cast<ifc_devicesupportedcommandenum*>(this);
  68. else
  69. {
  70. *object = NULL;
  71. return E_NOINTERFACE;
  72. }
  73. if (NULL == *object)
  74. return E_UNEXPECTED;
  75. AddRef();
  76. return S_OK;
  77. }
  78. HRESULT DeviceSupportedCommandEnum::Next(ifc_devicesupportedcommand **objects, size_t bufferMax, size_t *fetched)
  79. {
  80. size_t available, copied, index;
  81. ifc_devicesupportedcommand **source;
  82. if (NULL == objects)
  83. return E_POINTER;
  84. if (0 == bufferMax)
  85. return E_INVALIDARG;
  86. if (cursor >= count)
  87. {
  88. if (NULL != fetched)
  89. *fetched = 0;
  90. return S_FALSE;
  91. }
  92. available = count - cursor;
  93. copied = ((available > bufferMax) ? bufferMax : available);
  94. source = commands + cursor;
  95. CopyMemory(objects, source, copied * sizeof(ifc_devicesupportedcommand*));
  96. for(index = 0; index < copied; index++)
  97. objects[index]->AddRef();
  98. cursor += copied;
  99. if (NULL != fetched)
  100. *fetched = copied;
  101. return (bufferMax == copied) ? S_OK : S_FALSE;
  102. }
  103. HRESULT DeviceSupportedCommandEnum::Reset(void)
  104. {
  105. cursor = 0;
  106. return S_OK;
  107. }
  108. HRESULT DeviceSupportedCommandEnum::Skip(size_t count)
  109. {
  110. cursor += count;
  111. if (cursor > this->count)
  112. cursor = this->count;
  113. return (cursor < this->count) ? S_OK : S_FALSE;
  114. }
  115. HRESULT DeviceSupportedCommandEnum::GetCount(size_t *count)
  116. {
  117. if (NULL == count)
  118. return E_POINTER;
  119. *count = this->count;
  120. return S_OK;
  121. }
  122. #define CBCLASS DeviceSupportedCommandEnum
  123. START_DISPATCH;
  124. CB(ADDREF, AddRef)
  125. CB(RELEASE, Release)
  126. CB(QUERYINTERFACE, QueryInterface)
  127. CB(API_NEXT, Next)
  128. CB(API_RESET, Reset)
  129. CB(API_SKIP, Skip)
  130. CB(API_GETCOUNT, GetCount)
  131. END_DISPATCH;
  132. #undef CBCLASS