callable_method_pointer.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. /*************************************************************************/
  2. /* callable_method_pointer.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 CALLABLE_METHOD_POINTER_H
  31. #define CALLABLE_METHOD_POINTER_H
  32. #include "core/callable.h"
  33. #include "core/hashfuncs.h"
  34. #include "core/object.h"
  35. #include "core/os/copymem.h"
  36. #include "core/simple_type.h"
  37. #include "core/variant_internal.h"
  38. class CallableCustomMethodPointerBase : public CallableCustom {
  39. uint32_t *comp_ptr;
  40. uint32_t comp_size;
  41. uint32_t h;
  42. #ifdef DEBUG_METHODS_ENABLED
  43. const char *text = "";
  44. #endif
  45. static bool compare_equal(const CallableCustom *p_a, const CallableCustom *p_b);
  46. static bool compare_less(const CallableCustom *p_a, const CallableCustom *p_b);
  47. protected:
  48. void _setup(uint32_t *p_base_ptr, uint32_t p_ptr_size);
  49. public:
  50. #ifdef DEBUG_METHODS_ENABLED
  51. void set_text(const char *p_text) {
  52. text = p_text;
  53. }
  54. virtual String get_as_text() const {
  55. return text;
  56. }
  57. #else
  58. virtual String get_as_text() const {
  59. return String();
  60. }
  61. #endif
  62. virtual CompareEqualFunc get_compare_equal_func() const;
  63. virtual CompareLessFunc get_compare_less_func() const;
  64. virtual uint32_t hash() const;
  65. };
  66. #ifdef DEBUG_METHODS_ENABLED
  67. template <class T>
  68. struct VariantCasterAndValidate {
  69. static _FORCE_INLINE_ T cast(const Variant **p_args, uint32_t p_arg_idx, Callable::CallError &r_error) {
  70. Variant::Type argtype = GetTypeInfo<T>::VARIANT_TYPE;
  71. if (!Variant::can_convert_strict(p_args[p_arg_idx]->get_type(), argtype)) {
  72. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  73. r_error.argument = p_arg_idx;
  74. r_error.expected = argtype;
  75. }
  76. return VariantCaster<T>::cast(*p_args[p_arg_idx]);
  77. }
  78. };
  79. template <class T>
  80. struct VariantCasterAndValidate<T &> {
  81. static _FORCE_INLINE_ T cast(const Variant **p_args, uint32_t p_arg_idx, Callable::CallError &r_error) {
  82. Variant::Type argtype = GetTypeInfo<T>::VARIANT_TYPE;
  83. if (!Variant::can_convert_strict(p_args[p_arg_idx]->get_type(), argtype)) {
  84. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  85. r_error.argument = p_arg_idx;
  86. r_error.expected = argtype;
  87. }
  88. return VariantCaster<T>::cast(*p_args[p_arg_idx]);
  89. }
  90. };
  91. template <class T>
  92. struct VariantCasterAndValidate<const T &> {
  93. static _FORCE_INLINE_ T cast(const Variant **p_args, uint32_t p_arg_idx, Callable::CallError &r_error) {
  94. Variant::Type argtype = GetTypeInfo<T>::VARIANT_TYPE;
  95. if (!Variant::can_convert_strict(p_args[p_arg_idx]->get_type(), argtype)) {
  96. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  97. r_error.argument = p_arg_idx;
  98. r_error.expected = argtype;
  99. }
  100. return VariantCaster<T>::cast(*p_args[p_arg_idx]);
  101. }
  102. };
  103. #endif // DEBUG_METHODS_ENABLED
  104. // GCC raises "parameter 'p_args' set but not used" here, probably using a
  105. // template version that does not have arguments and thus sees it unused, but
  106. // obviously the template can be used for functions with and without them, and
  107. // the optimizer will get rid of it anyway.
  108. #if defined(DEBUG_METHODS_ENABLED) && defined(__GNUC__) && !defined(__clang__)
  109. #pragma GCC diagnostic push
  110. #pragma GCC diagnostic ignored "-Wunused-but-set-parameter"
  111. #endif
  112. template <class T, class... P, size_t... Is>
  113. void call_with_variant_args_helper(T *p_instance, void (T::*p_method)(P...), const Variant **p_args, Callable::CallError &r_error, IndexSequence<Is...>) {
  114. r_error.error = Callable::CallError::CALL_OK;
  115. #ifdef DEBUG_METHODS_ENABLED
  116. (p_instance->*p_method)(VariantCasterAndValidate<P>::cast(p_args, Is, r_error)...);
  117. #else
  118. (p_instance->*p_method)(VariantCaster<P>::cast(*p_args[Is])...);
  119. #endif
  120. }
  121. #ifdef PTRCALL_ENABLED
  122. template <class T, class... P, size_t... Is>
  123. void call_with_ptr_args_helper(T *p_instance, void (T::*p_method)(P...), const void **p_args, IndexSequence<Is...>) {
  124. (p_instance->*p_method)(PtrToArg<P>::convert(p_args[Is])...);
  125. }
  126. template <class T, class R, class... P, size_t... Is>
  127. void call_with_ptr_args_ret_helper(T *p_instance, R (T::*p_method)(P...), const void **p_args, void *r_ret, IndexSequence<Is...>) {
  128. PtrToArg<R>::encode((p_instance->*p_method)(PtrToArg<P>::convert(p_args[Is])...), r_ret);
  129. }
  130. template <class T, class R, class... P, size_t... Is>
  131. void call_with_ptr_args_retc_helper(T *p_instance, R (T::*p_method)(P...) const, const void **p_args, void *r_ret, IndexSequence<Is...>) {
  132. PtrToArg<R>::encode((p_instance->*p_method)(PtrToArg<P>::convert(p_args[Is])...), r_ret);
  133. }
  134. template <class T, class R, class... P, size_t... Is>
  135. void call_with_ptr_args_static_retc_helper(T *p_instance, R (*p_method)(T *, P...), const void **p_args, void *r_ret, IndexSequence<Is...>) {
  136. PtrToArg<R>::encode(p_method(p_instance, PtrToArg<P>::convert(p_args[Is])...), r_ret);
  137. }
  138. #endif // PTRCALL_ENABLED
  139. template <class T, class... P, size_t... Is>
  140. void call_with_validated_variant_args_helper(T *p_instance, void (T::*p_method)(P...), const Variant **p_args, IndexSequence<Is...>) {
  141. (p_instance->*p_method)((VariantInternalAccessor<typename GetSimpleTypeT<P>::type_t>::get(p_args[Is]))...);
  142. }
  143. template <class T, class R, class... P, size_t... Is>
  144. void call_with_validated_variant_args_ret_helper(T *p_instance, R (T::*p_method)(P...), const Variant **p_args, Variant *r_ret, IndexSequence<Is...>) {
  145. VariantInternalAccessor<typename GetSimpleTypeT<R>::type_t>::set(r_ret, (p_instance->*p_method)((VariantInternalAccessor<typename GetSimpleTypeT<P>::type_t>::get(p_args[Is]))...));
  146. }
  147. template <class T, class R, class... P, size_t... Is>
  148. void call_with_validated_variant_args_retc_helper(T *p_instance, R (T::*p_method)(P...) const, const Variant **p_args, Variant *r_ret, IndexSequence<Is...>) {
  149. VariantInternalAccessor<typename GetSimpleTypeT<R>::type_t>::set(r_ret, (p_instance->*p_method)((VariantInternalAccessor<typename GetSimpleTypeT<P>::type_t>::get(p_args[Is]))...));
  150. }
  151. template <class T, class R, class... P, size_t... Is>
  152. void call_with_validated_variant_args_static_retc_helper(T *p_instance, R (*p_method)(T *, P...), const Variant **p_args, Variant *r_ret, IndexSequence<Is...>) {
  153. VariantInternalAccessor<typename GetSimpleTypeT<R>::type_t>::set(r_ret, p_method(p_instance, (VariantInternalAccessor<typename GetSimpleTypeT<P>::type_t>::get(p_args[Is]))...));
  154. }
  155. template <class T, class... P>
  156. void call_with_variant_args(T *p_instance, void (T::*p_method)(P...), const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  157. #ifdef DEBUG_METHODS_ENABLED
  158. if ((size_t)p_argcount > sizeof...(P)) {
  159. r_error.error = Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS;
  160. r_error.argument = sizeof...(P);
  161. return;
  162. }
  163. if ((size_t)p_argcount < sizeof...(P)) {
  164. r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  165. r_error.argument = sizeof...(P);
  166. return;
  167. }
  168. #endif
  169. call_with_variant_args_helper<T, P...>(p_instance, p_method, p_args, r_error, BuildIndexSequence<sizeof...(P)>{});
  170. }
  171. #ifdef PTRCALL_ENABLED
  172. template <class T, class... P>
  173. void call_with_ptr_args(T *p_instance, void (T::*p_method)(P...), const void **p_args) {
  174. call_with_ptr_args_helper<T, P...>(p_instance, p_method, p_args, BuildIndexSequence<sizeof...(P)>{});
  175. }
  176. template <class T, class R, class... P>
  177. void call_with_ptr_args_ret(T *p_instance, R (T::*p_method)(P...), const void **p_args, void *r_ret) {
  178. call_with_ptr_args_ret_helper<T, R, P...>(p_instance, p_method, p_args, r_ret, BuildIndexSequence<sizeof...(P)>{});
  179. }
  180. template <class T, class R, class... P>
  181. void call_with_ptr_args_retc(T *p_instance, R (T::*p_method)(P...) const, const void **p_args, void *r_ret) {
  182. call_with_ptr_args_retc_helper<T, R, P...>(p_instance, p_method, p_args, r_ret, BuildIndexSequence<sizeof...(P)>{});
  183. }
  184. template <class T, class R, class... P>
  185. void call_with_ptr_args_static_retc(T *p_instance, R (*p_method)(T *, P...), const void **p_args, void *r_ret) {
  186. call_with_ptr_args_static_retc_helper<T, R, P...>(p_instance, p_method, p_args, r_ret, BuildIndexSequence<sizeof...(P)>{});
  187. }
  188. #endif // PTRCALL_ENABLED
  189. template <class T, class... P>
  190. void call_with_validated_variant_args(Variant *base, void (T::*p_method)(P...), const Variant **p_args) {
  191. call_with_validated_variant_args_helper<T, P...>(VariantGetInternalPtr<T>::get_ptr(base), p_method, p_args, BuildIndexSequence<sizeof...(P)>{});
  192. }
  193. template <class T, class R, class... P>
  194. void call_with_validated_variant_args_ret(Variant *base, R (T::*p_method)(P...), const Variant **p_args, Variant *r_ret) {
  195. call_with_validated_variant_args_ret_helper<T, R, P...>(VariantGetInternalPtr<T>::get_ptr(base), p_method, p_args, r_ret, BuildIndexSequence<sizeof...(P)>{});
  196. }
  197. template <class T, class R, class... P>
  198. void call_with_validated_variant_args_retc(Variant *base, R (T::*p_method)(P...) const, const Variant **p_args, Variant *r_ret) {
  199. call_with_validated_variant_args_retc_helper<T, R, P...>(VariantGetInternalPtr<T>::get_ptr(base), p_method, p_args, r_ret, BuildIndexSequence<sizeof...(P)>{});
  200. }
  201. template <class T, class R, class... P>
  202. void call_with_validated_variant_args_static_retc(Variant *base, R (*p_method)(T *, P...), const Variant **p_args, Variant *r_ret) {
  203. call_with_validated_variant_args_static_retc_helper<T, R, P...>(VariantGetInternalPtr<T>::get_ptr(base), p_method, p_args, r_ret, BuildIndexSequence<sizeof...(P)>{});
  204. }
  205. #ifdef DEBUG_METHODS_ENABLED
  206. template <class Q>
  207. void call_get_argument_type_helper(int p_arg, int &index, Variant::Type &type) {
  208. if (p_arg == index) {
  209. type = GetTypeInfo<Q>::VARIANT_TYPE;
  210. }
  211. index++;
  212. }
  213. // GCC's warnings checker really doesn't like variadic voodoo.
  214. // It sees `index` unused below in some branches, so it raises a warning.
  215. #if defined(__GNUC__) && !defined(__clang__)
  216. #pragma GCC diagnostic push
  217. #pragma GCC diagnostic ignored "-Wunused-but-set-variable"
  218. #endif
  219. template <class... P>
  220. Variant::Type call_get_argument_type(int p_arg) {
  221. Variant::Type type = Variant::NIL;
  222. int index = 0;
  223. // I think rocket science is simpler than modern C++.
  224. using expand_type = int[];
  225. expand_type a{ 0, (call_get_argument_type_helper<P>(p_arg, index, type), 0)... };
  226. (void)a; // Suppress (valid, but unavoidable) -Wunused-variable warning.
  227. return type;
  228. }
  229. #if defined(__GNUC__) && !defined(__clang__)
  230. #pragma GCC diagnostic pop
  231. #endif
  232. #else
  233. template <class... P>
  234. Variant::Type call_get_argument_type(int p_arg) {
  235. return Variant::NIL;
  236. }
  237. #endif // DEBUG_METHODS_ENABLED
  238. template <class T, class... P>
  239. class CallableCustomMethodPointer : public CallableCustomMethodPointerBase {
  240. struct Data {
  241. T *instance;
  242. #ifdef DEBUG_ENABLED
  243. uint64_t object_id;
  244. #endif
  245. void (T::*method)(P...);
  246. } data;
  247. public:
  248. virtual ObjectID get_object() const {
  249. #ifdef DEBUG_ENABLED
  250. if (ObjectDB::get_instance(ObjectID(data.object_id)) == nullptr) {
  251. return ObjectID();
  252. }
  253. #endif
  254. return data.instance->get_instance_id();
  255. }
  256. virtual void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const {
  257. #ifdef DEBUG_ENABLED
  258. ERR_FAIL_COND_MSG(ObjectDB::get_instance(ObjectID(data.object_id)) == nullptr, "Invalid Object id '" + uitos(data.object_id) + "', can't call method.");
  259. #endif
  260. call_with_variant_args(data.instance, data.method, p_arguments, p_argcount, r_call_error);
  261. }
  262. CallableCustomMethodPointer(T *p_instance, void (T::*p_method)(P...)) {
  263. zeromem(&data, sizeof(Data)); // Clear beforehand, may have padding bytes.
  264. data.instance = p_instance;
  265. #ifdef DEBUG_ENABLED
  266. data.object_id = p_instance->get_instance_id();
  267. #endif
  268. data.method = p_method;
  269. _setup((uint32_t *)&data, sizeof(Data));
  270. }
  271. };
  272. template <class T, class... P>
  273. Callable create_custom_callable_function_pointer(T *p_instance,
  274. #ifdef DEBUG_METHODS_ENABLED
  275. const char *p_func_text,
  276. #endif
  277. void (T::*p_method)(P...)) {
  278. typedef CallableCustomMethodPointer<T, P...> CCMP; // Messes with memnew otherwise.
  279. CCMP *ccmp = memnew(CCMP(p_instance, p_method));
  280. #ifdef DEBUG_METHODS_ENABLED
  281. ccmp->set_text(p_func_text + 1); // Try to get rid of the ampersand.
  282. #endif
  283. return Callable(ccmp);
  284. }
  285. // VERSION WITH RETURN
  286. template <class T, class R, class... P, size_t... Is>
  287. void call_with_variant_args_ret_helper(T *p_instance, R (T::*p_method)(P...), const Variant **p_args, Variant &r_ret, Callable::CallError &r_error, IndexSequence<Is...>) {
  288. r_error.error = Callable::CallError::CALL_OK;
  289. #ifdef DEBUG_METHODS_ENABLED
  290. r_ret = (p_instance->*p_method)(VariantCasterAndValidate<P>::cast(p_args, Is, r_error)...);
  291. #else
  292. r_ret = (p_instance->*p_method)(VariantCaster<P>::cast(*p_args[Is])...);
  293. #endif
  294. }
  295. template <class T, class R, class... P>
  296. void call_with_variant_args_ret(T *p_instance, R (T::*p_method)(P...), const Variant **p_args, int p_argcount, Variant &r_ret, Callable::CallError &r_error) {
  297. #ifdef DEBUG_METHODS_ENABLED
  298. if ((size_t)p_argcount > sizeof...(P)) {
  299. r_error.error = Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS;
  300. r_error.argument = sizeof...(P);
  301. return;
  302. }
  303. if ((size_t)p_argcount < sizeof...(P)) {
  304. r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  305. r_error.argument = sizeof...(P);
  306. return;
  307. }
  308. #endif
  309. call_with_variant_args_ret_helper<T, R, P...>(p_instance, p_method, p_args, r_ret, r_error, BuildIndexSequence<sizeof...(P)>{});
  310. }
  311. template <class T, class R, class... P>
  312. class CallableCustomMethodPointerRet : public CallableCustomMethodPointerBase {
  313. struct Data {
  314. T *instance;
  315. #ifdef DEBUG_ENABLED
  316. uint64_t object_id;
  317. #endif
  318. R(T::*method)
  319. (P...);
  320. } data;
  321. public:
  322. virtual ObjectID get_object() const {
  323. #ifdef DEBUG_ENABLED
  324. if (ObjectDB::get_instance(ObjectID(data.object_id)) == nullptr) {
  325. return ObjectID();
  326. }
  327. #endif
  328. return data.instance->get_instance_id();
  329. }
  330. virtual void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const {
  331. #ifdef DEBUG_ENABLED
  332. ERR_FAIL_COND_MSG(ObjectDB::get_instance(ObjectID(data.object_id)) == nullptr, "Invalid Object id '" + uitos(data.object_id) + "', can't call method.");
  333. #endif
  334. call_with_variant_args_ret(data.instance, data.method, p_arguments, p_argcount, r_return_value, r_call_error);
  335. }
  336. CallableCustomMethodPointerRet(T *p_instance, R (T::*p_method)(P...)) {
  337. zeromem(&data, sizeof(Data)); // Clear beforehand, may have padding bytes.
  338. data.instance = p_instance;
  339. #ifdef DEBUG_ENABLED
  340. data.object_id = p_instance->get_instance_id();
  341. #endif
  342. data.method = p_method;
  343. _setup((uint32_t *)&data, sizeof(Data));
  344. }
  345. };
  346. template <class T, class R, class... P>
  347. Callable create_custom_callable_function_pointer(T *p_instance,
  348. #ifdef DEBUG_METHODS_ENABLED
  349. const char *p_func_text,
  350. #endif
  351. R (T::*p_method)(P...)) {
  352. typedef CallableCustomMethodPointerRet<T, R, P...> CCMP; // Messes with memnew otherwise.
  353. CCMP *ccmp = memnew(CCMP(p_instance, p_method));
  354. #ifdef DEBUG_METHODS_ENABLED
  355. ccmp->set_text(p_func_text + 1); // Try to get rid of the ampersand.
  356. #endif
  357. return Callable(ccmp);
  358. }
  359. // CONST VERSION WITH RETURN
  360. template <class T, class R, class... P, size_t... Is>
  361. void call_with_variant_args_retc_helper(T *p_instance, R (T::*p_method)(P...) const, const Variant **p_args, Variant &r_ret, Callable::CallError &r_error, IndexSequence<Is...>) {
  362. r_error.error = Callable::CallError::CALL_OK;
  363. #ifdef DEBUG_METHODS_ENABLED
  364. r_ret = (p_instance->*p_method)(VariantCasterAndValidate<P>::cast(p_args, Is, r_error)...);
  365. #else
  366. r_ret = (p_instance->*p_method)(VariantCaster<P>::cast(*p_args[Is])...);
  367. #endif
  368. }
  369. template <class T, class R, class... P>
  370. void call_with_variant_args_retc(T *p_instance, R (T::*p_method)(P...) const, const Variant **p_args, int p_argcount, Variant &r_ret, Callable::CallError &r_error) {
  371. #ifdef DEBUG_METHODS_ENABLED
  372. if ((size_t)p_argcount > sizeof...(P)) {
  373. r_error.error = Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS;
  374. r_error.argument = sizeof...(P);
  375. return;
  376. }
  377. if ((size_t)p_argcount < sizeof...(P)) {
  378. r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  379. r_error.argument = sizeof...(P);
  380. return;
  381. }
  382. #endif
  383. call_with_variant_args_retc_helper<T, R, P...>(p_instance, p_method, p_args, r_ret, r_error, BuildIndexSequence<sizeof...(P)>{});
  384. }
  385. template <class T, class R, class... P>
  386. class CallableCustomMethodPointerRetC : public CallableCustomMethodPointerBase {
  387. struct Data {
  388. T *instance;
  389. #ifdef DEBUG_ENABLED
  390. uint64_t object_id;
  391. #endif
  392. R(T::*method)
  393. (P...) const;
  394. } data;
  395. public:
  396. virtual ObjectID get_object() const {
  397. #ifdef DEBUG_ENABLED
  398. if (ObjectDB::get_instance(ObjectID(data.object_id)) == nullptr) {
  399. return ObjectID();
  400. }
  401. #endif
  402. return data.instance->get_instance_id();
  403. }
  404. virtual void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const {
  405. #ifdef DEBUG_ENABLED
  406. ERR_FAIL_COND_MSG(ObjectDB::get_instance(ObjectID(data.object_id)) == nullptr, "Invalid Object id '" + uitos(data.object_id) + "', can't call method.");
  407. #endif
  408. call_with_variant_args_retc(data.instance, data.method, p_arguments, p_argcount, r_return_value, r_call_error);
  409. }
  410. CallableCustomMethodPointerRetC(T *p_instance, R (T::*p_method)(P...) const) {
  411. zeromem(&data, sizeof(Data)); // Clear beforehand, may have padding bytes.
  412. data.instance = p_instance;
  413. #ifdef DEBUG_ENABLED
  414. data.object_id = p_instance->get_instance_id();
  415. #endif
  416. data.method = p_method;
  417. _setup((uint32_t *)&data, sizeof(Data));
  418. }
  419. };
  420. template <class T, class R, class... P>
  421. Callable create_custom_callable_function_pointer(T *p_instance,
  422. #ifdef DEBUG_METHODS_ENABLED
  423. const char *p_func_text,
  424. #endif
  425. R (T::*p_method)(P...) const) {
  426. typedef CallableCustomMethodPointerRetC<T, R, P...> CCMP; // Messes with memnew otherwise.
  427. CCMP *ccmp = memnew(CCMP(p_instance, p_method));
  428. #ifdef DEBUG_METHODS_ENABLED
  429. ccmp->set_text(p_func_text + 1); // Try to get rid of the ampersand.
  430. #endif
  431. return Callable(ccmp);
  432. }
  433. #ifdef DEBUG_METHODS_ENABLED
  434. #define callable_mp(I, M) create_custom_callable_function_pointer(I, #M, M)
  435. #else
  436. #define callable_mp(I, M) create_custom_callable_function_pointer(I, M)
  437. #endif
  438. template <class T, class R, class... P, size_t... Is>
  439. void call_with_variant_args_retc_static_helper(T *p_instance, R (*p_method)(T *, P...), const Variant **p_args, Variant &r_ret, Callable::CallError &r_error, IndexSequence<Is...>) {
  440. r_error.error = Callable::CallError::CALL_OK;
  441. #ifdef DEBUG_METHODS_ENABLED
  442. r_ret = (p_method)(p_instance, VariantCasterAndValidate<P>::cast(p_args, Is, r_error)...);
  443. #else
  444. r_ret = (p_method)(p_instance, VariantCaster<P>::cast(*p_args[Is])...);
  445. #endif
  446. }
  447. #if defined(DEBUG_METHODS_ENABLED) && defined(__GNUC__) && !defined(__clang__)
  448. #pragma GCC diagnostic pop
  449. #endif
  450. #endif // CALLABLE_METHOD_POINTER_H