method_ptrcall.hpp 12 KB

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