BsMonoArray.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #pragma once
  2. #include "BsMonoPrerequisites.h"
  3. #include "BsScriptMeta.h"
  4. #include "BsMonoManager.h"
  5. #include <mono/jit/jit.h>
  6. namespace BansheeEngine
  7. {
  8. /** Helper class for creating and parsing managed arrays.*/
  9. class BS_MONO_EXPORT ScriptArray
  10. {
  11. public:
  12. /** Wraps an existing array and allows you to get/set its values. */
  13. ScriptArray(MonoArray* existingArray);
  14. /** Creates a new array of specified size with elements of the specified type. */
  15. ScriptArray(MonoClass& klass, UINT32 size);
  16. /** Creates a new array of specified size with elements of the specified type. */
  17. ScriptArray(::MonoClass* klass, UINT32 size);
  18. /** Retrieves an entry from the array at the specified index. */
  19. template<class T>
  20. T get(UINT32 idx)
  21. {
  22. return mono_array_get(mInternal, T, idx);
  23. }
  24. /** Sets an entry from the array at the specified index. */
  25. template<class T>
  26. void set(UINT32 idx, const T& value)
  27. {
  28. mono_array_set(mInternal, T, idx, value);
  29. }
  30. /** Retrieves an entry as a native string from the array at the specified index. */
  31. template<>
  32. String get(UINT32 idx);
  33. /** Retrieves an entry as a native string from the array at the specified index. */
  34. template<>
  35. WString get(UINT32 idx);
  36. /** Sets a native string entry from the array at the specified index. */
  37. template<>
  38. void set<String>(UINT32 idx, const String& value);
  39. /** Sets a native string entry from the array at the specified index. */
  40. template<>
  41. void set<WString>(UINT32 idx, const WString& value);
  42. /** Returns the raw object from the array at the specified index. */
  43. template<class T>
  44. T* getRawPtr(UINT32 idx = 0)
  45. {
  46. #if BS_DEBUG_MODE
  47. assert(sizeof(T) == elementSize());
  48. #endif
  49. return (T*)mono_array_addr(mInternal, T, idx);
  50. }
  51. /**
  52. * Returns the raw object from the array at the specified index. Provided size determines the size of each
  53. * element in the array. Caller must ensure it is correct for the specified array.
  54. */
  55. UINT8* getRawPtr(UINT32 size, UINT32 idx = 0)
  56. {
  57. #if BS_DEBUG_MODE
  58. assert(size == elementSize());
  59. #endif
  60. return (UINT8*)mono_array_addr_with_size(mInternal, size, idx);
  61. }
  62. /**
  63. * Creates a new array of managed objects.
  64. *
  65. * @tparam T ScriptObject wrapper for the specified managed type.
  66. */
  67. template<class T>
  68. static ScriptArray create(UINT32 size)
  69. {
  70. return ScriptArray(*T::getMetaData()->scriptClass, size);
  71. }
  72. /** Creates a new array of integers. */
  73. template<>
  74. static ScriptArray create<UINT32>(UINT32 size)
  75. {
  76. return ScriptArray(mono_get_uint32_class(), size);
  77. }
  78. /** Creates a new array of integers. */
  79. template<>
  80. static ScriptArray create<INT32>(UINT32 size)
  81. {
  82. return ScriptArray(mono_get_int32_class(), size);
  83. }
  84. /** Creates a new array of integers. */
  85. template<>
  86. static ScriptArray create<UINT64>(UINT32 size)
  87. {
  88. return ScriptArray(mono_get_uint64_class(), size);
  89. }
  90. /** Creates a new array of integers. */
  91. template<>
  92. static ScriptArray create<INT64>(UINT32 size)
  93. {
  94. return ScriptArray(mono_get_int64_class(), size);
  95. }
  96. /** Creates a new array of strings. */
  97. template<>
  98. static ScriptArray create<WString>(UINT32 size)
  99. {
  100. return ScriptArray(mono_get_string_class(), size);
  101. }
  102. /** Creates a new array of strings. */
  103. template<>
  104. static ScriptArray create<String>(UINT32 size)
  105. {
  106. return ScriptArray(mono_get_string_class(), size);
  107. }
  108. /** Creates a new array of floats. */
  109. template<>
  110. static ScriptArray create<float>(UINT32 size)
  111. {
  112. return ScriptArray(mono_get_single_class(), size);
  113. }
  114. /** Creates a new array of booleans. */
  115. template<>
  116. static ScriptArray create<bool>(UINT32 size)
  117. {
  118. return ScriptArray(mono_get_boolean_class(), size);
  119. }
  120. /** Returns number of elements in the array. */
  121. UINT32 size() const;
  122. /** Returns the size of an individual element in the array, in bytes. */
  123. UINT32 elementSize() const;
  124. /** Returns the managed object representing this array. */
  125. MonoArray* getInternal() const { return mInternal; }
  126. private:
  127. MonoArray* mInternal;
  128. };
  129. }