method_bind.hpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. /*************************************************************************/
  2. /* method_bind.hpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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_CPP_METHOD_BIND_HPP
  31. #define GODOT_CPP_METHOD_BIND_HPP
  32. #include <godot_cpp/core/binder_common.hpp>
  33. #include <godot_cpp/core/type_info.hpp>
  34. #include <godot_cpp/core/memory.hpp>
  35. #include <godot/gdnative_interface.h>
  36. #include <godot_cpp/classes/global_constants.hpp>
  37. #include <string>
  38. #include <vector>
  39. #include <iostream>
  40. namespace godot {
  41. class MethodBind {
  42. const char *name = nullptr;
  43. const char *instance_class = nullptr;
  44. int argument_count = 0;
  45. uint32_t hint_flags = METHOD_FLAGS_DEFAULT;
  46. bool _is_const = false;
  47. bool _has_return = false;
  48. std::vector<std::string> argument_names;
  49. GDNativeVariantType *argument_types = nullptr;
  50. std::vector<Variant> default_arguments;
  51. protected:
  52. virtual GDNativeVariantType gen_argument_type(int p_arg) const = 0;
  53. virtual GDNativePropertyInfo gen_argument_type_info(int p_arg) const = 0;
  54. void generate_argument_types(int p_count);
  55. void set_const(bool p_const);
  56. void set_return(bool p_return);
  57. void set_argument_count(int p_count);
  58. public:
  59. const char *get_name() const;
  60. void set_name(const char *p_name);
  61. _FORCE_INLINE_ int get_default_argument_count() const { return (int)default_arguments.size(); }
  62. _FORCE_INLINE_ const std::vector<Variant> &get_default_arguments() const { return default_arguments; }
  63. _FORCE_INLINE_ Variant has_default_argument(int p_arg) const {
  64. int idx = p_arg - (argument_count - (int)default_arguments.size());
  65. if (idx < 0 || idx >= default_arguments.size()) {
  66. return false;
  67. } else {
  68. return true;
  69. }
  70. }
  71. _FORCE_INLINE_ Variant get_default_argument(int p_arg) const {
  72. int idx = p_arg - (argument_count - (int)default_arguments.size());
  73. if (idx < 0 || idx >= default_arguments.size()) {
  74. return Variant();
  75. } else {
  76. return default_arguments[idx];
  77. }
  78. }
  79. _FORCE_INLINE_ const char *get_instance_class() const { return instance_class; }
  80. _FORCE_INLINE_ void set_instance_class(const char *p_class) { instance_class = p_class; }
  81. _FORCE_INLINE_ int get_argument_count() const { return argument_count; };
  82. _FORCE_INLINE_ bool is_const() const { return _is_const; }
  83. _FORCE_INLINE_ bool has_return() const { return _has_return; }
  84. _FORCE_INLINE_ uint32_t get_hint_flags() const { return hint_flags; }
  85. _FORCE_INLINE_ void set_hint_flags(uint32_t p_hint_flags) { hint_flags = p_hint_flags; }
  86. void set_argument_names(const std::vector<std::string> &p_names);
  87. std::vector<std::string> get_argument_names() const;
  88. void set_default_arguments(const std::vector<Variant> &p_default_arguments) { default_arguments = p_default_arguments; }
  89. _FORCE_INLINE_ GDNativeVariantType get_argument_type(int p_argument) const {
  90. ERR_FAIL_COND_V(p_argument < -1 || p_argument > argument_count, GDNATIVE_VARIANT_TYPE_NIL);
  91. return argument_types[p_argument + 1];
  92. }
  93. GDNativePropertyInfo get_argument_info(int p_argument) const;
  94. virtual GDNativeExtensionClassMethodArgumentMetadata get_argument_metadata(int p_argument) const = 0;
  95. virtual Variant call(GDExtensionClassInstancePtr p_instance, const GDNativeVariantPtr *p_args, const GDNativeInt p_argument_count, GDNativeCallError &r_error) const = 0;
  96. virtual void ptrcall(GDExtensionClassInstancePtr p_instance, const GDNativeTypePtr *p_args, GDNativeTypePtr r_return) const = 0;
  97. // Extension info.
  98. static GDNativeVariantType bind_get_argument_type(void *p_method_userdata, int32_t p_argument);
  99. static void bind_get_argument_info(void *p_method_userdata, int32_t p_argument, GDNativePropertyInfo *r_info);
  100. static GDNativeExtensionClassMethodArgumentMetadata bind_get_argument_metadata(void *p_method_userdata, int32_t p_argument);
  101. static void bind_call(void *p_method_userdata, GDExtensionClassInstancePtr p_instance, const GDNativeVariantPtr *p_args, const GDNativeInt p_argument_count, GDNativeVariantPtr r_return, GDNativeCallError *r_error);
  102. static void bind_ptrcall(void *p_method_userdata, GDExtensionClassInstancePtr p_instance, const GDNativeTypePtr *p_args, GDNativeTypePtr r_return);
  103. virtual ~MethodBind();
  104. };
  105. template <class T>
  106. class MethodBindVarArg : public MethodBind {
  107. public:
  108. typedef Variant (T::*NativeCall)(const Variant **, GDNativeInt, GDNativeCallError &);
  109. protected:
  110. NativeCall call_method = nullptr;
  111. MethodInfo arguments;
  112. public:
  113. virtual GDNativePropertyInfo gen_argument_type_info(int p_arg) const {
  114. if (p_arg < 0) {
  115. return arguments.return_val;
  116. } else if (p_arg < arguments.arguments.size()) {
  117. return arguments.arguments[p_arg];
  118. } else {
  119. return PropertyInfo(Variant::NIL, "vararg", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_NIL_IS_VARIANT);
  120. }
  121. }
  122. virtual GDNativeVariantType gen_argument_type(int p_arg) const {
  123. return static_cast<GDNativeVariantType>(gen_argument_type_info(p_arg).type);
  124. }
  125. virtual GDNativeExtensionClassMethodArgumentMetadata get_argument_metadata(int) const {
  126. return GDNATIVE_EXTENSION_METHOD_ARGUMENT_METADATA_NONE;
  127. }
  128. virtual Variant call(GDExtensionClassInstancePtr p_instance, const GDNativeVariantPtr *p_args, const GDNativeInt p_argument_count, GDNativeCallError &r_error) const {
  129. T *instance = static_cast<T *>(p_instance);
  130. return (instance->*call_method)((const Variant **)p_args, p_argument_count, r_error);
  131. }
  132. void set_method_info(const MethodInfo &p_info, bool p_return_nil_is_variant) {
  133. set_argument_count((int)p_info.arguments.size());
  134. if (p_info.arguments.size()) {
  135. std::vector<std::string> names;
  136. names.reserve(p_info.arguments.size());
  137. for (int i = 0; i < p_info.arguments.size(); i++) {
  138. names.push_back(p_info.arguments[i].name);
  139. }
  140. set_argument_names(names);
  141. }
  142. arguments = p_info;
  143. if (p_return_nil_is_variant) {
  144. arguments.return_val.usage |= PROPERTY_USAGE_NIL_IS_VARIANT;
  145. }
  146. generate_argument_types((int)p_info.arguments.size());
  147. }
  148. virtual void ptrcall(GDExtensionClassInstancePtr p_instance, const GDNativeTypePtr *p_args, GDNativeTypePtr r_return) const {
  149. ERR_FAIL(); // Can't call.
  150. }
  151. void set_method(NativeCall p_method) { call_method = p_method; }
  152. virtual bool is_const() const { return false; }
  153. virtual bool is_vararg() const { return true; }
  154. MethodBindVarArg() {
  155. set_return(true);
  156. }
  157. };
  158. template <class T>
  159. MethodBind *create_vararg_method_bind(Variant (T::*p_method)(const Variant **, GDNativeInt, GDNativeCallError &), const MethodInfo &p_info, bool p_return_nil_is_variant) {
  160. MethodBindVarArg<T> *a = memnew(MethodBindVarArg<T>());
  161. a->set_method(p_method);
  162. a->set_method_info(p_info, p_return_nil_is_variant);
  163. a->set_instance_class(T::get_class_static());
  164. return a;
  165. }
  166. #ifndef TYPED_METHOD_BIND
  167. class ___UnexistingClass;
  168. #define MB_T ___UnexistingClass
  169. #else
  170. #define MB_T T
  171. #endif
  172. // No return, not const.
  173. #ifdef TYPED_METHOD_BIND
  174. template <class T, class... P>
  175. #else
  176. template <class... P>
  177. #endif // TYPED_METHOD_BIND
  178. class MethodBindT : public MethodBind {
  179. void (MB_T::*method)(P...);
  180. protected:
  181. // GCC raises warnings in the case P = {} as the comparison is always false...
  182. #if defined(__GNUC__) && !defined(__clang__)
  183. #pragma GCC diagnostic push
  184. #pragma GCC diagnostic ignored "-Wlogical-op"
  185. #endif
  186. virtual GDNativeVariantType gen_argument_type(int p_arg) const {
  187. if (p_arg >= 0 && p_arg < (int)sizeof...(P)) {
  188. return call_get_argument_type<P...>(p_arg);
  189. } else {
  190. return GDNATIVE_VARIANT_TYPE_NIL;
  191. }
  192. }
  193. virtual GDNativePropertyInfo gen_argument_type_info(int p_arg) const {
  194. GDNativePropertyInfo pi;
  195. if (p_arg >= 0 && p_arg < (int)sizeof...(P)) {
  196. call_get_argument_type_info<P...>(p_arg, pi);
  197. } else {
  198. pi = PropertyInfo();
  199. }
  200. return pi;
  201. }
  202. #if defined(__GNUC__) && !defined(__clang__)
  203. #pragma GCC diagnostic pop
  204. #endif
  205. public:
  206. virtual GDNativeExtensionClassMethodArgumentMetadata get_argument_metadata(int p_argument) const {
  207. return call_get_argument_metadata<P...>(p_argument);
  208. }
  209. virtual Variant call(GDExtensionClassInstancePtr p_instance, const GDNativeVariantPtr *p_args, const GDNativeInt p_argument_count, GDNativeCallError &r_error) const {
  210. #ifdef TYPED_METHOD_BIND
  211. call_with_variant_args_dv(static_cast<T *>(p_instance), method, p_args, (int)p_argument_count, r_error, get_default_arguments());
  212. #else
  213. call_with_variant_args_dv(reinterpret_cast<MB_T *>(p_instance), method, p_args, p_argument_count, r_error, get_default_arguments());
  214. #endif
  215. return Variant();
  216. }
  217. virtual void ptrcall(GDExtensionClassInstancePtr p_instance, const GDNativeTypePtr *p_args, GDNativeTypePtr r_ret) const {
  218. #ifdef TYPED_METHOD_BIND
  219. call_with_ptr_args<T, P...>(static_cast<T *>(p_instance), method, p_args, nullptr);
  220. #else
  221. call_with_ptr_args<MB_T, P...>(reinterpret_cast<MB_T *>(p_instance), method, p_args, nullptr);
  222. #endif // TYPED_METHOD_BIND
  223. }
  224. MethodBindT(void (MB_T::*p_method)(P...)) {
  225. method = p_method;
  226. #ifdef DEBUG_METHODS_ENABLED
  227. generate_argument_types(sizeof...(P));
  228. #endif // DEBUG_METHODS_ENABLED
  229. set_argument_count(sizeof...(P));
  230. }
  231. };
  232. template <class T, class... P>
  233. MethodBind *create_method_bind(void (T::*p_method)(P...)) {
  234. #ifdef TYPED_METHOD_BIND
  235. MethodBind *a = memnew((MethodBindT<T, P...>)(p_method));
  236. #else
  237. MethodBind *a = memnew((MethodBindT<P...>)(reinterpret_cast<void (MB_T::*)(P...)>(p_method)));
  238. #endif // TYPED_METHOD_BIND
  239. a->set_instance_class(T::get_class_static());
  240. return a;
  241. }
  242. // No return, const.
  243. #ifdef TYPED_METHOD_BIND
  244. template <class T, class... P>
  245. #else
  246. template <class... P>
  247. #endif // TYPED_METHOD_BIND
  248. class MethodBindTC : public MethodBind {
  249. void (MB_T::*method)(P...) const;
  250. protected:
  251. // GCC raises warnings in the case P = {} as the comparison is always false...
  252. #if defined(__GNUC__) && !defined(__clang__)
  253. #pragma GCC diagnostic push
  254. #pragma GCC diagnostic ignored "-Wlogical-op"
  255. #endif
  256. virtual GDNativeVariantType gen_argument_type(int p_arg) const {
  257. if (p_arg >= 0 && p_arg < (int)sizeof...(P)) {
  258. return call_get_argument_type<P...>(p_arg);
  259. } else {
  260. return GDNATIVE_VARIANT_TYPE_NIL;
  261. }
  262. }
  263. virtual GDNativePropertyInfo gen_argument_type_info(int p_arg) const {
  264. GDNativePropertyInfo pi;
  265. if (p_arg >= 0 && p_arg < (int)sizeof...(P)) {
  266. call_get_argument_type_info<P...>(p_arg, pi);
  267. } else {
  268. pi = PropertyInfo();
  269. }
  270. return pi;
  271. }
  272. #if defined(__GNUC__) && !defined(__clang__)
  273. #pragma GCC diagnostic pop
  274. #endif
  275. public:
  276. virtual GDNativeExtensionClassMethodArgumentMetadata get_argument_metadata(int p_argument) const {
  277. return call_get_argument_metadata<P...>(p_argument);
  278. }
  279. virtual Variant call(GDExtensionClassInstancePtr p_instance, const GDNativeVariantPtr *p_args, const GDNativeInt p_argument_count, GDNativeCallError &r_error) const {
  280. #ifdef TYPED_METHOD_BIND
  281. call_with_variant_argsc_dv(static_cast<T *>(p_instance), method, p_args, (int)p_argument_count, r_error, get_default_arguments());
  282. #else
  283. call_with_variant_argsc_dv(reinterpret_cast<MB_T *>(p_instance), method, p_args, p_argument_count, r_error, get_default_arguments());
  284. #endif
  285. return Variant();
  286. }
  287. virtual void ptrcall(GDExtensionClassInstancePtr p_instance, const GDNativeTypePtr *p_args, GDNativeTypePtr r_ret) const {
  288. #ifdef TYPED_METHOD_BIND
  289. call_with_ptr_args<T, P...>(static_cast<T *>(p_instance), method, p_args, nullptr);
  290. #else
  291. call_with_ptr_args<MB_T, P...>(reinterpret_cast<MB_T *>(p_instance), method, p_args, nullptr);
  292. #endif // TYPED_METHOD_BIND
  293. }
  294. MethodBindTC(void (MB_T::*p_method)(P...) const) {
  295. method = p_method;
  296. #ifdef DEBUG_METHODS_ENABLED
  297. generate_argument_types(sizeof...(P));
  298. #endif // DEBUG_METHODS_ENABLED
  299. set_argument_count(sizeof...(P));
  300. }
  301. };
  302. template <class T, class... P>
  303. MethodBind *create_method_bind(void (T::*p_method)(P...) const) {
  304. #ifdef TYPED_METHOD_BIND
  305. MethodBind *a = memnew((MethodBindTC<T, P...>)(p_method));
  306. #else
  307. MethodBind *a = memnew((MethodBindTC<P...>)(reinterpret_cast<void (MB_T::*)(P...) const>(p_method)));
  308. #endif // TYPED_METHOD_BIND
  309. a->set_instance_class(T::get_class_static());
  310. return a;
  311. }
  312. // Return, not const.
  313. #ifdef TYPED_METHOD_BIND
  314. template <class T, class R, class... P>
  315. #else
  316. template <class R, class... P>
  317. #endif // TYPED_METHOD_BIND
  318. class MethodBindTR : public MethodBind {
  319. R(MB_T::*method)
  320. (P...);
  321. protected:
  322. // GCC raises warnings in the case P = {} as the comparison is always false...
  323. #if defined(__GNUC__) && !defined(__clang__)
  324. #pragma GCC diagnostic push
  325. #pragma GCC diagnostic ignored "-Wlogical-op"
  326. #endif
  327. virtual GDNativeVariantType gen_argument_type(int p_arg) const {
  328. if (p_arg >= 0 && p_arg < (int)sizeof...(P)) {
  329. return call_get_argument_type<P...>(p_arg);
  330. } else {
  331. return GetTypeInfo<R>::VARIANT_TYPE;
  332. }
  333. }
  334. virtual GDNativePropertyInfo gen_argument_type_info(int p_arg) const {
  335. if (p_arg >= 0 && p_arg < (int)sizeof...(P)) {
  336. GDNativePropertyInfo pi;
  337. call_get_argument_type_info<P...>(p_arg, pi);
  338. return pi;
  339. } else {
  340. return GetTypeInfo<R>::get_class_info();
  341. }
  342. }
  343. #if defined(__GNUC__) && !defined(__clang__)
  344. #pragma GCC diagnostic pop
  345. #endif
  346. public:
  347. virtual GDNativeExtensionClassMethodArgumentMetadata get_argument_metadata(int p_argument) const {
  348. if (p_argument >= 0) {
  349. return call_get_argument_metadata<P...>(p_argument);
  350. } else {
  351. return GetTypeInfo<R>::METADATA;
  352. }
  353. }
  354. virtual Variant call(GDExtensionClassInstancePtr p_instance, const GDNativeVariantPtr *p_args, const GDNativeInt p_argument_count, GDNativeCallError &r_error) const {
  355. Variant ret;
  356. #ifdef TYPED_METHOD_BIND
  357. call_with_variant_args_ret_dv(static_cast<T *>(p_instance), method, p_args, (int)p_argument_count, ret, r_error, get_default_arguments());
  358. #else
  359. call_with_variant_args_ret_dv((MB_T *)p_instance, method, p_args, p_argument_count, ret, r_error, get_default_arguments());
  360. #endif
  361. return ret;
  362. }
  363. virtual void ptrcall(GDExtensionClassInstancePtr p_instance, const GDNativeTypePtr *p_args, GDNativeTypePtr r_ret) const {
  364. #ifdef TYPED_METHOD_BIND
  365. call_with_ptr_args<T, R, P...>(static_cast<T *>(p_instance), method, p_args, r_ret);
  366. #else
  367. call_with_ptr_args<MB_T, R, P...>(reinterpret_cast<MB_T *>(p_instance), method, p_args, r_ret);
  368. #endif // TYPED_METHOD_BIND
  369. }
  370. MethodBindTR(R (MB_T::*p_method)(P...)) {
  371. method = p_method;
  372. #ifdef DEBUG_METHODS_ENABLED
  373. generate_argument_types(sizeof...(P));
  374. #endif // DEBUG_METHODS_ENABLED
  375. set_argument_count(sizeof...(P));
  376. set_return(true);
  377. }
  378. };
  379. template <class T, class R, class... P>
  380. MethodBind *create_method_bind(R (T::*p_method)(P...)) {
  381. #ifdef TYPED_METHOD_BIND
  382. MethodBind *a = memnew((MethodBindTR<T, R, P...>)(p_method));
  383. #else
  384. MethodBind *a = memnew((MethodBindTR<R, P...>)(reinterpret_cast<R (MB_T::*)(P...)>(p_method)));
  385. #endif // TYPED_METHOD_BIND
  386. a->set_instance_class(T::get_class_static());
  387. return a;
  388. }
  389. // Return, const.
  390. #ifdef TYPED_METHOD_BIND
  391. template <class T, class R, class... P>
  392. #else
  393. template <class R, class... P>
  394. #endif // TYPED_METHOD_BIND
  395. class MethodBindTRC : public MethodBind {
  396. R(MB_T::*method)
  397. (P...) const;
  398. protected:
  399. // GCC raises warnings in the case P = {} as the comparison is always false...
  400. #if defined(__GNUC__) && !defined(__clang__)
  401. #pragma GCC diagnostic push
  402. #pragma GCC diagnostic ignored "-Wlogical-op"
  403. #endif
  404. virtual GDNativeVariantType gen_argument_type(int p_arg) const {
  405. if (p_arg >= 0 && p_arg < (int)sizeof...(P)) {
  406. return call_get_argument_type<P...>(p_arg);
  407. } else {
  408. return GetTypeInfo<R>::VARIANT_TYPE;
  409. }
  410. }
  411. virtual GDNativePropertyInfo gen_argument_type_info(int p_arg) const {
  412. if (p_arg >= 0 && p_arg < (int)sizeof...(P)) {
  413. GDNativePropertyInfo pi;
  414. call_get_argument_type_info<P...>(p_arg, pi);
  415. return pi;
  416. } else {
  417. return GetTypeInfo<R>::get_class_info();
  418. }
  419. }
  420. #if defined(__GNUC__) && !defined(__clang__)
  421. #pragma GCC diagnostic pop
  422. #endif
  423. public:
  424. virtual GDNativeExtensionClassMethodArgumentMetadata get_argument_metadata(int p_argument) const {
  425. if (p_argument >= 0) {
  426. return call_get_argument_metadata<P...>(p_argument);
  427. } else {
  428. return GetTypeInfo<R>::METADATA;
  429. }
  430. }
  431. virtual Variant call(GDExtensionClassInstancePtr p_instance, const GDNativeVariantPtr *p_args, const GDNativeInt p_argument_count, GDNativeCallError &r_error) const {
  432. Variant ret;
  433. #ifdef TYPED_METHOD_BIND
  434. call_with_variant_args_retc_dv(static_cast<T *>(p_instance), method, p_args, (int)p_argument_count, ret, r_error, get_default_arguments());
  435. #else
  436. call_with_variant_args_retc_dv((MB_T *)p_instance, method, p_args, p_argument_count, ret, r_error, get_default_arguments());
  437. #endif
  438. return ret;
  439. }
  440. virtual void ptrcall(GDExtensionClassInstancePtr p_instance, const GDNativeTypePtr *p_args, GDNativeTypePtr r_ret) const {
  441. #ifdef TYPED_METHOD_BIND
  442. call_with_ptr_args<T, R, P...>(static_cast<T *>(p_instance), method, p_args, r_ret);
  443. #else
  444. call_with_ptr_args<MB_T, R, P...>(reinterpret_cast<MB_T *>(p_instance), method, p_args, r_ret);
  445. #endif // TYPED_METHOD_BIND
  446. }
  447. MethodBindTRC(R (MB_T::*p_method)(P...) const) {
  448. method = p_method;
  449. #ifdef DEBUG_METHODS_ENABLED
  450. generate_argument_types(sizeof...(P));
  451. #endif // DEBUG_METHODS_ENABLED
  452. set_argument_count(sizeof...(P));
  453. set_return(true);
  454. }
  455. };
  456. template <class T, class R, class... P>
  457. MethodBind *create_method_bind(R (T::*p_method)(P...) const) {
  458. #ifdef TYPED_METHOD_BIND
  459. MethodBind *a = memnew((MethodBindTRC<T, R, P...>)(p_method));
  460. #else
  461. MethodBind *a = memnew((MethodBindTRC<R, P...>)(reinterpret_cast<R (MB_T::*)(P...) const>(p_method)));
  462. #endif // TYPED_METHOD_BIND
  463. a->set_instance_class(T::get_class_static());
  464. return a;
  465. }
  466. } // namespace godot
  467. #endif // ! GODOT_CPP_METHOD_BIND_HPP