method_ptrcall.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /*************************************************************************/
  2. /* method_ptrcall.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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_PTRARGR(m_type, m_ret) \
  59. template <> \
  60. struct PtrToArg<m_type> { \
  61. _FORCE_INLINE_ static m_type convert(const void *p_ptr) { \
  62. return *reinterpret_cast<const m_type *>(p_ptr); \
  63. } \
  64. _FORCE_INLINE_ static void encode(m_type p_val, void *p_ptr) { \
  65. *((m_ret *)p_ptr) = 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 *reinterpret_cast<const m_type *>(p_ptr); \
  72. } \
  73. _FORCE_INLINE_ static void encode(m_type p_val, void *p_ptr) { \
  74. *((m_ret *)p_ptr) = p_val; \
  75. } \
  76. }
  77. MAKE_PTRARG(bool);
  78. MAKE_PTRARGR(uint8_t, int);
  79. MAKE_PTRARGR(int8_t, int);
  80. MAKE_PTRARGR(uint16_t, int);
  81. MAKE_PTRARGR(int16_t, int);
  82. MAKE_PTRARGR(uint32_t, int);
  83. MAKE_PTRARGR(int32_t, int);
  84. MAKE_PTRARGR(int64_t, int);
  85. MAKE_PTRARGR(uint64_t, int);
  86. MAKE_PTRARG(float);
  87. MAKE_PTRARGR(double, float);
  88. MAKE_PTRARG(String);
  89. MAKE_PTRARG(Vector2);
  90. MAKE_PTRARG(Rect2);
  91. MAKE_PTRARG(Vector3);
  92. MAKE_PTRARG(Matrix32);
  93. MAKE_PTRARG(Plane);
  94. MAKE_PTRARG(Quat);
  95. MAKE_PTRARG(AABB);
  96. MAKE_PTRARG(Matrix3);
  97. MAKE_PTRARG(Transform);
  98. MAKE_PTRARG(Color);
  99. MAKE_PTRARG(Image);
  100. MAKE_PTRARG(NodePath);
  101. MAKE_PTRARG(RID);
  102. MAKE_PTRARG(InputEvent);
  103. MAKE_PTRARG(Dictionary);
  104. MAKE_PTRARG(Array);
  105. MAKE_PTRARG(ByteArray);
  106. MAKE_PTRARG(IntArray);
  107. MAKE_PTRARG(RealArray);
  108. MAKE_PTRARG(StringArray);
  109. MAKE_PTRARG(Vector2Array);
  110. MAKE_PTRARG(Vector3Array);
  111. MAKE_PTRARG(ColorArray);
  112. MAKE_PTRARG(Variant);
  113. //this is for Object
  114. template <class T>
  115. struct PtrToArg<T *> {
  116. _FORCE_INLINE_ static T *convert(const void *p_ptr) {
  117. return const_cast<T *>(reinterpret_cast<const T *>(p_ptr));
  118. }
  119. _FORCE_INLINE_ static void encode(T *p_var, void *p_ptr) {
  120. *((T **)p_ptr) = p_var;
  121. }
  122. };
  123. template <class T>
  124. struct PtrToArg<const T *> {
  125. _FORCE_INLINE_ static const T *convert(const void *p_ptr) {
  126. return reinterpret_cast<const T *>(p_ptr);
  127. }
  128. _FORCE_INLINE_ static void encode(T *p_var, void *p_ptr) {
  129. *((T **)p_ptr) = p_var;
  130. }
  131. };
  132. //this is for the special cases used by Variant
  133. #define MAKE_VECARG(m_type) \
  134. template <> \
  135. struct PtrToArg<Vector<m_type> > { \
  136. _FORCE_INLINE_ static Vector<m_type> convert(const void *p_ptr) { \
  137. const DVector<m_type> *dvs = reinterpret_cast<const DVector<m_type> *>(p_ptr); \
  138. Vector<m_type> ret; \
  139. int len = dvs->size(); \
  140. ret.resize(len); \
  141. { \
  142. DVector<m_type>::Read r = dvs->read(); \
  143. for (int i = 0; i < len; i++) { \
  144. ret[i] = r[i]; \
  145. } \
  146. } \
  147. return ret; \
  148. } \
  149. _FORCE_INLINE_ static void encode(Vector<m_type> p_vec, void *p_ptr) { \
  150. DVector<m_type> *dv = reinterpret_cast<DVector<m_type> *>(p_ptr); \
  151. int len = p_vec.size(); \
  152. dv->resize(len); \
  153. { \
  154. DVector<m_type>::Write w = dv->write(); \
  155. for (int i = 0; i < len; i++) { \
  156. w[i] = p_vec[i]; \
  157. } \
  158. } \
  159. } \
  160. }; \
  161. template <> \
  162. struct PtrToArg<const Vector<m_type> &> { \
  163. _FORCE_INLINE_ static Vector<m_type> convert(const void *p_ptr) { \
  164. const DVector<m_type> *dvs = reinterpret_cast<const DVector<m_type> *>(p_ptr); \
  165. Vector<m_type> ret; \
  166. int len = dvs->size(); \
  167. ret.resize(len); \
  168. { \
  169. DVector<m_type>::Read r = dvs->read(); \
  170. for (int i = 0; i < len; i++) { \
  171. ret[i] = r[i]; \
  172. } \
  173. } \
  174. return ret; \
  175. } \
  176. }
  177. MAKE_VECARG(String);
  178. MAKE_VECARG(uint8_t);
  179. MAKE_VECARG(int);
  180. MAKE_VECARG(float);
  181. MAKE_VECARG(Vector2);
  182. MAKE_VECARG(Vector3);
  183. MAKE_VECARG(Color);
  184. //for stuff that gets converted to Array vectors
  185. #define MAKE_VECARR(m_type) \
  186. template <> \
  187. struct PtrToArg<Vector<m_type> > { \
  188. _FORCE_INLINE_ static Vector<m_type> convert(const void *p_ptr) { \
  189. const Array *arr = reinterpret_cast<const Array *>(p_ptr); \
  190. Vector<m_type> ret; \
  191. int len = arr->size(); \
  192. ret.resize(len); \
  193. for (int i = 0; i < len; i++) { \
  194. ret[i] = (*arr)[i]; \
  195. } \
  196. return ret; \
  197. } \
  198. _FORCE_INLINE_ static void encode(Vector<m_type> p_vec, void *p_ptr) { \
  199. Array *arr = reinterpret_cast<Array *>(p_ptr); \
  200. int len = p_vec.size(); \
  201. arr->resize(len); \
  202. for (int i = 0; i < len; i++) { \
  203. (*arr)[i] = p_vec[i]; \
  204. } \
  205. } \
  206. }; \
  207. template <> \
  208. struct PtrToArg<const Vector<m_type> &> { \
  209. _FORCE_INLINE_ static Vector<m_type> convert(const void *p_ptr) { \
  210. const Array *arr = reinterpret_cast<const Array *>(p_ptr); \
  211. Vector<m_type> ret; \
  212. int len = arr->size(); \
  213. ret.resize(len); \
  214. for (int i = 0; i < len; i++) { \
  215. ret[i] = (*arr)[i]; \
  216. } \
  217. return ret; \
  218. } \
  219. }
  220. MAKE_VECARR(Variant);
  221. MAKE_VECARR(RID);
  222. MAKE_VECARR(Plane);
  223. #define MAKE_DVECARR(m_type) \
  224. template <> \
  225. struct PtrToArg<DVector<m_type> > { \
  226. _FORCE_INLINE_ static DVector<m_type> convert(const void *p_ptr) { \
  227. const Array *arr = reinterpret_cast<const Array *>(p_ptr); \
  228. DVector<m_type> ret; \
  229. int len = arr->size(); \
  230. ret.resize(len); \
  231. { \
  232. DVector<m_type>::Write w = ret.write(); \
  233. for (int i = 0; i < len; i++) { \
  234. w[i] = (*arr)[i]; \
  235. } \
  236. } \
  237. return ret; \
  238. } \
  239. _FORCE_INLINE_ static void encode(DVector<m_type> p_vec, void *p_ptr) { \
  240. Array *arr = reinterpret_cast<Array *>(p_ptr); \
  241. int len = p_vec.size(); \
  242. arr->resize(len); \
  243. { \
  244. DVector<m_type>::Read r = p_vec.read(); \
  245. for (int i = 0; i < len; i++) { \
  246. (*arr)[i] = r[i]; \
  247. } \
  248. } \
  249. } \
  250. }; \
  251. template <> \
  252. struct PtrToArg<const DVector<m_type> &> { \
  253. _FORCE_INLINE_ static DVector<m_type> convert(const void *p_ptr) { \
  254. const Array *arr = reinterpret_cast<const Array *>(p_ptr); \
  255. DVector<m_type> ret; \
  256. int len = arr->size(); \
  257. ret.resize(len); \
  258. { \
  259. DVector<m_type>::Write w = ret.write(); \
  260. for (int i = 0; i < len; i++) { \
  261. w[i] = (*arr)[i]; \
  262. } \
  263. } \
  264. return ret; \
  265. } \
  266. }
  267. MAKE_DVECARR(Plane);
  268. //for special case StringName
  269. #define MAKE_STRINGCONV(m_type) \
  270. template <> \
  271. struct PtrToArg<m_type> { \
  272. _FORCE_INLINE_ static m_type convert(const void *p_ptr) { \
  273. m_type s = *reinterpret_cast<const String *>(p_ptr); \
  274. return s; \
  275. } \
  276. _FORCE_INLINE_ static void encode(m_type p_vec, void *p_ptr) { \
  277. String *arr = reinterpret_cast<String *>(p_ptr); \
  278. *arr = p_vec; \
  279. } \
  280. }; \
  281. \
  282. template <> \
  283. struct PtrToArg<const m_type &> { \
  284. _FORCE_INLINE_ static m_type convert(const void *p_ptr) { \
  285. m_type s = *reinterpret_cast<const String *>(p_ptr); \
  286. return s; \
  287. } \
  288. }
  289. MAKE_STRINGCONV(StringName);
  290. MAKE_STRINGCONV(IP_Address);
  291. template <>
  292. struct PtrToArg<DVector<Face3> > {
  293. _FORCE_INLINE_ static DVector<Face3> convert(const void *p_ptr) {
  294. const DVector<Vector3> *dvs = reinterpret_cast<const DVector<Vector3> *>(p_ptr);
  295. DVector<Face3> ret;
  296. int len = dvs->size() / 3;
  297. ret.resize(len);
  298. {
  299. DVector<Vector3>::Read r = dvs->read();
  300. DVector<Face3>::Write w = ret.write();
  301. for (int i = 0; i < len; i++) {
  302. w[i].vertex[0] = r[i * 3 + 0];
  303. w[i].vertex[1] = r[i * 3 + 1];
  304. w[i].vertex[2] = r[i * 3 + 2];
  305. }
  306. }
  307. return ret;
  308. }
  309. _FORCE_INLINE_ static void encode(DVector<Face3> p_vec, void *p_ptr) {
  310. DVector<Vector3> *arr = reinterpret_cast<DVector<Vector3> *>(p_ptr);
  311. int len = p_vec.size();
  312. arr->resize(len * 3);
  313. {
  314. DVector<Face3>::Read r = p_vec.read();
  315. DVector<Vector3>::Write w = arr->write();
  316. for (int i = 0; i < len; i++) {
  317. w[i * 3 + 0] = r[i].vertex[0];
  318. w[i * 3 + 1] = r[i].vertex[1];
  319. w[i * 3 + 2] = r[i].vertex[2];
  320. }
  321. }
  322. }
  323. };
  324. template <>
  325. struct PtrToArg<const DVector<Face3> &> {
  326. _FORCE_INLINE_ static DVector<Face3> convert(const void *p_ptr) {
  327. const DVector<Vector3> *dvs = reinterpret_cast<const DVector<Vector3> *>(p_ptr);
  328. DVector<Face3> ret;
  329. int len = dvs->size() / 3;
  330. ret.resize(len);
  331. {
  332. DVector<Vector3>::Read r = dvs->read();
  333. DVector<Face3>::Write w = ret.write();
  334. for (int i = 0; i < len; i++) {
  335. w[i].vertex[0] = r[i * 3 + 0];
  336. w[i].vertex[1] = r[i * 3 + 1];
  337. w[i].vertex[2] = r[i * 3 + 2];
  338. }
  339. }
  340. return ret;
  341. }
  342. };
  343. #endif // METHOD_PTRCALL_H
  344. #endif