binder_common.hpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. /**************************************************************************/
  2. /* binder_common.hpp */
  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 GODOT_BINDER_COMMON_HPP
  31. #define GODOT_BINDER_COMMON_HPP
  32. #include <gdextension_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_enum) \
  39. namespace godot { \
  40. MAKE_ENUM_TYPE_INFO(m_enum) \
  41. template <> \
  42. struct VariantCaster<m_enum> { \
  43. static _FORCE_INLINE_ m_enum cast(const Variant &p_variant) { \
  44. return (m_enum)p_variant.operator int64_t(); \
  45. } \
  46. }; \
  47. template <> \
  48. struct PtrToArg<m_enum> { \
  49. _FORCE_INLINE_ static m_enum convert(const void *p_ptr) { \
  50. return m_enum(*reinterpret_cast<const int64_t *>(p_ptr)); \
  51. } \
  52. typedef int64_t EncodeT; \
  53. _FORCE_INLINE_ static void encode(m_enum p_val, void *p_ptr) { \
  54. *reinterpret_cast<int64_t *>(p_ptr) = p_val; \
  55. } \
  56. }; \
  57. }
  58. #define VARIANT_BITFIELD_CAST(m_enum) \
  59. namespace godot { \
  60. MAKE_BITFIELD_TYPE_INFO(m_enum) \
  61. template <> \
  62. struct VariantCaster<BitField<m_enum>> { \
  63. static _FORCE_INLINE_ BitField<m_enum> cast(const Variant &p_variant) { \
  64. return BitField<m_enum>(p_variant.operator int64_t()); \
  65. } \
  66. }; \
  67. template <> \
  68. struct PtrToArg<BitField<m_enum>> { \
  69. _FORCE_INLINE_ static BitField<m_enum> convert(const void *p_ptr) { \
  70. return BitField<m_enum>(*reinterpret_cast<const int64_t *>(p_ptr)); \
  71. } \
  72. typedef int64_t EncodeT; \
  73. _FORCE_INLINE_ static void encode(BitField<m_enum> p_val, void *p_ptr) { \
  74. *reinterpret_cast<int64_t *>(p_ptr) = p_val; \
  75. } \
  76. }; \
  77. }
  78. template <class T>
  79. struct VariantCaster {
  80. static _FORCE_INLINE_ T cast(const Variant &p_variant) {
  81. using TStripped = std::remove_pointer_t<T>;
  82. if constexpr (std::is_base_of<Object, TStripped>::value) {
  83. return Object::cast_to<TStripped>(p_variant);
  84. } else {
  85. return p_variant;
  86. }
  87. }
  88. };
  89. template <class T>
  90. struct VariantCaster<T &> {
  91. static _FORCE_INLINE_ T cast(const Variant &p_variant) {
  92. using TStripped = std::remove_pointer_t<T>;
  93. if constexpr (std::is_base_of<Object, TStripped>::value) {
  94. return Object::cast_to<TStripped>(p_variant);
  95. } else {
  96. return p_variant;
  97. }
  98. }
  99. };
  100. template <class T>
  101. struct VariantCaster<const T &> {
  102. static _FORCE_INLINE_ T cast(const Variant &p_variant) {
  103. using TStripped = std::remove_pointer_t<T>;
  104. if constexpr (std::is_base_of<Object, TStripped>::value) {
  105. return Object::cast_to<TStripped>(p_variant);
  106. } else {
  107. return p_variant;
  108. }
  109. }
  110. };
  111. template <typename T>
  112. struct VariantObjectClassChecker {
  113. static _FORCE_INLINE_ bool check(const Variant &p_variant) {
  114. using TStripped = std::remove_pointer_t<T>;
  115. if constexpr (std::is_base_of<Object, TStripped>::value) {
  116. Object *obj = p_variant;
  117. return Object::cast_to<TStripped>(p_variant) || !obj;
  118. } else {
  119. return true;
  120. }
  121. }
  122. };
  123. template <typename T>
  124. class Ref;
  125. template <typename T>
  126. struct VariantObjectClassChecker<const Ref<T> &> {
  127. static _FORCE_INLINE_ bool check(const Variant &p_variant) {
  128. Object *obj = p_variant;
  129. const Ref<T> node = p_variant;
  130. return node.ptr() || !obj;
  131. }
  132. };
  133. template <class T>
  134. struct VariantCasterAndValidate {
  135. static _FORCE_INLINE_ T cast(const Variant **p_args, uint32_t p_arg_idx, GDExtensionCallError &r_error) {
  136. GDExtensionVariantType argtype = GDExtensionVariantType(GetTypeInfo<T>::VARIANT_TYPE);
  137. if (!internal::gdextension_interface_variant_can_convert_strict(static_cast<GDExtensionVariantType>(p_args[p_arg_idx]->get_type()), argtype) ||
  138. !VariantObjectClassChecker<T>::check(p_args[p_arg_idx])) {
  139. r_error.error = GDEXTENSION_CALL_ERROR_INVALID_ARGUMENT;
  140. r_error.argument = p_arg_idx;
  141. r_error.expected = argtype;
  142. }
  143. return VariantCaster<T>::cast(*p_args[p_arg_idx]);
  144. }
  145. };
  146. template <class T>
  147. struct VariantCasterAndValidate<T &> {
  148. static _FORCE_INLINE_ T cast(const Variant **p_args, uint32_t p_arg_idx, GDExtensionCallError &r_error) {
  149. GDExtensionVariantType argtype = GDExtensionVariantType(GetTypeInfo<T>::VARIANT_TYPE);
  150. if (!internal::gdextension_interface_variant_can_convert_strict(static_cast<GDExtensionVariantType>(p_args[p_arg_idx]->get_type()), argtype) ||
  151. !VariantObjectClassChecker<T>::check(p_args[p_arg_idx])) {
  152. r_error.error = GDEXTENSION_CALL_ERROR_INVALID_ARGUMENT;
  153. r_error.argument = p_arg_idx;
  154. r_error.expected = argtype;
  155. }
  156. return VariantCaster<T>::cast(*p_args[p_arg_idx]);
  157. }
  158. };
  159. template <class T>
  160. struct VariantCasterAndValidate<const T &> {
  161. static _FORCE_INLINE_ T cast(const Variant **p_args, uint32_t p_arg_idx, GDExtensionCallError &r_error) {
  162. GDExtensionVariantType argtype = GDExtensionVariantType(GetTypeInfo<T>::VARIANT_TYPE);
  163. if (!internal::gdextension_interface_variant_can_convert_strict(static_cast<GDExtensionVariantType>(p_args[p_arg_idx]->get_type()), argtype) ||
  164. !VariantObjectClassChecker<T>::check(p_args[p_arg_idx])) {
  165. r_error.error = GDEXTENSION_CALL_ERROR_INVALID_ARGUMENT;
  166. r_error.argument = p_arg_idx;
  167. r_error.expected = argtype;
  168. }
  169. return VariantCaster<T>::cast(*p_args[p_arg_idx]);
  170. }
  171. };
  172. template <class T, class... P, size_t... Is>
  173. void call_with_ptr_args_helper(T *p_instance, void (T::*p_method)(P...), const GDExtensionConstTypePtr *p_args, IndexSequence<Is...>) {
  174. (p_instance->*p_method)(PtrToArg<P>::convert(p_args[Is])...);
  175. }
  176. template <class T, class... P, size_t... Is>
  177. void call_with_ptr_argsc_helper(T *p_instance, void (T::*p_method)(P...) const, const GDExtensionConstTypePtr *p_args, IndexSequence<Is...>) {
  178. (p_instance->*p_method)(PtrToArg<P>::convert(p_args[Is])...);
  179. }
  180. template <class T, class R, class... P, size_t... Is>
  181. void call_with_ptr_args_ret_helper(T *p_instance, R (T::*p_method)(P...), const GDExtensionConstTypePtr *p_args, void *r_ret, IndexSequence<Is...>) {
  182. PtrToArg<R>::encode((p_instance->*p_method)(PtrToArg<P>::convert(p_args[Is])...), r_ret);
  183. }
  184. template <class T, class R, class... P, size_t... Is>
  185. void call_with_ptr_args_retc_helper(T *p_instance, R (T::*p_method)(P...) const, const GDExtensionConstTypePtr *p_args, void *r_ret, IndexSequence<Is...>) {
  186. PtrToArg<R>::encode((p_instance->*p_method)(PtrToArg<P>::convert(p_args[Is])...), r_ret);
  187. }
  188. template <class T, class... P>
  189. void call_with_ptr_args(T *p_instance, void (T::*p_method)(P...), const GDExtensionConstTypePtr *p_args, void * /*ret*/) {
  190. call_with_ptr_args_helper<T, P...>(p_instance, p_method, p_args, BuildIndexSequence<sizeof...(P)>{});
  191. }
  192. template <class T, class... P>
  193. void call_with_ptr_args(T *p_instance, void (T::*p_method)(P...) const, const GDExtensionConstTypePtr *p_args, void * /*ret*/) {
  194. call_with_ptr_argsc_helper<T, P...>(p_instance, p_method, p_args, BuildIndexSequence<sizeof...(P)>{});
  195. }
  196. template <class T, class R, class... P>
  197. void call_with_ptr_args(T *p_instance, R (T::*p_method)(P...), const GDExtensionConstTypePtr *p_args, void *r_ret) {
  198. call_with_ptr_args_ret_helper<T, R, P...>(p_instance, p_method, p_args, r_ret, BuildIndexSequence<sizeof...(P)>{});
  199. }
  200. template <class T, class R, class... P>
  201. void call_with_ptr_args(T *p_instance, R (T::*p_method)(P...) const, const GDExtensionConstTypePtr *p_args, void *r_ret) {
  202. call_with_ptr_args_retc_helper<T, R, P...>(p_instance, p_method, p_args, r_ret, BuildIndexSequence<sizeof...(P)>{});
  203. }
  204. template <class T, class... P, size_t... Is>
  205. void call_with_variant_args_helper(T *p_instance, void (T::*p_method)(P...), const Variant **p_args, GDExtensionCallError &r_error, IndexSequence<Is...>) {
  206. r_error.error = GDEXTENSION_CALL_OK;
  207. #ifdef DEBUG_METHODS_ENABLED
  208. (p_instance->*p_method)(VariantCasterAndValidate<P>::cast(p_args, Is, r_error)...);
  209. #else
  210. (p_instance->*p_method)(VariantCaster<P>::cast(*p_args[Is])...);
  211. #endif
  212. (void)(p_args); // Avoid warning.
  213. }
  214. template <class T, class... P, size_t... Is>
  215. void call_with_variant_argsc_helper(T *p_instance, void (T::*p_method)(P...) const, const Variant **p_args, GDExtensionCallError &r_error, IndexSequence<Is...>) {
  216. r_error.error = GDEXTENSION_CALL_OK;
  217. #ifdef DEBUG_METHODS_ENABLED
  218. (p_instance->*p_method)(VariantCasterAndValidate<P>::cast(p_args, Is, r_error)...);
  219. #else
  220. (p_instance->*p_method)(VariantCaster<P>::cast(*p_args[Is])...);
  221. #endif
  222. (void)(p_args); // Avoid warning.
  223. }
  224. template <class T, class R, class... P, size_t... Is>
  225. void call_with_variant_args_ret_helper(T *p_instance, R (T::*p_method)(P...), const Variant **p_args, Variant &r_ret, GDExtensionCallError &r_error, IndexSequence<Is...>) {
  226. r_error.error = GDEXTENSION_CALL_OK;
  227. #ifdef DEBUG_METHODS_ENABLED
  228. r_ret = (p_instance->*p_method)(VariantCasterAndValidate<P>::cast(p_args, Is, r_error)...);
  229. #else
  230. r_ret = (p_instance->*p_method)(VariantCaster<P>::cast(*p_args[Is])...);
  231. #endif
  232. }
  233. template <class T, class R, class... P, size_t... Is>
  234. void call_with_variant_args_retc_helper(T *p_instance, R (T::*p_method)(P...) const, const Variant **p_args, Variant &r_ret, GDExtensionCallError &r_error, IndexSequence<Is...>) {
  235. r_error.error = GDEXTENSION_CALL_OK;
  236. #ifdef DEBUG_METHODS_ENABLED
  237. r_ret = (p_instance->*p_method)(VariantCasterAndValidate<P>::cast(p_args, Is, r_error)...);
  238. #else
  239. r_ret = (p_instance->*p_method)(VariantCaster<P>::cast(*p_args[Is])...);
  240. #endif
  241. (void)p_args;
  242. }
  243. template <class T, class... P>
  244. void call_with_variant_args_dv(T *p_instance, void (T::*p_method)(P...), const GDExtensionConstVariantPtr *p_args, int p_argcount, GDExtensionCallError &r_error, const std::vector<Variant> &default_values) {
  245. #ifdef DEBUG_ENABLED
  246. if ((size_t)p_argcount > sizeof...(P)) {
  247. r_error.error = GDEXTENSION_CALL_ERROR_TOO_MANY_ARGUMENTS;
  248. r_error.argument = (int32_t)sizeof...(P);
  249. return;
  250. }
  251. #endif
  252. int32_t missing = (int32_t)sizeof...(P) - (int32_t)p_argcount;
  253. int32_t dvs = (int32_t)default_values.size();
  254. #ifdef DEBUG_ENABLED
  255. if (missing > dvs) {
  256. r_error.error = GDEXTENSION_CALL_ERROR_TOO_FEW_ARGUMENTS;
  257. r_error.argument = (int32_t)sizeof...(P);
  258. return;
  259. }
  260. #endif
  261. Variant args[sizeof...(P) == 0 ? 1 : sizeof...(P)]; // Avoid zero sized array.
  262. std::array<const Variant *, sizeof...(P)> argsp;
  263. for (int32_t i = 0; i < (int32_t)sizeof...(P); i++) {
  264. if (i < p_argcount) {
  265. args[i] = Variant(p_args[i]);
  266. } else {
  267. args[i] = default_values[i - p_argcount + (dvs - missing)];
  268. }
  269. argsp[i] = &args[i];
  270. }
  271. call_with_variant_args_helper(p_instance, p_method, argsp.data(), r_error, BuildIndexSequence<sizeof...(P)>{});
  272. }
  273. template <class T, class... P>
  274. void call_with_variant_argsc_dv(T *p_instance, void (T::*p_method)(P...) const, const GDExtensionConstVariantPtr *p_args, int p_argcount, GDExtensionCallError &r_error, const std::vector<Variant> &default_values) {
  275. #ifdef DEBUG_ENABLED
  276. if ((size_t)p_argcount > sizeof...(P)) {
  277. r_error.error = GDEXTENSION_CALL_ERROR_TOO_MANY_ARGUMENTS;
  278. r_error.argument = (int32_t)sizeof...(P);
  279. return;
  280. }
  281. #endif
  282. int32_t missing = (int32_t)sizeof...(P) - (int32_t)p_argcount;
  283. int32_t dvs = (int32_t)default_values.size();
  284. #ifdef DEBUG_ENABLED
  285. if (missing > dvs) {
  286. r_error.error = GDEXTENSION_CALL_ERROR_TOO_FEW_ARGUMENTS;
  287. r_error.argument = (int32_t)sizeof...(P);
  288. return;
  289. }
  290. #endif
  291. Variant args[sizeof...(P) == 0 ? 1 : sizeof...(P)]; // Avoid zero sized array.
  292. std::array<const Variant *, sizeof...(P)> argsp;
  293. for (int32_t i = 0; i < (int32_t)sizeof...(P); i++) {
  294. if (i < p_argcount) {
  295. args[i] = Variant(p_args[i]);
  296. } else {
  297. args[i] = default_values[i - p_argcount + (dvs - missing)];
  298. }
  299. argsp[i] = &args[i];
  300. }
  301. call_with_variant_argsc_helper(p_instance, p_method, argsp.data(), r_error, BuildIndexSequence<sizeof...(P)>{});
  302. }
  303. template <class T, class R, class... P>
  304. void call_with_variant_args_ret_dv(T *p_instance, R (T::*p_method)(P...), const GDExtensionConstVariantPtr *p_args, int p_argcount, Variant &r_ret, GDExtensionCallError &r_error, const std::vector<Variant> &default_values) {
  305. #ifdef DEBUG_ENABLED
  306. if ((size_t)p_argcount > sizeof...(P)) {
  307. r_error.error = GDEXTENSION_CALL_ERROR_TOO_MANY_ARGUMENTS;
  308. r_error.argument = (int32_t)sizeof...(P);
  309. return;
  310. }
  311. #endif
  312. int32_t missing = (int32_t)sizeof...(P) - (int32_t)p_argcount;
  313. int32_t dvs = (int32_t)default_values.size();
  314. #ifdef DEBUG_ENABLED
  315. if (missing > dvs) {
  316. r_error.error = GDEXTENSION_CALL_ERROR_TOO_FEW_ARGUMENTS;
  317. r_error.argument = (int32_t)sizeof...(P);
  318. return;
  319. }
  320. #endif
  321. Variant args[sizeof...(P) == 0 ? 1 : sizeof...(P)]; // Avoid zero sized array.
  322. std::array<const Variant *, sizeof...(P)> argsp;
  323. for (int32_t i = 0; i < (int32_t)sizeof...(P); i++) {
  324. if (i < p_argcount) {
  325. args[i] = Variant(p_args[i]);
  326. } else {
  327. args[i] = default_values[i - p_argcount + (dvs - missing)];
  328. }
  329. argsp[i] = &args[i];
  330. }
  331. call_with_variant_args_ret_helper(p_instance, p_method, argsp.data(), r_ret, r_error, BuildIndexSequence<sizeof...(P)>{});
  332. }
  333. template <class T, class R, class... P>
  334. void call_with_variant_args_retc_dv(T *p_instance, R (T::*p_method)(P...) const, const GDExtensionConstVariantPtr *p_args, int p_argcount, Variant &r_ret, GDExtensionCallError &r_error, const std::vector<Variant> &default_values) {
  335. #ifdef DEBUG_ENABLED
  336. if ((size_t)p_argcount > sizeof...(P)) {
  337. r_error.error = GDEXTENSION_CALL_ERROR_TOO_MANY_ARGUMENTS;
  338. r_error.argument = (int32_t)sizeof...(P);
  339. return;
  340. }
  341. #endif
  342. int32_t missing = (int32_t)sizeof...(P) - (int32_t)p_argcount;
  343. int32_t dvs = (int32_t)default_values.size();
  344. #ifdef DEBUG_ENABLED
  345. if (missing > dvs) {
  346. r_error.error = GDEXTENSION_CALL_ERROR_TOO_FEW_ARGUMENTS;
  347. r_error.argument = (int32_t)sizeof...(P);
  348. return;
  349. }
  350. #endif
  351. Variant args[sizeof...(P) == 0 ? 1 : sizeof...(P)]; // Avoid zero sized array.
  352. std::array<const Variant *, sizeof...(P)> argsp;
  353. for (int32_t i = 0; i < (int32_t)sizeof...(P); i++) {
  354. if (i < p_argcount) {
  355. args[i] = Variant(p_args[i]);
  356. } else {
  357. args[i] = default_values[i - p_argcount + (dvs - missing)];
  358. }
  359. argsp[i] = &args[i];
  360. }
  361. call_with_variant_args_retc_helper(p_instance, p_method, argsp.data(), r_ret, r_error, BuildIndexSequence<sizeof...(P)>{});
  362. }
  363. // GCC raises "parameter 'p_args' set but not used" when P = {},
  364. // it's not clever enough to treat other P values as making this branch valid.
  365. #if defined(DEBUG_METHODS_ENABLED) && defined(__GNUC__) && !defined(__clang__)
  366. #pragma GCC diagnostic push
  367. #pragma GCC diagnostic ignored "-Wunused-but-set-parameter"
  368. #endif
  369. template <class Q>
  370. void call_get_argument_type_helper(int p_arg, int &index, GDExtensionVariantType &type) {
  371. if (p_arg == index) {
  372. type = GDExtensionVariantType(GetTypeInfo<Q>::VARIANT_TYPE);
  373. }
  374. index++;
  375. }
  376. template <class... P>
  377. GDExtensionVariantType call_get_argument_type(int p_arg) {
  378. GDExtensionVariantType type = GDEXTENSION_VARIANT_TYPE_NIL;
  379. int index = 0;
  380. // I think rocket science is simpler than modern C++.
  381. using expand_type = int[];
  382. expand_type a{ 0, (call_get_argument_type_helper<P>(p_arg, index, type), 0)... };
  383. (void)a; // Suppress (valid, but unavoidable) -Wunused-variable warning.
  384. (void)index; // Suppress GCC warning.
  385. return type;
  386. }
  387. template <class Q>
  388. void call_get_argument_type_info_helper(int p_arg, int &index, PropertyInfo &info) {
  389. if (p_arg == index) {
  390. info = GetTypeInfo<Q>::get_class_info();
  391. }
  392. index++;
  393. }
  394. template <class... P>
  395. void call_get_argument_type_info(int p_arg, PropertyInfo &info) {
  396. int index = 0;
  397. // I think rocket science is simpler than modern C++.
  398. using expand_type = int[];
  399. expand_type a{ 0, (call_get_argument_type_info_helper<P>(p_arg, index, info), 0)... };
  400. (void)a; // Suppress (valid, but unavoidable) -Wunused-variable warning.
  401. (void)index; // Suppress GCC warning.
  402. }
  403. template <class Q>
  404. void call_get_argument_metadata_helper(int p_arg, int &index, GDExtensionClassMethodArgumentMetadata &md) {
  405. if (p_arg == index) {
  406. md = GetTypeInfo<Q>::METADATA;
  407. }
  408. index++;
  409. }
  410. template <class... P>
  411. GDExtensionClassMethodArgumentMetadata call_get_argument_metadata(int p_arg) {
  412. GDExtensionClassMethodArgumentMetadata md = GDEXTENSION_METHOD_ARGUMENT_METADATA_NONE;
  413. int index = 0;
  414. // I think rocket science is simpler than modern C++.
  415. using expand_type = int[];
  416. expand_type a{ 0, (call_get_argument_metadata_helper<P>(p_arg, index, md), 0)... };
  417. (void)a; // Suppress (valid, but unavoidable) -Wunused-variable warning.
  418. (void)index;
  419. return md;
  420. }
  421. template <class... P, size_t... Is>
  422. void call_with_variant_args_static(void (*p_method)(P...), const Variant **p_args, GDExtensionCallError &r_error, IndexSequence<Is...>) {
  423. r_error.error = GDEXTENSION_CALL_OK;
  424. #ifdef DEBUG_METHODS_ENABLED
  425. (p_method)(VariantCasterAndValidate<P>::cast(p_args, Is, r_error)...);
  426. #else
  427. (p_method)(VariantCaster<P>::cast(*p_args[Is])...);
  428. #endif
  429. }
  430. template <class... P>
  431. void call_with_variant_args_static_dv(void (*p_method)(P...), const GDExtensionConstVariantPtr *p_args, int p_argcount, GDExtensionCallError &r_error, const std::vector<Variant> &default_values) {
  432. #ifdef DEBUG_ENABLED
  433. if ((size_t)p_argcount > sizeof...(P)) {
  434. r_error.error = GDEXTENSION_CALL_ERROR_TOO_MANY_ARGUMENTS;
  435. r_error.argument = sizeof...(P);
  436. return;
  437. }
  438. #endif
  439. int32_t missing = (int32_t)sizeof...(P) - (int32_t)p_argcount;
  440. int32_t dvs = default_values.size();
  441. #ifdef DEBUG_ENABLED
  442. if (missing > dvs) {
  443. r_error.error = GDEXTENSION_CALL_ERROR_TOO_FEW_ARGUMENTS;
  444. r_error.argument = sizeof...(P);
  445. return;
  446. }
  447. #endif
  448. Variant args[sizeof...(P) == 0 ? 1 : sizeof...(P)]; // Avoid zero sized array.
  449. std::array<const Variant *, sizeof...(P)> argsp;
  450. for (int32_t i = 0; i < (int32_t)sizeof...(P); i++) {
  451. if (i < p_argcount) {
  452. args[i] = Variant(p_args[i]);
  453. } else {
  454. args[i] = default_values[i - p_argcount + (dvs - missing)];
  455. }
  456. argsp[i] = &args[i];
  457. }
  458. call_with_variant_args_static(p_method, argsp.data(), r_error, BuildIndexSequence<sizeof...(P)>{});
  459. }
  460. template <class... P, size_t... Is>
  461. void call_with_ptr_args_static_method_helper(void (*p_method)(P...), const GDExtensionConstTypePtr *p_args, IndexSequence<Is...>) {
  462. p_method(PtrToArg<P>::convert(p_args[Is])...);
  463. }
  464. template <class... P>
  465. void call_with_ptr_args_static_method(void (*p_method)(P...), const GDExtensionConstTypePtr *p_args) {
  466. call_with_ptr_args_static_method_helper<P...>(p_method, p_args, BuildIndexSequence<sizeof...(P)>{});
  467. }
  468. template <class R, class... P, size_t... Is>
  469. void call_with_variant_args_static_ret(R (*p_method)(P...), const Variant **p_args, Variant &r_ret, GDExtensionCallError &r_error, IndexSequence<Is...>) {
  470. r_error.error = GDEXTENSION_CALL_OK;
  471. #ifdef DEBUG_METHODS_ENABLED
  472. r_ret = (p_method)(VariantCasterAndValidate<P>::cast(p_args, Is, r_error)...);
  473. #else
  474. r_ret = (p_method)(VariantCaster<P>::cast(*p_args[Is])...);
  475. #endif
  476. }
  477. template <class R, class... P>
  478. void call_with_variant_args_static_ret_dv(R (*p_method)(P...), const GDExtensionConstVariantPtr *p_args, int p_argcount, Variant &r_ret, GDExtensionCallError &r_error, const std::vector<Variant> &default_values) {
  479. #ifdef DEBUG_ENABLED
  480. if ((size_t)p_argcount > sizeof...(P)) {
  481. r_error.error = GDEXTENSION_CALL_ERROR_TOO_MANY_ARGUMENTS;
  482. r_error.argument = sizeof...(P);
  483. return;
  484. }
  485. #endif
  486. int32_t missing = (int32_t)sizeof...(P) - (int32_t)p_argcount;
  487. int32_t dvs = default_values.size();
  488. #ifdef DEBUG_ENABLED
  489. if (missing > dvs) {
  490. r_error.error = GDEXTENSION_CALL_ERROR_TOO_FEW_ARGUMENTS;
  491. r_error.argument = sizeof...(P);
  492. return;
  493. }
  494. #endif
  495. Variant args[sizeof...(P) == 0 ? 1 : sizeof...(P)]; // Avoid zero sized array.
  496. std::array<const Variant *, sizeof...(P)> argsp;
  497. for (int32_t i = 0; i < (int32_t)sizeof...(P); i++) {
  498. if (i < p_argcount) {
  499. args[i] = Variant(p_args[i]);
  500. } else {
  501. args[i] = default_values[i - p_argcount + (dvs - missing)];
  502. }
  503. argsp[i] = &args[i];
  504. }
  505. call_with_variant_args_static_ret(p_method, argsp.data(), r_ret, r_error, BuildIndexSequence<sizeof...(P)>{});
  506. }
  507. template <class R, class... P, size_t... Is>
  508. void call_with_ptr_args_static_method_ret_helper(R (*p_method)(P...), const GDExtensionConstTypePtr *p_args, void *r_ret, IndexSequence<Is...>) {
  509. PtrToArg<R>::encode(p_method(PtrToArg<P>::convert(p_args[Is])...), r_ret);
  510. }
  511. template <class R, class... P>
  512. void call_with_ptr_args_static_method_ret(R (*p_method)(P...), const GDExtensionConstTypePtr *p_args, void *r_ret) {
  513. call_with_ptr_args_static_method_ret_helper<R, P...>(p_method, p_args, r_ret, BuildIndexSequence<sizeof...(P)>{});
  514. }
  515. #if defined(__GNUC__) && !defined(__clang__)
  516. #pragma GCC diagnostic pop
  517. #endif
  518. } // namespace godot
  519. #include <godot_cpp/classes/global_constants_binds.hpp>
  520. #include <godot_cpp/variant/builtin_binds.hpp>
  521. #endif // GODOT_BINDER_COMMON_HPP