storageEnum.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #include "main.h"
  2. #include "./storageEnum.h"
  3. #include "./ifc_omstorage.h"
  4. OmStorageEnumerator::OmStorageEnumerator(ifc_omstorage **storageList, size_t storageSize, const GUID *type, UINT capabilities)
  5. : ref(1), index(0), list(storageList), size(storageSize)
  6. {
  7. fType = (NULL != type) ? *type : GUID_NULL;
  8. fCapabilities = capabilities;
  9. }
  10. OmStorageEnumerator::~OmStorageEnumerator()
  11. {
  12. }
  13. HRESULT OmStorageEnumerator::CreateInstance(ifc_omstorage **storageList, size_t storageSize, const GUID *type, UINT capabilities, OmStorageEnumerator **instance)
  14. {
  15. if (NULL == instance) return E_POINTER;
  16. *instance = new OmStorageEnumerator(storageList, storageSize, type, capabilities);
  17. if (NULL == *instance) return E_OUTOFMEMORY;
  18. return S_OK;
  19. }
  20. size_t OmStorageEnumerator::AddRef()
  21. {
  22. return InterlockedIncrement((LONG*)&ref);
  23. }
  24. size_t OmStorageEnumerator::Release()
  25. {
  26. if (0 == ref)
  27. return ref;
  28. LONG r = InterlockedDecrement((LONG*)&ref);
  29. if (0 == r)
  30. delete(this);
  31. return r;
  32. }
  33. int OmStorageEnumerator::QueryInterface(GUID interface_guid, void **object)
  34. {
  35. if (NULL == object) return E_POINTER;
  36. if (IsEqualIID(interface_guid, IFC_OmStorageEnumerator))
  37. *object = static_cast<ifc_omstorageenumerator*>(this);
  38. else
  39. {
  40. *object = NULL;
  41. return E_NOINTERFACE;
  42. }
  43. if (NULL == *object)
  44. return E_UNEXPECTED;
  45. AddRef();
  46. return S_OK;
  47. }
  48. HRESULT OmStorageEnumerator::Next(ULONG listSize, ifc_omstorage **elementList, ULONG *elementCount)
  49. {
  50. if (NULL == elementList || 0 == listSize) return E_INVALIDARG;
  51. if (index >= size)
  52. {
  53. if (NULL != elementCount) *elementCount = 0;
  54. return S_FALSE;
  55. }
  56. ULONG count = 0;
  57. ifc_omstorage *storage = NULL;
  58. GUID storageId = GUID_NULL;
  59. for (;index < size && count < listSize; index++)
  60. {
  61. storage = list[index];
  62. if (NULL != storage &&
  63. SUCCEEDED(storage->GetType(&storageId)) && IsEqualGUID(storageId, fType) &&
  64. (0 == fCapabilities || fCapabilities == (fCapabilities & storage->GetCapabilities())))
  65. {
  66. elementList[count] = storage;
  67. storage->AddRef();
  68. count++;
  69. }
  70. }
  71. if (NULL != elementCount) *elementCount = count;
  72. return (count == listSize) ? S_OK : S_FALSE;
  73. }
  74. HRESULT OmStorageEnumerator::Reset(void)
  75. {
  76. index = 0;
  77. return S_OK;
  78. }
  79. HRESULT OmStorageEnumerator::Skip(ULONG elementCount)
  80. {
  81. index += elementCount;
  82. if (index >= size)
  83. {
  84. index = (size - 1);
  85. return S_FALSE;
  86. }
  87. return S_OK;
  88. }
  89. #define CBCLASS OmStorageEnumerator
  90. START_DISPATCH;
  91. CB(ADDREF, AddRef)
  92. CB(RELEASE, Release)
  93. CB(QUERYINTERFACE, QueryInterface)
  94. CB(API_NEXT, Next)
  95. CB(API_RESET, Reset)
  96. CB(API_SKIP, Skip)
  97. END_DISPATCH;
  98. #undef CBCLASS