BsMonoArray.h 4.4 KB

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