BsMonoArray.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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<>
  127. BS_MONO_EXPORT void ScriptArray_set<nullptr_t>(MonoArray* array, UINT32 idx, const nullptr_t& value);
  128. template<class T>
  129. inline ScriptArray ScriptArray_create(UINT32 size)
  130. {
  131. return ScriptArray(*T::getMetaData()->scriptClass, size);
  132. }
  133. template<>
  134. inline ScriptArray ScriptArray_create<UINT8>(UINT32 size)
  135. {
  136. return ScriptArray(MonoUtil::getByteClass(), size);
  137. }
  138. template<>
  139. inline ScriptArray ScriptArray_create<INT8>(UINT32 size)
  140. {
  141. return ScriptArray(MonoUtil::getSByteClass(), size);
  142. }
  143. template<>
  144. inline ScriptArray ScriptArray_create<UINT16>(UINT32 size)
  145. {
  146. return ScriptArray(MonoUtil::getUINT16Class(), size);
  147. }
  148. template<>
  149. inline ScriptArray ScriptArray_create<INT16>(UINT32 size)
  150. {
  151. return ScriptArray(MonoUtil::getINT16Class(), size);
  152. }
  153. template<>
  154. inline ScriptArray ScriptArray_create<UINT32>(UINT32 size)
  155. {
  156. return ScriptArray(MonoUtil::getUINT32Class(), size);
  157. }
  158. template<>
  159. inline ScriptArray ScriptArray_create<INT32>(UINT32 size)
  160. {
  161. return ScriptArray(MonoUtil::getINT32Class(), size);
  162. }
  163. template<>
  164. inline ScriptArray ScriptArray_create<UINT64>(UINT32 size)
  165. {
  166. return ScriptArray(MonoUtil::getUINT64Class(), size);
  167. }
  168. template<>
  169. inline ScriptArray ScriptArray_create<INT64>(UINT32 size)
  170. {
  171. return ScriptArray(MonoUtil::getINT64Class(), size);
  172. }
  173. template<>
  174. inline ScriptArray ScriptArray_create<WString>(UINT32 size)
  175. {
  176. return ScriptArray(MonoUtil::getStringClass(), size);
  177. }
  178. template<>
  179. inline ScriptArray ScriptArray_create<String>(UINT32 size)
  180. {
  181. return ScriptArray(MonoUtil::getStringClass(), size);
  182. }
  183. template<>
  184. inline ScriptArray ScriptArray_create<float>(UINT32 size)
  185. {
  186. return ScriptArray(MonoUtil::getFloatClass(), size);
  187. }
  188. template<>
  189. inline ScriptArray ScriptArray_create<double>(UINT32 size)
  190. {
  191. return ScriptArray(MonoUtil::getDoubleClass(), size);
  192. }
  193. template<>
  194. inline ScriptArray ScriptArray_create<bool>(UINT32 size)
  195. {
  196. return ScriptArray(MonoUtil::getBoolClass(), size);
  197. }
  198. template<>
  199. inline ScriptArray ScriptArray_create<MonoObject*>(UINT32 size)
  200. {
  201. return ScriptArray(MonoUtil::getObjectClass(), size);
  202. }
  203. }
  204. /** @} */
  205. template<class T>
  206. T ScriptArray::get(UINT32 idx)
  207. {
  208. return Detail::ScriptArray_get<T>(mInternal, idx);
  209. }
  210. /** Sets an entry from the array at the specified index. */
  211. template<class T>
  212. void ScriptArray::set(UINT32 idx, const T& value)
  213. {
  214. Detail::ScriptArray_set<T>(mInternal, idx, value);
  215. }
  216. template<class T>
  217. ScriptArray ScriptArray::create(UINT32 size)
  218. {
  219. return Detail::ScriptArray_create<T>(size);
  220. }
  221. }