binder_common.h 43 KB

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