method_ptrcall.h 26 KB

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