BaselibHandleUtils.h 1.2 KB

12345678910111213141516171819202122232425262728293031323334
  1. #pragma once
  2. #include <string.h>
  3. namespace il2cpp
  4. {
  5. namespace utils
  6. {
  7. class BaselibHandleUtils
  8. {
  9. public:
  10. template<typename BaselibHandleType>
  11. static void* HandleToVoidPtr(BaselibHandleType baselibHandle)
  12. {
  13. // following asserts check that the handle fits into void* in its entirety
  14. static_assert(sizeof(BaselibHandleType) <= sizeof(void*), "baselib handle does not fit void*");
  15. static_assert(sizeof(BaselibHandleType::handle) <= sizeof(void*), "baselib handle does not fit void*");
  16. void* result = nullptr;
  17. memcpy(&result, &baselibHandle.handle, sizeof(result));
  18. return result;
  19. }
  20. template<typename BaselibHandleType>
  21. static BaselibHandleType VoidPtrToHandle(void* ptr)
  22. {
  23. static_assert(sizeof(BaselibHandleType) <= sizeof(void*), "baselib handle does not fit void*");
  24. static_assert(sizeof(BaselibHandleType::handle) <= sizeof(void*), "baselib handle does not fit void*");
  25. decltype(BaselibHandleType::handle)result = {};
  26. memcpy(&result, &ptr, sizeof(ptr));
  27. return BaselibHandleType { result };
  28. }
  29. };
  30. }
  31. }