2
0

method_bind.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. /*************************************************************************/
  2. /* method_bind.h */
  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 METHOD_BIND_H
  31. #define METHOD_BIND_H
  32. #include "core/variant/binder_common.h"
  33. enum MethodFlags {
  34. METHOD_FLAG_NORMAL = 1,
  35. METHOD_FLAG_EDITOR = 2,
  36. METHOD_FLAG_NOSCRIPT = 4,
  37. METHOD_FLAG_CONST = 8,
  38. METHOD_FLAG_REVERSE = 16, // used for events
  39. METHOD_FLAG_VIRTUAL = 32,
  40. METHOD_FLAG_FROM_SCRIPT = 64,
  41. METHOD_FLAG_VARARG = 128,
  42. METHOD_FLAG_STATIC = 256,
  43. METHOD_FLAG_OBJECT_CORE = 512,
  44. METHOD_FLAGS_DEFAULT = METHOD_FLAG_NORMAL,
  45. };
  46. VARIANT_ENUM_CAST(MethodFlags)
  47. // some helpers
  48. class MethodBind {
  49. int method_id;
  50. uint32_t hint_flags = METHOD_FLAGS_DEFAULT;
  51. StringName name;
  52. StringName instance_class;
  53. Vector<Variant> default_arguments;
  54. int default_argument_count = 0;
  55. int argument_count = 0;
  56. bool _const = false;
  57. bool _returns = false;
  58. protected:
  59. Variant::Type *argument_types = nullptr;
  60. #ifdef DEBUG_METHODS_ENABLED
  61. Vector<StringName> arg_names;
  62. #endif
  63. void _set_const(bool p_const);
  64. void _set_returns(bool p_returns);
  65. virtual Variant::Type _gen_argument_type(int p_arg) const = 0;
  66. virtual PropertyInfo _gen_argument_type_info(int p_arg) const = 0;
  67. void _generate_argument_types(int p_count);
  68. void set_argument_count(int p_count) { argument_count = p_count; }
  69. public:
  70. _FORCE_INLINE_ const Vector<Variant> &get_default_arguments() const { return default_arguments; }
  71. _FORCE_INLINE_ int get_default_argument_count() const { return default_argument_count; }
  72. _FORCE_INLINE_ Variant has_default_argument(int p_arg) const {
  73. int idx = p_arg - (argument_count - default_arguments.size());
  74. if (idx < 0 || idx >= default_arguments.size()) {
  75. return false;
  76. } else {
  77. return true;
  78. }
  79. }
  80. _FORCE_INLINE_ Variant get_default_argument(int p_arg) const {
  81. int idx = p_arg - (argument_count - default_arguments.size());
  82. if (idx < 0 || idx >= default_arguments.size()) {
  83. return Variant();
  84. } else {
  85. return default_arguments[idx];
  86. }
  87. }
  88. _FORCE_INLINE_ Variant::Type get_argument_type(int p_argument) const {
  89. ERR_FAIL_COND_V(p_argument < -1 || p_argument > argument_count, Variant::NIL);
  90. return argument_types[p_argument + 1];
  91. }
  92. PropertyInfo get_argument_info(int p_argument) const;
  93. PropertyInfo get_return_info() const;
  94. #ifdef DEBUG_METHODS_ENABLED
  95. void set_argument_names(const Vector<StringName> &p_names); // Set by ClassDB, can't be inferred otherwise.
  96. Vector<StringName> get_argument_names() const;
  97. virtual GodotTypeInfo::Metadata get_argument_meta(int p_arg) const = 0;
  98. #endif
  99. void set_hint_flags(uint32_t p_hint) { hint_flags = p_hint; }
  100. uint32_t get_hint_flags() const { return hint_flags | (is_const() ? METHOD_FLAG_CONST : 0) | (is_vararg() ? METHOD_FLAG_VARARG : 0); }
  101. _FORCE_INLINE_ StringName get_instance_class() const { return instance_class; }
  102. _FORCE_INLINE_ void set_instance_class(const StringName &p_class) { instance_class = p_class; }
  103. _FORCE_INLINE_ int get_argument_count() const { return argument_count; };
  104. virtual Variant call(Object *p_object, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) = 0;
  105. virtual void ptrcall(Object *p_object, const void **p_args, void *r_ret) = 0;
  106. StringName get_name() const;
  107. void set_name(const StringName &p_name);
  108. _FORCE_INLINE_ int get_method_id() const { return method_id; }
  109. _FORCE_INLINE_ bool is_const() const { return _const; }
  110. _FORCE_INLINE_ bool has_return() const { return _returns; }
  111. virtual bool is_vararg() const { return false; }
  112. void set_default_arguments(const Vector<Variant> &p_defargs);
  113. uint32_t get_hash() const;
  114. MethodBind();
  115. virtual ~MethodBind();
  116. };
  117. template <class T>
  118. class MethodBindVarArg : public MethodBind {
  119. public:
  120. typedef Variant (T::*NativeCall)(const Variant **, int, Callable::CallError &);
  121. protected:
  122. NativeCall call_method = nullptr;
  123. MethodInfo arguments;
  124. public:
  125. virtual PropertyInfo _gen_argument_type_info(int p_arg) const {
  126. if (p_arg < 0) {
  127. return arguments.return_val;
  128. } else if (p_arg < arguments.arguments.size()) {
  129. return arguments.arguments[p_arg];
  130. } else {
  131. return PropertyInfo(Variant::NIL, "arg_" + itos(p_arg), PROPERTY_HINT_NONE, String(), PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_NIL_IS_VARIANT);
  132. }
  133. }
  134. virtual Variant::Type _gen_argument_type(int p_arg) const {
  135. return _gen_argument_type_info(p_arg).type;
  136. }
  137. #ifdef DEBUG_METHODS_ENABLED
  138. virtual GodotTypeInfo::Metadata get_argument_meta(int) const {
  139. return GodotTypeInfo::METADATA_NONE;
  140. }
  141. #endif
  142. virtual Variant call(Object *p_object, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) {
  143. T *instance = static_cast<T *>(p_object);
  144. return (instance->*call_method)(p_args, p_arg_count, r_error);
  145. }
  146. void set_method_info(const MethodInfo &p_info, bool p_return_nil_is_variant) {
  147. set_argument_count(p_info.arguments.size());
  148. Variant::Type *at = memnew_arr(Variant::Type, p_info.arguments.size() + 1);
  149. at[0] = p_info.return_val.type;
  150. if (p_info.arguments.size()) {
  151. #ifdef DEBUG_METHODS_ENABLED
  152. Vector<StringName> names;
  153. names.resize(p_info.arguments.size());
  154. #endif
  155. for (int i = 0; i < p_info.arguments.size(); i++) {
  156. at[i + 1] = p_info.arguments[i].type;
  157. #ifdef DEBUG_METHODS_ENABLED
  158. names.write[i] = p_info.arguments[i].name;
  159. #endif
  160. }
  161. #ifdef DEBUG_METHODS_ENABLED
  162. set_argument_names(names);
  163. #endif
  164. }
  165. argument_types = at;
  166. arguments = p_info;
  167. if (p_return_nil_is_variant) {
  168. arguments.return_val.usage |= PROPERTY_USAGE_NIL_IS_VARIANT;
  169. }
  170. }
  171. virtual void ptrcall(Object *p_object, const void **p_args, void *r_ret) {
  172. ERR_FAIL(); // Can't call.
  173. }
  174. void set_method(NativeCall p_method) { call_method = p_method; }
  175. virtual bool is_const() const { return false; }
  176. virtual bool is_vararg() const { return true; }
  177. MethodBindVarArg() {
  178. _set_returns(true);
  179. }
  180. };
  181. template <class T>
  182. MethodBind *create_vararg_method_bind(Variant (T::*p_method)(const Variant **, int, Callable::CallError &), const MethodInfo &p_info, bool p_return_nil_is_variant) {
  183. MethodBindVarArg<T> *a = memnew((MethodBindVarArg<T>));
  184. a->set_method(p_method);
  185. a->set_method_info(p_info, p_return_nil_is_variant);
  186. a->set_instance_class(T::get_class_static());
  187. return a;
  188. }
  189. /**** VARIADIC TEMPLATES ****/
  190. #ifndef TYPED_METHOD_BIND
  191. class __UnexistingClass;
  192. #define MB_T __UnexistingClass
  193. #else
  194. #define MB_T T
  195. #endif
  196. // no return, not const
  197. #ifdef TYPED_METHOD_BIND
  198. template <class T, class... P>
  199. #else
  200. template <class... P>
  201. #endif
  202. class MethodBindT : public MethodBind {
  203. void (MB_T::*method)(P...);
  204. protected:
  205. // GCC raises warnings in the case P = {} as the comparison is always false...
  206. #if defined(__GNUC__) && !defined(__clang__)
  207. #pragma GCC diagnostic push
  208. #pragma GCC diagnostic ignored "-Wlogical-op"
  209. #endif
  210. virtual Variant::Type _gen_argument_type(int p_arg) const {
  211. if (p_arg >= 0 && p_arg < (int)sizeof...(P)) {
  212. return call_get_argument_type<P...>(p_arg);
  213. } else {
  214. return Variant::NIL;
  215. }
  216. }
  217. #if defined(__GNUC__) && !defined(__clang__)
  218. #pragma GCC diagnostic pop
  219. #endif
  220. virtual PropertyInfo _gen_argument_type_info(int p_arg) const {
  221. PropertyInfo pi;
  222. call_get_argument_type_info<P...>(p_arg, pi);
  223. return pi;
  224. }
  225. public:
  226. #ifdef DEBUG_METHODS_ENABLED
  227. virtual GodotTypeInfo::Metadata get_argument_meta(int p_arg) const {
  228. return call_get_argument_metadata<P...>(p_arg);
  229. }
  230. #endif
  231. virtual Variant call(Object *p_object, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) {
  232. #ifdef TYPED_METHOD_BIND
  233. call_with_variant_args_dv(static_cast<T *>(p_object), method, p_args, p_arg_count, r_error, get_default_arguments());
  234. #else
  235. call_with_variant_args_dv((MB_T *)(p_object), method, p_args, p_arg_count, r_error, get_default_arguments());
  236. #endif
  237. return Variant();
  238. }
  239. virtual void ptrcall(Object *p_object, const void **p_args, void *r_ret) {
  240. #ifdef TYPED_METHOD_BIND
  241. call_with_ptr_args<T, P...>(static_cast<T *>(p_object), method, p_args);
  242. #else
  243. call_with_ptr_args<MB_T, P...>((MB_T *)(p_object), method, p_args);
  244. #endif
  245. }
  246. MethodBindT(void (MB_T::*p_method)(P...)) {
  247. method = p_method;
  248. _generate_argument_types(sizeof...(P));
  249. set_argument_count(sizeof...(P));
  250. }
  251. };
  252. template <class T, class... P>
  253. MethodBind *create_method_bind(void (T::*p_method)(P...)) {
  254. #ifdef TYPED_METHOD_BIND
  255. MethodBind *a = memnew((MethodBindT<T, P...>)(p_method));
  256. #else
  257. MethodBind *a = memnew((MethodBindT<P...>)(reinterpret_cast<void (MB_T::*)(P...)>(p_method)));
  258. #endif
  259. a->set_instance_class(T::get_class_static());
  260. return a;
  261. }
  262. // no return, not const
  263. #ifdef TYPED_METHOD_BIND
  264. template <class T, class... P>
  265. #else
  266. template <class... P>
  267. #endif
  268. class MethodBindTC : public MethodBind {
  269. void (MB_T::*method)(P...) const;
  270. protected:
  271. // GCC raises warnings in the case P = {} as the comparison is always false...
  272. #if defined(__GNUC__) && !defined(__clang__)
  273. #pragma GCC diagnostic push
  274. #pragma GCC diagnostic ignored "-Wlogical-op"
  275. #endif
  276. virtual Variant::Type _gen_argument_type(int p_arg) const {
  277. if (p_arg >= 0 && p_arg < (int)sizeof...(P)) {
  278. return call_get_argument_type<P...>(p_arg);
  279. } else {
  280. return Variant::NIL;
  281. }
  282. }
  283. #if defined(__GNUC__) && !defined(__clang__)
  284. #pragma GCC diagnostic pop
  285. #endif
  286. virtual PropertyInfo _gen_argument_type_info(int p_arg) const {
  287. PropertyInfo pi;
  288. call_get_argument_type_info<P...>(p_arg, pi);
  289. return pi;
  290. }
  291. public:
  292. #ifdef DEBUG_METHODS_ENABLED
  293. virtual GodotTypeInfo::Metadata get_argument_meta(int p_arg) const {
  294. return call_get_argument_metadata<P...>(p_arg);
  295. }
  296. #endif
  297. virtual Variant call(Object *p_object, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) {
  298. #ifdef TYPED_METHOD_BIND
  299. call_with_variant_argsc_dv(static_cast<T *>(p_object), method, p_args, p_arg_count, r_error, get_default_arguments());
  300. #else
  301. call_with_variant_argsc_dv((MB_T *)(p_object), method, p_args, p_arg_count, r_error, get_default_arguments());
  302. #endif
  303. return Variant();
  304. }
  305. virtual void ptrcall(Object *p_object, const void **p_args, void *r_ret) {
  306. #ifdef TYPED_METHOD_BIND
  307. call_with_ptr_argsc<T, P...>(static_cast<T *>(p_object), method, p_args);
  308. #else
  309. call_with_ptr_argsc<MB_T, P...>((MB_T *)(p_object), method, p_args);
  310. #endif
  311. }
  312. MethodBindTC(void (MB_T::*p_method)(P...) const) {
  313. method = p_method;
  314. _set_const(true);
  315. _generate_argument_types(sizeof...(P));
  316. set_argument_count(sizeof...(P));
  317. }
  318. };
  319. template <class T, class... P>
  320. MethodBind *create_method_bind(void (T::*p_method)(P...) const) {
  321. #ifdef TYPED_METHOD_BIND
  322. MethodBind *a = memnew((MethodBindTC<T, P...>)(p_method));
  323. #else
  324. MethodBind *a = memnew((MethodBindTC<P...>)(reinterpret_cast<void (MB_T::*)(P...) const>(p_method)));
  325. #endif
  326. a->set_instance_class(T::get_class_static());
  327. return a;
  328. }
  329. // return, not const
  330. #ifdef TYPED_METHOD_BIND
  331. template <class T, class R, class... P>
  332. #else
  333. template <class R, class... P>
  334. #endif
  335. class MethodBindTR : public MethodBind {
  336. R(MB_T::*method)
  337. (P...);
  338. protected:
  339. // GCC raises warnings in the case P = {} as the comparison is always false...
  340. #if defined(__GNUC__) && !defined(__clang__)
  341. #pragma GCC diagnostic push
  342. #pragma GCC diagnostic ignored "-Wlogical-op"
  343. #endif
  344. virtual Variant::Type _gen_argument_type(int p_arg) const {
  345. if (p_arg >= 0 && p_arg < (int)sizeof...(P)) {
  346. return call_get_argument_type<P...>(p_arg);
  347. } else {
  348. return GetTypeInfo<R>::VARIANT_TYPE;
  349. }
  350. }
  351. virtual PropertyInfo _gen_argument_type_info(int p_arg) const {
  352. if (p_arg >= 0 && p_arg < (int)sizeof...(P)) {
  353. PropertyInfo pi;
  354. call_get_argument_type_info<P...>(p_arg, pi);
  355. return pi;
  356. } else {
  357. return GetTypeInfo<R>::get_class_info();
  358. }
  359. }
  360. #if defined(__GNUC__) && !defined(__clang__)
  361. #pragma GCC diagnostic pop
  362. #endif
  363. public:
  364. #ifdef DEBUG_METHODS_ENABLED
  365. virtual GodotTypeInfo::Metadata get_argument_meta(int p_arg) const {
  366. if (p_arg >= 0) {
  367. return call_get_argument_metadata<P...>(p_arg);
  368. } else {
  369. return GetTypeInfo<R>::METADATA;
  370. }
  371. }
  372. #endif
  373. virtual Variant call(Object *p_object, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) {
  374. Variant ret;
  375. #ifdef TYPED_METHOD_BIND
  376. call_with_variant_args_ret_dv(static_cast<T *>(p_object), method, p_args, p_arg_count, ret, r_error, get_default_arguments());
  377. #else
  378. call_with_variant_args_ret_dv((MB_T *)p_object, method, p_args, p_arg_count, ret, r_error, get_default_arguments());
  379. #endif
  380. return ret;
  381. }
  382. virtual void ptrcall(Object *p_object, const void **p_args, void *r_ret) {
  383. #ifdef TYPED_METHOD_BIND
  384. call_with_ptr_args_ret<T, R, P...>(static_cast<T *>(p_object), method, p_args, r_ret);
  385. #else
  386. call_with_ptr_args_ret<MB_T, R, P...>((MB_T *)(p_object), method, p_args, r_ret);
  387. #endif
  388. }
  389. MethodBindTR(R (MB_T::*p_method)(P...)) {
  390. method = p_method;
  391. _set_returns(true);
  392. _generate_argument_types(sizeof...(P));
  393. set_argument_count(sizeof...(P));
  394. }
  395. };
  396. template <class T, class R, class... P>
  397. MethodBind *create_method_bind(R (T::*p_method)(P...)) {
  398. #ifdef TYPED_METHOD_BIND
  399. MethodBind *a = memnew((MethodBindTR<T, R, P...>)(p_method));
  400. #else
  401. MethodBind *a = memnew((MethodBindTR<R, P...>)(reinterpret_cast<R (MB_T::*)(P...)>(p_method)));
  402. #endif
  403. a->set_instance_class(T::get_class_static());
  404. return a;
  405. }
  406. // return, const
  407. #ifdef TYPED_METHOD_BIND
  408. template <class T, class R, class... P>
  409. #else
  410. template <class R, class... P>
  411. #endif
  412. class MethodBindTRC : public MethodBind {
  413. R(MB_T::*method)
  414. (P...) const;
  415. protected:
  416. // GCC raises warnings in the case P = {} as the comparison is always false...
  417. #if defined(__GNUC__) && !defined(__clang__)
  418. #pragma GCC diagnostic push
  419. #pragma GCC diagnostic ignored "-Wlogical-op"
  420. #endif
  421. virtual Variant::Type _gen_argument_type(int p_arg) const {
  422. if (p_arg >= 0 && p_arg < (int)sizeof...(P)) {
  423. return call_get_argument_type<P...>(p_arg);
  424. } else {
  425. return GetTypeInfo<R>::VARIANT_TYPE;
  426. }
  427. }
  428. virtual PropertyInfo _gen_argument_type_info(int p_arg) const {
  429. if (p_arg >= 0 && p_arg < (int)sizeof...(P)) {
  430. PropertyInfo pi;
  431. call_get_argument_type_info<P...>(p_arg, pi);
  432. return pi;
  433. } else {
  434. return GetTypeInfo<R>::get_class_info();
  435. }
  436. }
  437. #if defined(__GNUC__) && !defined(__clang__)
  438. #pragma GCC diagnostic pop
  439. #endif
  440. public:
  441. #ifdef DEBUG_METHODS_ENABLED
  442. virtual GodotTypeInfo::Metadata get_argument_meta(int p_arg) const {
  443. if (p_arg >= 0) {
  444. return call_get_argument_metadata<P...>(p_arg);
  445. } else {
  446. return GetTypeInfo<R>::METADATA;
  447. }
  448. }
  449. #endif
  450. virtual Variant call(Object *p_object, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) {
  451. Variant ret;
  452. #ifdef TYPED_METHOD_BIND
  453. call_with_variant_args_retc_dv(static_cast<T *>(p_object), method, p_args, p_arg_count, ret, r_error, get_default_arguments());
  454. #else
  455. call_with_variant_args_retc_dv((MB_T *)(p_object), method, p_args, p_arg_count, ret, r_error, get_default_arguments());
  456. #endif
  457. return ret;
  458. }
  459. virtual void ptrcall(Object *p_object, const void **p_args, void *r_ret) {
  460. #ifdef TYPED_METHOD_BIND
  461. call_with_ptr_args_retc<T, R, P...>(static_cast<T *>(p_object), method, p_args, r_ret);
  462. #else
  463. call_with_ptr_args_retc<MB_T, R, P...>((MB_T *)(p_object), method, p_args, r_ret);
  464. #endif
  465. }
  466. MethodBindTRC(R (MB_T::*p_method)(P...) const) {
  467. method = p_method;
  468. _set_returns(true);
  469. _set_const(true);
  470. _generate_argument_types(sizeof...(P));
  471. set_argument_count(sizeof...(P));
  472. }
  473. };
  474. template <class T, class R, class... P>
  475. MethodBind *create_method_bind(R (T::*p_method)(P...) const) {
  476. #ifdef TYPED_METHOD_BIND
  477. MethodBind *a = memnew((MethodBindTRC<T, R, P...>)(p_method));
  478. #else
  479. MethodBind *a = memnew((MethodBindTRC<R, P...>)(reinterpret_cast<R (MB_T::*)(P...) const>(p_method)));
  480. #endif
  481. a->set_instance_class(T::get_class_static());
  482. return a;
  483. }
  484. #endif // METHOD_BIND_H