BsMonoArray.h 4.3 KB

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