ActivationFactoryBase.h 2.0 KB

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