ActivationFactoryBase.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #pragma once
  2. #include "os/WindowsRuntime.h"
  3. #include "vm/Atomic.h"
  4. #include "vm/ComObjectBase.h"
  5. #include "vm/Exception.h"
  6. #include "utils/Memory.h"
  7. #include "utils/TemplateUtils.h"
  8. #include <new>
  9. namespace il2cpp
  10. {
  11. namespace vm
  12. {
  13. template<typename TDerived>
  14. struct NOVTABLE ActivationFactoryBase : public ComObjectBase, Il2CppIActivationFactory
  15. {
  16. private:
  17. volatile uint32_t m_RefCount;
  18. public:
  19. ActivationFactoryBase() :
  20. m_RefCount(1) // We start with a ref count of 1
  21. {
  22. Il2CppStaticAssert(utils::TemplateUtils::IsBaseOf<ActivationFactoryBase<TDerived>, TDerived>::value);
  23. }
  24. virtual il2cpp_hresult_t STDCALL ActivateInstance(Il2CppIInspectable** instance) IL2CPP_OVERRIDE
  25. {
  26. return IL2CPP_E_NOTIMPL;
  27. }
  28. IL2CPP_FORCE_INLINE uint32_t AddRefImpl()
  29. {
  30. return Atomic::Increment(&m_RefCount);
  31. }
  32. IL2CPP_FORCE_INLINE uint32_t ReleaseImpl()
  33. {
  34. const uint32_t count = Atomic::Decrement(&m_RefCount);
  35. if (count == 0)
  36. Destroy();
  37. return count;
  38. }
  39. IL2CPP_FORCE_INLINE il2cpp_hresult_t GetRuntimeClassNameImpl(Il2CppHString* className)
  40. {
  41. utils::StringView<Il2CppNativeChar> classNameView(IL2CPP_NATIVE_STRING("System.Runtime.InteropServices.WindowsRuntime.IActivationFactory"));
  42. return os::WindowsRuntime::CreateHString(classNameView, className);
  43. }
  44. IL2CPP_FORCE_INLINE static TDerived* __CreateInstance()
  45. {
  46. void* memory = utils::Memory::Malloc(sizeof(TDerived));
  47. if (memory == NULL)
  48. Exception::RaiseOutOfMemoryException();
  49. return new(memory) TDerived;
  50. }
  51. private:
  52. IL2CPP_FORCE_INLINE void Destroy()
  53. {
  54. IL2CPP_ASSERT(m_RefCount == 0);
  55. TDerived* instance = static_cast<TDerived*>(this);
  56. instance->~TDerived();
  57. utils::Memory::Free(instance);
  58. }
  59. };
  60. }
  61. }