BsMonoArray.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. /** Assigns a value to the specified index. */
  27. template<class T>
  28. void set(UINT32 idx, const T& value);
  29. /**
  30. * Assigns some data represented as raw memory to the array at the specified index. User must provide the size of
  31. * the data, and it must match the element size expected by the array. Multiple array elements can be provided
  32. * sequentially by setting the @p count parameter.
  33. */
  34. void setRaw(UINT32 idx, const UINT8* value, UINT32 size, UINT32 count = 1);
  35. /**
  36. * Returns the raw memory of the data at the specified array index. Returned value should not be used for writing
  37. * to the array and set() or setRaw() methods should be used instead.
  38. */
  39. UINT8* getRaw(UINT32 idx, UINT32 size)
  40. {
  41. #if BS_DEBUG_MODE
  42. assert(size == elementSize());
  43. #endif
  44. return _getArrayAddr(mInternal, size, idx);
  45. }
  46. /**
  47. * Returns the raw memory of the data at the specified array index. Returned value should not be used for writing
  48. * to the array and set() or setRaw() methods should be used instead.
  49. */
  50. template<class T>
  51. T* getRaw(UINT32 idx = 0)
  52. {
  53. #if BS_DEBUG_MODE
  54. assert(sizeof(T) == elementSize());
  55. #endif
  56. return (T*)_getArrayAddr(mInternal, sizeof(T), idx);
  57. }
  58. /**
  59. * Creates a new array of managed objects.
  60. *
  61. * @tparam T ScriptObject wrapper for the specified managed type.
  62. */
  63. template<class T>
  64. static ScriptArray create(UINT32 size);
  65. /** Returns number of elements in the array. */
  66. UINT32 size() const;
  67. /** Returns the size of an individual element in the array, in bytes. */
  68. UINT32 elementSize() const;
  69. /** Returns the managed object representing this array. */
  70. MonoArray* getInternal() const { return mInternal; }
  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. /**
  78. * @name Internal
  79. * @{
  80. */
  81. /**
  82. * Returns the address of an array item at the specified index.
  83. *
  84. * @param[in] array Array from which to retrieve the item.
  85. * @param[in] size Size of a single item in the array.
  86. * @param[in] idx Index of the item to retrieve.
  87. * @return Address of the array item at the requested index.
  88. */
  89. static UINT8* _getArrayAddr(MonoArray* array, UINT32 size, UINT32 idx);
  90. /**
  91. * Sets one or multiple entries from the array at the specified index, from raw memory. User must provide the size
  92. * of the element, and it must match the element size expected by the array.
  93. */
  94. static void _setArrayVal(MonoArray* array, UINT32 idx, const UINT8* value, UINT32 size, UINT32 count = 1);
  95. /**
  96. * @}
  97. */
  98. private:
  99. MonoArray* mInternal;
  100. };
  101. /** @} */
  102. /** @addtogroup Implementation
  103. * @{
  104. */
  105. namespace Detail
  106. {
  107. // A layer of indirection for all methods specialized by ScriptArray. */
  108. template<class T>
  109. T ScriptArray_get(MonoArray* array, UINT32 idx)
  110. {
  111. return *(T*)ScriptArray::_getArrayAddr(array, sizeof(T), idx);
  112. }
  113. template<class T>
  114. void ScriptArray_set(MonoArray* array, UINT32 idx, const T& value)
  115. {
  116. ScriptArray::_setArrayVal(array, idx, (UINT8*)&value, sizeof(T));
  117. }
  118. template<>
  119. BS_MONO_EXPORT String ScriptArray_get(MonoArray* array, UINT32 idx);
  120. template<>
  121. BS_MONO_EXPORT WString ScriptArray_get(MonoArray* array, UINT32 idx);
  122. template<>
  123. BS_MONO_EXPORT void ScriptArray_set<String>(MonoArray* array, UINT32 idx, const String& value);
  124. template<>
  125. BS_MONO_EXPORT void ScriptArray_set<WString>(MonoArray* array, UINT32 idx, const WString& value);
  126. template<class T>
  127. inline ScriptArray ScriptArray_create(UINT32 size)
  128. {
  129. return ScriptArray(*T::getMetaData()->scriptClass, size);
  130. }
  131. template<>
  132. inline ScriptArray ScriptArray_create<UINT8>(UINT32 size)
  133. {
  134. return ScriptArray(MonoUtil::getByteClass(), size);
  135. }
  136. template<>
  137. inline ScriptArray ScriptArray_create<INT8>(UINT32 size)
  138. {
  139. return ScriptArray(MonoUtil::getSByteClass(), size);
  140. }
  141. template<>
  142. inline ScriptArray ScriptArray_create<UINT16>(UINT32 size)
  143. {
  144. return ScriptArray(MonoUtil::getUINT16Class(), size);
  145. }
  146. template<>
  147. inline ScriptArray ScriptArray_create<INT16>(UINT32 size)
  148. {
  149. return ScriptArray(MonoUtil::getINT16Class(), size);
  150. }
  151. template<>
  152. inline ScriptArray ScriptArray_create<UINT32>(UINT32 size)
  153. {
  154. return ScriptArray(MonoUtil::getUINT32Class(), size);
  155. }
  156. template<>
  157. inline ScriptArray ScriptArray_create<INT32>(UINT32 size)
  158. {
  159. return ScriptArray(MonoUtil::getINT32Class(), size);
  160. }
  161. template<>
  162. inline ScriptArray ScriptArray_create<UINT64>(UINT32 size)
  163. {
  164. return ScriptArray(MonoUtil::getUINT64Class(), size);
  165. }
  166. template<>
  167. inline ScriptArray ScriptArray_create<INT64>(UINT32 size)
  168. {
  169. return ScriptArray(MonoUtil::getINT64Class(), size);
  170. }
  171. template<>
  172. inline ScriptArray ScriptArray_create<WString>(UINT32 size)
  173. {
  174. return ScriptArray(MonoUtil::getStringClass(), size);
  175. }
  176. template<>
  177. inline ScriptArray ScriptArray_create<String>(UINT32 size)
  178. {
  179. return ScriptArray(MonoUtil::getStringClass(), size);
  180. }
  181. template<>
  182. inline ScriptArray ScriptArray_create<float>(UINT32 size)
  183. {
  184. return ScriptArray(MonoUtil::getFloatClass(), size);
  185. }
  186. template<>
  187. inline ScriptArray ScriptArray_create<double>(UINT32 size)
  188. {
  189. return ScriptArray(MonoUtil::getDoubleClass(), size);
  190. }
  191. template<>
  192. inline ScriptArray ScriptArray_create<bool>(UINT32 size)
  193. {
  194. return ScriptArray(MonoUtil::getBoolClass(), size);
  195. }
  196. template<>
  197. inline ScriptArray ScriptArray_create<MonoObject*>(UINT32 size)
  198. {
  199. return ScriptArray(MonoUtil::getObjectClass(), size);
  200. }
  201. }
  202. /** @} */
  203. template<class T>
  204. T ScriptArray::get(UINT32 idx)
  205. {
  206. return Detail::ScriptArray_get<T>(mInternal, idx);
  207. }
  208. /** Sets an entry from the array at the specified index. */
  209. template<class T>
  210. void ScriptArray::set(UINT32 idx, const T& value)
  211. {
  212. Detail::ScriptArray_set<T>(mInternal, idx, value);
  213. }
  214. template<class T>
  215. ScriptArray ScriptArray::create(UINT32 size)
  216. {
  217. return Detail::ScriptArray_create<T>(size);
  218. }
  219. }