binder_common.hpp 27 KB

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