ReferenceCounter.h 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #pragma once
  2. #include <utility>
  3. #include <hstring.h>
  4. #include <Unknwn.h>
  5. namespace il2cpp
  6. {
  7. namespace winrt
  8. {
  9. template<typename T, bool isIUnknown = std::is_base_of<IUnknown, std::remove_pointer<T>::type>::value>
  10. struct ReferenceCounter;
  11. template<typename T>
  12. struct ReferenceCounter<T, false>
  13. {
  14. static inline void AddRef(T& value)
  15. {
  16. }
  17. static inline void Release(T& value)
  18. {
  19. }
  20. };
  21. template<typename T>
  22. struct ReferenceCounter<T, true>
  23. {
  24. static inline void AddRef(T& value)
  25. {
  26. if (value == nullptr)
  27. return;
  28. value->AddRef();
  29. }
  30. static inline void Release(T& value)
  31. {
  32. if (value == nullptr)
  33. return;
  34. value->Release();
  35. }
  36. };
  37. template<>
  38. struct ReferenceCounter<HSTRING, false>
  39. {
  40. static inline void AddRef(HSTRING& value)
  41. {
  42. if (value == nullptr)
  43. return;
  44. auto hr = WindowsDuplicateString(value, &value);
  45. Assert(SUCCEEDED(hr));
  46. }
  47. static inline void Release(HSTRING& value)
  48. {
  49. if (value == nullptr)
  50. return;
  51. auto hr = WindowsDeleteString(value);
  52. Assert(SUCCEEDED(hr));
  53. }
  54. };
  55. }
  56. }