BsMonoUtil.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. /** Creates a new GC handle for the provided managed object, ensuring it doesn't go out of scope. */
  37. static UINT32 newGCHandle(MonoObject* object);
  38. /** Frees a GC handle previously allocated with newGCHandle. */
  39. static void freeGCHandle(UINT32 handle);
  40. /** Converts a managed value type into a reference type by boxing it. */
  41. static MonoObject* box(::MonoClass* klass, void* value);
  42. /** Unboxes a managed object back to a raw value type. */
  43. static void* unbox(MonoObject* object);
  44. /** Checks if this class is a sub class of the specified class. */
  45. static bool isSubClassOf(::MonoClass* subClass, ::MonoClass* parentClass);
  46. /** Checks is the specified class a value type. */
  47. static bool isValueType(::MonoClass* object);
  48. /** Checks is the specified class an enum. */
  49. static bool isEnum(::MonoClass* object);
  50. /** Returns the underlying primitive type for an enum. */
  51. static MonoPrimitiveType getEnumPrimitiveType(::MonoClass* enumClass);
  52. /** Returns the primitive type of the provided class. */
  53. static MonoPrimitiveType getPrimitiveType(::MonoClass* monoClass);
  54. /** Binds parameters to a generic class, and returns a new instantiable class with the bound parameters. */
  55. static ::MonoClass* bindGenericParameters(::MonoClass* klass, ::MonoClass** params, UINT32 numParams);
  56. /** Returns Mono class for a 16-bit unsigned integer. */
  57. static ::MonoClass* getUINT16Class();
  58. /** Returns Mono class for a 16-bit signed integer. */
  59. static ::MonoClass* getINT16Class();
  60. /** Returns Mono class for a 32-bit unsigned integer. */
  61. static ::MonoClass* getUINT32Class();
  62. /** Returns Mono class for a 32-bit signed integer. */
  63. static ::MonoClass* getINT32Class();
  64. /** Returns Mono class for a 64-bit unsigned integer. */
  65. static ::MonoClass* getUINT64Class();
  66. /** Returns Mono class for a 32-bit signed integer. */
  67. static ::MonoClass* getINT64Class();
  68. /** Returns Mono class for a string. */
  69. static ::MonoClass* getStringClass();
  70. /** Returns Mono class for a floating point number. */
  71. static ::MonoClass* getFloatClass();
  72. /** Returns Mono class for a double floating point number. */
  73. static ::MonoClass* getDoubleClass();
  74. /** Returns Mono class for a boolean. */
  75. static ::MonoClass* getBoolClass();
  76. /** Returns Mono class for an unsigned byte. */
  77. static ::MonoClass* getByteClass();
  78. /** Returns Mono class for a byte. */
  79. static ::MonoClass* getSByteClass();
  80. /** Returns Mono class for a char. */
  81. static ::MonoClass* getCharClass();
  82. /** Returns Mono class for a generic object. */
  83. static ::MonoClass* getObjectClass();
  84. /** @copydoc throwIfException(MonoObject*) */
  85. static void throwIfException(MonoException* exception);
  86. /** Throws a native exception if the provided object is a valid managed exception. */
  87. static void throwIfException(MonoObject* exception);
  88. /** Invokes a thunk retrieved from MonoMethod::getThunk const and automatically handles exceptions. */
  89. template<class T, class... Args>
  90. static void invokeThunk(T* thunk, Args... args)
  91. {
  92. MonoException* exception = nullptr;
  93. thunk(std::forward<Args>(args)..., &exception);
  94. throwIfException(exception);
  95. }
  96. };
  97. /** @} */
  98. }
  99. #include "BsMonoArray.h"