binder_common.hpp 24 KB

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