1
0

storageHandlerEnum.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #include "main.h"
  2. #include "./storageHandlerEnum.h"
  3. #include "./storageHandler.h"
  4. StorageHandlerEnum::StorageHandlerEnum()
  5. : ref(1), index(0)
  6. {
  7. }
  8. StorageHandlerEnum::~StorageHandlerEnum()
  9. {
  10. size_t index = handlerList.size();
  11. while(index--)
  12. {
  13. handlerList[index]->Release();
  14. }
  15. }
  16. HRESULT StorageHandlerEnum::CreateInstance(StorageHandlerEnum **instance)
  17. {
  18. if (NULL == instance) return E_POINTER;
  19. *instance = new StorageHandlerEnum();
  20. if (NULL == *instance) return E_OUTOFMEMORY;
  21. return S_OK;
  22. }
  23. HRESULT StorageHandlerEnum::CreateFromTemplate(const ifc_omstoragehelper::TemplateRecord *recordList, size_t recordCount, StorageHandlerEnum **instance)
  24. {
  25. if (NULL == instance) return E_POINTER;
  26. *instance = new StorageHandlerEnum();
  27. if (NULL == *instance)
  28. return E_OUTOFMEMORY;
  29. if (NULL != recordList)
  30. {
  31. StorageHandler *handler = NULL;
  32. for (size_t i = 0; i < recordCount; i++)
  33. {
  34. const ifc_omstoragehelper::TemplateRecord *record = &recordList[i];
  35. if (NULL != record &&
  36. SUCCEEDED(StorageHandler::CreateInstance(record->key, record->handler, StorageHandler::flagCopyKey, &handler)))
  37. {
  38. if (FAILED((*instance)->AddHandler(handler)))
  39. {
  40. handler->Release();
  41. }
  42. }
  43. }
  44. }
  45. return S_OK;
  46. }
  47. size_t StorageHandlerEnum::AddRef()
  48. {
  49. return InterlockedIncrement((LONG*)&ref);
  50. }
  51. size_t StorageHandlerEnum::Release()
  52. {
  53. if (0 == ref)
  54. return ref;
  55. LONG r = InterlockedDecrement((LONG*)&ref);
  56. if (0 == r)
  57. delete(this);
  58. return r;
  59. }
  60. int StorageHandlerEnum::QueryInterface(GUID interface_guid, void **object)
  61. {
  62. if (NULL == object) return E_POINTER;
  63. if (IsEqualIID(interface_guid, IFC_OmStorageHandlerEnum))
  64. *object = static_cast<ifc_omstoragehandlerenum*>(this);
  65. else
  66. {
  67. *object = NULL;
  68. return E_NOINTERFACE;
  69. }
  70. if (NULL == *object)
  71. return E_UNEXPECTED;
  72. AddRef();
  73. return S_OK;
  74. }
  75. HRESULT StorageHandlerEnum::Next(ULONG listSize, ifc_omstoragehandler **elementList, ULONG *elementCount)
  76. {
  77. if (NULL == elementList || 0 == listSize) return E_INVALIDARG;
  78. size_t size = handlerList.size();
  79. if (index >= size)
  80. {
  81. if (NULL != elementCount) *elementCount = 0;
  82. return S_FALSE;
  83. }
  84. ULONG count = 0;
  85. ifc_omstoragehandler *handler = NULL;
  86. for (;index < size && count < listSize; index++)
  87. {
  88. handler = handlerList[index];
  89. elementList[count] = handler;
  90. handler->AddRef();
  91. count++;
  92. }
  93. if (NULL != elementCount)
  94. *elementCount = count;
  95. return (count == listSize) ? S_OK : S_FALSE;
  96. }
  97. HRESULT StorageHandlerEnum::Reset(void)
  98. {
  99. index = 0;
  100. return S_OK;
  101. }
  102. HRESULT StorageHandlerEnum::Skip(ULONG elementCount)
  103. {
  104. index += elementCount;
  105. if (index >= handlerList.size())
  106. {
  107. index = (handlerList.size() - 1);
  108. return S_FALSE;
  109. }
  110. return S_OK;
  111. }
  112. HRESULT StorageHandlerEnum::AddHandler(ifc_omstoragehandler *handler)
  113. {
  114. if (NULL == handler)
  115. return E_INVALIDARG;
  116. handlerList.push_back(handler);
  117. return S_OK;
  118. }
  119. #define CBCLASS StorageHandlerEnum
  120. START_DISPATCH;
  121. CB(ADDREF, AddRef)
  122. CB(RELEASE, Release)
  123. CB(QUERYINTERFACE, QueryInterface)
  124. CB(API_NEXT, Next)
  125. CB(API_RESET, Reset)
  126. CB(API_SKIP, Skip)
  127. END_DISPATCH;
  128. #undef CBCLASS