storageHandler.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #include "main.h"
  2. #include "./storageHandler.h"
  3. StorageHandler::StorageHandler(LPCWSTR pszKey, ifc_omstoragehelper::HandlerProc handlerProc, UINT flags)
  4. : ref(1), key(NULL), handler(handlerProc), flags(flags)
  5. {
  6. if (0 != (flagCopyKey & flags))
  7. key = Plugin_CopyString(pszKey);
  8. else
  9. key = (LPWSTR)pszKey;
  10. }
  11. StorageHandler::~StorageHandler()
  12. {
  13. if (0 != (flagCopyKey & flags))
  14. Plugin_FreeString(key);
  15. }
  16. HRESULT StorageHandler::CreateInstance(LPCWSTR pszKey, ifc_omstoragehelper::HandlerProc handlerProc, UINT flags, StorageHandler **instance)
  17. {
  18. if (NULL == instance)
  19. return E_POINTER;
  20. if (NULL == pszKey || L'\0' == pszKey || NULL == handlerProc)
  21. {
  22. *instance = NULL;
  23. return E_INVALIDARG;
  24. }
  25. *instance = new StorageHandler(pszKey, handlerProc, flags);
  26. if (NULL == *instance) return E_OUTOFMEMORY;
  27. return S_OK;
  28. }
  29. size_t StorageHandler::AddRef()
  30. {
  31. return InterlockedIncrement((LONG*)&ref);
  32. }
  33. size_t StorageHandler::Release()
  34. {
  35. if (0 == ref)
  36. return ref;
  37. LONG r = InterlockedDecrement((LONG*)&ref);
  38. if (0 == r)
  39. delete(this);
  40. return r;
  41. }
  42. int StorageHandler::QueryInterface(GUID interface_guid, void **object)
  43. {
  44. if (NULL == object) return E_POINTER;
  45. if (IsEqualIID(interface_guid, IFC_OmStorageHandler))
  46. *object = static_cast<ifc_omstoragehandler*>(this);
  47. else
  48. {
  49. *object = NULL;
  50. return E_NOINTERFACE;
  51. }
  52. if (NULL == *object)
  53. return E_UNEXPECTED;
  54. AddRef();
  55. return S_OK;
  56. }
  57. HRESULT StorageHandler::GetKey(LPCWSTR *ppKey)
  58. {
  59. if (NULL == ppKey) return E_POINTER;
  60. *ppKey = key;
  61. return S_OK;
  62. }
  63. void StorageHandler::Invoke(ifc_omservice *service, LPCWSTR pszKey, LPCWSTR pszValue)
  64. {
  65. handler(service, pszKey, pszValue);
  66. }
  67. #define CBCLASS StorageHandler
  68. START_DISPATCH;
  69. CB(ADDREF, AddRef)
  70. CB(RELEASE, Release)
  71. CB(QUERYINTERFACE, QueryInterface)
  72. CB(API_GETKEY, GetKey)
  73. VCB(API_INVOKE, Invoke)
  74. END_DISPATCH;
  75. #undef CBCLASS