method_ptrcall.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /*************************************************************************/
  2. /* method_ptrcall.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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 "math_2d.h"
  33. #include "typedefs.h"
  34. #include "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[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[i] = r[i]; \
  189. } \
  190. } \
  191. return ret; \
  192. } \
  193. }
  194. MAKE_VECARG(String);
  195. MAKE_VECARG(uint8_t);
  196. MAKE_VECARG(int);
  197. MAKE_VECARG(float);
  198. MAKE_VECARG(Vector2);
  199. MAKE_VECARG(Vector3);
  200. MAKE_VECARG(Color);
  201. //for stuff that gets converted to Array vectors
  202. #define MAKE_VECARR(m_type) \
  203. template <> \
  204. struct PtrToArg<Vector<m_type> > { \
  205. _FORCE_INLINE_ static Vector<m_type> convert(const void *p_ptr) { \
  206. const Array *arr = reinterpret_cast<const Array *>(p_ptr); \
  207. Vector<m_type> ret; \
  208. int len = arr->size(); \
  209. ret.resize(len); \
  210. for (int i = 0; i < len; i++) { \
  211. ret[i] = (*arr)[i]; \
  212. } \
  213. return ret; \
  214. } \
  215. _FORCE_INLINE_ static void encode(Vector<m_type> p_vec, void *p_ptr) { \
  216. Array *arr = reinterpret_cast<Array *>(p_ptr); \
  217. int len = p_vec.size(); \
  218. arr->resize(len); \
  219. for (int i = 0; i < len; i++) { \
  220. (*arr)[i] = p_vec[i]; \
  221. } \
  222. } \
  223. }; \
  224. template <> \
  225. struct PtrToArg<const Vector<m_type> &> { \
  226. _FORCE_INLINE_ static Vector<m_type> convert(const void *p_ptr) { \
  227. const Array *arr = reinterpret_cast<const Array *>(p_ptr); \
  228. Vector<m_type> ret; \
  229. int len = arr->size(); \
  230. ret.resize(len); \
  231. for (int i = 0; i < len; i++) { \
  232. ret[i] = (*arr)[i]; \
  233. } \
  234. return ret; \
  235. } \
  236. }
  237. MAKE_VECARR(Variant);
  238. MAKE_VECARR(RID);
  239. MAKE_VECARR(Plane);
  240. #define MAKE_DVECARR(m_type) \
  241. template <> \
  242. struct PtrToArg<PoolVector<m_type> > { \
  243. _FORCE_INLINE_ static PoolVector<m_type> convert(const void *p_ptr) { \
  244. const Array *arr = reinterpret_cast<const Array *>(p_ptr); \
  245. PoolVector<m_type> ret; \
  246. int len = arr->size(); \
  247. ret.resize(len); \
  248. { \
  249. PoolVector<m_type>::Write w = ret.write(); \
  250. for (int i = 0; i < len; i++) { \
  251. w[i] = (*arr)[i]; \
  252. } \
  253. } \
  254. return ret; \
  255. } \
  256. _FORCE_INLINE_ static void encode(PoolVector<m_type> p_vec, void *p_ptr) { \
  257. Array *arr = reinterpret_cast<Array *>(p_ptr); \
  258. int len = p_vec.size(); \
  259. arr->resize(len); \
  260. { \
  261. PoolVector<m_type>::Read r = p_vec.read(); \
  262. for (int i = 0; i < len; i++) { \
  263. (*arr)[i] = r[i]; \
  264. } \
  265. } \
  266. } \
  267. }; \
  268. template <> \
  269. struct PtrToArg<const PoolVector<m_type> &> { \
  270. _FORCE_INLINE_ static PoolVector<m_type> convert(const void *p_ptr) { \
  271. const Array *arr = reinterpret_cast<const Array *>(p_ptr); \
  272. PoolVector<m_type> ret; \
  273. int len = arr->size(); \
  274. ret.resize(len); \
  275. { \
  276. PoolVector<m_type>::Write w = ret.write(); \
  277. for (int i = 0; i < len; i++) { \
  278. w[i] = (*arr)[i]; \
  279. } \
  280. } \
  281. return ret; \
  282. } \
  283. }
  284. MAKE_DVECARR(Plane);
  285. //for special case StringName
  286. #define MAKE_STRINGCONV(m_type) \
  287. template <> \
  288. struct PtrToArg<m_type> { \
  289. _FORCE_INLINE_ static m_type convert(const void *p_ptr) { \
  290. m_type s = *reinterpret_cast<const String *>(p_ptr); \
  291. return s; \
  292. } \
  293. _FORCE_INLINE_ static void encode(m_type p_vec, void *p_ptr) { \
  294. String *arr = reinterpret_cast<String *>(p_ptr); \
  295. *arr = p_vec; \
  296. } \
  297. }; \
  298. \
  299. template <> \
  300. struct PtrToArg<const m_type &> { \
  301. _FORCE_INLINE_ static m_type convert(const void *p_ptr) { \
  302. m_type s = *reinterpret_cast<const String *>(p_ptr); \
  303. return s; \
  304. } \
  305. }
  306. #define MAKE_STRINGCONV_BY_REFERENCE(m_type) \
  307. template <> \
  308. struct PtrToArg<m_type> { \
  309. _FORCE_INLINE_ static m_type convert(const void *p_ptr) { \
  310. m_type s = *reinterpret_cast<const String *>(p_ptr); \
  311. return s; \
  312. } \
  313. _FORCE_INLINE_ static void encode(const m_type &p_vec, void *p_ptr) { \
  314. String *arr = reinterpret_cast<String *>(p_ptr); \
  315. *arr = p_vec; \
  316. } \
  317. }; \
  318. \
  319. template <> \
  320. struct PtrToArg<const m_type &> { \
  321. _FORCE_INLINE_ static m_type convert(const void *p_ptr) { \
  322. m_type s = *reinterpret_cast<const String *>(p_ptr); \
  323. return s; \
  324. } \
  325. }
  326. MAKE_STRINGCONV(StringName);
  327. MAKE_STRINGCONV_BY_REFERENCE(IP_Address);
  328. template <>
  329. struct PtrToArg<PoolVector<Face3> > {
  330. _FORCE_INLINE_ static PoolVector<Face3> convert(const void *p_ptr) {
  331. const PoolVector<Vector3> *dvs = reinterpret_cast<const PoolVector<Vector3> *>(p_ptr);
  332. PoolVector<Face3> ret;
  333. int len = dvs->size() / 3;
  334. ret.resize(len);
  335. {
  336. PoolVector<Vector3>::Read r = dvs->read();
  337. PoolVector<Face3>::Write w = ret.write();
  338. for (int i = 0; i < len; i++) {
  339. w[i].vertex[0] = r[i * 3 + 0];
  340. w[i].vertex[1] = r[i * 3 + 1];
  341. w[i].vertex[2] = r[i * 3 + 2];
  342. }
  343. }
  344. return ret;
  345. }
  346. _FORCE_INLINE_ static void encode(PoolVector<Face3> p_vec, void *p_ptr) {
  347. PoolVector<Vector3> *arr = reinterpret_cast<PoolVector<Vector3> *>(p_ptr);
  348. int len = p_vec.size();
  349. arr->resize(len * 3);
  350. {
  351. PoolVector<Face3>::Read r = p_vec.read();
  352. PoolVector<Vector3>::Write w = arr->write();
  353. for (int i = 0; i < len; i++) {
  354. w[i * 3 + 0] = r[i].vertex[0];
  355. w[i * 3 + 1] = r[i].vertex[1];
  356. w[i * 3 + 2] = r[i].vertex[2];
  357. }
  358. }
  359. }
  360. };
  361. template <>
  362. struct PtrToArg<const PoolVector<Face3> &> {
  363. _FORCE_INLINE_ static PoolVector<Face3> convert(const void *p_ptr) {
  364. const PoolVector<Vector3> *dvs = reinterpret_cast<const PoolVector<Vector3> *>(p_ptr);
  365. PoolVector<Face3> ret;
  366. int len = dvs->size() / 3;
  367. ret.resize(len);
  368. {
  369. PoolVector<Vector3>::Read r = dvs->read();
  370. PoolVector<Face3>::Write w = ret.write();
  371. for (int i = 0; i < len; i++) {
  372. w[i].vertex[0] = r[i * 3 + 0];
  373. w[i].vertex[1] = r[i * 3 + 1];
  374. w[i].vertex[2] = r[i * 3 + 2];
  375. }
  376. }
  377. return ret;
  378. }
  379. };
  380. #endif // METHOD_PTRCALL_H
  381. #endif