binder_common.hpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. /*************************************************************************/
  2. /* binder_common.hpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef GODOT_CPP_BINDER_COMMON_HPP
  31. #define GODOT_CPP_BINDER_COMMON_HPP
  32. #include <godot/gdnative_interface.h>
  33. #include <godot_cpp/core/method_ptrcall.hpp>
  34. #include <godot_cpp/core/type_info.hpp>
  35. #include <array>
  36. #include <vector>
  37. namespace godot {
  38. #define VARIANT_ENUM_CAST(m_class, m_enum) \
  39. namespace godot { \
  40. MAKE_ENUM_TYPE_INFO(m_class, m_enum) \
  41. template <> \
  42. struct VariantCaster<m_class::m_enum> { \
  43. static _FORCE_INLINE_ m_class::m_enum cast(const Variant &p_variant) { \
  44. return (m_class::m_enum)p_variant.operator int64_t(); \
  45. } \
  46. }; \
  47. template <> \
  48. struct PtrToArg<m_class::m_enum> { \
  49. _FORCE_INLINE_ static m_class::m_enum convert(const void *p_ptr) { \
  50. return m_class::m_enum(*reinterpret_cast<const int64_t *>(p_ptr)); \
  51. } \
  52. typedef int64_t EncodeT; \
  53. _FORCE_INLINE_ static void encode(m_class::m_enum p_val, const void *p_ptr) { \
  54. *(int64_t *)p_ptr = p_val; \
  55. } \
  56. }; \
  57. }
  58. template <class T>
  59. struct VariantCaster {
  60. static _FORCE_INLINE_ T cast(const Variant &p_variant) {
  61. return p_variant;
  62. }
  63. };
  64. template <class T>
  65. struct VariantCaster<T &> {
  66. static _FORCE_INLINE_ T cast(const Variant &p_variant) {
  67. return p_variant;
  68. }
  69. };
  70. template <class T>
  71. struct VariantCaster<const T &> {
  72. static _FORCE_INLINE_ T cast(const Variant &p_variant) {
  73. return p_variant;
  74. }
  75. };
  76. template <typename T>
  77. struct VariantObjectClassChecker {
  78. static _FORCE_INLINE_ bool check(const Variant &p_variant) {
  79. return true;
  80. }
  81. };
  82. template <typename T>
  83. class Ref;
  84. template <typename T>
  85. struct VariantObjectClassChecker<const Ref<T> &> {
  86. static _FORCE_INLINE_ bool check(const Variant &p_variant) {
  87. Object *obj = p_variant;
  88. const Ref<T> node = p_variant;
  89. return node.ptr() || !obj;
  90. }
  91. };
  92. template <class T>
  93. struct VariantCasterAndValidate {
  94. static _FORCE_INLINE_ T cast(const Variant **p_args, uint32_t p_arg_idx, GDNativeCallError &r_error) {
  95. GDNativeVariantType argtype = GDNativeVariantType(GetTypeInfo<T>::VARIANT_TYPE);
  96. if (!internal::gdn_interface->variant_can_convert_strict(static_cast<GDNativeVariantType>(p_args[p_arg_idx]->get_type()), argtype) ||
  97. !VariantObjectClassChecker<T>::check(p_args[p_arg_idx])) {
  98. r_error.error = GDNATIVE_CALL_ERROR_INVALID_ARGUMENT;
  99. r_error.argument = p_arg_idx;
  100. r_error.expected = argtype;
  101. }
  102. return VariantCaster<T>::cast(*p_args[p_arg_idx]);
  103. }
  104. };
  105. template <class T>
  106. struct VariantCasterAndValidate<T &> {
  107. static _FORCE_INLINE_ T cast(const Variant **p_args, uint32_t p_arg_idx, GDNativeCallError &r_error) {
  108. GDNativeVariantType argtype = GDNativeVariantType(GetTypeInfo<T>::VARIANT_TYPE);
  109. if (!internal::gdn_interface->variant_can_convert_strict(static_cast<GDNativeVariantType>(p_args[p_arg_idx]->get_type()), argtype) ||
  110. !VariantObjectClassChecker<T>::check(p_args[p_arg_idx])) {
  111. r_error.error = GDNATIVE_CALL_ERROR_INVALID_ARGUMENT;
  112. r_error.argument = p_arg_idx;
  113. r_error.expected = argtype;
  114. }
  115. return VariantCaster<T>::cast(*p_args[p_arg_idx]);
  116. }
  117. };
  118. template <class T>
  119. struct VariantCasterAndValidate<const T &> {
  120. static _FORCE_INLINE_ T cast(const Variant **p_args, uint32_t p_arg_idx, GDNativeCallError &r_error) {
  121. GDNativeVariantType argtype = GDNativeVariantType(GetTypeInfo<T>::VARIANT_TYPE);
  122. if (!internal::gdn_interface->variant_can_convert_strict(static_cast<GDNativeVariantType>(p_args[p_arg_idx]->get_type()), argtype) ||
  123. !VariantObjectClassChecker<T>::check(p_args[p_arg_idx])) {
  124. r_error.error = GDNATIVE_CALL_ERROR_INVALID_ARGUMENT;
  125. r_error.argument = p_arg_idx;
  126. r_error.expected = argtype;
  127. }
  128. return VariantCaster<T>::cast(*p_args[p_arg_idx]);
  129. }
  130. };
  131. template <class T, class... P, size_t... Is>
  132. void call_with_ptr_args_helper(T *p_instance, void (T::*p_method)(P...), const GDNativeTypePtr *p_args, IndexSequence<Is...>) {
  133. (p_instance->*p_method)(PtrToArg<P>::convert(p_args[Is])...);
  134. }
  135. template <class T, class... P, size_t... Is>
  136. void call_with_ptr_argsc_helper(T *p_instance, void (T::*p_method)(P...) const, const GDNativeTypePtr *p_args, IndexSequence<Is...>) {
  137. (p_instance->*p_method)(PtrToArg<P>::convert(p_args[Is])...);
  138. }
  139. template <class T, class R, class... P, size_t... Is>
  140. void call_with_ptr_args_ret_helper(T *p_instance, R (T::*p_method)(P...), const GDNativeTypePtr *p_args, void *r_ret, IndexSequence<Is...>) {
  141. PtrToArg<R>::encode((p_instance->*p_method)(PtrToArg<P>::convert(p_args[Is])...), r_ret);
  142. }
  143. template <class T, class R, class... P, size_t... Is>
  144. void call_with_ptr_args_retc_helper(T *p_instance, R (T::*p_method)(P...) const, const GDNativeTypePtr *p_args, void *r_ret, IndexSequence<Is...>) {
  145. PtrToArg<R>::encode((p_instance->*p_method)(PtrToArg<P>::convert(p_args[Is])...), r_ret);
  146. }
  147. template <class T, class... P>
  148. void call_with_ptr_args(T *p_instance, void (T::*p_method)(P...), const GDNativeTypePtr *p_args, void * /*ret*/) {
  149. call_with_ptr_args_helper<T, P...>(p_instance, p_method, p_args, BuildIndexSequence<sizeof...(P)>{});
  150. }
  151. template <class T, class... P>
  152. void call_with_ptr_args(T *p_instance, void (T::*p_method)(P...) const, const GDNativeTypePtr *p_args, void * /*ret*/) {
  153. call_with_ptr_argsc_helper<T, P...>(p_instance, p_method, p_args, BuildIndexSequence<sizeof...(P)>{});
  154. }
  155. template <class T, class R, class... P>
  156. void call_with_ptr_args(T *p_instance, R (T::*p_method)(P...), const GDNativeTypePtr *p_args, void *r_ret) {
  157. call_with_ptr_args_ret_helper<T, R, P...>(p_instance, p_method, p_args, r_ret, BuildIndexSequence<sizeof...(P)>{});
  158. }
  159. template <class T, class R, class... P>
  160. void call_with_ptr_args(T *p_instance, R (T::*p_method)(P...) const, const GDNativeTypePtr *p_args, void *r_ret) {
  161. call_with_ptr_args_retc_helper<T, R, P...>(p_instance, p_method, p_args, r_ret, BuildIndexSequence<sizeof...(P)>{});
  162. }
  163. template <class T, class... P, size_t... Is>
  164. void call_with_variant_args_helper(T *p_instance, void (T::*p_method)(P...), const Variant **p_args, GDNativeCallError &r_error, IndexSequence<Is...>) {
  165. r_error.error = GDNATIVE_CALL_OK;
  166. #ifdef DEBUG_METHODS_ENABLED
  167. (p_instance->*p_method)(VariantCasterAndValidate<P>::cast(p_args, Is, r_error)...);
  168. #else
  169. (p_instance->*p_method)(VariantCaster<P>::cast(*p_args[Is])...);
  170. #endif
  171. (void)(p_args); // Avoid warning.
  172. }
  173. template <class T, class... P, size_t... Is>
  174. void call_with_variant_argsc_helper(T *p_instance, void (T::*p_method)(P...) const, const Variant **p_args, GDNativeCallError &r_error, IndexSequence<Is...>) {
  175. r_error.error = GDNATIVE_CALL_OK;
  176. #ifdef DEBUG_METHODS_ENABLED
  177. (p_instance->*p_method)(VariantCasterAndValidate<P>::cast(p_args, Is, r_error)...);
  178. #else
  179. (p_instance->*p_method)(VariantCaster<P>::cast(*p_args[Is])...);
  180. #endif
  181. (void)(p_args); // Avoid warning.
  182. }
  183. template <class T, class R, class... P, size_t... Is>
  184. void call_with_variant_args_ret_helper(T *p_instance, R (T::*p_method)(P...), const Variant **p_args, Variant &r_ret, GDNativeCallError &r_error, IndexSequence<Is...>) {
  185. r_error.error = GDNATIVE_CALL_OK;
  186. #ifdef DEBUG_METHODS_ENABLED
  187. r_ret = (p_instance->*p_method)(VariantCasterAndValidate<P>::cast(p_args, Is, r_error)...);
  188. #else
  189. r_ret = (p_instance->*p_method)(VariantCaster<P>::cast(*p_args[Is])...);
  190. #endif
  191. }
  192. template <class T, class R, class... P, size_t... Is>
  193. void call_with_variant_args_retc_helper(T *p_instance, R (T::*p_method)(P...) const, const Variant **p_args, Variant &r_ret, GDNativeCallError &r_error, IndexSequence<Is...>) {
  194. r_error.error = GDNATIVE_CALL_OK;
  195. #ifdef DEBUG_METHODS_ENABLED
  196. r_ret = (p_instance->*p_method)(VariantCasterAndValidate<P>::cast(p_args, Is, r_error)...);
  197. #else
  198. r_ret = (p_instance->*p_method)(VariantCaster<P>::cast(*p_args[Is])...);
  199. #endif
  200. (void)p_args;
  201. }
  202. template <class T, class... P>
  203. void call_with_variant_args_dv(T *p_instance, void (T::*p_method)(P...), const GDNativeVariantPtr *p_args, int p_argcount, GDNativeCallError &r_error, const std::vector<Variant> &default_values) {
  204. #ifdef DEBUG_ENABLED
  205. if ((size_t)p_argcount > sizeof...(P)) {
  206. r_error.error = GDNATIVE_CALL_ERROR_TOO_MANY_ARGUMENTS;
  207. r_error.argument = (int32_t)sizeof...(P);
  208. return;
  209. }
  210. #endif
  211. int32_t missing = (int32_t)sizeof...(P) - (int32_t)p_argcount;
  212. int32_t dvs = (int32_t)default_values.size();
  213. #ifdef DEBUG_ENABLED
  214. if (missing > dvs) {
  215. r_error.error = GDNATIVE_CALL_ERROR_TOO_FEW_ARGUMENTS;
  216. r_error.argument = (int32_t)sizeof...(P);
  217. return;
  218. }
  219. #endif
  220. Variant args[sizeof...(P) == 0 ? 1 : sizeof...(P)]; // Avoid zero sized array.
  221. std::array<const Variant *, sizeof...(P)> argsp;
  222. for (int32_t i = 0; i < (int32_t)sizeof...(P); i++) {
  223. if (i < p_argcount) {
  224. args[i] = Variant(p_args[i]);
  225. } else {
  226. args[i] = default_values[i - p_argcount + (dvs - missing)];
  227. }
  228. argsp[i] = &args[i];
  229. }
  230. call_with_variant_args_helper(p_instance, p_method, argsp.data(), r_error, BuildIndexSequence<sizeof...(P)>{});
  231. }
  232. template <class T, class... P>
  233. void call_with_variant_argsc_dv(T *p_instance, void (T::*p_method)(P...) const, const GDNativeVariantPtr *p_args, int p_argcount, GDNativeCallError &r_error, const std::vector<Variant> &default_values) {
  234. #ifdef DEBUG_ENABLED
  235. if ((size_t)p_argcount > sizeof...(P)) {
  236. r_error.error = GDNATIVE_CALL_ERROR_TOO_MANY_ARGUMENTS;
  237. r_error.argument = (int32_t)sizeof...(P);
  238. return;
  239. }
  240. #endif
  241. int32_t missing = (int32_t)sizeof...(P) - (int32_t)p_argcount;
  242. int32_t dvs = (int32_t)default_values.size();
  243. #ifdef DEBUG_ENABLED
  244. if (missing > dvs) {
  245. r_error.error = GDNATIVE_CALL_ERROR_TOO_FEW_ARGUMENTS;
  246. r_error.argument = (int32_t)sizeof...(P);
  247. return;
  248. }
  249. #endif
  250. Variant args[sizeof...(P) == 0 ? 1 : sizeof...(P)]; // Avoid zero sized array.
  251. std::array<const Variant *, sizeof...(P)> argsp;
  252. for (int32_t i = 0; i < (int32_t)sizeof...(P); i++) {
  253. if (i < p_argcount) {
  254. args[i] = Variant(p_args[i]);
  255. } else {
  256. args[i] = default_values[i - p_argcount + (dvs - missing)];
  257. }
  258. argsp[i] = &args[i];
  259. }
  260. call_with_variant_argsc_helper(p_instance, p_method, argsp.data(), r_error, BuildIndexSequence<sizeof...(P)>{});
  261. }
  262. template <class T, class R, class... P>
  263. void call_with_variant_args_ret_dv(T *p_instance, R (T::*p_method)(P...), const GDNativeVariantPtr *p_args, int p_argcount, Variant &r_ret, GDNativeCallError &r_error, const std::vector<Variant> &default_values) {
  264. #ifdef DEBUG_ENABLED
  265. if ((size_t)p_argcount > sizeof...(P)) {
  266. r_error.error = GDNATIVE_CALL_ERROR_TOO_MANY_ARGUMENTS;
  267. r_error.argument = (int32_t)sizeof...(P);
  268. return;
  269. }
  270. #endif
  271. int32_t missing = (int32_t)sizeof...(P) - (int32_t)p_argcount;
  272. int32_t dvs = (int32_t)default_values.size();
  273. #ifdef DEBUG_ENABLED
  274. if (missing > dvs) {
  275. r_error.error = GDNATIVE_CALL_ERROR_TOO_FEW_ARGUMENTS;
  276. r_error.argument = (int32_t)sizeof...(P);
  277. return;
  278. }
  279. #endif
  280. Variant args[sizeof...(P) == 0 ? 1 : sizeof...(P)]; // Avoid zero sized array.
  281. std::array<const Variant *, sizeof...(P)> argsp;
  282. for (int32_t i = 0; i < (int32_t)sizeof...(P); i++) {
  283. if (i < p_argcount) {
  284. args[i] = Variant(p_args[i]);
  285. } else {
  286. args[i] = default_values[i - p_argcount + (dvs - missing)];
  287. }
  288. argsp[i] = &args[i];
  289. }
  290. call_with_variant_args_ret_helper(p_instance, p_method, argsp.data(), r_ret, r_error, BuildIndexSequence<sizeof...(P)>{});
  291. }
  292. template <class T, class R, class... P>
  293. void call_with_variant_args_retc_dv(T *p_instance, R (T::*p_method)(P...) const, const GDNativeVariantPtr *p_args, int p_argcount, Variant &r_ret, GDNativeCallError &r_error, const std::vector<Variant> &default_values) {
  294. #ifdef DEBUG_ENABLED
  295. if ((size_t)p_argcount > sizeof...(P)) {
  296. r_error.error = GDNATIVE_CALL_ERROR_TOO_MANY_ARGUMENTS;
  297. r_error.argument = (int32_t)sizeof...(P);
  298. return;
  299. }
  300. #endif
  301. int32_t missing = (int32_t)sizeof...(P) - (int32_t)p_argcount;
  302. int32_t dvs = (int32_t)default_values.size();
  303. #ifdef DEBUG_ENABLED
  304. if (missing > dvs) {
  305. r_error.error = GDNATIVE_CALL_ERROR_TOO_FEW_ARGUMENTS;
  306. r_error.argument = (int32_t)sizeof...(P);
  307. return;
  308. }
  309. #endif
  310. Variant args[sizeof...(P) == 0 ? 1 : sizeof...(P)]; // Avoid zero sized array.
  311. std::array<const Variant *, sizeof...(P)> argsp;
  312. for (int32_t i = 0; i < (int32_t)sizeof...(P); i++) {
  313. if (i < p_argcount) {
  314. args[i] = Variant(p_args[i]);
  315. } else {
  316. args[i] = default_values[i - p_argcount + (dvs - missing)];
  317. }
  318. argsp[i] = &args[i];
  319. }
  320. call_with_variant_args_retc_helper(p_instance, p_method, argsp.data(), r_ret, r_error, BuildIndexSequence<sizeof...(P)>{});
  321. }
  322. // GCC raises "parameter 'p_args' set but not used" when P = {},
  323. // it's not clever enough to treat other P values as making this branch valid.
  324. #if defined(DEBUG_METHODS_ENABLED) && defined(__GNUC__) && !defined(__clang__)
  325. #pragma GCC diagnostic push
  326. #pragma GCC diagnostic ignored "-Wunused-but-set-parameter"
  327. #endif
  328. template <class Q>
  329. void call_get_argument_type_helper(int p_arg, int &index, GDNativeVariantType &type) {
  330. if (p_arg == index) {
  331. type = GDNativeVariantType(GetTypeInfo<Q>::VARIANT_TYPE);
  332. }
  333. index++;
  334. }
  335. template <class... P>
  336. GDNativeVariantType call_get_argument_type(int p_arg) {
  337. GDNativeVariantType type = GDNATIVE_VARIANT_TYPE_NIL;
  338. int index = 0;
  339. // I think rocket science is simpler than modern C++.
  340. using expand_type = int[];
  341. expand_type a{ 0, (call_get_argument_type_helper<P>(p_arg, index, type), 0)... };
  342. (void)a; // Suppress (valid, but unavoidable) -Wunused-variable warning.
  343. (void)index; // Suppress GCC warning.
  344. return type;
  345. }
  346. template <class Q>
  347. void call_get_argument_type_info_helper(int p_arg, int &index, GDNativePropertyInfo &info) {
  348. if (p_arg == index) {
  349. info = GetTypeInfo<Q>::get_class_info();
  350. }
  351. index++;
  352. }
  353. template <class... P>
  354. void call_get_argument_type_info(int p_arg, GDNativePropertyInfo &info) {
  355. int index = 0;
  356. // I think rocket science is simpler than modern C++.
  357. using expand_type = int[];
  358. expand_type a{ 0, (call_get_argument_type_info_helper<P>(p_arg, index, info), 0)... };
  359. (void)a; // Suppress (valid, but unavoidable) -Wunused-variable warning.
  360. (void)index; // Suppress GCC warning.
  361. }
  362. template <class Q>
  363. void call_get_argument_metadata_helper(int p_arg, int &index, GDNativeExtensionClassMethodArgumentMetadata &md) {
  364. if (p_arg == index) {
  365. md = GetTypeInfo<Q>::METADATA;
  366. }
  367. index++;
  368. }
  369. template <class... P>
  370. GDNativeExtensionClassMethodArgumentMetadata call_get_argument_metadata(int p_arg) {
  371. GDNativeExtensionClassMethodArgumentMetadata md = GDNATIVE_EXTENSION_METHOD_ARGUMENT_METADATA_NONE;
  372. int index = 0;
  373. // I think rocket science is simpler than modern C++.
  374. using expand_type = int[];
  375. expand_type a{ 0, (call_get_argument_metadata_helper<P>(p_arg, index, md), 0)... };
  376. (void)a; // Suppress (valid, but unavoidable) -Wunused-variable warning.
  377. (void)index;
  378. return md;
  379. }
  380. template <class... P, size_t... Is>
  381. void call_with_variant_args_static(void (*p_method)(P...), const Variant **p_args, GDNativeCallError &r_error, IndexSequence<Is...>) {
  382. r_error.error = GDNATIVE_CALL_OK;
  383. #ifdef DEBUG_METHODS_ENABLED
  384. (p_method)(VariantCasterAndValidate<P>::cast(p_args, Is, r_error)...);
  385. #else
  386. (p_method)(VariantCaster<P>::cast(*p_args[Is])...);
  387. #endif
  388. }
  389. template <class... P>
  390. void call_with_variant_args_static_dv(void (*p_method)(P...), const GDNativeVariantPtr *p_args, int p_argcount, GDNativeCallError &r_error, const std::vector<Variant> &default_values) {
  391. #ifdef DEBUG_ENABLED
  392. if ((size_t)p_argcount > sizeof...(P)) {
  393. r_error.error = GDNATIVE_CALL_ERROR_TOO_MANY_ARGUMENTS;
  394. r_error.argument = sizeof...(P);
  395. return;
  396. }
  397. #endif
  398. int32_t missing = (int32_t)sizeof...(P) - (int32_t)p_argcount;
  399. int32_t dvs = default_values.size();
  400. #ifdef DEBUG_ENABLED
  401. if (missing > dvs) {
  402. r_error.error = GDNATIVE_CALL_ERROR_TOO_FEW_ARGUMENTS;
  403. r_error.argument = sizeof...(P);
  404. return;
  405. }
  406. #endif
  407. Variant args[sizeof...(P) == 0 ? 1 : sizeof...(P)]; // Avoid zero sized array.
  408. std::array<const Variant *, sizeof...(P)> argsp;
  409. for (int32_t i = 0; i < (int32_t)sizeof...(P); i++) {
  410. if (i < p_argcount) {
  411. args[i] = Variant(p_args[i]);
  412. } else {
  413. args[i] = default_values[i - p_argcount + (dvs - missing)];
  414. }
  415. argsp[i] = &args[i];
  416. }
  417. call_with_variant_args_static(p_method, argsp.data(), r_error, BuildIndexSequence<sizeof...(P)>{});
  418. }
  419. template <class... P, size_t... Is>
  420. void call_with_ptr_args_static_method_helper(void (*p_method)(P...), const GDNativeTypePtr *p_args, IndexSequence<Is...>) {
  421. p_method(PtrToArg<P>::convert(p_args[Is])...);
  422. }
  423. template <class... P>
  424. void call_with_ptr_args_static_method(void (*p_method)(P...), const GDNativeTypePtr *p_args) {
  425. call_with_ptr_args_static_method_helper<P...>(p_method, p_args, BuildIndexSequence<sizeof...(P)>{});
  426. }
  427. template <class R, class... P, size_t... Is>
  428. void call_with_variant_args_static_ret(R (*p_method)(P...), const Variant **p_args, Variant &r_ret, GDNativeCallError &r_error, IndexSequence<Is...>) {
  429. r_error.error = GDNATIVE_CALL_OK;
  430. #ifdef DEBUG_METHODS_ENABLED
  431. r_ret = (p_method)(VariantCasterAndValidate<P>::cast(p_args, Is, r_error)...);
  432. #else
  433. r_ret = (p_method)(VariantCaster<P>::cast(*p_args[Is])...);
  434. #endif
  435. }
  436. template <class R, class... P>
  437. void call_with_variant_args_static_ret_dv(R (*p_method)(P...), const GDNativeVariantPtr *p_args, int p_argcount, Variant &r_ret, GDNativeCallError &r_error, const std::vector<Variant> &default_values) {
  438. #ifdef DEBUG_ENABLED
  439. if ((size_t)p_argcount > sizeof...(P)) {
  440. r_error.error = GDNATIVE_CALL_ERROR_TOO_MANY_ARGUMENTS;
  441. r_error.argument = 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 = GDNATIVE_CALL_ERROR_TOO_FEW_ARGUMENTS;
  450. r_error.argument = sizeof...(P);
  451. return;
  452. }
  453. #endif
  454. Variant args[sizeof...(P) == 0 ? 1 : sizeof...(P)]; // Avoid zero sized array.
  455. std::array<const Variant *, sizeof...(P)> argsp;
  456. for (int32_t i = 0; i < (int32_t)sizeof...(P); i++) {
  457. if (i < p_argcount) {
  458. args[i] = Variant(p_args[i]);
  459. } else {
  460. args[i] = default_values[i - p_argcount + (dvs - missing)];
  461. }
  462. argsp[i] = &args[i];
  463. }
  464. call_with_variant_args_static_ret(p_method, argsp.data(), r_ret, r_error, BuildIndexSequence<sizeof...(P)>{});
  465. }
  466. template <class R, class... P, size_t... Is>
  467. void call_with_ptr_args_static_method_ret_helper(R (*p_method)(P...), const GDNativeTypePtr *p_args, void *r_ret, IndexSequence<Is...>) {
  468. PtrToArg<R>::encode(p_method(PtrToArg<P>::convert(p_args[Is])...), r_ret);
  469. }
  470. template <class R, class... P>
  471. void call_with_ptr_args_static_method_ret(R (*p_method)(P...), const GDNativeTypePtr *p_args, void *r_ret) {
  472. call_with_ptr_args_static_method_ret_helper<R, P...>(p_method, p_args, r_ret, BuildIndexSequence<sizeof...(P)>{});
  473. }
  474. #if defined(__GNUC__) && !defined(__clang__)
  475. #pragma GCC diagnostic pop
  476. #endif
  477. } // namespace godot
  478. #endif // ! GODOT_CPP_BINDER_COMMON_HPP