binder_common.h 43 KB

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