method_bind.hpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  1. /*************************************************************************/
  2. /* method_bind.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_BIND_HPP
  31. #define GODOT_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. StringName name;
  43. StringName instance_class;
  44. int argument_count = 0;
  45. uint32_t hint_flags = METHOD_FLAGS_DEFAULT;
  46. bool _static = false;
  47. bool _is_const = false;
  48. bool _has_return = false;
  49. bool _vararg = false;
  50. std::vector<StringName> argument_names;
  51. GDNativeVariantType *argument_types = nullptr;
  52. std::vector<Variant> default_arguments;
  53. protected:
  54. virtual GDNativeVariantType gen_argument_type(int p_arg) const = 0;
  55. virtual PropertyInfo gen_argument_type_info(int p_arg) const = 0;
  56. void generate_argument_types(int p_count);
  57. void set_const(bool p_const);
  58. void set_return(bool p_return);
  59. void set_static(bool p_static);
  60. void set_vararg(bool p_vararg);
  61. void set_argument_count(int p_count);
  62. public:
  63. StringName get_name() const;
  64. void set_name(const StringName &p_name);
  65. _FORCE_INLINE_ int get_default_argument_count() const { return (int)default_arguments.size(); }
  66. _FORCE_INLINE_ const std::vector<Variant> &get_default_arguments() const { return default_arguments; }
  67. _FORCE_INLINE_ Variant has_default_argument(int p_arg) const {
  68. int idx = p_arg - (argument_count - (int)default_arguments.size());
  69. if (idx < 0 || idx >= default_arguments.size()) {
  70. return false;
  71. } else {
  72. return true;
  73. }
  74. }
  75. _FORCE_INLINE_ Variant get_default_argument(int p_arg) const {
  76. int idx = p_arg - (argument_count - (int)default_arguments.size());
  77. if (idx < 0 || idx >= default_arguments.size()) {
  78. return Variant();
  79. } else {
  80. return default_arguments[idx];
  81. }
  82. }
  83. _FORCE_INLINE_ StringName get_instance_class() const { return instance_class; }
  84. _FORCE_INLINE_ void set_instance_class(StringName p_class) { instance_class = p_class; }
  85. _FORCE_INLINE_ int get_argument_count() const { return argument_count; };
  86. _FORCE_INLINE_ bool is_const() const { return _is_const; }
  87. _FORCE_INLINE_ bool is_static() const { return _static; }
  88. _FORCE_INLINE_ bool is_vararg() const { return _vararg; }
  89. _FORCE_INLINE_ bool has_return() const { return _has_return; }
  90. _FORCE_INLINE_ uint32_t get_hint_flags() const { return hint_flags | (is_const() ? GDNATIVE_EXTENSION_METHOD_FLAG_CONST : 0) | (is_vararg() ? GDNATIVE_EXTENSION_METHOD_FLAG_VARARG : 0) | (is_static() ? GDNATIVE_EXTENSION_METHOD_FLAG_STATIC : 0); }
  91. _FORCE_INLINE_ void set_hint_flags(uint32_t p_hint_flags) { hint_flags = p_hint_flags; }
  92. void set_argument_names(const std::vector<StringName> &p_names);
  93. std::vector<StringName> get_argument_names() const;
  94. void set_default_arguments(const std::vector<Variant> &p_default_arguments) { default_arguments = p_default_arguments; }
  95. _FORCE_INLINE_ GDNativeVariantType get_argument_type(int p_argument) const {
  96. ERR_FAIL_COND_V(p_argument < -1 || p_argument > argument_count, GDNATIVE_VARIANT_TYPE_NIL);
  97. return argument_types[p_argument + 1];
  98. }
  99. PropertyInfo get_argument_info(int p_argument) const;
  100. virtual GDNativeExtensionClassMethodArgumentMetadata get_argument_metadata(int p_argument) const = 0;
  101. std::vector<PropertyInfo> get_arguments_info_list() const {
  102. std::vector<PropertyInfo> vec;
  103. // First element is return value
  104. vec.reserve(argument_count + 1);
  105. for (int i = 0; i < argument_count; i++) {
  106. vec.push_back(get_argument_info(i - 1));
  107. }
  108. return vec;
  109. }
  110. std::vector<GDNativeExtensionClassMethodArgumentMetadata> get_arguments_metadata_list() const {
  111. std::vector<GDNativeExtensionClassMethodArgumentMetadata> vec;
  112. // First element is return value
  113. vec.reserve(argument_count + 1);
  114. for (int i = 0; i < argument_count; i++) {
  115. vec.push_back(get_argument_metadata(i - 1));
  116. }
  117. return vec;
  118. }
  119. virtual Variant call(GDExtensionClassInstancePtr p_instance, const GDNativeVariantPtr *p_args, const GDNativeInt p_argument_count, GDNativeCallError &r_error) const = 0;
  120. virtual void ptrcall(GDExtensionClassInstancePtr p_instance, const GDNativeTypePtr *p_args, GDNativeTypePtr r_return) const = 0;
  121. 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);
  122. static void bind_ptrcall(void *p_method_userdata, GDExtensionClassInstancePtr p_instance, const GDNativeTypePtr *p_args, GDNativeTypePtr r_return);
  123. virtual ~MethodBind();
  124. };
  125. template <class Derived, class T, class R, bool should_returns>
  126. class MethodBindVarArgBase : public MethodBind {
  127. protected:
  128. R(T::*method)
  129. (const Variant **, GDNativeInt, GDNativeCallError &);
  130. std::vector<PropertyInfo> arguments;
  131. public:
  132. virtual PropertyInfo gen_argument_type_info(int p_arg) const {
  133. if (p_arg < 0) {
  134. return _gen_return_type_info();
  135. } else if (p_arg < arguments.size()) {
  136. return arguments[p_arg];
  137. } else {
  138. return make_property_info(Variant::Type::NIL, "vararg", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_NIL_IS_VARIANT);
  139. }
  140. }
  141. virtual GDNativeVariantType gen_argument_type(int p_arg) const {
  142. return static_cast<GDNativeVariantType>(gen_argument_type_info(p_arg).type);
  143. }
  144. virtual GDNativeExtensionClassMethodArgumentMetadata get_argument_metadata(int) const {
  145. return GDNATIVE_EXTENSION_METHOD_ARGUMENT_METADATA_NONE;
  146. }
  147. virtual void ptrcall(GDExtensionClassInstancePtr p_instance, const GDNativeTypePtr *p_args, GDNativeTypePtr r_return) const {
  148. ERR_FAIL(); // Can't call.
  149. }
  150. static _FORCE_INLINE_ char *_alloc_and_copy_cstr(const char *p_str) {
  151. size_t size = strlen(p_str) + 1;
  152. char *ret = reinterpret_cast<char *>(memalloc(size));
  153. memcpy(ret, p_str, size);
  154. return ret;
  155. }
  156. MethodBindVarArgBase(
  157. R (T::*p_method)(const Variant **, GDNativeInt, GDNativeCallError &),
  158. const MethodInfo &p_method_info,
  159. bool p_return_nil_is_variant) :
  160. method(p_method) {
  161. set_vararg(true);
  162. set_const(true);
  163. set_argument_count(p_method_info.arguments.size());
  164. if (p_method_info.arguments.size()) {
  165. arguments = p_method_info.arguments;
  166. std::vector<StringName> names;
  167. names.reserve(p_method_info.arguments.size());
  168. for (int i = 0; i < p_method_info.arguments.size(); i++) {
  169. names.push_back(p_method_info.arguments[i].name);
  170. }
  171. set_argument_names(names);
  172. }
  173. generate_argument_types((int)p_method_info.arguments.size());
  174. set_return(should_returns);
  175. }
  176. ~MethodBindVarArgBase() {}
  177. private:
  178. PropertyInfo _gen_return_type_info() const {
  179. return reinterpret_cast<const Derived *>(this)->_gen_return_type_info_impl();
  180. }
  181. };
  182. template <class T>
  183. class MethodBindVarArgT : public MethodBindVarArgBase<MethodBindVarArgT<T>, T, void, false> {
  184. friend class MethodBindVarArgBase<MethodBindVarArgT<T>, T, void, false>;
  185. public:
  186. virtual Variant call(GDExtensionClassInstancePtr p_instance, const GDNativeVariantPtr *p_args, const GDNativeInt p_argument_count, GDNativeCallError &r_error) const {
  187. (static_cast<T *>(p_instance)->*MethodBindVarArgBase<MethodBindVarArgT<T>, T, void, false>::method)((const Variant **)p_args, p_argument_count, r_error);
  188. return {};
  189. }
  190. MethodBindVarArgT(
  191. void (T::*p_method)(const Variant **, GDNativeInt, GDNativeCallError &),
  192. const MethodInfo &p_method_info,
  193. bool p_return_nil_is_variant) :
  194. MethodBindVarArgBase<MethodBindVarArgT<T>, T, void, false>(p_method, p_method_info, p_return_nil_is_variant) {
  195. }
  196. private:
  197. PropertyInfo _gen_return_type_info_impl() const {
  198. return {};
  199. }
  200. };
  201. template <class T>
  202. MethodBind *create_vararg_method_bind(void (T::*p_method)(const Variant **, GDNativeInt, GDNativeCallError &), const MethodInfo &p_info, bool p_return_nil_is_variant) {
  203. MethodBind *a = memnew((MethodBindVarArgT<T>)(p_method, p_info, p_return_nil_is_variant));
  204. a->set_instance_class(T::get_class_static());
  205. return a;
  206. }
  207. template <class T, class R>
  208. class MethodBindVarArgTR : public MethodBindVarArgBase<MethodBindVarArgTR<T, R>, T, R, true> {
  209. friend class MethodBindVarArgBase<MethodBindVarArgTR<T, R>, T, R, true>;
  210. public:
  211. virtual Variant call(GDExtensionClassInstancePtr p_instance, const GDNativeVariantPtr *p_args, const GDNativeInt p_argument_count, GDNativeCallError &r_error) const {
  212. return (static_cast<T *>(p_instance)->*MethodBindVarArgBase<MethodBindVarArgTR<T, R>, T, R, true>::method)((const Variant **)p_args, p_argument_count, r_error);
  213. }
  214. MethodBindVarArgTR(
  215. R (T::*p_method)(const Variant **, GDNativeInt, GDNativeCallError &),
  216. const MethodInfo &p_info,
  217. bool p_return_nil_is_variant) :
  218. MethodBindVarArgBase<MethodBindVarArgTR<T, R>, T, R, true>(p_method, p_info, p_return_nil_is_variant) {
  219. }
  220. private:
  221. PropertyInfo _gen_return_type_info_impl() const {
  222. return GetTypeInfo<R>::get_class_info();
  223. }
  224. };
  225. template <class T, class R>
  226. MethodBind *create_vararg_method_bind(R (T::*p_method)(const Variant **, GDNativeInt, GDNativeCallError &), const MethodInfo &p_info, bool p_return_nil_is_variant) {
  227. MethodBind *a = memnew((MethodBindVarArgTR<T, R>)(p_method, p_info, p_return_nil_is_variant));
  228. a->set_instance_class(T::get_class_static());
  229. return a;
  230. }
  231. #ifndef TYPED_METHOD_BIND
  232. class ___UnexistingClass;
  233. #define MB_T ___UnexistingClass
  234. #else
  235. #define MB_T T
  236. #endif
  237. // No return, not const.
  238. #ifdef TYPED_METHOD_BIND
  239. template <class T, class... P>
  240. #else
  241. template <class... P>
  242. #endif // TYPED_METHOD_BIND
  243. class MethodBindT : public MethodBind {
  244. void (MB_T::*method)(P...);
  245. protected:
  246. // GCC raises warnings in the case P = {} as the comparison is always false...
  247. #if defined(__GNUC__) && !defined(__clang__)
  248. #pragma GCC diagnostic push
  249. #pragma GCC diagnostic ignored "-Wlogical-op"
  250. #endif
  251. virtual GDNativeVariantType gen_argument_type(int p_arg) const {
  252. if (p_arg >= 0 && p_arg < (int)sizeof...(P)) {
  253. return call_get_argument_type<P...>(p_arg);
  254. } else {
  255. return GDNATIVE_VARIANT_TYPE_NIL;
  256. }
  257. }
  258. virtual PropertyInfo gen_argument_type_info(int p_arg) const {
  259. PropertyInfo pi;
  260. if (p_arg >= 0 && p_arg < (int)sizeof...(P)) {
  261. call_get_argument_type_info<P...>(p_arg, pi);
  262. } else {
  263. pi = PropertyInfo();
  264. }
  265. return pi;
  266. }
  267. #if defined(__GNUC__) && !defined(__clang__)
  268. #pragma GCC diagnostic pop
  269. #endif
  270. public:
  271. virtual GDNativeExtensionClassMethodArgumentMetadata get_argument_metadata(int p_argument) const {
  272. return call_get_argument_metadata<P...>(p_argument);
  273. }
  274. virtual Variant call(GDExtensionClassInstancePtr p_instance, const GDNativeVariantPtr *p_args, const GDNativeInt p_argument_count, GDNativeCallError &r_error) const {
  275. #ifdef TYPED_METHOD_BIND
  276. call_with_variant_args_dv(static_cast<T *>(p_instance), method, p_args, (int)p_argument_count, r_error, get_default_arguments());
  277. #else
  278. call_with_variant_args_dv(reinterpret_cast<MB_T *>(p_instance), method, p_args, p_argument_count, r_error, get_default_arguments());
  279. #endif
  280. return Variant();
  281. }
  282. virtual void ptrcall(GDExtensionClassInstancePtr p_instance, const GDNativeTypePtr *p_args, GDNativeTypePtr r_ret) const {
  283. #ifdef TYPED_METHOD_BIND
  284. call_with_ptr_args<T, P...>(static_cast<T *>(p_instance), method, p_args, nullptr);
  285. #else
  286. call_with_ptr_args<MB_T, P...>(reinterpret_cast<MB_T *>(p_instance), method, p_args, nullptr);
  287. #endif // TYPED_METHOD_BIND
  288. }
  289. MethodBindT(void (MB_T::*p_method)(P...)) {
  290. method = p_method;
  291. generate_argument_types(sizeof...(P));
  292. set_argument_count(sizeof...(P));
  293. }
  294. };
  295. template <class T, class... P>
  296. MethodBind *create_method_bind(void (T::*p_method)(P...)) {
  297. #ifdef TYPED_METHOD_BIND
  298. MethodBind *a = memnew((MethodBindT<T, P...>)(p_method));
  299. #else
  300. MethodBind *a = memnew((MethodBindT<P...>)(reinterpret_cast<void (MB_T::*)(P...)>(p_method)));
  301. #endif // TYPED_METHOD_BIND
  302. a->set_instance_class(T::get_class_static());
  303. return a;
  304. }
  305. // No return, const.
  306. #ifdef TYPED_METHOD_BIND
  307. template <class T, class... P>
  308. #else
  309. template <class... P>
  310. #endif // TYPED_METHOD_BIND
  311. class MethodBindTC : public MethodBind {
  312. void (MB_T::*method)(P...) const;
  313. protected:
  314. // GCC raises warnings in the case P = {} as the comparison is always false...
  315. #if defined(__GNUC__) && !defined(__clang__)
  316. #pragma GCC diagnostic push
  317. #pragma GCC diagnostic ignored "-Wlogical-op"
  318. #endif
  319. virtual GDNativeVariantType gen_argument_type(int p_arg) const {
  320. if (p_arg >= 0 && p_arg < (int)sizeof...(P)) {
  321. return call_get_argument_type<P...>(p_arg);
  322. } else {
  323. return GDNATIVE_VARIANT_TYPE_NIL;
  324. }
  325. }
  326. virtual PropertyInfo gen_argument_type_info(int p_arg) const {
  327. PropertyInfo pi;
  328. if (p_arg >= 0 && p_arg < (int)sizeof...(P)) {
  329. call_get_argument_type_info<P...>(p_arg, pi);
  330. } else {
  331. pi = PropertyInfo();
  332. }
  333. return pi;
  334. }
  335. #if defined(__GNUC__) && !defined(__clang__)
  336. #pragma GCC diagnostic pop
  337. #endif
  338. public:
  339. virtual GDNativeExtensionClassMethodArgumentMetadata get_argument_metadata(int p_argument) const {
  340. return call_get_argument_metadata<P...>(p_argument);
  341. }
  342. virtual Variant call(GDExtensionClassInstancePtr p_instance, const GDNativeVariantPtr *p_args, const GDNativeInt p_argument_count, GDNativeCallError &r_error) const {
  343. #ifdef TYPED_METHOD_BIND
  344. call_with_variant_argsc_dv(static_cast<T *>(p_instance), method, p_args, (int)p_argument_count, r_error, get_default_arguments());
  345. #else
  346. call_with_variant_argsc_dv(reinterpret_cast<MB_T *>(p_instance), method, p_args, p_argument_count, r_error, get_default_arguments());
  347. #endif
  348. return Variant();
  349. }
  350. virtual void ptrcall(GDExtensionClassInstancePtr p_instance, const GDNativeTypePtr *p_args, GDNativeTypePtr r_ret) const {
  351. #ifdef TYPED_METHOD_BIND
  352. call_with_ptr_args<T, P...>(static_cast<T *>(p_instance), method, p_args, nullptr);
  353. #else
  354. call_with_ptr_args<MB_T, P...>(reinterpret_cast<MB_T *>(p_instance), method, p_args, nullptr);
  355. #endif // TYPED_METHOD_BIND
  356. }
  357. MethodBindTC(void (MB_T::*p_method)(P...) const) {
  358. method = p_method;
  359. generate_argument_types(sizeof...(P));
  360. set_argument_count(sizeof...(P));
  361. }
  362. };
  363. template <class T, class... P>
  364. MethodBind *create_method_bind(void (T::*p_method)(P...) const) {
  365. #ifdef TYPED_METHOD_BIND
  366. MethodBind *a = memnew((MethodBindTC<T, P...>)(p_method));
  367. #else
  368. MethodBind *a = memnew((MethodBindTC<P...>)(reinterpret_cast<void (MB_T::*)(P...) const>(p_method)));
  369. #endif // TYPED_METHOD_BIND
  370. a->set_instance_class(T::get_class_static());
  371. return a;
  372. }
  373. // Return, not const.
  374. #ifdef TYPED_METHOD_BIND
  375. template <class T, class R, class... P>
  376. #else
  377. template <class R, class... P>
  378. #endif // TYPED_METHOD_BIND
  379. class MethodBindTR : public MethodBind {
  380. R(MB_T::*method)
  381. (P...);
  382. protected:
  383. // GCC raises warnings in the case P = {} as the comparison is always false...
  384. #if defined(__GNUC__) && !defined(__clang__)
  385. #pragma GCC diagnostic push
  386. #pragma GCC diagnostic ignored "-Wlogical-op"
  387. #endif
  388. virtual GDNativeVariantType gen_argument_type(int p_arg) const {
  389. if (p_arg >= 0 && p_arg < (int)sizeof...(P)) {
  390. return call_get_argument_type<P...>(p_arg);
  391. } else {
  392. return GDNativeVariantType(GetTypeInfo<R>::VARIANT_TYPE);
  393. }
  394. }
  395. virtual PropertyInfo gen_argument_type_info(int p_arg) const {
  396. if (p_arg >= 0 && p_arg < (int)sizeof...(P)) {
  397. PropertyInfo pi;
  398. call_get_argument_type_info<P...>(p_arg, pi);
  399. return pi;
  400. } else {
  401. return GetTypeInfo<R>::get_class_info();
  402. }
  403. }
  404. #if defined(__GNUC__) && !defined(__clang__)
  405. #pragma GCC diagnostic pop
  406. #endif
  407. public:
  408. virtual GDNativeExtensionClassMethodArgumentMetadata get_argument_metadata(int p_argument) const {
  409. if (p_argument >= 0) {
  410. return call_get_argument_metadata<P...>(p_argument);
  411. } else {
  412. return GetTypeInfo<R>::METADATA;
  413. }
  414. }
  415. virtual Variant call(GDExtensionClassInstancePtr p_instance, const GDNativeVariantPtr *p_args, const GDNativeInt p_argument_count, GDNativeCallError &r_error) const {
  416. Variant ret;
  417. #ifdef TYPED_METHOD_BIND
  418. call_with_variant_args_ret_dv(static_cast<T *>(p_instance), method, p_args, (int)p_argument_count, ret, r_error, get_default_arguments());
  419. #else
  420. call_with_variant_args_ret_dv((MB_T *)p_instance, method, p_args, p_argument_count, ret, r_error, get_default_arguments());
  421. #endif
  422. return ret;
  423. }
  424. virtual void ptrcall(GDExtensionClassInstancePtr p_instance, const GDNativeTypePtr *p_args, GDNativeTypePtr r_ret) const {
  425. #ifdef TYPED_METHOD_BIND
  426. call_with_ptr_args<T, R, P...>(static_cast<T *>(p_instance), method, p_args, r_ret);
  427. #else
  428. call_with_ptr_args<MB_T, R, P...>(reinterpret_cast<MB_T *>(p_instance), method, p_args, r_ret);
  429. #endif // TYPED_METHOD_BIND
  430. }
  431. MethodBindTR(R (MB_T::*p_method)(P...)) {
  432. method = p_method;
  433. generate_argument_types(sizeof...(P));
  434. set_argument_count(sizeof...(P));
  435. set_return(true);
  436. }
  437. };
  438. template <class T, class R, class... P>
  439. MethodBind *create_method_bind(R (T::*p_method)(P...)) {
  440. #ifdef TYPED_METHOD_BIND
  441. MethodBind *a = memnew((MethodBindTR<T, R, P...>)(p_method));
  442. #else
  443. MethodBind *a = memnew((MethodBindTR<R, P...>)(reinterpret_cast<R (MB_T::*)(P...)>(p_method)));
  444. #endif // TYPED_METHOD_BIND
  445. a->set_instance_class(T::get_class_static());
  446. return a;
  447. }
  448. // Return, const.
  449. #ifdef TYPED_METHOD_BIND
  450. template <class T, class R, class... P>
  451. #else
  452. template <class R, class... P>
  453. #endif // TYPED_METHOD_BIND
  454. class MethodBindTRC : public MethodBind {
  455. R(MB_T::*method)
  456. (P...) const;
  457. protected:
  458. // GCC raises warnings in the case P = {} as the comparison is always false...
  459. #if defined(__GNUC__) && !defined(__clang__)
  460. #pragma GCC diagnostic push
  461. #pragma GCC diagnostic ignored "-Wlogical-op"
  462. #endif
  463. virtual GDNativeVariantType gen_argument_type(int p_arg) const {
  464. if (p_arg >= 0 && p_arg < (int)sizeof...(P)) {
  465. return call_get_argument_type<P...>(p_arg);
  466. } else {
  467. return GDNativeVariantType(GetTypeInfo<R>::VARIANT_TYPE);
  468. }
  469. }
  470. virtual PropertyInfo gen_argument_type_info(int p_arg) const {
  471. if (p_arg >= 0 && p_arg < (int)sizeof...(P)) {
  472. PropertyInfo pi;
  473. call_get_argument_type_info<P...>(p_arg, pi);
  474. return pi;
  475. } else {
  476. return GetTypeInfo<R>::get_class_info();
  477. }
  478. }
  479. #if defined(__GNUC__) && !defined(__clang__)
  480. #pragma GCC diagnostic pop
  481. #endif
  482. public:
  483. virtual GDNativeExtensionClassMethodArgumentMetadata get_argument_metadata(int p_argument) const {
  484. if (p_argument >= 0) {
  485. return call_get_argument_metadata<P...>(p_argument);
  486. } else {
  487. return GetTypeInfo<R>::METADATA;
  488. }
  489. }
  490. virtual Variant call(GDExtensionClassInstancePtr p_instance, const GDNativeVariantPtr *p_args, const GDNativeInt p_argument_count, GDNativeCallError &r_error) const {
  491. Variant ret;
  492. #ifdef TYPED_METHOD_BIND
  493. call_with_variant_args_retc_dv(static_cast<T *>(p_instance), method, p_args, (int)p_argument_count, ret, r_error, get_default_arguments());
  494. #else
  495. call_with_variant_args_retc_dv((MB_T *)p_instance, method, p_args, p_argument_count, ret, r_error, get_default_arguments());
  496. #endif
  497. return ret;
  498. }
  499. virtual void ptrcall(GDExtensionClassInstancePtr p_instance, const GDNativeTypePtr *p_args, GDNativeTypePtr r_ret) const {
  500. #ifdef TYPED_METHOD_BIND
  501. call_with_ptr_args<T, R, P...>(static_cast<T *>(p_instance), method, p_args, r_ret);
  502. #else
  503. call_with_ptr_args<MB_T, R, P...>(reinterpret_cast<MB_T *>(p_instance), method, p_args, r_ret);
  504. #endif // TYPED_METHOD_BIND
  505. }
  506. MethodBindTRC(R (MB_T::*p_method)(P...) const) {
  507. method = p_method;
  508. generate_argument_types(sizeof...(P));
  509. set_argument_count(sizeof...(P));
  510. set_return(true);
  511. }
  512. };
  513. template <class T, class R, class... P>
  514. MethodBind *create_method_bind(R (T::*p_method)(P...) const) {
  515. #ifdef TYPED_METHOD_BIND
  516. MethodBind *a = memnew((MethodBindTRC<T, R, P...>)(p_method));
  517. #else
  518. MethodBind *a = memnew((MethodBindTRC<R, P...>)(reinterpret_cast<R (MB_T::*)(P...) const>(p_method)));
  519. #endif // TYPED_METHOD_BIND
  520. a->set_instance_class(T::get_class_static());
  521. return a;
  522. }
  523. // STATIC BINDS
  524. // no return
  525. template <class... P>
  526. class MethodBindTS : public MethodBind {
  527. void (*function)(P...);
  528. protected:
  529. // GCC raises warnings in the case P = {} as the comparison is always false...
  530. #if defined(__GNUC__) && !defined(__clang__)
  531. #pragma GCC diagnostic push
  532. #pragma GCC diagnostic ignored "-Wlogical-op"
  533. #endif
  534. virtual GDNativeVariantType gen_argument_type(int p_arg) const {
  535. if (p_arg >= 0 && p_arg < (int)sizeof...(P)) {
  536. return call_get_argument_type<P...>(p_arg);
  537. } else {
  538. return GDNATIVE_VARIANT_TYPE_NIL;
  539. }
  540. }
  541. virtual PropertyInfo gen_argument_type_info(int p_arg) const {
  542. PropertyInfo pi;
  543. if (p_arg >= 0 && p_arg < (int)sizeof...(P)) {
  544. call_get_argument_type_info<P...>(p_arg, pi);
  545. } else {
  546. pi = PropertyInfo();
  547. }
  548. return pi;
  549. }
  550. #if defined(__GNUC__) && !defined(__clang__)
  551. #pragma GCC diagnostic pop
  552. #endif
  553. public:
  554. virtual GDNativeExtensionClassMethodArgumentMetadata get_argument_metadata(int p_arg) const {
  555. return call_get_argument_metadata<P...>(p_arg);
  556. }
  557. virtual Variant call(GDExtensionClassInstancePtr p_object, const GDNativeVariantPtr *p_args, const GDNativeInt p_arg_count, GDNativeCallError &r_error) const {
  558. (void)p_object; // unused
  559. call_with_variant_args_static_dv(function, p_args, p_arg_count, r_error, get_default_arguments());
  560. return Variant();
  561. }
  562. virtual void ptrcall(GDExtensionClassInstancePtr p_object, const GDNativeTypePtr *p_args, GDNativeTypePtr r_ret) const {
  563. (void)p_object;
  564. (void)r_ret;
  565. call_with_ptr_args_static_method(function, p_args);
  566. }
  567. MethodBindTS(void (*p_function)(P...)) {
  568. function = p_function;
  569. generate_argument_types(sizeof...(P));
  570. set_argument_count(sizeof...(P));
  571. set_static(true);
  572. }
  573. };
  574. template <class... P>
  575. MethodBind *create_static_method_bind(void (*p_method)(P...)) {
  576. MethodBind *a = memnew((MethodBindTS<P...>)(p_method));
  577. return a;
  578. }
  579. // return
  580. template <class R, class... P>
  581. class MethodBindTRS : public MethodBind {
  582. R(*function)
  583. (P...);
  584. protected:
  585. // GCC raises warnings in the case P = {} as the comparison is always false...
  586. #if defined(__GNUC__) && !defined(__clang__)
  587. #pragma GCC diagnostic push
  588. #pragma GCC diagnostic ignored "-Wlogical-op"
  589. #endif
  590. virtual GDNativeVariantType gen_argument_type(int p_arg) const {
  591. if (p_arg >= 0 && p_arg < (int)sizeof...(P)) {
  592. return call_get_argument_type<P...>(p_arg);
  593. } else {
  594. return GDNativeVariantType(GetTypeInfo<R>::VARIANT_TYPE);
  595. }
  596. }
  597. virtual PropertyInfo gen_argument_type_info(int p_arg) const {
  598. if (p_arg >= 0 && p_arg < (int)sizeof...(P)) {
  599. PropertyInfo pi;
  600. call_get_argument_type_info<P...>(p_arg, pi);
  601. return pi;
  602. } else {
  603. return GetTypeInfo<R>::get_class_info();
  604. }
  605. }
  606. #if defined(__GNUC__) && !defined(__clang__)
  607. #pragma GCC diagnostic pop
  608. #endif
  609. public:
  610. virtual GDNativeExtensionClassMethodArgumentMetadata get_argument_metadata(int p_arg) const {
  611. if (p_arg >= 0) {
  612. return call_get_argument_metadata<P...>(p_arg);
  613. } else {
  614. return GetTypeInfo<R>::METADATA;
  615. }
  616. }
  617. virtual Variant call(GDExtensionClassInstancePtr p_object, const GDNativeVariantPtr *p_args, const GDNativeInt p_arg_count, GDNativeCallError &r_error) const {
  618. Variant ret;
  619. call_with_variant_args_static_ret_dv(function, p_args, p_arg_count, ret, r_error, get_default_arguments());
  620. return ret;
  621. }
  622. virtual void ptrcall(GDExtensionClassInstancePtr p_object, const GDNativeTypePtr *p_args, GDNativeTypePtr r_ret) const {
  623. (void)p_object;
  624. call_with_ptr_args_static_method_ret(function, p_args, r_ret);
  625. }
  626. MethodBindTRS(R (*p_function)(P...)) {
  627. function = p_function;
  628. generate_argument_types(sizeof...(P));
  629. set_argument_count(sizeof...(P));
  630. set_static(true);
  631. set_return(true);
  632. }
  633. };
  634. template <class R, class... P>
  635. MethodBind *create_static_method_bind(R (*p_method)(P...)) {
  636. MethodBind *a = memnew((MethodBindTRS<R, P...>)(p_method));
  637. return a;
  638. }
  639. } // namespace godot
  640. #endif // GODOT_METHOD_BIND_HPP