BsMonoUtil.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsMonoPrerequisites.h"
  5. namespace bs
  6. {
  7. /** @addtogroup Mono
  8. * @{
  9. */
  10. /** Utility class containing methods for various common Mono/Script related operations. */
  11. class BS_MONO_EXPORT MonoUtil
  12. {
  13. public:
  14. /** Converts a Mono (managed) string to a native wide string. */
  15. static WString monoToWString(MonoString* str);
  16. /** Converts a Mono (managed) string to a native narrow string. */
  17. static String monoToString(MonoString* str);
  18. /** Converts a native wide string to a Mono (managed) string. */
  19. static MonoString* wstringToMono(const WString& str);
  20. /** Converts a native narrow string to a Mono (managed) string. */
  21. static MonoString* stringToMono(const String& str);
  22. /** Outputs name and namespace for the type of the specified object. */
  23. static void getClassName(MonoObject* obj, String& ns, String& typeName);
  24. /** Outputs name and namespace for the specified type. */
  25. static void getClassName(::MonoClass* monoClass, String& ns, String& typeName);
  26. /** Outputs name and namespace for the specified type. */
  27. static void getClassName(MonoReflectionType* monoReflType, String& ns, String& typeName);
  28. /** Returns the class of the provided object. */
  29. static ::MonoClass* getClass(MonoObject* object);
  30. /** Returns the class of the provided type. */
  31. static ::MonoClass* getClass(MonoReflectionType* type);
  32. /** Returns the type of the provided object. */
  33. static MonoReflectionType* getType(MonoObject* object);
  34. /** Returns the type of the provided class. */
  35. static MonoReflectionType* getType(::MonoClass* klass);
  36. /**
  37. * Creates a new GC handle for the provided managed object. The handle can be stored and later used for retrieving
  38. * the MonoObject* related to it by calling getObjectFromGCHandle(). This is a strong handle, meaning it will
  39. * prevent the garbage collector from collecting the object until it is released by calling freeGCHandle().
  40. *
  41. * @param[in] object Managed object to create the handle for.
  42. * @param[in] pinned If true the object will be pinned in memory, meaning you will be allowed to store
  43. * a reference to the MonoObject directly. Never store MonoObject* unless they have been
  44. * previously pinned (instead use getObjectFromGCHandle*( to get the current pointer).
  45. * Note that pinning can have an impact on memory fragmentation as it prevents the GC from
  46. * moving the object, so use it sparingly.
  47. */
  48. static UINT32 newGCHandle(MonoObject* object, bool pinned = true);
  49. /**
  50. * Creates a new GC handle for the provided managed object. The handle can be stored and later used for retrieving
  51. * the MonoObject* related to it by calling getObjectFromGCHandle(). This is a weak handle, meaning it will NOT
  52. * prevent the garbage collector from collecting the object. getObjectFromGCHandle() will return null if the GC
  53. * collected the object and handle is no longer valid.
  54. */
  55. static UINT32 newWeakGCHandle(MonoObject* object);
  56. /** Frees a GC handle previously allocated with newGCHandle. */
  57. static void freeGCHandle(UINT32 handle);
  58. /** Returns a MonoObject from an allocated GC handle. */
  59. static MonoObject* getObjectFromGCHandle(UINT32 handle);
  60. /** Converts a managed value type into a reference type by boxing it. */
  61. static MonoObject* box(::MonoClass* klass, void* value);
  62. /** Unboxes a managed object back to a raw value type. */
  63. static void* unbox(MonoObject* object);
  64. /** Checks if this class is a sub class of the specified class. */
  65. static bool isSubClassOf(::MonoClass* subClass, ::MonoClass* parentClass);
  66. /** Checks is the specified class a value type. */
  67. static bool isValueType(::MonoClass* object);
  68. /** Checks is the specified class an enum. */
  69. static bool isEnum(::MonoClass* object);
  70. /** Returns the underlying primitive type for an enum. */
  71. static MonoPrimitiveType getEnumPrimitiveType(::MonoClass* enumClass);
  72. /** Returns the primitive type of the provided class. */
  73. static MonoPrimitiveType getPrimitiveType(::MonoClass* monoClass);
  74. /** Binds parameters to a generic class, and returns a new instantiable class with the bound parameters. */
  75. static ::MonoClass* bindGenericParameters(::MonoClass* klass, ::MonoClass** params, UINT32 numParams);
  76. /** Returns Mono class for a 16-bit unsigned integer. */
  77. static ::MonoClass* getUINT16Class();
  78. /** Returns Mono class for a 16-bit signed integer. */
  79. static ::MonoClass* getINT16Class();
  80. /** Returns Mono class for a 32-bit unsigned integer. */
  81. static ::MonoClass* getUINT32Class();
  82. /** Returns Mono class for a 32-bit signed integer. */
  83. static ::MonoClass* getINT32Class();
  84. /** Returns Mono class for a 64-bit unsigned integer. */
  85. static ::MonoClass* getUINT64Class();
  86. /** Returns Mono class for a 32-bit signed integer. */
  87. static ::MonoClass* getINT64Class();
  88. /** Returns Mono class for a string. */
  89. static ::MonoClass* getStringClass();
  90. /** Returns Mono class for a floating point number. */
  91. static ::MonoClass* getFloatClass();
  92. /** Returns Mono class for a double floating point number. */
  93. static ::MonoClass* getDoubleClass();
  94. /** Returns Mono class for a boolean. */
  95. static ::MonoClass* getBoolClass();
  96. /** Returns Mono class for an unsigned byte. */
  97. static ::MonoClass* getByteClass();
  98. /** Returns Mono class for a byte. */
  99. static ::MonoClass* getSByteClass();
  100. /** Returns Mono class for a char. */
  101. static ::MonoClass* getCharClass();
  102. /** Returns Mono class for a generic object. */
  103. static ::MonoClass* getObjectClass();
  104. /** @copydoc throwIfException(MonoObject*) */
  105. static void throwIfException(MonoException* exception);
  106. /** Throws a native exception if the provided object is a valid managed exception. */
  107. static void throwIfException(MonoObject* exception);
  108. /** Invokes a thunk retrieved from MonoMethod::getThunk const and automatically handles exceptions. */
  109. template<class T, class... Args>
  110. static void invokeThunk(T* thunk, Args... args)
  111. {
  112. MonoException* exception = nullptr;
  113. thunk(std::forward<Args>(args)..., &exception);
  114. throwIfException(exception);
  115. }
  116. };
  117. /** @} */
  118. }
  119. #include "BsMonoArray.h"