binder_common.h 42 KB

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