BsMonoArray.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. /** Sets an entry from the array at the specified index. */
  27. template<class T>
  28. void set(UINT32 idx, const T& value);
  29. /** Returns the raw object from the array at the specified index. */
  30. template<class T>
  31. T* getRawPtr(UINT32 idx = 0)
  32. {
  33. #if BS_DEBUG_MODE
  34. assert(sizeof(T) == elementSize());
  35. #endif
  36. return (T*)mono_array_addr(mInternal, T, idx);
  37. }
  38. /**
  39. * Returns the raw object from the array at the specified index. Provided size determines the size of each
  40. * element in the array. Caller must ensure it is correct for the specified array.
  41. */
  42. UINT8* getRawPtr(UINT32 size, UINT32 idx = 0)
  43. {
  44. #if BS_DEBUG_MODE
  45. assert(size == elementSize());
  46. #endif
  47. return (UINT8*)mono_array_addr_with_size(mInternal, size, idx);
  48. }
  49. /**
  50. * Creates a new array of managed objects.
  51. *
  52. * @tparam T ScriptObject wrapper for the specified managed type.
  53. */
  54. template<class T>
  55. static ScriptArray create(UINT32 size);
  56. /** Returns number of elements in the array. */
  57. UINT32 size() const;
  58. /** Returns the size of an individual element in the array, in bytes. */
  59. UINT32 elementSize() const;
  60. /** Returns the managed object representing this array. */
  61. MonoArray* getInternal() const { return mInternal; }
  62. private:
  63. MonoArray* mInternal;
  64. };
  65. /** @} */
  66. /** @addtogroup Implementation
  67. * @{
  68. */
  69. namespace Detail
  70. {
  71. // A layer of indirection for all methods specialized by ScriptArray. */
  72. template<class T>
  73. T ScriptArray_get(MonoArray* array, UINT32 idx)
  74. {
  75. return mono_array_get(array, T, idx);
  76. }
  77. template<class T>
  78. void ScriptArray_set(MonoArray* array, UINT32 idx, const T& value)
  79. {
  80. mono_array_set(array, T, idx, value);
  81. }
  82. template<>
  83. BS_MONO_EXPORT String ScriptArray_get(MonoArray* array, UINT32 idx);
  84. template<>
  85. BS_MONO_EXPORT WString ScriptArray_get(MonoArray* array, UINT32 idx);
  86. template<>
  87. BS_MONO_EXPORT void ScriptArray_set<String>(MonoArray* array, UINT32 idx, const String& value);
  88. template<>
  89. BS_MONO_EXPORT void ScriptArray_set<WString>(MonoArray* array, UINT32 idx, const WString& value);
  90. template<class T>
  91. inline ScriptArray ScriptArray_create(UINT32 size)
  92. {
  93. return ScriptArray(*T::getMetaData()->scriptClass, size);
  94. }
  95. template<>
  96. inline ScriptArray ScriptArray_create<UINT32>(UINT32 size)
  97. {
  98. return ScriptArray(mono_get_uint32_class(), size);
  99. }
  100. template<>
  101. inline ScriptArray ScriptArray_create<INT32>(UINT32 size)
  102. {
  103. return ScriptArray(mono_get_int32_class(), size);
  104. }
  105. template<>
  106. inline ScriptArray ScriptArray_create<UINT64>(UINT32 size)
  107. {
  108. return ScriptArray(mono_get_uint64_class(), size);
  109. }
  110. template<>
  111. inline ScriptArray ScriptArray_create<INT64>(UINT32 size)
  112. {
  113. return ScriptArray(mono_get_int64_class(), size);
  114. }
  115. template<>
  116. inline ScriptArray ScriptArray_create<WString>(UINT32 size)
  117. {
  118. return ScriptArray(mono_get_string_class(), size);
  119. }
  120. template<>
  121. inline ScriptArray ScriptArray_create<String>(UINT32 size)
  122. {
  123. return ScriptArray(mono_get_string_class(), size);
  124. }
  125. template<>
  126. inline ScriptArray ScriptArray_create<float>(UINT32 size)
  127. {
  128. return ScriptArray(mono_get_single_class(), size);
  129. }
  130. template<>
  131. inline ScriptArray ScriptArray_create<bool>(UINT32 size)
  132. {
  133. return ScriptArray(mono_get_boolean_class(), size);
  134. }
  135. }
  136. /** @} */
  137. template<class T>
  138. T ScriptArray::get(UINT32 idx)
  139. {
  140. return Detail::ScriptArray_get<T>(mInternal, idx);
  141. }
  142. /** Sets an entry from the array at the specified index. */
  143. template<class T>
  144. void ScriptArray::set(UINT32 idx, const T& value)
  145. {
  146. Detail::ScriptArray_set<T>(mInternal, idx, value);
  147. }
  148. template<class T>
  149. ScriptArray ScriptArray::create(UINT32 size)
  150. {
  151. return Detail::ScriptArray_create<T>(size);
  152. }
  153. }