BsMonoArray.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 "BsMonoUtil.h"
  7. #include "BsMonoManager.h"
  8. namespace bs
  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*)getArrayAddr(mInternal, sizeof(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)
  43. {
  44. #if BS_DEBUG_MODE
  45. assert(size == elementSize());
  46. #endif
  47. return getArrayAddr(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. /**
  63. * Returns the address of an array item at the specified index.
  64. *
  65. * @param[in] array Array from which to retrieve the item.
  66. * @param[in] size Size of a single item in the array.
  67. * @param[in] idx Index of the item to retrieve.
  68. * @return Address of the array item at the requested index.
  69. */
  70. static UINT8* getArrayAddr(MonoArray* array, UINT32 size, UINT32 idx);
  71. /** Returns the class of the elements within an array class. */
  72. static ::MonoClass* getElementClass(::MonoClass* arrayClass);
  73. /** Returns the rank of the provided array class. */
  74. static UINT32 getRank(::MonoClass* arrayClass);
  75. /** Builds an array class from the provided element class and a rank. */
  76. static ::MonoClass* buildArrayClass(::MonoClass* elementClass, UINT32 rank);
  77. private:
  78. MonoArray* mInternal;
  79. };
  80. /** @} */
  81. /** @addtogroup Implementation
  82. * @{
  83. */
  84. namespace Detail
  85. {
  86. // A layer of indirection for all methods specialized by ScriptArray. */
  87. template<class T>
  88. T ScriptArray_get(MonoArray* array, UINT32 idx)
  89. {
  90. return *(T*)ScriptArray::getArrayAddr(array, sizeof(T), idx);
  91. }
  92. template<class T>
  93. void ScriptArray_set(MonoArray* array, UINT32 idx, const T& value)
  94. {
  95. T* item = (T*)ScriptArray::getArrayAddr(array, sizeof(T), idx);
  96. *item = value;
  97. }
  98. template<>
  99. BS_MONO_EXPORT String ScriptArray_get(MonoArray* array, UINT32 idx);
  100. template<>
  101. BS_MONO_EXPORT WString ScriptArray_get(MonoArray* array, UINT32 idx);
  102. template<>
  103. BS_MONO_EXPORT void ScriptArray_set<String>(MonoArray* array, UINT32 idx, const String& value);
  104. template<>
  105. BS_MONO_EXPORT void ScriptArray_set<WString>(MonoArray* array, UINT32 idx, const WString& value);
  106. template<class T>
  107. inline ScriptArray ScriptArray_create(UINT32 size)
  108. {
  109. return ScriptArray(*T::getMetaData()->scriptClass, size);
  110. }
  111. template<>
  112. inline ScriptArray ScriptArray_create<UINT8>(UINT32 size)
  113. {
  114. return ScriptArray(MonoUtil::getByteClass(), size);
  115. }
  116. template<>
  117. inline ScriptArray ScriptArray_create<INT8>(UINT32 size)
  118. {
  119. return ScriptArray(MonoUtil::getSByteClass(), size);
  120. }
  121. template<>
  122. inline ScriptArray ScriptArray_create<UINT16>(UINT32 size)
  123. {
  124. return ScriptArray(MonoUtil::getUINT16Class(), size);
  125. }
  126. template<>
  127. inline ScriptArray ScriptArray_create<INT16>(UINT32 size)
  128. {
  129. return ScriptArray(MonoUtil::getINT16Class(), size);
  130. }
  131. template<>
  132. inline ScriptArray ScriptArray_create<UINT32>(UINT32 size)
  133. {
  134. return ScriptArray(MonoUtil::getUINT32Class(), size);
  135. }
  136. template<>
  137. inline ScriptArray ScriptArray_create<INT32>(UINT32 size)
  138. {
  139. return ScriptArray(MonoUtil::getINT32Class(), size);
  140. }
  141. template<>
  142. inline ScriptArray ScriptArray_create<UINT64>(UINT32 size)
  143. {
  144. return ScriptArray(MonoUtil::getUINT64Class(), size);
  145. }
  146. template<>
  147. inline ScriptArray ScriptArray_create<INT64>(UINT32 size)
  148. {
  149. return ScriptArray(MonoUtil::getINT64Class(), size);
  150. }
  151. template<>
  152. inline ScriptArray ScriptArray_create<WString>(UINT32 size)
  153. {
  154. return ScriptArray(MonoUtil::getStringClass(), size);
  155. }
  156. template<>
  157. inline ScriptArray ScriptArray_create<String>(UINT32 size)
  158. {
  159. return ScriptArray(MonoUtil::getStringClass(), size);
  160. }
  161. template<>
  162. inline ScriptArray ScriptArray_create<float>(UINT32 size)
  163. {
  164. return ScriptArray(MonoUtil::getFloatClass(), size);
  165. }
  166. template<>
  167. inline ScriptArray ScriptArray_create<double>(UINT32 size)
  168. {
  169. return ScriptArray(MonoUtil::getDoubleClass(), size);
  170. }
  171. template<>
  172. inline ScriptArray ScriptArray_create<bool>(UINT32 size)
  173. {
  174. return ScriptArray(MonoUtil::getBoolClass(), size);
  175. }
  176. template<>
  177. inline ScriptArray ScriptArray_create<MonoObject*>(UINT32 size)
  178. {
  179. return ScriptArray(MonoUtil::getObjectClass(), size);
  180. }
  181. }
  182. /** @} */
  183. template<class T>
  184. T ScriptArray::get(UINT32 idx)
  185. {
  186. return Detail::ScriptArray_get<T>(mInternal, idx);
  187. }
  188. /** Sets an entry from the array at the specified index. */
  189. template<class T>
  190. void ScriptArray::set(UINT32 idx, const T& value)
  191. {
  192. Detail::ScriptArray_set<T>(mInternal, idx, value);
  193. }
  194. template<class T>
  195. ScriptArray ScriptArray::create(UINT32 size)
  196. {
  197. return Detail::ScriptArray_create<T>(size);
  198. }
  199. }