method_ptrcall.h 25 KB

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