method_ptrcall.h 25 KB

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