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