method_ptrcall.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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. if (p_ptr == nullptr) {
  153. return nullptr;
  154. }
  155. return const_cast<T *>(*reinterpret_cast<T *const *>(p_ptr));
  156. }
  157. typedef Object *EncodeT;
  158. _FORCE_INLINE_ static void encode(T *p_var, void *p_ptr) {
  159. *((T **)p_ptr) = p_var;
  160. }
  161. };
  162. template <class T>
  163. struct PtrToArg<const T *> {
  164. _FORCE_INLINE_ static const T *convert(const void *p_ptr) {
  165. if (p_ptr == nullptr) {
  166. return nullptr;
  167. }
  168. return *reinterpret_cast<T *const *>(p_ptr);
  169. }
  170. typedef const Object *EncodeT;
  171. _FORCE_INLINE_ static void encode(T *p_var, void *p_ptr) {
  172. *((T **)p_ptr) = p_var;
  173. }
  174. };
  175. // This is for ObjectID.
  176. template <>
  177. struct PtrToArg<ObjectID> {
  178. _FORCE_INLINE_ static const ObjectID convert(const void *p_ptr) {
  179. return ObjectID(*reinterpret_cast<const uint64_t *>(p_ptr));
  180. }
  181. typedef uint64_t EncodeT;
  182. _FORCE_INLINE_ static void encode(const ObjectID &p_val, void *p_ptr) {
  183. *((uint64_t *)p_ptr) = p_val;
  184. }
  185. };
  186. // This is for the special cases used by Variant.
  187. // No EncodeT because direct pointer conversion not possible.
  188. #define MAKE_VECARG(m_type) \
  189. template <> \
  190. struct PtrToArg<Vector<m_type>> { \
  191. _FORCE_INLINE_ static Vector<m_type> convert(const void *p_ptr) { \
  192. const Vector<m_type> *dvs = reinterpret_cast<const Vector<m_type> *>(p_ptr); \
  193. Vector<m_type> ret; \
  194. int len = dvs->size(); \
  195. ret.resize(len); \
  196. { \
  197. const m_type *r = dvs->ptr(); \
  198. for (int i = 0; i < len; i++) { \
  199. ret.write[i] = r[i]; \
  200. } \
  201. } \
  202. return ret; \
  203. } \
  204. _FORCE_INLINE_ static void encode(Vector<m_type> p_vec, void *p_ptr) { \
  205. Vector<m_type> *dv = reinterpret_cast<Vector<m_type> *>(p_ptr); \
  206. int len = p_vec.size(); \
  207. dv->resize(len); \
  208. { \
  209. m_type *w = dv->ptrw(); \
  210. for (int i = 0; i < len; i++) { \
  211. w[i] = p_vec[i]; \
  212. } \
  213. } \
  214. } \
  215. }; \
  216. template <> \
  217. struct PtrToArg<const Vector<m_type> &> { \
  218. _FORCE_INLINE_ static Vector<m_type> convert(const void *p_ptr) { \
  219. const Vector<m_type> *dvs = reinterpret_cast<const Vector<m_type> *>(p_ptr); \
  220. Vector<m_type> ret; \
  221. int len = dvs->size(); \
  222. ret.resize(len); \
  223. { \
  224. const m_type *r = dvs->ptr(); \
  225. for (int i = 0; i < len; i++) { \
  226. ret.write[i] = r[i]; \
  227. } \
  228. } \
  229. return ret; \
  230. } \
  231. }
  232. // No EncodeT because direct pointer conversion not possible.
  233. #define MAKE_VECARG_ALT(m_type, m_type_alt) \
  234. template <> \
  235. struct PtrToArg<Vector<m_type_alt>> { \
  236. _FORCE_INLINE_ static Vector<m_type_alt> convert(const void *p_ptr) { \
  237. const Vector<m_type> *dvs = reinterpret_cast<const Vector<m_type> *>(p_ptr); \
  238. Vector<m_type_alt> ret; \
  239. int len = dvs->size(); \
  240. ret.resize(len); \
  241. { \
  242. const m_type *r = dvs->ptr(); \
  243. for (int i = 0; i < len; i++) { \
  244. ret.write[i] = r[i]; \
  245. } \
  246. } \
  247. return ret; \
  248. } \
  249. _FORCE_INLINE_ static void encode(Vector<m_type_alt> p_vec, void *p_ptr) { \
  250. Vector<m_type> *dv = reinterpret_cast<Vector<m_type> *>(p_ptr); \
  251. int len = p_vec.size(); \
  252. dv->resize(len); \
  253. { \
  254. m_type *w = dv->ptrw(); \
  255. for (int i = 0; i < len; i++) { \
  256. w[i] = p_vec[i]; \
  257. } \
  258. } \
  259. } \
  260. }; \
  261. template <> \
  262. struct PtrToArg<const Vector<m_type_alt> &> { \
  263. _FORCE_INLINE_ static Vector<m_type_alt> convert(const void *p_ptr) { \
  264. const Vector<m_type> *dvs = reinterpret_cast<const Vector<m_type> *>(p_ptr); \
  265. Vector<m_type_alt> ret; \
  266. int len = dvs->size(); \
  267. ret.resize(len); \
  268. { \
  269. const m_type *r = dvs->ptr(); \
  270. for (int i = 0; i < len; i++) { \
  271. ret.write[i] = r[i]; \
  272. } \
  273. } \
  274. return ret; \
  275. } \
  276. }
  277. MAKE_VECARG_ALT(String, StringName);
  278. // For stuff that gets converted to Array vectors.
  279. // No EncodeT because direct pointer conversion not possible.
  280. #define MAKE_VECARR(m_type) \
  281. template <> \
  282. struct PtrToArg<Vector<m_type>> { \
  283. _FORCE_INLINE_ static Vector<m_type> convert(const void *p_ptr) { \
  284. const Array *arr = reinterpret_cast<const Array *>(p_ptr); \
  285. Vector<m_type> ret; \
  286. int len = arr->size(); \
  287. ret.resize(len); \
  288. for (int i = 0; i < len; i++) { \
  289. ret.write[i] = (*arr)[i]; \
  290. } \
  291. return ret; \
  292. } \
  293. _FORCE_INLINE_ static void encode(Vector<m_type> p_vec, void *p_ptr) { \
  294. Array *arr = reinterpret_cast<Array *>(p_ptr); \
  295. int len = p_vec.size(); \
  296. arr->resize(len); \
  297. for (int i = 0; i < len; i++) { \
  298. (*arr)[i] = p_vec[i]; \
  299. } \
  300. } \
  301. }; \
  302. template <> \
  303. struct PtrToArg<const Vector<m_type> &> { \
  304. _FORCE_INLINE_ static Vector<m_type> convert(const void *p_ptr) { \
  305. const Array *arr = reinterpret_cast<const Array *>(p_ptr); \
  306. Vector<m_type> ret; \
  307. int len = arr->size(); \
  308. ret.resize(len); \
  309. for (int i = 0; i < len; i++) { \
  310. ret.write[i] = (*arr)[i]; \
  311. } \
  312. return ret; \
  313. } \
  314. }
  315. MAKE_VECARR(Variant);
  316. MAKE_VECARR(RID);
  317. MAKE_VECARR(Plane);
  318. // No EncodeT because direct pointer conversion not possible.
  319. #define MAKE_DVECARR(m_type) \
  320. template <> \
  321. struct PtrToArg<Vector<m_type>> { \
  322. _FORCE_INLINE_ static Vector<m_type> convert(const void *p_ptr) { \
  323. const Array *arr = reinterpret_cast<const Array *>(p_ptr); \
  324. Vector<m_type> ret; \
  325. int len = arr->size(); \
  326. ret.resize(len); \
  327. { \
  328. m_type *w = ret.ptrw(); \
  329. for (int i = 0; i < len; i++) { \
  330. w[i] = (*arr)[i]; \
  331. } \
  332. } \
  333. return ret; \
  334. } \
  335. _FORCE_INLINE_ static void encode(Vector<m_type> p_vec, void *p_ptr) { \
  336. Array *arr = reinterpret_cast<Array *>(p_ptr); \
  337. int len = p_vec.size(); \
  338. arr->resize(len); \
  339. { \
  340. const m_type *r = p_vec.ptr(); \
  341. for (int i = 0; i < len; i++) { \
  342. (*arr)[i] = r[i]; \
  343. } \
  344. } \
  345. } \
  346. }; \
  347. template <> \
  348. struct PtrToArg<const Vector<m_type> &> { \
  349. _FORCE_INLINE_ static Vector<m_type> convert(const void *p_ptr) { \
  350. const Array *arr = reinterpret_cast<const Array *>(p_ptr); \
  351. Vector<m_type> ret; \
  352. int len = arr->size(); \
  353. ret.resize(len); \
  354. { \
  355. m_type *w = ret.ptrw(); \
  356. for (int i = 0; i < len; i++) { \
  357. w[i] = (*arr)[i]; \
  358. } \
  359. } \
  360. return ret; \
  361. } \
  362. }
  363. // Special case for IPAddress.
  364. // No EncodeT because direct pointer conversion not possible.
  365. #define MAKE_STRINGCONV_BY_REFERENCE(m_type) \
  366. template <> \
  367. struct PtrToArg<m_type> { \
  368. _FORCE_INLINE_ static m_type convert(const void *p_ptr) { \
  369. m_type s = *reinterpret_cast<const String *>(p_ptr); \
  370. return s; \
  371. } \
  372. _FORCE_INLINE_ static void encode(const m_type &p_vec, void *p_ptr) { \
  373. String *arr = reinterpret_cast<String *>(p_ptr); \
  374. *arr = p_vec; \
  375. } \
  376. }; \
  377. \
  378. template <> \
  379. struct PtrToArg<const m_type &> { \
  380. _FORCE_INLINE_ static m_type convert(const void *p_ptr) { \
  381. m_type s = *reinterpret_cast<const String *>(p_ptr); \
  382. return s; \
  383. } \
  384. }
  385. MAKE_STRINGCONV_BY_REFERENCE(IPAddress);
  386. // No EncodeT because direct pointer conversion not possible.
  387. template <>
  388. struct PtrToArg<Vector<Face3>> {
  389. _FORCE_INLINE_ static Vector<Face3> convert(const void *p_ptr) {
  390. const Vector<Vector3> *dvs = reinterpret_cast<const Vector<Vector3> *>(p_ptr);
  391. Vector<Face3> ret;
  392. int len = dvs->size() / 3;
  393. ret.resize(len);
  394. {
  395. const Vector3 *r = dvs->ptr();
  396. Face3 *w = ret.ptrw();
  397. for (int i = 0; i < len; i++) {
  398. w[i].vertex[0] = r[i * 3 + 0];
  399. w[i].vertex[1] = r[i * 3 + 1];
  400. w[i].vertex[2] = r[i * 3 + 2];
  401. }
  402. }
  403. return ret;
  404. }
  405. _FORCE_INLINE_ static void encode(Vector<Face3> p_vec, void *p_ptr) {
  406. Vector<Vector3> *arr = reinterpret_cast<Vector<Vector3> *>(p_ptr);
  407. int len = p_vec.size();
  408. arr->resize(len * 3);
  409. {
  410. const Face3 *r = p_vec.ptr();
  411. Vector3 *w = arr->ptrw();
  412. for (int i = 0; i < len; i++) {
  413. w[i * 3 + 0] = r[i].vertex[0];
  414. w[i * 3 + 1] = r[i].vertex[1];
  415. w[i * 3 + 2] = r[i].vertex[2];
  416. }
  417. }
  418. }
  419. };
  420. // No EncodeT because direct pointer conversion not possible.
  421. template <>
  422. struct PtrToArg<const Vector<Face3> &> {
  423. _FORCE_INLINE_ static Vector<Face3> convert(const void *p_ptr) {
  424. const Vector<Vector3> *dvs = reinterpret_cast<const Vector<Vector3> *>(p_ptr);
  425. Vector<Face3> ret;
  426. int len = dvs->size() / 3;
  427. ret.resize(len);
  428. {
  429. const Vector3 *r = dvs->ptr();
  430. Face3 *w = ret.ptrw();
  431. for (int i = 0; i < len; i++) {
  432. w[i].vertex[0] = r[i * 3 + 0];
  433. w[i].vertex[1] = r[i * 3 + 1];
  434. w[i].vertex[2] = r[i * 3 + 2];
  435. }
  436. }
  437. return ret;
  438. }
  439. };
  440. #endif // METHOD_PTRCALL_H