method_bind.hpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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. generate_argument_types(sizeof...(P));
  227. set_argument_count(sizeof...(P));
  228. }
  229. };
  230. template <class T, class... P>
  231. MethodBind *create_method_bind(void (T::*p_method)(P...)) {
  232. #ifdef TYPED_METHOD_BIND
  233. MethodBind *a = memnew((MethodBindT<T, P...>)(p_method));
  234. #else
  235. MethodBind *a = memnew((MethodBindT<P...>)(reinterpret_cast<void (MB_T::*)(P...)>(p_method)));
  236. #endif // TYPED_METHOD_BIND
  237. a->set_instance_class(T::get_class_static());
  238. return a;
  239. }
  240. // No return, const.
  241. #ifdef TYPED_METHOD_BIND
  242. template <class T, class... P>
  243. #else
  244. template <class... P>
  245. #endif // TYPED_METHOD_BIND
  246. class MethodBindTC : public MethodBind {
  247. void (MB_T::*method)(P...) const;
  248. protected:
  249. // GCC raises warnings in the case P = {} as the comparison is always false...
  250. #if defined(__GNUC__) && !defined(__clang__)
  251. #pragma GCC diagnostic push
  252. #pragma GCC diagnostic ignored "-Wlogical-op"
  253. #endif
  254. virtual GDNativeVariantType gen_argument_type(int p_arg) const {
  255. if (p_arg >= 0 && p_arg < (int)sizeof...(P)) {
  256. return call_get_argument_type<P...>(p_arg);
  257. } else {
  258. return GDNATIVE_VARIANT_TYPE_NIL;
  259. }
  260. }
  261. virtual GDNativePropertyInfo gen_argument_type_info(int p_arg) const {
  262. GDNativePropertyInfo pi;
  263. if (p_arg >= 0 && p_arg < (int)sizeof...(P)) {
  264. call_get_argument_type_info<P...>(p_arg, pi);
  265. } else {
  266. pi = PropertyInfo();
  267. }
  268. return pi;
  269. }
  270. #if defined(__GNUC__) && !defined(__clang__)
  271. #pragma GCC diagnostic pop
  272. #endif
  273. public:
  274. virtual GDNativeExtensionClassMethodArgumentMetadata get_argument_metadata(int p_argument) const {
  275. return call_get_argument_metadata<P...>(p_argument);
  276. }
  277. virtual Variant call(GDExtensionClassInstancePtr p_instance, const GDNativeVariantPtr *p_args, const GDNativeInt p_argument_count, GDNativeCallError &r_error) const {
  278. #ifdef TYPED_METHOD_BIND
  279. call_with_variant_argsc_dv(static_cast<T *>(p_instance), method, p_args, (int)p_argument_count, r_error, get_default_arguments());
  280. #else
  281. call_with_variant_argsc_dv(reinterpret_cast<MB_T *>(p_instance), method, p_args, p_argument_count, r_error, get_default_arguments());
  282. #endif
  283. return Variant();
  284. }
  285. virtual void ptrcall(GDExtensionClassInstancePtr p_instance, const GDNativeTypePtr *p_args, GDNativeTypePtr r_ret) const {
  286. #ifdef TYPED_METHOD_BIND
  287. call_with_ptr_args<T, P...>(static_cast<T *>(p_instance), method, p_args, nullptr);
  288. #else
  289. call_with_ptr_args<MB_T, P...>(reinterpret_cast<MB_T *>(p_instance), method, p_args, nullptr);
  290. #endif // TYPED_METHOD_BIND
  291. }
  292. MethodBindTC(void (MB_T::*p_method)(P...) const) {
  293. method = p_method;
  294. generate_argument_types(sizeof...(P));
  295. set_argument_count(sizeof...(P));
  296. }
  297. };
  298. template <class T, class... P>
  299. MethodBind *create_method_bind(void (T::*p_method)(P...) const) {
  300. #ifdef TYPED_METHOD_BIND
  301. MethodBind *a = memnew((MethodBindTC<T, P...>)(p_method));
  302. #else
  303. MethodBind *a = memnew((MethodBindTC<P...>)(reinterpret_cast<void (MB_T::*)(P...) const>(p_method)));
  304. #endif // TYPED_METHOD_BIND
  305. a->set_instance_class(T::get_class_static());
  306. return a;
  307. }
  308. // Return, not const.
  309. #ifdef TYPED_METHOD_BIND
  310. template <class T, class R, class... P>
  311. #else
  312. template <class R, class... P>
  313. #endif // TYPED_METHOD_BIND
  314. class MethodBindTR : public MethodBind {
  315. R(MB_T::*method)
  316. (P...);
  317. protected:
  318. // GCC raises warnings in the case P = {} as the comparison is always false...
  319. #if defined(__GNUC__) && !defined(__clang__)
  320. #pragma GCC diagnostic push
  321. #pragma GCC diagnostic ignored "-Wlogical-op"
  322. #endif
  323. virtual GDNativeVariantType gen_argument_type(int p_arg) const {
  324. if (p_arg >= 0 && p_arg < (int)sizeof...(P)) {
  325. return call_get_argument_type<P...>(p_arg);
  326. } else {
  327. return GetTypeInfo<R>::VARIANT_TYPE;
  328. }
  329. }
  330. virtual GDNativePropertyInfo gen_argument_type_info(int p_arg) const {
  331. if (p_arg >= 0 && p_arg < (int)sizeof...(P)) {
  332. GDNativePropertyInfo pi;
  333. call_get_argument_type_info<P...>(p_arg, pi);
  334. return pi;
  335. } else {
  336. return GetTypeInfo<R>::get_class_info();
  337. }
  338. }
  339. #if defined(__GNUC__) && !defined(__clang__)
  340. #pragma GCC diagnostic pop
  341. #endif
  342. public:
  343. virtual GDNativeExtensionClassMethodArgumentMetadata get_argument_metadata(int p_argument) const {
  344. if (p_argument >= 0) {
  345. return call_get_argument_metadata<P...>(p_argument);
  346. } else {
  347. return GetTypeInfo<R>::METADATA;
  348. }
  349. }
  350. virtual Variant call(GDExtensionClassInstancePtr p_instance, const GDNativeVariantPtr *p_args, const GDNativeInt p_argument_count, GDNativeCallError &r_error) const {
  351. Variant ret;
  352. #ifdef TYPED_METHOD_BIND
  353. call_with_variant_args_ret_dv(static_cast<T *>(p_instance), method, p_args, (int)p_argument_count, ret, r_error, get_default_arguments());
  354. #else
  355. call_with_variant_args_ret_dv((MB_T *)p_instance, method, p_args, p_argument_count, ret, r_error, get_default_arguments());
  356. #endif
  357. return ret;
  358. }
  359. virtual void ptrcall(GDExtensionClassInstancePtr p_instance, const GDNativeTypePtr *p_args, GDNativeTypePtr r_ret) const {
  360. #ifdef TYPED_METHOD_BIND
  361. call_with_ptr_args<T, R, P...>(static_cast<T *>(p_instance), method, p_args, r_ret);
  362. #else
  363. call_with_ptr_args<MB_T, R, P...>(reinterpret_cast<MB_T *>(p_instance), method, p_args, r_ret);
  364. #endif // TYPED_METHOD_BIND
  365. }
  366. MethodBindTR(R (MB_T::*p_method)(P...)) {
  367. method = p_method;
  368. generate_argument_types(sizeof...(P));
  369. set_argument_count(sizeof...(P));
  370. set_return(true);
  371. }
  372. };
  373. template <class T, class R, class... P>
  374. MethodBind *create_method_bind(R (T::*p_method)(P...)) {
  375. #ifdef TYPED_METHOD_BIND
  376. MethodBind *a = memnew((MethodBindTR<T, R, P...>)(p_method));
  377. #else
  378. MethodBind *a = memnew((MethodBindTR<R, P...>)(reinterpret_cast<R (MB_T::*)(P...)>(p_method)));
  379. #endif // TYPED_METHOD_BIND
  380. a->set_instance_class(T::get_class_static());
  381. return a;
  382. }
  383. // Return, const.
  384. #ifdef TYPED_METHOD_BIND
  385. template <class T, class R, class... P>
  386. #else
  387. template <class R, class... P>
  388. #endif // TYPED_METHOD_BIND
  389. class MethodBindTRC : public MethodBind {
  390. R(MB_T::*method)
  391. (P...) const;
  392. protected:
  393. // GCC raises warnings in the case P = {} as the comparison is always false...
  394. #if defined(__GNUC__) && !defined(__clang__)
  395. #pragma GCC diagnostic push
  396. #pragma GCC diagnostic ignored "-Wlogical-op"
  397. #endif
  398. virtual GDNativeVariantType gen_argument_type(int p_arg) const {
  399. if (p_arg >= 0 && p_arg < (int)sizeof...(P)) {
  400. return call_get_argument_type<P...>(p_arg);
  401. } else {
  402. return GetTypeInfo<R>::VARIANT_TYPE;
  403. }
  404. }
  405. virtual GDNativePropertyInfo gen_argument_type_info(int p_arg) const {
  406. if (p_arg >= 0 && p_arg < (int)sizeof...(P)) {
  407. GDNativePropertyInfo pi;
  408. call_get_argument_type_info<P...>(p_arg, pi);
  409. return pi;
  410. } else {
  411. return GetTypeInfo<R>::get_class_info();
  412. }
  413. }
  414. #if defined(__GNUC__) && !defined(__clang__)
  415. #pragma GCC diagnostic pop
  416. #endif
  417. public:
  418. virtual GDNativeExtensionClassMethodArgumentMetadata get_argument_metadata(int p_argument) const {
  419. if (p_argument >= 0) {
  420. return call_get_argument_metadata<P...>(p_argument);
  421. } else {
  422. return GetTypeInfo<R>::METADATA;
  423. }
  424. }
  425. virtual Variant call(GDExtensionClassInstancePtr p_instance, const GDNativeVariantPtr *p_args, const GDNativeInt p_argument_count, GDNativeCallError &r_error) const {
  426. Variant ret;
  427. #ifdef TYPED_METHOD_BIND
  428. call_with_variant_args_retc_dv(static_cast<T *>(p_instance), method, p_args, (int)p_argument_count, ret, r_error, get_default_arguments());
  429. #else
  430. call_with_variant_args_retc_dv((MB_T *)p_instance, method, p_args, p_argument_count, ret, r_error, get_default_arguments());
  431. #endif
  432. return ret;
  433. }
  434. virtual void ptrcall(GDExtensionClassInstancePtr p_instance, const GDNativeTypePtr *p_args, GDNativeTypePtr r_ret) const {
  435. #ifdef TYPED_METHOD_BIND
  436. call_with_ptr_args<T, R, P...>(static_cast<T *>(p_instance), method, p_args, r_ret);
  437. #else
  438. call_with_ptr_args<MB_T, R, P...>(reinterpret_cast<MB_T *>(p_instance), method, p_args, r_ret);
  439. #endif // TYPED_METHOD_BIND
  440. }
  441. MethodBindTRC(R (MB_T::*p_method)(P...) const) {
  442. method = p_method;
  443. generate_argument_types(sizeof...(P));
  444. set_argument_count(sizeof...(P));
  445. set_return(true);
  446. }
  447. };
  448. template <class T, class R, class... P>
  449. MethodBind *create_method_bind(R (T::*p_method)(P...) const) {
  450. #ifdef TYPED_METHOD_BIND
  451. MethodBind *a = memnew((MethodBindTRC<T, R, P...>)(p_method));
  452. #else
  453. MethodBind *a = memnew((MethodBindTRC<R, P...>)(reinterpret_cast<R (MB_T::*)(P...) const>(p_method)));
  454. #endif // TYPED_METHOD_BIND
  455. a->set_instance_class(T::get_class_static());
  456. return a;
  457. }
  458. } // namespace godot
  459. #endif // ! GODOT_CPP_METHOD_BIND_HPP