gd_mono_marshal.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. /*************************************************************************/
  2. /* gd_mono_marshal.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef GDMONOMARSHAL_H
  31. #define GDMONOMARSHAL_H
  32. #include "core/variant.h"
  33. #include "../managed_callable.h"
  34. #include "gd_mono.h"
  35. #include "gd_mono_utils.h"
  36. namespace GDMonoMarshal {
  37. template <typename T>
  38. T unbox(MonoObject *p_obj) {
  39. return *(T *)mono_object_unbox(p_obj);
  40. }
  41. template <typename T>
  42. T *unbox_addr(MonoObject *p_obj) {
  43. return (T *)mono_object_unbox(p_obj);
  44. }
  45. #define BOX_DOUBLE(x) mono_value_box(mono_domain_get(), CACHED_CLASS_RAW(double), &x)
  46. #define BOX_FLOAT(x) mono_value_box(mono_domain_get(), CACHED_CLASS_RAW(float), &x)
  47. #define BOX_INT64(x) mono_value_box(mono_domain_get(), CACHED_CLASS_RAW(int64_t), &x)
  48. #define BOX_INT32(x) mono_value_box(mono_domain_get(), CACHED_CLASS_RAW(int32_t), &x)
  49. #define BOX_INT16(x) mono_value_box(mono_domain_get(), CACHED_CLASS_RAW(int16_t), &x)
  50. #define BOX_INT8(x) mono_value_box(mono_domain_get(), CACHED_CLASS_RAW(int8_t), &x)
  51. #define BOX_UINT64(x) mono_value_box(mono_domain_get(), CACHED_CLASS_RAW(uint64_t), &x)
  52. #define BOX_UINT32(x) mono_value_box(mono_domain_get(), CACHED_CLASS_RAW(uint32_t), &x)
  53. #define BOX_UINT16(x) mono_value_box(mono_domain_get(), CACHED_CLASS_RAW(uint16_t), &x)
  54. #define BOX_UINT8(x) mono_value_box(mono_domain_get(), CACHED_CLASS_RAW(uint8_t), &x)
  55. #define BOX_BOOLEAN(x) mono_value_box(mono_domain_get(), CACHED_CLASS_RAW(bool), &x)
  56. #define BOX_PTR(x) mono_value_box(mono_domain_get(), CACHED_CLASS_RAW(IntPtr), x)
  57. #define BOX_ENUM(m_enum_class, x) mono_value_box(mono_domain_get(), m_enum_class, &x)
  58. Variant::Type managed_to_variant_type(const ManagedType &p_type, bool *r_nil_is_variant = nullptr);
  59. bool try_get_array_element_type(const ManagedType &p_array_type, ManagedType &r_elem_type);
  60. bool try_get_dictionary_key_value_types(const ManagedType &p_dictionary_type, ManagedType &r_key_type, ManagedType &r_value_type);
  61. // String
  62. String mono_to_utf8_string(MonoString *p_mono_string);
  63. String mono_to_utf16_string(MonoString *p_mono_string);
  64. _FORCE_INLINE_ String mono_string_to_godot_not_null(MonoString *p_mono_string) {
  65. if (sizeof(CharType) == 2)
  66. return mono_to_utf16_string(p_mono_string);
  67. return mono_to_utf8_string(p_mono_string);
  68. }
  69. _FORCE_INLINE_ String mono_string_to_godot(MonoString *p_mono_string) {
  70. if (p_mono_string == nullptr)
  71. return String();
  72. return mono_string_to_godot_not_null(p_mono_string);
  73. }
  74. _FORCE_INLINE_ MonoString *mono_from_utf8_string(const String &p_string) {
  75. return mono_string_new(mono_domain_get(), p_string.utf8().get_data());
  76. }
  77. _FORCE_INLINE_ MonoString *mono_from_utf16_string(const String &p_string) {
  78. return mono_string_from_utf16((mono_unichar2 *)p_string.c_str());
  79. }
  80. _FORCE_INLINE_ MonoString *mono_string_from_godot(const String &p_string) {
  81. if (sizeof(CharType) == 2)
  82. return mono_from_utf16_string(p_string);
  83. return mono_from_utf8_string(p_string);
  84. }
  85. // Variant
  86. MonoObject *variant_to_mono_object(const Variant *p_var, const ManagedType &p_type);
  87. MonoObject *variant_to_mono_object(const Variant *p_var);
  88. _FORCE_INLINE_ MonoObject *variant_to_mono_object(const Variant &p_var) {
  89. return variant_to_mono_object(&p_var);
  90. }
  91. _FORCE_INLINE_ MonoObject *variant_to_mono_object(const Variant &p_var, const ManagedType &p_type) {
  92. return variant_to_mono_object(&p_var, p_type);
  93. }
  94. Variant mono_object_to_variant(MonoObject *p_obj);
  95. Variant mono_object_to_variant(MonoObject *p_obj, const ManagedType &p_type);
  96. Variant mono_object_to_variant_no_err(MonoObject *p_obj, const ManagedType &p_type);
  97. /// Tries to convert the MonoObject* to Variant and then convert the Variant to String.
  98. /// If the MonoObject* cannot be converted to Variant, then 'ToString()' is called instead.
  99. String mono_object_to_variant_string(MonoObject *p_obj, MonoException **r_exc);
  100. // System.Collections.Generic
  101. MonoObject *Dictionary_to_system_generic_dict(const Dictionary &p_dict, GDMonoClass *p_class, MonoReflectionType *p_key_reftype, MonoReflectionType *p_value_reftype);
  102. Dictionary system_generic_dict_to_Dictionary(MonoObject *p_obj, GDMonoClass *p_class, MonoReflectionType *p_key_reftype, MonoReflectionType *p_value_reftype);
  103. MonoObject *Array_to_system_generic_list(const Array &p_array, GDMonoClass *p_class, MonoReflectionType *p_elem_reftype);
  104. Array system_generic_list_to_Array(MonoObject *p_obj, GDMonoClass *p_class, MonoReflectionType *p_elem_reftype);
  105. // Array
  106. MonoArray *Array_to_mono_array(const Array &p_array);
  107. MonoArray *Array_to_mono_array(const Array &p_array, GDMonoClass *p_array_type_class);
  108. Array mono_array_to_Array(MonoArray *p_array);
  109. // PackedInt32Array
  110. MonoArray *PackedInt32Array_to_mono_array(const PackedInt32Array &p_array);
  111. PackedInt32Array mono_array_to_PackedInt32Array(MonoArray *p_array);
  112. // PackedInt64Array
  113. MonoArray *PackedInt64Array_to_mono_array(const PackedInt64Array &p_array);
  114. PackedInt64Array mono_array_to_PackedInt64Array(MonoArray *p_array);
  115. // PackedByteArray
  116. MonoArray *PackedByteArray_to_mono_array(const PackedByteArray &p_array);
  117. PackedByteArray mono_array_to_PackedByteArray(MonoArray *p_array);
  118. // PackedFloat32Array
  119. MonoArray *PackedFloat32Array_to_mono_array(const PackedFloat32Array &p_array);
  120. PackedFloat32Array mono_array_to_PackedFloat32Array(MonoArray *p_array);
  121. // PackedFloat64Array
  122. MonoArray *PackedFloat64Array_to_mono_array(const PackedFloat64Array &p_array);
  123. PackedFloat64Array mono_array_to_PackedFloat64Array(MonoArray *p_array);
  124. // PackedStringArray
  125. MonoArray *PackedStringArray_to_mono_array(const PackedStringArray &p_array);
  126. PackedStringArray mono_array_to_PackedStringArray(MonoArray *p_array);
  127. // PackedColorArray
  128. MonoArray *PackedColorArray_to_mono_array(const PackedColorArray &p_array);
  129. PackedColorArray mono_array_to_PackedColorArray(MonoArray *p_array);
  130. // PackedVector2Array
  131. MonoArray *PackedVector2Array_to_mono_array(const PackedVector2Array &p_array);
  132. PackedVector2Array mono_array_to_PackedVector2Array(MonoArray *p_array);
  133. // PackedVector3Array
  134. MonoArray *PackedVector3Array_to_mono_array(const PackedVector3Array &p_array);
  135. PackedVector3Array mono_array_to_PackedVector3Array(MonoArray *p_array);
  136. #pragma pack(push, 1)
  137. struct M_Callable {
  138. MonoObject *target;
  139. MonoObject *method_string_name;
  140. MonoDelegate *delegate;
  141. };
  142. struct M_SignalInfo {
  143. MonoObject *owner;
  144. MonoObject *name_string_name;
  145. };
  146. #pragma pack(pop)
  147. // Callable
  148. Callable managed_to_callable(const M_Callable &p_managed_callable);
  149. M_Callable callable_to_managed(const Callable &p_callable);
  150. // SignalInfo
  151. Signal managed_to_signal_info(const M_SignalInfo &p_managed_signal);
  152. M_SignalInfo signal_info_to_managed(const Signal &p_signal);
  153. // Structures
  154. namespace InteropLayout {
  155. enum {
  156. MATCHES_int = (sizeof(int32_t) == sizeof(uint32_t)),
  157. MATCHES_float = (sizeof(float) == sizeof(uint32_t)),
  158. MATCHES_double = (sizeof(double) == sizeof(uint64_t)),
  159. #ifdef REAL_T_IS_DOUBLE
  160. MATCHES_real_t = (sizeof(real_t) == sizeof(uint64_t)),
  161. #else
  162. MATCHES_real_t = (sizeof(real_t) == sizeof(uint32_t)),
  163. #endif
  164. MATCHES_Vector2 = (MATCHES_real_t && (sizeof(Vector2) == (sizeof(real_t) * 2)) &&
  165. offsetof(Vector2, x) == (sizeof(real_t) * 0) &&
  166. offsetof(Vector2, y) == (sizeof(real_t) * 1)),
  167. MATCHES_Vector2i = (MATCHES_int && (sizeof(Vector2i) == (sizeof(int32_t) * 2)) &&
  168. offsetof(Vector2i, x) == (sizeof(int32_t) * 0) &&
  169. offsetof(Vector2i, y) == (sizeof(int32_t) * 1)),
  170. MATCHES_Rect2 = (MATCHES_Vector2 && (sizeof(Rect2) == (sizeof(Vector2) * 2)) &&
  171. offsetof(Rect2, position) == (sizeof(Vector2) * 0) &&
  172. offsetof(Rect2, size) == (sizeof(Vector2) * 1)),
  173. MATCHES_Rect2i = (MATCHES_Vector2i && (sizeof(Rect2i) == (sizeof(Vector2i) * 2)) &&
  174. offsetof(Rect2i, position) == (sizeof(Vector2i) * 0) &&
  175. offsetof(Rect2i, size) == (sizeof(Vector2i) * 1)),
  176. MATCHES_Transform2D = (MATCHES_Vector2 && (sizeof(Transform2D) == (sizeof(Vector2) * 3))), // No field offset required, it stores an array
  177. MATCHES_Vector3 = (MATCHES_real_t && (sizeof(Vector3) == (sizeof(real_t) * 3)) &&
  178. offsetof(Vector3, x) == (sizeof(real_t) * 0) &&
  179. offsetof(Vector3, y) == (sizeof(real_t) * 1) &&
  180. offsetof(Vector3, z) == (sizeof(real_t) * 2)),
  181. MATCHES_Vector3i = (MATCHES_int && (sizeof(Vector3i) == (sizeof(int32_t) * 3)) &&
  182. offsetof(Vector3i, x) == (sizeof(int32_t) * 0) &&
  183. offsetof(Vector3i, y) == (sizeof(int32_t) * 1) &&
  184. offsetof(Vector3i, z) == (sizeof(int32_t) * 2)),
  185. MATCHES_Basis = (MATCHES_Vector3 && (sizeof(Basis) == (sizeof(Vector3) * 3))), // No field offset required, it stores an array
  186. MATCHES_Quat = (MATCHES_real_t && (sizeof(Quat) == (sizeof(real_t) * 4)) &&
  187. offsetof(Quat, x) == (sizeof(real_t) * 0) &&
  188. offsetof(Quat, y) == (sizeof(real_t) * 1) &&
  189. offsetof(Quat, z) == (sizeof(real_t) * 2) &&
  190. offsetof(Quat, w) == (sizeof(real_t) * 3)),
  191. MATCHES_Transform = (MATCHES_Basis && MATCHES_Vector3 && (sizeof(Transform) == (sizeof(Basis) + sizeof(Vector3))) &&
  192. offsetof(Transform, basis) == 0 &&
  193. offsetof(Transform, origin) == sizeof(Basis)),
  194. MATCHES_AABB = (MATCHES_Vector3 && (sizeof(AABB) == (sizeof(Vector3) * 2)) &&
  195. offsetof(AABB, position) == (sizeof(Vector3) * 0) &&
  196. offsetof(AABB, size) == (sizeof(Vector3) * 1)),
  197. MATCHES_Color = (MATCHES_float && (sizeof(Color) == (sizeof(float) * 4)) &&
  198. offsetof(Color, r) == (sizeof(float) * 0) &&
  199. offsetof(Color, g) == (sizeof(float) * 1) &&
  200. offsetof(Color, b) == (sizeof(float) * 2) &&
  201. offsetof(Color, a) == (sizeof(float) * 3)),
  202. MATCHES_Plane = (MATCHES_Vector3 && MATCHES_real_t && (sizeof(Plane) == (sizeof(Vector3) + sizeof(real_t))) &&
  203. offsetof(Plane, normal) == 0 &&
  204. offsetof(Plane, distance) == sizeof(Vector3))
  205. };
  206. // In the future we may force this if we want to ref return these structs
  207. #ifdef GD_MONO_FORCE_INTEROP_STRUCT_COPY
  208. /* clang-format off */
  209. static_assert(MATCHES_Vector2 && MATCHES_Rect2 && MATCHES_Transform2D && MATCHES_Vector3 &&
  210. MATCHES_Basis && MATCHES_Quat && MATCHES_Transform && MATCHES_AABB && MATCHES_Color &&
  211. MATCHES_Plane && MATCHES_Vector2i && MATCHES_Rect2i && MATCHES_Vector3i);
  212. /* clang-format on */
  213. #endif
  214. } // namespace InteropLayout
  215. #pragma pack(push, 1)
  216. struct M_Vector2 {
  217. real_t x, y;
  218. static _FORCE_INLINE_ Vector2 convert_to(const M_Vector2 &p_from) {
  219. return Vector2(p_from.x, p_from.y);
  220. }
  221. static _FORCE_INLINE_ M_Vector2 convert_from(const Vector2 &p_from) {
  222. M_Vector2 ret = { p_from.x, p_from.y };
  223. return ret;
  224. }
  225. };
  226. struct M_Vector2i {
  227. int32_t x, y;
  228. static _FORCE_INLINE_ Vector2i convert_to(const M_Vector2i &p_from) {
  229. return Vector2i(p_from.x, p_from.y);
  230. }
  231. static _FORCE_INLINE_ M_Vector2i convert_from(const Vector2i &p_from) {
  232. M_Vector2i ret = { p_from.x, p_from.y };
  233. return ret;
  234. }
  235. };
  236. struct M_Rect2 {
  237. M_Vector2 position;
  238. M_Vector2 size;
  239. static _FORCE_INLINE_ Rect2 convert_to(const M_Rect2 &p_from) {
  240. return Rect2(M_Vector2::convert_to(p_from.position),
  241. M_Vector2::convert_to(p_from.size));
  242. }
  243. static _FORCE_INLINE_ M_Rect2 convert_from(const Rect2 &p_from) {
  244. M_Rect2 ret = { M_Vector2::convert_from(p_from.position), M_Vector2::convert_from(p_from.size) };
  245. return ret;
  246. }
  247. };
  248. struct M_Rect2i {
  249. M_Vector2i position;
  250. M_Vector2i size;
  251. static _FORCE_INLINE_ Rect2i convert_to(const M_Rect2i &p_from) {
  252. return Rect2i(M_Vector2i::convert_to(p_from.position),
  253. M_Vector2i::convert_to(p_from.size));
  254. }
  255. static _FORCE_INLINE_ M_Rect2i convert_from(const Rect2i &p_from) {
  256. M_Rect2i ret = { M_Vector2i::convert_from(p_from.position), M_Vector2i::convert_from(p_from.size) };
  257. return ret;
  258. }
  259. };
  260. struct M_Transform2D {
  261. M_Vector2 elements[3];
  262. static _FORCE_INLINE_ Transform2D convert_to(const M_Transform2D &p_from) {
  263. return Transform2D(p_from.elements[0].x, p_from.elements[0].y,
  264. p_from.elements[1].x, p_from.elements[1].y,
  265. p_from.elements[2].x, p_from.elements[2].y);
  266. }
  267. static _FORCE_INLINE_ M_Transform2D convert_from(const Transform2D &p_from) {
  268. M_Transform2D ret = {
  269. M_Vector2::convert_from(p_from.elements[0]),
  270. M_Vector2::convert_from(p_from.elements[1]),
  271. M_Vector2::convert_from(p_from.elements[2])
  272. };
  273. return ret;
  274. }
  275. };
  276. struct M_Vector3 {
  277. real_t x, y, z;
  278. static _FORCE_INLINE_ Vector3 convert_to(const M_Vector3 &p_from) {
  279. return Vector3(p_from.x, p_from.y, p_from.z);
  280. }
  281. static _FORCE_INLINE_ M_Vector3 convert_from(const Vector3 &p_from) {
  282. M_Vector3 ret = { p_from.x, p_from.y, p_from.z };
  283. return ret;
  284. }
  285. };
  286. struct M_Vector3i {
  287. int32_t x, y, z;
  288. static _FORCE_INLINE_ Vector3i convert_to(const M_Vector3i &p_from) {
  289. return Vector3i(p_from.x, p_from.y, p_from.z);
  290. }
  291. static _FORCE_INLINE_ M_Vector3i convert_from(const Vector3i &p_from) {
  292. M_Vector3i ret = { p_from.x, p_from.y, p_from.z };
  293. return ret;
  294. }
  295. };
  296. struct M_Basis {
  297. M_Vector3 elements[3];
  298. static _FORCE_INLINE_ Basis convert_to(const M_Basis &p_from) {
  299. return Basis(M_Vector3::convert_to(p_from.elements[0]),
  300. M_Vector3::convert_to(p_from.elements[1]),
  301. M_Vector3::convert_to(p_from.elements[2]));
  302. }
  303. static _FORCE_INLINE_ M_Basis convert_from(const Basis &p_from) {
  304. M_Basis ret = {
  305. M_Vector3::convert_from(p_from.elements[0]),
  306. M_Vector3::convert_from(p_from.elements[1]),
  307. M_Vector3::convert_from(p_from.elements[2])
  308. };
  309. return ret;
  310. }
  311. };
  312. struct M_Quat {
  313. real_t x, y, z, w;
  314. static _FORCE_INLINE_ Quat convert_to(const M_Quat &p_from) {
  315. return Quat(p_from.x, p_from.y, p_from.z, p_from.w);
  316. }
  317. static _FORCE_INLINE_ M_Quat convert_from(const Quat &p_from) {
  318. M_Quat ret = { p_from.x, p_from.y, p_from.z, p_from.w };
  319. return ret;
  320. }
  321. };
  322. struct M_Transform {
  323. M_Basis basis;
  324. M_Vector3 origin;
  325. static _FORCE_INLINE_ Transform convert_to(const M_Transform &p_from) {
  326. return Transform(M_Basis::convert_to(p_from.basis), M_Vector3::convert_to(p_from.origin));
  327. }
  328. static _FORCE_INLINE_ M_Transform convert_from(const Transform &p_from) {
  329. M_Transform ret = { M_Basis::convert_from(p_from.basis), M_Vector3::convert_from(p_from.origin) };
  330. return ret;
  331. }
  332. };
  333. struct M_AABB {
  334. M_Vector3 position;
  335. M_Vector3 size;
  336. static _FORCE_INLINE_ AABB convert_to(const M_AABB &p_from) {
  337. return AABB(M_Vector3::convert_to(p_from.position), M_Vector3::convert_to(p_from.size));
  338. }
  339. static _FORCE_INLINE_ M_AABB convert_from(const AABB &p_from) {
  340. M_AABB ret = { M_Vector3::convert_from(p_from.position), M_Vector3::convert_from(p_from.size) };
  341. return ret;
  342. }
  343. };
  344. struct M_Color {
  345. float r, g, b, a;
  346. static _FORCE_INLINE_ Color convert_to(const M_Color &p_from) {
  347. return Color(p_from.r, p_from.g, p_from.b, p_from.a);
  348. }
  349. static _FORCE_INLINE_ M_Color convert_from(const Color &p_from) {
  350. M_Color ret = { p_from.r, p_from.g, p_from.b, p_from.a };
  351. return ret;
  352. }
  353. };
  354. struct M_Plane {
  355. M_Vector3 normal;
  356. real_t distance;
  357. static _FORCE_INLINE_ Plane convert_to(const M_Plane &p_from) {
  358. return Plane(M_Vector3::convert_to(p_from.normal), p_from.distance);
  359. }
  360. static _FORCE_INLINE_ M_Plane convert_from(const Plane &p_from) {
  361. M_Plane ret = { M_Vector3::convert_from(p_from.normal), p_from.distance };
  362. return ret;
  363. }
  364. };
  365. #pragma pack(pop)
  366. #define DECL_TYPE_MARSHAL_TEMPLATES(m_type) \
  367. template <int> \
  368. _FORCE_INLINE_ m_type marshalled_in_##m_type##_impl(const M_##m_type *p_from); \
  369. \
  370. template <> \
  371. _FORCE_INLINE_ m_type marshalled_in_##m_type##_impl<0>(const M_##m_type *p_from) { \
  372. return M_##m_type::convert_to(*p_from); \
  373. } \
  374. \
  375. template <> \
  376. _FORCE_INLINE_ m_type marshalled_in_##m_type##_impl<1>(const M_##m_type *p_from) { \
  377. return *reinterpret_cast<const m_type *>(p_from); \
  378. } \
  379. \
  380. _FORCE_INLINE_ m_type marshalled_in_##m_type(const M_##m_type *p_from) { \
  381. return marshalled_in_##m_type##_impl<InteropLayout::MATCHES_##m_type>(p_from); \
  382. } \
  383. \
  384. template <int> \
  385. _FORCE_INLINE_ M_##m_type marshalled_out_##m_type##_impl(const m_type &p_from); \
  386. \
  387. template <> \
  388. _FORCE_INLINE_ M_##m_type marshalled_out_##m_type##_impl<0>(const m_type &p_from) { \
  389. return M_##m_type::convert_from(p_from); \
  390. } \
  391. \
  392. template <> \
  393. _FORCE_INLINE_ M_##m_type marshalled_out_##m_type##_impl<1>(const m_type &p_from) { \
  394. return *reinterpret_cast<const M_##m_type *>(&p_from); \
  395. } \
  396. \
  397. _FORCE_INLINE_ M_##m_type marshalled_out_##m_type(const m_type &p_from) { \
  398. return marshalled_out_##m_type##_impl<InteropLayout::MATCHES_##m_type>(p_from); \
  399. }
  400. DECL_TYPE_MARSHAL_TEMPLATES(Vector2)
  401. DECL_TYPE_MARSHAL_TEMPLATES(Vector2i)
  402. DECL_TYPE_MARSHAL_TEMPLATES(Rect2)
  403. DECL_TYPE_MARSHAL_TEMPLATES(Rect2i)
  404. DECL_TYPE_MARSHAL_TEMPLATES(Transform2D)
  405. DECL_TYPE_MARSHAL_TEMPLATES(Vector3)
  406. DECL_TYPE_MARSHAL_TEMPLATES(Vector3i)
  407. DECL_TYPE_MARSHAL_TEMPLATES(Basis)
  408. DECL_TYPE_MARSHAL_TEMPLATES(Quat)
  409. DECL_TYPE_MARSHAL_TEMPLATES(Transform)
  410. DECL_TYPE_MARSHAL_TEMPLATES(AABB)
  411. DECL_TYPE_MARSHAL_TEMPLATES(Color)
  412. DECL_TYPE_MARSHAL_TEMPLATES(Plane)
  413. #define MARSHALLED_IN(m_type, m_from_ptr) (GDMonoMarshal::marshalled_in_##m_type(m_from_ptr))
  414. #define MARSHALLED_OUT(m_type, m_from) (GDMonoMarshal::marshalled_out_##m_type(m_from))
  415. } // namespace GDMonoMarshal
  416. #endif // GDMONOMARSHAL_H