2
0

binder_common.h 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909
  1. /*************************************************************************/
  2. /* binder_common.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 BINDER_COMMON_H
  31. #define BINDER_COMMON_H
  32. #include "core/object/object.h"
  33. #include "core/templates/list.h"
  34. #include "core/templates/simple_type.h"
  35. #include "core/typedefs.h"
  36. #include "core/variant/method_ptrcall.h"
  37. #include "core/variant/type_info.h"
  38. #include "core/variant/variant.h"
  39. #include "core/variant/variant_internal.h"
  40. #include <stdio.h>
  41. template <class T>
  42. struct VariantCaster {
  43. static _FORCE_INLINE_ T cast(const Variant &p_variant) {
  44. return p_variant;
  45. }
  46. };
  47. template <class T>
  48. struct VariantCaster<T &> {
  49. static _FORCE_INLINE_ T cast(const Variant &p_variant) {
  50. return p_variant;
  51. }
  52. };
  53. template <class T>
  54. struct VariantCaster<const T &> {
  55. static _FORCE_INLINE_ T cast(const Variant &p_variant) {
  56. return p_variant;
  57. }
  58. };
  59. #define VARIANT_ENUM_CAST(m_enum) \
  60. MAKE_ENUM_TYPE_INFO(m_enum) \
  61. template <> \
  62. struct VariantCaster<m_enum> { \
  63. static _FORCE_INLINE_ m_enum cast(const Variant &p_variant) { \
  64. return (m_enum)p_variant.operator int(); \
  65. } \
  66. }; \
  67. template <> \
  68. struct PtrToArg<m_enum> { \
  69. _FORCE_INLINE_ static m_enum convert(const void *p_ptr) { \
  70. return m_enum(*reinterpret_cast<const int *>(p_ptr)); \
  71. } \
  72. _FORCE_INLINE_ static void encode(m_enum p_val, const void *p_ptr) { \
  73. *(int *)p_ptr = p_val; \
  74. } \
  75. };
  76. // Object enum casts must go here
  77. VARIANT_ENUM_CAST(Object::ConnectFlags);
  78. VARIANT_ENUM_CAST(Vector3::Axis);
  79. VARIANT_ENUM_CAST(Error);
  80. VARIANT_ENUM_CAST(Side);
  81. VARIANT_ENUM_CAST(ClockDirection);
  82. VARIANT_ENUM_CAST(Corner);
  83. VARIANT_ENUM_CAST(Orientation);
  84. VARIANT_ENUM_CAST(HAlign);
  85. VARIANT_ENUM_CAST(VAlign);
  86. VARIANT_ENUM_CAST(PropertyHint);
  87. VARIANT_ENUM_CAST(PropertyUsageFlags);
  88. VARIANT_ENUM_CAST(Variant::Type);
  89. VARIANT_ENUM_CAST(Variant::Operator);
  90. template <>
  91. struct VariantCaster<char32_t> {
  92. static _FORCE_INLINE_ char32_t cast(const Variant &p_variant) {
  93. return (char32_t)p_variant.operator int();
  94. }
  95. };
  96. template <>
  97. struct PtrToArg<char32_t> {
  98. _FORCE_INLINE_ static char32_t convert(const void *p_ptr) {
  99. return char32_t(*reinterpret_cast<const int *>(p_ptr));
  100. }
  101. _FORCE_INLINE_ static void encode(char32_t p_val, const void *p_ptr) {
  102. *(int *)p_ptr = p_val;
  103. }
  104. };
  105. template <typename T>
  106. struct VariantObjectClassChecker {
  107. static _FORCE_INLINE_ bool check(const Variant &p_variant) {
  108. return true;
  109. }
  110. };
  111. template <typename T>
  112. class Ref;
  113. template <typename T>
  114. struct VariantObjectClassChecker<const Ref<T> &> {
  115. static _FORCE_INLINE_ bool check(const Variant &p_variant) {
  116. Object *obj = p_variant;
  117. const Ref<T> node = p_variant;
  118. return node.ptr() || !obj;
  119. }
  120. };
  121. template <>
  122. struct VariantObjectClassChecker<Node *> {
  123. static _FORCE_INLINE_ bool check(const Variant &p_variant) {
  124. Object *obj = p_variant;
  125. Node *node = p_variant;
  126. return node || !obj;
  127. }
  128. };
  129. template <>
  130. struct VariantObjectClassChecker<Control *> {
  131. static _FORCE_INLINE_ bool check(const Variant &p_variant) {
  132. Object *obj = p_variant;
  133. Control *control = p_variant;
  134. return control || !obj;
  135. }
  136. };
  137. #ifdef DEBUG_METHODS_ENABLED
  138. template <class T>
  139. struct VariantCasterAndValidate {
  140. static _FORCE_INLINE_ T cast(const Variant **p_args, uint32_t p_arg_idx, Callable::CallError &r_error) {
  141. Variant::Type argtype = GetTypeInfo<T>::VARIANT_TYPE;
  142. if (!Variant::can_convert_strict(p_args[p_arg_idx]->get_type(), argtype) ||
  143. !VariantObjectClassChecker<T>::check(p_args[p_arg_idx])) {
  144. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  145. r_error.argument = p_arg_idx;
  146. r_error.expected = argtype;
  147. }
  148. return VariantCaster<T>::cast(*p_args[p_arg_idx]);
  149. }
  150. };
  151. template <class T>
  152. struct VariantCasterAndValidate<T &> {
  153. static _FORCE_INLINE_ T cast(const Variant **p_args, uint32_t p_arg_idx, Callable::CallError &r_error) {
  154. Variant::Type argtype = GetTypeInfo<T>::VARIANT_TYPE;
  155. if (!Variant::can_convert_strict(p_args[p_arg_idx]->get_type(), argtype) ||
  156. !VariantObjectClassChecker<T>::check(p_args[p_arg_idx])) {
  157. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  158. r_error.argument = p_arg_idx;
  159. r_error.expected = argtype;
  160. }
  161. return VariantCaster<T>::cast(*p_args[p_arg_idx]);
  162. }
  163. };
  164. template <class T>
  165. struct VariantCasterAndValidate<const T &> {
  166. static _FORCE_INLINE_ T cast(const Variant **p_args, uint32_t p_arg_idx, Callable::CallError &r_error) {
  167. Variant::Type argtype = GetTypeInfo<T>::VARIANT_TYPE;
  168. if (!Variant::can_convert_strict(p_args[p_arg_idx]->get_type(), argtype) ||
  169. !VariantObjectClassChecker<T>::check(p_args[p_arg_idx])) {
  170. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  171. r_error.argument = p_arg_idx;
  172. r_error.expected = argtype;
  173. }
  174. return VariantCaster<T>::cast(*p_args[p_arg_idx]);
  175. }
  176. };
  177. #endif // DEBUG_METHODS_ENABLED
  178. template <class T, class... P, size_t... Is>
  179. void call_with_variant_args_helper(T *p_instance, void (T::*p_method)(P...), const Variant **p_args, Callable::CallError &r_error, IndexSequence<Is...>) {
  180. r_error.error = Callable::CallError::CALL_OK;
  181. #ifdef DEBUG_METHODS_ENABLED
  182. (p_instance->*p_method)(VariantCasterAndValidate<P>::cast(p_args, Is, r_error)...);
  183. #else
  184. (p_instance->*p_method)(VariantCaster<P>::cast(*p_args[Is])...);
  185. #endif
  186. (void)(p_args); //avoid warning
  187. }
  188. template <class T, class... P, size_t... Is>
  189. void call_with_variant_argsc_helper(T *p_instance, void (T::*p_method)(P...) const, const Variant **p_args, Callable::CallError &r_error, IndexSequence<Is...>) {
  190. r_error.error = Callable::CallError::CALL_OK;
  191. #ifdef DEBUG_METHODS_ENABLED
  192. (p_instance->*p_method)(VariantCasterAndValidate<P>::cast(p_args, Is, r_error)...);
  193. #else
  194. (p_instance->*p_method)(VariantCaster<P>::cast(*p_args[Is])...);
  195. #endif
  196. (void)(p_args); //avoid warning
  197. }
  198. template <class T, class... P, size_t... Is>
  199. void call_with_ptr_args_helper(T *p_instance, void (T::*p_method)(P...), const void **p_args, IndexSequence<Is...>) {
  200. (p_instance->*p_method)(PtrToArg<P>::convert(p_args[Is])...);
  201. }
  202. template <class T, class... P, size_t... Is>
  203. void call_with_ptr_argsc_helper(T *p_instance, void (T::*p_method)(P...) const, const void **p_args, IndexSequence<Is...>) {
  204. (p_instance->*p_method)(PtrToArg<P>::convert(p_args[Is])...);
  205. }
  206. template <class T, class R, class... P, size_t... Is>
  207. void call_with_ptr_args_ret_helper(T *p_instance, R (T::*p_method)(P...), const void **p_args, void *r_ret, IndexSequence<Is...>) {
  208. PtrToArg<R>::encode((p_instance->*p_method)(PtrToArg<P>::convert(p_args[Is])...), r_ret);
  209. }
  210. template <class T, class R, class... P, size_t... Is>
  211. 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...>) {
  212. PtrToArg<R>::encode((p_instance->*p_method)(PtrToArg<P>::convert(p_args[Is])...), r_ret);
  213. }
  214. template <class T, class... P, size_t... Is>
  215. void call_with_ptr_args_static_helper(T *p_instance, void (*p_method)(T *, P...), const void **p_args, IndexSequence<Is...>) {
  216. p_method(p_instance, PtrToArg<P>::convert(p_args[Is])...);
  217. }
  218. template <class T, class R, class... P, size_t... Is>
  219. 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...>) {
  220. PtrToArg<R>::encode(p_method(p_instance, PtrToArg<P>::convert(p_args[Is])...), r_ret);
  221. }
  222. template <class R, class... P, size_t... Is>
  223. void call_with_ptr_args_static_method_ret_helper(R (*p_method)(P...), const void **p_args, void *r_ret, IndexSequence<Is...>) {
  224. PtrToArg<R>::encode(p_method(PtrToArg<P>::convert(p_args[Is])...), r_ret);
  225. }
  226. template <class... P, size_t... Is>
  227. void call_with_ptr_args_static_method_helper(void (*p_method)(P...), const void **p_args, IndexSequence<Is...>) {
  228. p_method(PtrToArg<P>::convert(p_args[Is])...);
  229. }
  230. template <class T, class... P, size_t... Is>
  231. void call_with_validated_variant_args_helper(T *p_instance, void (T::*p_method)(P...), const Variant **p_args, IndexSequence<Is...>) {
  232. (p_instance->*p_method)((VariantInternalAccessor<typename GetSimpleTypeT<P>::type_t>::get(p_args[Is]))...);
  233. }
  234. template <class T, class... P, size_t... Is>
  235. void call_with_validated_variant_argsc_helper(T *p_instance, void (T::*p_method)(P...) const, const Variant **p_args, IndexSequence<Is...>) {
  236. (p_instance->*p_method)((VariantInternalAccessor<typename GetSimpleTypeT<P>::type_t>::get(p_args[Is]))...);
  237. }
  238. template <class T, class R, class... P, size_t... Is>
  239. 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...>) {
  240. VariantInternalAccessor<typename GetSimpleTypeT<R>::type_t>::set(r_ret, (p_instance->*p_method)((VariantInternalAccessor<typename GetSimpleTypeT<P>::type_t>::get(p_args[Is]))...));
  241. }
  242. template <class T, class R, class... P, size_t... Is>
  243. 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...>) {
  244. VariantInternalAccessor<typename GetSimpleTypeT<R>::type_t>::set(r_ret, (p_instance->*p_method)((VariantInternalAccessor<typename GetSimpleTypeT<P>::type_t>::get(p_args[Is]))...));
  245. }
  246. template <class T, class R, class... P, size_t... Is>
  247. 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...>) {
  248. VariantInternalAccessor<typename GetSimpleTypeT<R>::type_t>::set(r_ret, p_method(p_instance, (VariantInternalAccessor<typename GetSimpleTypeT<P>::type_t>::get(p_args[Is]))...));
  249. }
  250. template <class T, class... P, size_t... Is>
  251. void call_with_validated_variant_args_static_helper(T *p_instance, void (*p_method)(T *, P...), const Variant **p_args, IndexSequence<Is...>) {
  252. p_method(p_instance, (VariantInternalAccessor<typename GetSimpleTypeT<P>::type_t>::get(p_args[Is]))...);
  253. }
  254. template <class R, class... P, size_t... Is>
  255. void call_with_validated_variant_args_static_method_ret_helper(R (*p_method)(P...), const Variant **p_args, Variant *r_ret, IndexSequence<Is...>) {
  256. VariantInternalAccessor<typename GetSimpleTypeT<R>::type_t>::set(r_ret, p_method((VariantInternalAccessor<typename GetSimpleTypeT<P>::type_t>::get(p_args[Is]))...));
  257. }
  258. template <class... P, size_t... Is>
  259. void call_with_validated_variant_args_static_method_helper(void (*p_method)(P...), const Variant **p_args, IndexSequence<Is...>) {
  260. p_method((VariantInternalAccessor<typename GetSimpleTypeT<P>::type_t>::get(p_args[Is]))...);
  261. }
  262. template <class T, class... P>
  263. void call_with_variant_args(T *p_instance, void (T::*p_method)(P...), const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  264. #ifdef DEBUG_METHODS_ENABLED
  265. if ((size_t)p_argcount > sizeof...(P)) {
  266. r_error.error = Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS;
  267. r_error.argument = sizeof...(P);
  268. return;
  269. }
  270. if ((size_t)p_argcount < sizeof...(P)) {
  271. r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  272. r_error.argument = sizeof...(P);
  273. return;
  274. }
  275. #endif
  276. call_with_variant_args_helper<T, P...>(p_instance, p_method, p_args, r_error, BuildIndexSequence<sizeof...(P)>{});
  277. }
  278. template <class T, class... P>
  279. void call_with_variant_args_dv(T *p_instance, void (T::*p_method)(P...), const Variant **p_args, int p_argcount, Callable::CallError &r_error, const Vector<Variant> &default_values) {
  280. #ifdef DEBUG_ENABLED
  281. if ((size_t)p_argcount > sizeof...(P)) {
  282. r_error.error = Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS;
  283. r_error.argument = sizeof...(P);
  284. return;
  285. }
  286. #endif
  287. int32_t missing = (int32_t)sizeof...(P) - (int32_t)p_argcount;
  288. int32_t dvs = default_values.size();
  289. #ifdef DEBUG_ENABLED
  290. if (missing > dvs) {
  291. r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  292. r_error.argument = sizeof...(P);
  293. return;
  294. }
  295. #endif
  296. const Variant *args[sizeof...(P) == 0 ? 1 : sizeof...(P)]; //avoid zero sized array
  297. for (int32_t i = 0; i < (int32_t)sizeof...(P); i++) {
  298. if (i < p_argcount) {
  299. args[i] = p_args[i];
  300. } else {
  301. args[i] = &default_values[i - p_argcount + (dvs - missing)];
  302. }
  303. }
  304. call_with_variant_args_helper(p_instance, p_method, args, r_error, BuildIndexSequence<sizeof...(P)>{});
  305. }
  306. template <class T, class... P>
  307. void call_with_variant_argsc(T *p_instance, void (T::*p_method)(P...) const, const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  308. #ifdef DEBUG_METHODS_ENABLED
  309. if ((size_t)p_argcount > sizeof...(P)) {
  310. r_error.error = Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS;
  311. r_error.argument = sizeof...(P);
  312. return;
  313. }
  314. if ((size_t)p_argcount < sizeof...(P)) {
  315. r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  316. r_error.argument = sizeof...(P);
  317. return;
  318. }
  319. #endif
  320. call_with_variant_args_helper<T, P...>(p_instance, p_method, p_args, r_error, BuildIndexSequence<sizeof...(P)>{});
  321. }
  322. template <class T, class... P>
  323. void call_with_variant_argsc_dv(T *p_instance, void (T::*p_method)(P...) const, const Variant **p_args, int p_argcount, Callable::CallError &r_error, const Vector<Variant> &default_values) {
  324. #ifdef DEBUG_ENABLED
  325. if ((size_t)p_argcount > sizeof...(P)) {
  326. r_error.error = Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS;
  327. r_error.argument = sizeof...(P);
  328. return;
  329. }
  330. #endif
  331. int32_t missing = (int32_t)sizeof...(P) - (int32_t)p_argcount;
  332. int32_t dvs = default_values.size();
  333. #ifdef DEBUG_ENABLED
  334. if (missing > dvs) {
  335. r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  336. r_error.argument = sizeof...(P);
  337. return;
  338. }
  339. #endif
  340. const Variant *args[sizeof...(P) == 0 ? 1 : sizeof...(P)]; //avoid zero sized array
  341. for (int32_t i = 0; i < (int32_t)sizeof...(P); i++) {
  342. if (i < p_argcount) {
  343. args[i] = p_args[i];
  344. } else {
  345. args[i] = &default_values[i - p_argcount + (dvs - missing)];
  346. }
  347. }
  348. call_with_variant_argsc_helper(p_instance, p_method, args, r_error, BuildIndexSequence<sizeof...(P)>{});
  349. }
  350. template <class T, class R, class... P>
  351. void call_with_variant_args_ret_dv(T *p_instance, R (T::*p_method)(P...), const Variant **p_args, int p_argcount, Variant &r_ret, Callable::CallError &r_error, const Vector<Variant> &default_values) {
  352. #ifdef DEBUG_ENABLED
  353. if ((size_t)p_argcount > sizeof...(P)) {
  354. r_error.error = Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS;
  355. r_error.argument = sizeof...(P);
  356. return;
  357. }
  358. #endif
  359. int32_t missing = (int32_t)sizeof...(P) - (int32_t)p_argcount;
  360. int32_t dvs = default_values.size();
  361. #ifdef DEBUG_ENABLED
  362. if (missing > dvs) {
  363. r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  364. r_error.argument = sizeof...(P);
  365. return;
  366. }
  367. #endif
  368. const Variant *args[sizeof...(P) == 0 ? 1 : sizeof...(P)]; //avoid zero sized array
  369. for (int32_t i = 0; i < (int32_t)sizeof...(P); i++) {
  370. if (i < p_argcount) {
  371. args[i] = p_args[i];
  372. } else {
  373. args[i] = &default_values[i - p_argcount + (dvs - missing)];
  374. }
  375. }
  376. call_with_variant_args_ret_helper(p_instance, p_method, args, r_ret, r_error, BuildIndexSequence<sizeof...(P)>{});
  377. }
  378. template <class T, class R, class... P>
  379. void call_with_variant_args_retc_dv(T *p_instance, R (T::*p_method)(P...) const, const Variant **p_args, int p_argcount, Variant &r_ret, Callable::CallError &r_error, const Vector<Variant> &default_values) {
  380. #ifdef DEBUG_ENABLED
  381. if ((size_t)p_argcount > sizeof...(P)) {
  382. r_error.error = Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS;
  383. r_error.argument = sizeof...(P);
  384. return;
  385. }
  386. #endif
  387. int32_t missing = (int32_t)sizeof...(P) - (int32_t)p_argcount;
  388. int32_t dvs = default_values.size();
  389. #ifdef DEBUG_ENABLED
  390. if (missing > dvs) {
  391. r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  392. r_error.argument = sizeof...(P);
  393. return;
  394. }
  395. #endif
  396. const Variant *args[sizeof...(P) == 0 ? 1 : sizeof...(P)]; //avoid zero sized array
  397. for (int32_t i = 0; i < (int32_t)sizeof...(P); i++) {
  398. if (i < p_argcount) {
  399. args[i] = p_args[i];
  400. } else {
  401. args[i] = &default_values[i - p_argcount + (dvs - missing)];
  402. }
  403. }
  404. call_with_variant_args_retc_helper(p_instance, p_method, args, r_ret, r_error, BuildIndexSequence<sizeof...(P)>{});
  405. }
  406. template <class T, class... P>
  407. void call_with_ptr_args(T *p_instance, void (T::*p_method)(P...), const void **p_args) {
  408. call_with_ptr_args_helper<T, P...>(p_instance, p_method, p_args, BuildIndexSequence<sizeof...(P)>{});
  409. }
  410. template <class T, class... P>
  411. void call_with_ptr_argsc(T *p_instance, void (T::*p_method)(P...) const, const void **p_args) {
  412. call_with_ptr_argsc_helper<T, P...>(p_instance, p_method, p_args, BuildIndexSequence<sizeof...(P)>{});
  413. }
  414. template <class T, class R, class... P>
  415. void call_with_ptr_args_ret(T *p_instance, R (T::*p_method)(P...), const void **p_args, void *r_ret) {
  416. call_with_ptr_args_ret_helper<T, R, P...>(p_instance, p_method, p_args, r_ret, BuildIndexSequence<sizeof...(P)>{});
  417. }
  418. template <class T, class R, class... P>
  419. void call_with_ptr_args_retc(T *p_instance, R (T::*p_method)(P...) const, const void **p_args, void *r_ret) {
  420. call_with_ptr_args_retc_helper<T, R, P...>(p_instance, p_method, p_args, r_ret, BuildIndexSequence<sizeof...(P)>{});
  421. }
  422. template <class T, class... P>
  423. void call_with_ptr_args_static(T *p_instance, void (*p_method)(T *, P...), const void **p_args) {
  424. call_with_ptr_args_static_helper<T, P...>(p_instance, p_method, p_args, BuildIndexSequence<sizeof...(P)>{});
  425. }
  426. template <class T, class R, class... P>
  427. void call_with_ptr_args_static_retc(T *p_instance, R (*p_method)(T *, P...), const void **p_args, void *r_ret) {
  428. call_with_ptr_args_static_retc_helper<T, R, P...>(p_instance, p_method, p_args, r_ret, BuildIndexSequence<sizeof...(P)>{});
  429. }
  430. template <class R, class... P>
  431. void call_with_ptr_args_static_method_ret(R (*p_method)(P...), const void **p_args, void *r_ret) {
  432. call_with_ptr_args_static_method_ret_helper<R, P...>(p_method, p_args, r_ret, BuildIndexSequence<sizeof...(P)>{});
  433. }
  434. template <class... P>
  435. void call_with_ptr_args_static_method(void (*p_method)(P...), const void **p_args) {
  436. call_with_ptr_args_static_method_helper<P...>(p_method, p_args, BuildIndexSequence<sizeof...(P)>{});
  437. }
  438. template <class T, class... P>
  439. void call_with_validated_variant_args(Variant *base, void (T::*p_method)(P...), const Variant **p_args) {
  440. call_with_validated_variant_args_helper<T, P...>(VariantGetInternalPtr<T>::get_ptr(base), p_method, p_args, BuildIndexSequence<sizeof...(P)>{});
  441. }
  442. template <class T, class R, class... P>
  443. void call_with_validated_variant_args_ret(Variant *base, R (T::*p_method)(P...), const Variant **p_args, Variant *r_ret) {
  444. call_with_validated_variant_args_ret_helper<T, R, P...>(VariantGetInternalPtr<T>::get_ptr(base), p_method, p_args, r_ret, BuildIndexSequence<sizeof...(P)>{});
  445. }
  446. template <class T, class R, class... P>
  447. void call_with_validated_variant_args_retc(Variant *base, R (T::*p_method)(P...) const, const Variant **p_args, Variant *r_ret) {
  448. call_with_validated_variant_args_retc_helper<T, R, P...>(VariantGetInternalPtr<T>::get_ptr(base), p_method, p_args, r_ret, BuildIndexSequence<sizeof...(P)>{});
  449. }
  450. template <class T, class... P>
  451. void call_with_validated_variant_args_static(Variant *base, void (*p_method)(T *, P...), const Variant **p_args) {
  452. call_with_validated_variant_args_static_helper<T, P...>(VariantGetInternalPtr<T>::get_ptr(base), p_method, p_args, BuildIndexSequence<sizeof...(P)>{});
  453. }
  454. template <class T, class R, class... P>
  455. void call_with_validated_variant_args_static_retc(Variant *base, R (*p_method)(T *, P...), const Variant **p_args, Variant *r_ret) {
  456. 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)>{});
  457. }
  458. template <class... P>
  459. void call_with_validated_variant_args_static_method(void (*p_method)(P...), const Variant **p_args) {
  460. call_with_validated_variant_args_static_method_helper<P...>(p_method, p_args, BuildIndexSequence<sizeof...(P)>{});
  461. }
  462. template <class R, class... P>
  463. void call_with_validated_variant_args_static_method_ret(R (*p_method)(P...), const Variant **p_args, Variant *r_ret) {
  464. call_with_validated_variant_args_static_method_ret_helper<R, P...>(p_method, p_args, r_ret, BuildIndexSequence<sizeof...(P)>{});
  465. }
  466. // GCC raises "parameter 'p_args' set but not used" when P = {},
  467. // it's not clever enough to treat other P values as making this branch valid.
  468. #if defined(DEBUG_METHODS_ENABLED) && defined(__GNUC__) && !defined(__clang__)
  469. #pragma GCC diagnostic push
  470. #pragma GCC diagnostic ignored "-Wunused-but-set-parameter"
  471. #endif
  472. #ifdef DEBUG_METHODS_ENABLED
  473. template <class Q>
  474. void call_get_argument_type_helper(int p_arg, int &index, Variant::Type &type) {
  475. if (p_arg == index) {
  476. type = GetTypeInfo<Q>::VARIANT_TYPE;
  477. }
  478. index++;
  479. }
  480. template <class... P>
  481. Variant::Type call_get_argument_type(int p_arg) {
  482. Variant::Type type = Variant::NIL;
  483. int index = 0;
  484. // I think rocket science is simpler than modern C++.
  485. using expand_type = int[];
  486. expand_type a{ 0, (call_get_argument_type_helper<P>(p_arg, index, type), 0)... };
  487. (void)a; // Suppress (valid, but unavoidable) -Wunused-variable warning.
  488. (void)index; // Suppress GCC warning.
  489. return type;
  490. }
  491. template <class Q>
  492. void call_get_argument_type_info_helper(int p_arg, int &index, PropertyInfo &info) {
  493. if (p_arg == index) {
  494. info = GetTypeInfo<Q>::get_class_info();
  495. }
  496. index++;
  497. }
  498. template <class... P>
  499. void call_get_argument_type_info(int p_arg, PropertyInfo &info) {
  500. int index = 0;
  501. // I think rocket science is simpler than modern C++.
  502. using expand_type = int[];
  503. expand_type a{ 0, (call_get_argument_type_info_helper<P>(p_arg, index, info), 0)... };
  504. (void)a; // Suppress (valid, but unavoidable) -Wunused-variable warning.
  505. (void)index; // Suppress GCC warning.
  506. }
  507. template <class Q>
  508. void call_get_argument_metadata_helper(int p_arg, int &index, GodotTypeInfo::Metadata &md) {
  509. if (p_arg == index) {
  510. md = GetTypeInfo<Q>::METADATA;
  511. }
  512. index++;
  513. }
  514. template <class... P>
  515. GodotTypeInfo::Metadata call_get_argument_metadata(int p_arg) {
  516. GodotTypeInfo::Metadata md = GodotTypeInfo::METADATA_NONE;
  517. int index = 0;
  518. // I think rocket science is simpler than modern C++.
  519. using expand_type = int[];
  520. expand_type a{ 0, (call_get_argument_metadata_helper<P>(p_arg, index, md), 0)... };
  521. (void)a; // Suppress (valid, but unavoidable) -Wunused-variable warning.
  522. (void)index;
  523. return md;
  524. }
  525. #else
  526. template <class... P>
  527. Variant::Type call_get_argument_type(int p_arg) {
  528. return Variant::NIL;
  529. }
  530. #endif // DEBUG_METHODS_ENABLED
  531. //////////////////////
  532. template <class T, class R, class... P, size_t... Is>
  533. 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...>) {
  534. r_error.error = Callable::CallError::CALL_OK;
  535. #ifdef DEBUG_METHODS_ENABLED
  536. r_ret = (p_instance->*p_method)(VariantCasterAndValidate<P>::cast(p_args, Is, r_error)...);
  537. #else
  538. r_ret = (p_instance->*p_method)(VariantCaster<P>::cast(*p_args[Is])...);
  539. #endif
  540. }
  541. template <class R, class... P, size_t... Is>
  542. void call_with_variant_args_static_ret(R (*p_method)(P...), const Variant **p_args, Variant &r_ret, Callable::CallError &r_error, IndexSequence<Is...>) {
  543. r_error.error = Callable::CallError::CALL_OK;
  544. #ifdef DEBUG_METHODS_ENABLED
  545. r_ret = (p_method)(VariantCasterAndValidate<P>::cast(p_args, Is, r_error)...);
  546. #else
  547. r_ret = (p_method)(VariantCaster<P>::cast(*p_args[Is])...);
  548. #endif
  549. }
  550. template <class... P, size_t... Is>
  551. void call_with_variant_args_static(void (*p_method)(P...), const Variant **p_args, Callable::CallError &r_error, IndexSequence<Is...>) {
  552. r_error.error = Callable::CallError::CALL_OK;
  553. #ifdef DEBUG_METHODS_ENABLED
  554. (p_method)(VariantCasterAndValidate<P>::cast(p_args, Is, r_error)...);
  555. #else
  556. (p_method)(VariantCaster<P>::cast(*p_args[Is])...);
  557. #endif
  558. }
  559. template <class T, class R, class... P>
  560. 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) {
  561. #ifdef DEBUG_METHODS_ENABLED
  562. if ((size_t)p_argcount > sizeof...(P)) {
  563. r_error.error = Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS;
  564. r_error.argument = sizeof...(P);
  565. return;
  566. }
  567. if ((size_t)p_argcount < sizeof...(P)) {
  568. r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  569. r_error.argument = sizeof...(P);
  570. return;
  571. }
  572. #endif
  573. call_with_variant_args_ret_helper<T, R, P...>(p_instance, p_method, p_args, r_ret, r_error, BuildIndexSequence<sizeof...(P)>{});
  574. }
  575. template <class T, class R, class... P, size_t... Is>
  576. 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...>) {
  577. r_error.error = Callable::CallError::CALL_OK;
  578. #ifdef DEBUG_METHODS_ENABLED
  579. r_ret = (p_instance->*p_method)(VariantCasterAndValidate<P>::cast(p_args, Is, r_error)...);
  580. #else
  581. r_ret = (p_instance->*p_method)(VariantCaster<P>::cast(*p_args[Is])...);
  582. #endif
  583. (void)p_args;
  584. }
  585. template <class R, class... P>
  586. void call_with_variant_args_static_ret(R (*p_method)(P...), const Variant **p_args, int p_argcount, Variant &r_ret, Callable::CallError &r_error) {
  587. #ifdef DEBUG_METHODS_ENABLED
  588. if ((size_t)p_argcount > sizeof...(P)) {
  589. r_error.error = Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS;
  590. r_error.argument = sizeof...(P);
  591. return;
  592. }
  593. if ((size_t)p_argcount < sizeof...(P)) {
  594. r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  595. r_error.argument = sizeof...(P);
  596. return;
  597. }
  598. #endif
  599. call_with_variant_args_static_ret<R, P...>(p_method, p_args, r_ret, r_error, BuildIndexSequence<sizeof...(P)>{});
  600. }
  601. template <class... P>
  602. void call_with_variant_args_static_ret(void (*p_method)(P...), const Variant **p_args, int p_argcount, Variant &r_ret, Callable::CallError &r_error) {
  603. #ifdef DEBUG_METHODS_ENABLED
  604. if ((size_t)p_argcount > sizeof...(P)) {
  605. r_error.error = Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS;
  606. r_error.argument = sizeof...(P);
  607. return;
  608. }
  609. if ((size_t)p_argcount < sizeof...(P)) {
  610. r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  611. r_error.argument = sizeof...(P);
  612. return;
  613. }
  614. #endif
  615. call_with_variant_args_static<P...>(p_method, p_args, r_error, BuildIndexSequence<sizeof...(P)>{});
  616. }
  617. template <class T, class R, class... P>
  618. 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) {
  619. #ifdef DEBUG_METHODS_ENABLED
  620. if ((size_t)p_argcount > sizeof...(P)) {
  621. r_error.error = Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS;
  622. r_error.argument = sizeof...(P);
  623. return;
  624. }
  625. if ((size_t)p_argcount < sizeof...(P)) {
  626. r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  627. r_error.argument = sizeof...(P);
  628. return;
  629. }
  630. #endif
  631. call_with_variant_args_retc_helper<T, R, P...>(p_instance, p_method, p_args, r_ret, r_error, BuildIndexSequence<sizeof...(P)>{});
  632. }
  633. template <class T, class R, class... P, size_t... Is>
  634. 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...>) {
  635. r_error.error = Callable::CallError::CALL_OK;
  636. #ifdef DEBUG_METHODS_ENABLED
  637. r_ret = (p_method)(p_instance, VariantCasterAndValidate<P>::cast(p_args, Is, r_error)...);
  638. #else
  639. r_ret = (p_method)(p_instance, VariantCaster<P>::cast(*p_args[Is])...);
  640. #endif
  641. (void)p_args;
  642. }
  643. template <class T, class R, class... P>
  644. void call_with_variant_args_retc_static_helper_dv(T *p_instance, R (*p_method)(T *, P...), const Variant **p_args, int p_argcount, Variant &r_ret, const Vector<Variant> &default_values, Callable::CallError &r_error) {
  645. #ifdef DEBUG_ENABLED
  646. if ((size_t)p_argcount > sizeof...(P)) {
  647. r_error.error = Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS;
  648. r_error.argument = sizeof...(P);
  649. return;
  650. }
  651. #endif
  652. int32_t missing = (int32_t)sizeof...(P) - (int32_t)p_argcount;
  653. int32_t dvs = default_values.size();
  654. #ifdef DEBUG_ENABLED
  655. if (missing > dvs) {
  656. r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  657. r_error.argument = sizeof...(P);
  658. return;
  659. }
  660. #endif
  661. const Variant *args[sizeof...(P) == 0 ? 1 : sizeof...(P)]; //avoid zero sized array
  662. for (int32_t i = 0; i < (int32_t)sizeof...(P); i++) {
  663. if (i < p_argcount) {
  664. args[i] = p_args[i];
  665. } else {
  666. args[i] = &default_values[i - p_argcount + (dvs - missing)];
  667. }
  668. }
  669. call_with_variant_args_retc_static_helper(p_instance, p_method, args, r_ret, r_error, BuildIndexSequence<sizeof...(P)>{});
  670. }
  671. template <class T, class... P, size_t... Is>
  672. void call_with_variant_args_static_helper(T *p_instance, void (*p_method)(T *, P...), const Variant **p_args, Callable::CallError &r_error, IndexSequence<Is...>) {
  673. r_error.error = Callable::CallError::CALL_OK;
  674. #ifdef DEBUG_METHODS_ENABLED
  675. (p_method)(p_instance, VariantCasterAndValidate<P>::cast(p_args, Is, r_error)...);
  676. #else
  677. (p_method)(p_instance, VariantCaster<P>::cast(*p_args[Is])...);
  678. #endif
  679. (void)p_args;
  680. }
  681. template <class T, class... P>
  682. void call_with_variant_args_static_helper_dv(T *p_instance, void (*p_method)(T *, P...), const Variant **p_args, int p_argcount, const Vector<Variant> &default_values, Callable::CallError &r_error) {
  683. #ifdef DEBUG_ENABLED
  684. if ((size_t)p_argcount > sizeof...(P)) {
  685. r_error.error = Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS;
  686. r_error.argument = sizeof...(P);
  687. return;
  688. }
  689. #endif
  690. int32_t missing = (int32_t)sizeof...(P) - (int32_t)p_argcount;
  691. int32_t dvs = default_values.size();
  692. #ifdef DEBUG_ENABLED
  693. if (missing > dvs) {
  694. r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  695. r_error.argument = sizeof...(P);
  696. return;
  697. }
  698. #endif
  699. const Variant *args[sizeof...(P) == 0 ? 1 : sizeof...(P)]; //avoid zero sized array
  700. for (int32_t i = 0; i < (int32_t)sizeof...(P); i++) {
  701. if (i < p_argcount) {
  702. args[i] = p_args[i];
  703. } else {
  704. args[i] = &default_values[i - p_argcount + (dvs - missing)];
  705. }
  706. }
  707. call_with_variant_args_static_helper(p_instance, p_method, args, r_error, BuildIndexSequence<sizeof...(P)>{});
  708. }
  709. template <class R, class... P>
  710. void call_with_variant_args_static_ret_dv(R (*p_method)(P...), const Variant **p_args, int p_argcount, Variant &r_ret, Callable::CallError &r_error, const Vector<Variant> &default_values) {
  711. #ifdef DEBUG_ENABLED
  712. if ((size_t)p_argcount > sizeof...(P)) {
  713. r_error.error = Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS;
  714. r_error.argument = sizeof...(P);
  715. return;
  716. }
  717. #endif
  718. int32_t missing = (int32_t)sizeof...(P) - (int32_t)p_argcount;
  719. int32_t dvs = default_values.size();
  720. #ifdef DEBUG_ENABLED
  721. if (missing > dvs) {
  722. r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  723. r_error.argument = sizeof...(P);
  724. return;
  725. }
  726. #endif
  727. const Variant *args[sizeof...(P) == 0 ? 1 : sizeof...(P)]; //avoid zero sized array
  728. for (int32_t i = 0; i < (int32_t)sizeof...(P); i++) {
  729. if (i < p_argcount) {
  730. args[i] = p_args[i];
  731. } else {
  732. args[i] = &default_values[i - p_argcount + (dvs - missing)];
  733. }
  734. }
  735. call_with_variant_args_static_ret(p_method, args, r_ret, r_error, BuildIndexSequence<sizeof...(P)>{});
  736. }
  737. template <class... P>
  738. void call_with_variant_args_static_dv(void (*p_method)(P...), const Variant **p_args, int p_argcount, Callable::CallError &r_error, const Vector<Variant> &default_values) {
  739. #ifdef DEBUG_ENABLED
  740. if ((size_t)p_argcount > sizeof...(P)) {
  741. r_error.error = Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS;
  742. r_error.argument = sizeof...(P);
  743. return;
  744. }
  745. #endif
  746. int32_t missing = (int32_t)sizeof...(P) - (int32_t)p_argcount;
  747. int32_t dvs = default_values.size();
  748. #ifdef DEBUG_ENABLED
  749. if (missing > dvs) {
  750. r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  751. r_error.argument = sizeof...(P);
  752. return;
  753. }
  754. #endif
  755. const Variant *args[sizeof...(P) == 0 ? 1 : sizeof...(P)]; //avoid zero sized array
  756. for (int32_t i = 0; i < (int32_t)sizeof...(P); i++) {
  757. if (i < p_argcount) {
  758. args[i] = p_args[i];
  759. } else {
  760. args[i] = &default_values[i - p_argcount + (dvs - missing)];
  761. }
  762. }
  763. call_with_variant_args_static(p_method, args, r_error, BuildIndexSequence<sizeof...(P)>{});
  764. }
  765. #if defined(DEBUG_METHODS_ENABLED) && defined(__GNUC__) && !defined(__clang__)
  766. #pragma GCC diagnostic pop
  767. #endif
  768. #endif // BINDER_COMMON_H