2
0

method_ptrcall.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*************************************************************************/
  2. /* method_ptrcall.hpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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 GODOT_METHOD_PTRCALL_HPP
  31. #define GODOT_METHOD_PTRCALL_HPP
  32. #include <godot_cpp/core/defs.hpp>
  33. #include <godot_cpp/godot.hpp>
  34. #include <godot_cpp/variant/variant.hpp>
  35. namespace godot {
  36. template <class T>
  37. struct PtrToArg {};
  38. #define MAKE_PTRARG(m_type) \
  39. template <> \
  40. struct PtrToArg<m_type> { \
  41. _FORCE_INLINE_ static m_type convert(const void *p_ptr) { \
  42. return *reinterpret_cast<const m_type *>(p_ptr); \
  43. } \
  44. typedef m_type EncodeT; \
  45. _FORCE_INLINE_ static void encode(m_type p_val, void *p_ptr) { \
  46. *reinterpret_cast<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. typedef m_type EncodeT; \
  55. _FORCE_INLINE_ static void encode(m_type p_val, void *p_ptr) { \
  56. *reinterpret_cast<m_type *>(p_ptr) = p_val; \
  57. } \
  58. }
  59. #define MAKE_PTRARGCONV(m_type, m_conv) \
  60. template <> \
  61. struct PtrToArg<m_type> { \
  62. _FORCE_INLINE_ static m_type convert(const void *p_ptr) { \
  63. return static_cast<m_type>(*reinterpret_cast<const m_conv *>(p_ptr)); \
  64. } \
  65. typedef m_conv EncodeT; \
  66. _FORCE_INLINE_ static void encode(m_type p_val, void *p_ptr) { \
  67. *reinterpret_cast<m_conv *>(p_ptr) = static_cast<m_conv>(p_val); \
  68. } \
  69. _FORCE_INLINE_ static m_conv encode_arg(m_type p_val) { \
  70. return static_cast<m_conv>(p_val); \
  71. } \
  72. }; \
  73. template <> \
  74. struct PtrToArg<const m_type &> { \
  75. _FORCE_INLINE_ static m_type convert(const void *p_ptr) { \
  76. return static_cast<m_type>(*reinterpret_cast<const m_conv *>(p_ptr)); \
  77. } \
  78. typedef m_conv EncodeT; \
  79. _FORCE_INLINE_ static void encode(m_type p_val, void *p_ptr) { \
  80. *reinterpret_cast<m_conv *>(p_ptr) = static_cast<m_conv>(p_val); \
  81. } \
  82. _FORCE_INLINE_ static m_conv encode_arg(m_type p_val) { \
  83. return static_cast<m_conv>(p_val); \
  84. } \
  85. }
  86. #define MAKE_PTRARG_BY_REFERENCE(m_type) \
  87. template <> \
  88. struct PtrToArg<m_type> { \
  89. _FORCE_INLINE_ static m_type convert(const void *p_ptr) { \
  90. return *reinterpret_cast<const m_type *>(p_ptr); \
  91. } \
  92. typedef m_type EncodeT; \
  93. _FORCE_INLINE_ static void encode(const m_type &p_val, void *p_ptr) { \
  94. *reinterpret_cast<m_type *>(p_ptr) = p_val; \
  95. } \
  96. }; \
  97. template <> \
  98. struct PtrToArg<const m_type &> { \
  99. _FORCE_INLINE_ static m_type convert(const void *p_ptr) { \
  100. return *reinterpret_cast<const m_type *>(p_ptr); \
  101. } \
  102. typedef m_type EncodeT; \
  103. _FORCE_INLINE_ static void encode(const m_type &p_val, void *p_ptr) { \
  104. *reinterpret_cast<m_type *>(p_ptr) = p_val; \
  105. } \
  106. }
  107. MAKE_PTRARGCONV(bool, uint8_t);
  108. // Integer types.
  109. MAKE_PTRARGCONV(uint8_t, int64_t);
  110. MAKE_PTRARGCONV(int8_t, int64_t);
  111. MAKE_PTRARGCONV(uint16_t, int64_t);
  112. MAKE_PTRARGCONV(int16_t, int64_t);
  113. MAKE_PTRARGCONV(uint32_t, int64_t);
  114. MAKE_PTRARGCONV(int32_t, int64_t);
  115. MAKE_PTRARG(int64_t);
  116. MAKE_PTRARG(uint64_t);
  117. // Float types
  118. MAKE_PTRARGCONV(float, double);
  119. MAKE_PTRARG(double);
  120. MAKE_PTRARG(String);
  121. MAKE_PTRARG(Vector2);
  122. MAKE_PTRARG(Vector2i);
  123. MAKE_PTRARG(Rect2);
  124. MAKE_PTRARG(Rect2i);
  125. MAKE_PTRARG_BY_REFERENCE(Vector3);
  126. MAKE_PTRARG_BY_REFERENCE(Vector3i);
  127. MAKE_PTRARG(Transform2D);
  128. MAKE_PTRARG_BY_REFERENCE(Vector4);
  129. MAKE_PTRARG_BY_REFERENCE(Vector4i);
  130. MAKE_PTRARG_BY_REFERENCE(Plane);
  131. MAKE_PTRARG(Quaternion);
  132. MAKE_PTRARG_BY_REFERENCE(AABB);
  133. MAKE_PTRARG_BY_REFERENCE(Basis);
  134. MAKE_PTRARG_BY_REFERENCE(Transform3D);
  135. MAKE_PTRARG_BY_REFERENCE(Projection);
  136. MAKE_PTRARG_BY_REFERENCE(Color);
  137. MAKE_PTRARG(StringName);
  138. MAKE_PTRARG(NodePath);
  139. MAKE_PTRARG(RID);
  140. // Object doesn't need this.
  141. MAKE_PTRARG(Callable);
  142. MAKE_PTRARG(Signal);
  143. MAKE_PTRARG(Dictionary);
  144. MAKE_PTRARG(Array);
  145. MAKE_PTRARG(PackedByteArray);
  146. MAKE_PTRARG(PackedInt32Array);
  147. MAKE_PTRARG(PackedInt64Array);
  148. MAKE_PTRARG(PackedFloat32Array);
  149. MAKE_PTRARG(PackedFloat64Array);
  150. MAKE_PTRARG(PackedStringArray);
  151. MAKE_PTRARG(PackedVector2Array);
  152. MAKE_PTRARG(PackedVector3Array);
  153. MAKE_PTRARG(PackedColorArray);
  154. MAKE_PTRARG_BY_REFERENCE(Variant);
  155. // This is for Object.
  156. template <class T>
  157. struct PtrToArg<T *> {
  158. _FORCE_INLINE_ static T *convert(const void *p_ptr) {
  159. return reinterpret_cast<T *>(godot::internal::gde_interface->object_get_instance_binding(*reinterpret_cast<GDExtensionObjectPtr *>(const_cast<void *>(p_ptr)), godot::internal::token, &T::___binding_callbacks));
  160. }
  161. typedef Object *EncodeT;
  162. _FORCE_INLINE_ static void encode(T *p_var, void *p_ptr) {
  163. *reinterpret_cast<const void **>(p_ptr) = p_var ? p_var->_owner : nullptr;
  164. }
  165. };
  166. template <class T>
  167. struct PtrToArg<const T *> {
  168. _FORCE_INLINE_ static const T *convert(const void *p_ptr) {
  169. return reinterpret_cast<const T *>(godot::internal::gde_interface->object_get_instance_binding(*reinterpret_cast<GDExtensionObjectPtr *>(const_cast<void *>(p_ptr)), godot::internal::token, &T::___binding_callbacks));
  170. }
  171. typedef const Object *EncodeT;
  172. _FORCE_INLINE_ static void encode(T *p_var, void *p_ptr) {
  173. *reinterpret_cast<const void **>(p_ptr) = p_var ? p_var->_owner : nullptr;
  174. }
  175. };
  176. // Pointers.
  177. #define GDVIRTUAL_NATIVE_PTR(m_type) \
  178. template <> \
  179. struct PtrToArg<m_type *> { \
  180. _FORCE_INLINE_ static m_type *convert(const void *p_ptr) { \
  181. return (m_type *)(*(void **)p_ptr); \
  182. } \
  183. typedef m_type *EncodeT; \
  184. _FORCE_INLINE_ static void encode(m_type *p_var, void *p_ptr) { \
  185. *reinterpret_cast<m_type **>(p_ptr) = p_var; \
  186. } \
  187. }; \
  188. \
  189. template <> \
  190. struct PtrToArg<const m_type *> { \
  191. _FORCE_INLINE_ static const m_type *convert(const void *p_ptr) { \
  192. return (const m_type *)(*(const void **)p_ptr); \
  193. } \
  194. typedef const m_type *EncodeT; \
  195. _FORCE_INLINE_ static void encode(const m_type *p_var, void *p_ptr) { \
  196. *reinterpret_cast<const m_type **>(p_ptr) = p_var; \
  197. } \
  198. }
  199. GDVIRTUAL_NATIVE_PTR(void);
  200. GDVIRTUAL_NATIVE_PTR(bool);
  201. GDVIRTUAL_NATIVE_PTR(char);
  202. GDVIRTUAL_NATIVE_PTR(char16_t);
  203. GDVIRTUAL_NATIVE_PTR(char32_t);
  204. GDVIRTUAL_NATIVE_PTR(wchar_t);
  205. GDVIRTUAL_NATIVE_PTR(uint8_t);
  206. GDVIRTUAL_NATIVE_PTR(uint8_t *);
  207. GDVIRTUAL_NATIVE_PTR(int8_t);
  208. GDVIRTUAL_NATIVE_PTR(uint16_t);
  209. GDVIRTUAL_NATIVE_PTR(int16_t);
  210. GDVIRTUAL_NATIVE_PTR(uint32_t);
  211. GDVIRTUAL_NATIVE_PTR(int32_t);
  212. GDVIRTUAL_NATIVE_PTR(int64_t);
  213. GDVIRTUAL_NATIVE_PTR(uint64_t);
  214. GDVIRTUAL_NATIVE_PTR(float);
  215. GDVIRTUAL_NATIVE_PTR(double);
  216. } // namespace godot
  217. #endif // GODOT_METHOD_PTRCALL_HPP