method_ptrcall.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. /*************************************************************************/
  2. /* method_ptrcall.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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 METHOD_PTRCALL_H
  31. #define METHOD_PTRCALL_H
  32. #include "core/math/transform_2d.h"
  33. #include "core/object/object_id.h"
  34. #include "core/typedefs.h"
  35. #include "core/variant/variant.h"
  36. template <class T>
  37. struct PtrToArg {};
  38. #define MAKE_PTRARG(m_type) \
  39. template <> \
  40. struct PtrToArg<m_type> { \
  41. _FORCE_INLINE_ static m_type convert(const void *p_ptr) { \
  42. return *reinterpret_cast<const m_type *>(p_ptr); \
  43. } \
  44. _FORCE_INLINE_ static void encode(m_type p_val, void *p_ptr) { \
  45. *((m_type *)p_ptr) = p_val; \
  46. } \
  47. }; \
  48. template <> \
  49. struct PtrToArg<const m_type &> { \
  50. _FORCE_INLINE_ static m_type convert(const void *p_ptr) { \
  51. return *reinterpret_cast<const m_type *>(p_ptr); \
  52. } \
  53. _FORCE_INLINE_ static void encode(m_type p_val, void *p_ptr) { \
  54. *((m_type *)p_ptr) = p_val; \
  55. } \
  56. }
  57. #define MAKE_PTRARGCONV(m_type, m_conv) \
  58. template <> \
  59. struct PtrToArg<m_type> { \
  60. _FORCE_INLINE_ static m_type convert(const void *p_ptr) { \
  61. return static_cast<m_type>(*reinterpret_cast<const m_conv *>(p_ptr)); \
  62. } \
  63. _FORCE_INLINE_ static void encode(m_type p_val, void *p_ptr) { \
  64. *((m_conv *)p_ptr) = static_cast<m_conv>(p_val); \
  65. } \
  66. }; \
  67. template <> \
  68. struct PtrToArg<const m_type &> { \
  69. _FORCE_INLINE_ static m_type convert(const void *p_ptr) { \
  70. return static_cast<m_type>(*reinterpret_cast<const m_conv *>(p_ptr)); \
  71. } \
  72. _FORCE_INLINE_ static void encode(m_type p_val, void *p_ptr) { \
  73. *((m_conv *)p_ptr) = static_cast<m_conv>(p_val); \
  74. } \
  75. }
  76. #define MAKE_PTRARG_BY_REFERENCE(m_type) \
  77. template <> \
  78. struct PtrToArg<m_type> { \
  79. _FORCE_INLINE_ static m_type convert(const void *p_ptr) { \
  80. return *reinterpret_cast<const m_type *>(p_ptr); \
  81. } \
  82. _FORCE_INLINE_ static void encode(const m_type &p_val, void *p_ptr) { \
  83. *((m_type *)p_ptr) = p_val; \
  84. } \
  85. }; \
  86. template <> \
  87. struct PtrToArg<const m_type &> { \
  88. _FORCE_INLINE_ static m_type convert(const void *p_ptr) { \
  89. return *reinterpret_cast<const m_type *>(p_ptr); \
  90. } \
  91. _FORCE_INLINE_ static void encode(const m_type &p_val, void *p_ptr) { \
  92. *((m_type *)p_ptr) = p_val; \
  93. } \
  94. }
  95. MAKE_PTRARG(bool);
  96. // Integer types.
  97. MAKE_PTRARGCONV(uint8_t, int64_t);
  98. MAKE_PTRARGCONV(int8_t, int64_t);
  99. MAKE_PTRARGCONV(uint16_t, int64_t);
  100. MAKE_PTRARGCONV(int16_t, int64_t);
  101. MAKE_PTRARGCONV(uint32_t, int64_t);
  102. MAKE_PTRARGCONV(int32_t, int64_t);
  103. MAKE_PTRARG(int64_t);
  104. MAKE_PTRARG(uint64_t);
  105. // Float types
  106. MAKE_PTRARGCONV(float, double);
  107. MAKE_PTRARG(double);
  108. MAKE_PTRARG(String);
  109. MAKE_PTRARG(Vector2);
  110. MAKE_PTRARG(Vector2i);
  111. MAKE_PTRARG(Rect2);
  112. MAKE_PTRARG(Rect2i);
  113. MAKE_PTRARG_BY_REFERENCE(Vector3);
  114. MAKE_PTRARG_BY_REFERENCE(Vector3i);
  115. MAKE_PTRARG(Transform2D);
  116. MAKE_PTRARG_BY_REFERENCE(Plane);
  117. MAKE_PTRARG(Quat);
  118. MAKE_PTRARG_BY_REFERENCE(AABB);
  119. MAKE_PTRARG_BY_REFERENCE(Basis);
  120. MAKE_PTRARG_BY_REFERENCE(Transform);
  121. MAKE_PTRARG_BY_REFERENCE(Color);
  122. MAKE_PTRARG(StringName);
  123. MAKE_PTRARG(NodePath);
  124. MAKE_PTRARG(RID);
  125. // Object doesn't need this.
  126. MAKE_PTRARG(Callable);
  127. MAKE_PTRARG(Signal);
  128. MAKE_PTRARG(Dictionary);
  129. MAKE_PTRARG(Array);
  130. MAKE_PTRARG(PackedByteArray);
  131. MAKE_PTRARG(PackedInt32Array);
  132. MAKE_PTRARG(PackedInt64Array);
  133. MAKE_PTRARG(PackedFloat32Array);
  134. MAKE_PTRARG(PackedFloat64Array);
  135. MAKE_PTRARG(PackedStringArray);
  136. MAKE_PTRARG(PackedVector2Array);
  137. MAKE_PTRARG(PackedVector3Array);
  138. MAKE_PTRARG(PackedColorArray);
  139. MAKE_PTRARG_BY_REFERENCE(Variant);
  140. // This is for Object.
  141. template <class T>
  142. struct PtrToArg<T *> {
  143. _FORCE_INLINE_ static T *convert(const void *p_ptr) {
  144. return const_cast<T *>(reinterpret_cast<const T *>(p_ptr));
  145. }
  146. _FORCE_INLINE_ static void encode(T *p_var, void *p_ptr) {
  147. *((T **)p_ptr) = p_var;
  148. }
  149. };
  150. template <class T>
  151. struct PtrToArg<const T *> {
  152. _FORCE_INLINE_ static const T *convert(const void *p_ptr) {
  153. return reinterpret_cast<const T *>(p_ptr);
  154. }
  155. _FORCE_INLINE_ static void encode(T *p_var, void *p_ptr) {
  156. *((T **)p_ptr) = p_var;
  157. }
  158. };
  159. // This is for ObjectID.
  160. template <>
  161. struct PtrToArg<ObjectID> {
  162. _FORCE_INLINE_ static const ObjectID convert(const void *p_ptr) {
  163. return ObjectID(*reinterpret_cast<const uint64_t *>(p_ptr));
  164. }
  165. _FORCE_INLINE_ static void encode(const ObjectID &p_val, void *p_ptr) {
  166. *((uint64_t *)p_ptr) = p_val;
  167. }
  168. };
  169. // This is for the special cases used by Variant.
  170. #define MAKE_VECARG(m_type) \
  171. template <> \
  172. struct PtrToArg<Vector<m_type>> { \
  173. _FORCE_INLINE_ static Vector<m_type> convert(const void *p_ptr) { \
  174. const Vector<m_type> *dvs = reinterpret_cast<const Vector<m_type> *>(p_ptr); \
  175. Vector<m_type> ret; \
  176. int len = dvs->size(); \
  177. ret.resize(len); \
  178. { \
  179. const m_type *r = dvs->ptr(); \
  180. for (int i = 0; i < len; i++) { \
  181. ret.write[i] = r[i]; \
  182. } \
  183. } \
  184. return ret; \
  185. } \
  186. _FORCE_INLINE_ static void encode(Vector<m_type> p_vec, void *p_ptr) { \
  187. Vector<m_type> *dv = reinterpret_cast<Vector<m_type> *>(p_ptr); \
  188. int len = p_vec.size(); \
  189. dv->resize(len); \
  190. { \
  191. m_type *w = dv->ptrw(); \
  192. for (int i = 0; i < len; i++) { \
  193. w[i] = p_vec[i]; \
  194. } \
  195. } \
  196. } \
  197. }; \
  198. template <> \
  199. struct PtrToArg<const Vector<m_type> &> { \
  200. _FORCE_INLINE_ static Vector<m_type> convert(const void *p_ptr) { \
  201. const Vector<m_type> *dvs = reinterpret_cast<const Vector<m_type> *>(p_ptr); \
  202. Vector<m_type> ret; \
  203. int len = dvs->size(); \
  204. ret.resize(len); \
  205. { \
  206. const m_type *r = dvs->ptr(); \
  207. for (int i = 0; i < len; i++) { \
  208. ret.write[i] = r[i]; \
  209. } \
  210. } \
  211. return ret; \
  212. } \
  213. }
  214. #define MAKE_VECARG_ALT(m_type, m_type_alt) \
  215. template <> \
  216. struct PtrToArg<Vector<m_type_alt>> { \
  217. _FORCE_INLINE_ static Vector<m_type_alt> convert(const void *p_ptr) { \
  218. const Vector<m_type> *dvs = reinterpret_cast<const Vector<m_type> *>(p_ptr); \
  219. Vector<m_type_alt> ret; \
  220. int len = dvs->size(); \
  221. ret.resize(len); \
  222. { \
  223. const m_type *r = dvs->ptr(); \
  224. for (int i = 0; i < len; i++) { \
  225. ret.write[i] = r[i]; \
  226. } \
  227. } \
  228. return ret; \
  229. } \
  230. _FORCE_INLINE_ static void encode(Vector<m_type_alt> p_vec, void *p_ptr) { \
  231. Vector<m_type> *dv = reinterpret_cast<Vector<m_type> *>(p_ptr); \
  232. int len = p_vec.size(); \
  233. dv->resize(len); \
  234. { \
  235. m_type *w = dv->ptrw(); \
  236. for (int i = 0; i < len; i++) { \
  237. w[i] = p_vec[i]; \
  238. } \
  239. } \
  240. } \
  241. }; \
  242. template <> \
  243. struct PtrToArg<const Vector<m_type_alt> &> { \
  244. _FORCE_INLINE_ static Vector<m_type_alt> convert(const void *p_ptr) { \
  245. const Vector<m_type> *dvs = reinterpret_cast<const Vector<m_type> *>(p_ptr); \
  246. Vector<m_type_alt> ret; \
  247. int len = dvs->size(); \
  248. ret.resize(len); \
  249. { \
  250. const m_type *r = dvs->ptr(); \
  251. for (int i = 0; i < len; i++) { \
  252. ret.write[i] = r[i]; \
  253. } \
  254. } \
  255. return ret; \
  256. } \
  257. }
  258. MAKE_VECARG_ALT(String, StringName);
  259. // For stuff that gets converted to Array vectors.
  260. #define MAKE_VECARR(m_type) \
  261. template <> \
  262. struct PtrToArg<Vector<m_type>> { \
  263. _FORCE_INLINE_ static Vector<m_type> convert(const void *p_ptr) { \
  264. const Array *arr = reinterpret_cast<const Array *>(p_ptr); \
  265. Vector<m_type> ret; \
  266. int len = arr->size(); \
  267. ret.resize(len); \
  268. for (int i = 0; i < len; i++) { \
  269. ret.write[i] = (*arr)[i]; \
  270. } \
  271. return ret; \
  272. } \
  273. _FORCE_INLINE_ static void encode(Vector<m_type> p_vec, void *p_ptr) { \
  274. Array *arr = reinterpret_cast<Array *>(p_ptr); \
  275. int len = p_vec.size(); \
  276. arr->resize(len); \
  277. for (int i = 0; i < len; i++) { \
  278. (*arr)[i] = p_vec[i]; \
  279. } \
  280. } \
  281. }; \
  282. template <> \
  283. struct PtrToArg<const Vector<m_type> &> { \
  284. _FORCE_INLINE_ static Vector<m_type> convert(const void *p_ptr) { \
  285. const Array *arr = reinterpret_cast<const Array *>(p_ptr); \
  286. Vector<m_type> ret; \
  287. int len = arr->size(); \
  288. ret.resize(len); \
  289. for (int i = 0; i < len; i++) { \
  290. ret.write[i] = (*arr)[i]; \
  291. } \
  292. return ret; \
  293. } \
  294. }
  295. MAKE_VECARR(Variant);
  296. MAKE_VECARR(RID);
  297. MAKE_VECARR(Plane);
  298. #define MAKE_DVECARR(m_type) \
  299. template <> \
  300. struct PtrToArg<Vector<m_type>> { \
  301. _FORCE_INLINE_ static Vector<m_type> convert(const void *p_ptr) { \
  302. const Array *arr = reinterpret_cast<const Array *>(p_ptr); \
  303. Vector<m_type> ret; \
  304. int len = arr->size(); \
  305. ret.resize(len); \
  306. { \
  307. m_type *w = ret.ptrw(); \
  308. for (int i = 0; i < len; i++) { \
  309. w[i] = (*arr)[i]; \
  310. } \
  311. } \
  312. return ret; \
  313. } \
  314. _FORCE_INLINE_ static void encode(Vector<m_type> p_vec, void *p_ptr) { \
  315. Array *arr = reinterpret_cast<Array *>(p_ptr); \
  316. int len = p_vec.size(); \
  317. arr->resize(len); \
  318. { \
  319. const m_type *r = p_vec.ptr(); \
  320. for (int i = 0; i < len; i++) { \
  321. (*arr)[i] = r[i]; \
  322. } \
  323. } \
  324. } \
  325. }; \
  326. template <> \
  327. struct PtrToArg<const Vector<m_type> &> { \
  328. _FORCE_INLINE_ static Vector<m_type> convert(const void *p_ptr) { \
  329. const Array *arr = reinterpret_cast<const Array *>(p_ptr); \
  330. Vector<m_type> ret; \
  331. int len = arr->size(); \
  332. ret.resize(len); \
  333. { \
  334. m_type *w = ret.ptrw(); \
  335. for (int i = 0; i < len; i++) { \
  336. w[i] = (*arr)[i]; \
  337. } \
  338. } \
  339. return ret; \
  340. } \
  341. }
  342. // Special case for IP_Address.
  343. #define MAKE_STRINGCONV_BY_REFERENCE(m_type) \
  344. template <> \
  345. struct PtrToArg<m_type> { \
  346. _FORCE_INLINE_ static m_type convert(const void *p_ptr) { \
  347. m_type s = *reinterpret_cast<const String *>(p_ptr); \
  348. return s; \
  349. } \
  350. _FORCE_INLINE_ static void encode(const m_type &p_vec, void *p_ptr) { \
  351. String *arr = reinterpret_cast<String *>(p_ptr); \
  352. *arr = p_vec; \
  353. } \
  354. }; \
  355. \
  356. template <> \
  357. struct PtrToArg<const m_type &> { \
  358. _FORCE_INLINE_ static m_type convert(const void *p_ptr) { \
  359. m_type s = *reinterpret_cast<const String *>(p_ptr); \
  360. return s; \
  361. } \
  362. }
  363. MAKE_STRINGCONV_BY_REFERENCE(IP_Address);
  364. template <>
  365. struct PtrToArg<Vector<Face3>> {
  366. _FORCE_INLINE_ static Vector<Face3> convert(const void *p_ptr) {
  367. const Vector<Vector3> *dvs = reinterpret_cast<const Vector<Vector3> *>(p_ptr);
  368. Vector<Face3> ret;
  369. int len = dvs->size() / 3;
  370. ret.resize(len);
  371. {
  372. const Vector3 *r = dvs->ptr();
  373. Face3 *w = ret.ptrw();
  374. for (int i = 0; i < len; i++) {
  375. w[i].vertex[0] = r[i * 3 + 0];
  376. w[i].vertex[1] = r[i * 3 + 1];
  377. w[i].vertex[2] = r[i * 3 + 2];
  378. }
  379. }
  380. return ret;
  381. }
  382. _FORCE_INLINE_ static void encode(Vector<Face3> p_vec, void *p_ptr) {
  383. Vector<Vector3> *arr = reinterpret_cast<Vector<Vector3> *>(p_ptr);
  384. int len = p_vec.size();
  385. arr->resize(len * 3);
  386. {
  387. const Face3 *r = p_vec.ptr();
  388. Vector3 *w = arr->ptrw();
  389. for (int i = 0; i < len; i++) {
  390. w[i * 3 + 0] = r[i].vertex[0];
  391. w[i * 3 + 1] = r[i].vertex[1];
  392. w[i * 3 + 2] = r[i].vertex[2];
  393. }
  394. }
  395. }
  396. };
  397. template <>
  398. struct PtrToArg<const Vector<Face3> &> {
  399. _FORCE_INLINE_ static Vector<Face3> convert(const void *p_ptr) {
  400. const Vector<Vector3> *dvs = reinterpret_cast<const Vector<Vector3> *>(p_ptr);
  401. Vector<Face3> ret;
  402. int len = dvs->size() / 3;
  403. ret.resize(len);
  404. {
  405. const Vector3 *r = dvs->ptr();
  406. Face3 *w = ret.ptrw();
  407. for (int i = 0; i < len; i++) {
  408. w[i].vertex[0] = r[i * 3 + 0];
  409. w[i].vertex[1] = r[i * 3 + 1];
  410. w[i].vertex[2] = r[i * 3 + 2];
  411. }
  412. }
  413. return ret;
  414. }
  415. };
  416. #endif // METHOD_PTRCALL_H