binder_common.hpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  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 <typename 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 <typename 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 <typename 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 <typename 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 <typename 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 <typename 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 <typename T, typename... 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 <typename T, typename... 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 <typename T, typename R, typename... 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 <typename T, typename R, typename... 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 <typename T, typename... 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 <typename T, typename... 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 <typename T, typename R, typename... 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 <typename T, typename R, typename... 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 <typename T, typename... 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 <typename T, typename... 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 <typename T, typename R, typename... 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 <typename T, typename R, typename... 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 <typename T, typename... P>
  244. void call_with_variant_args(T *p_instance, void (T::*p_method)(P...), const Variant **p_args, int p_argcount, GDExtensionCallError &r_error) {
  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.expected = (int32_t)sizeof...(P);
  249. return;
  250. }
  251. if ((size_t)p_argcount < sizeof...(P)) {
  252. r_error.error = GDEXTENSION_CALL_ERROR_TOO_FEW_ARGUMENTS;
  253. r_error.expected = (int32_t)sizeof...(P);
  254. return;
  255. }
  256. #endif
  257. call_with_variant_args_helper<T, P...>(p_instance, p_method, p_args, r_error, BuildIndexSequence<sizeof...(P)>{});
  258. }
  259. template <typename T, typename R, typename... P>
  260. 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) {
  261. #ifdef DEBUG_ENABLED
  262. if ((size_t)p_argcount > sizeof...(P)) {
  263. r_error.error = GDEXTENSION_CALL_ERROR_TOO_MANY_ARGUMENTS;
  264. r_error.expected = (int32_t)sizeof...(P);
  265. return;
  266. }
  267. if ((size_t)p_argcount < sizeof...(P)) {
  268. r_error.error = GDEXTENSION_CALL_ERROR_TOO_FEW_ARGUMENTS;
  269. r_error.expected = (int32_t)sizeof...(P);
  270. return;
  271. }
  272. #endif
  273. call_with_variant_args_ret_helper<T, R, P...>(p_instance, p_method, p_args, r_ret, r_error, BuildIndexSequence<sizeof...(P)>{});
  274. }
  275. template <typename T, typename R, typename... P>
  276. 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) {
  277. #ifdef DEBUG_ENABLED
  278. if ((size_t)p_argcount > sizeof...(P)) {
  279. r_error.error = GDEXTENSION_CALL_ERROR_TOO_MANY_ARGUMENTS;
  280. r_error.expected = (int32_t)sizeof...(P);
  281. return;
  282. }
  283. if ((size_t)p_argcount < sizeof...(P)) {
  284. r_error.error = GDEXTENSION_CALL_ERROR_TOO_FEW_ARGUMENTS;
  285. r_error.expected = (int32_t)sizeof...(P);
  286. return;
  287. }
  288. #endif
  289. call_with_variant_args_retc_helper<T, R, P...>(p_instance, p_method, p_args, r_ret, r_error, BuildIndexSequence<sizeof...(P)>{});
  290. }
  291. template <typename T, typename... P>
  292. 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) {
  293. #ifdef DEBUG_ENABLED
  294. if ((size_t)p_argcount > sizeof...(P)) {
  295. r_error.error = GDEXTENSION_CALL_ERROR_TOO_MANY_ARGUMENTS;
  296. r_error.expected = (int32_t)sizeof...(P);
  297. return;
  298. }
  299. #endif
  300. int32_t missing = (int32_t)sizeof...(P) - (int32_t)p_argcount;
  301. int32_t dvs = (int32_t)default_values.size();
  302. #ifdef DEBUG_ENABLED
  303. if (missing > dvs) {
  304. r_error.error = GDEXTENSION_CALL_ERROR_TOO_FEW_ARGUMENTS;
  305. r_error.expected = (int32_t)sizeof...(P);
  306. return;
  307. }
  308. #endif
  309. Variant args[sizeof...(P) == 0 ? 1 : sizeof...(P)]; // Avoid zero sized array.
  310. std::array<const Variant *, sizeof...(P)> argsp;
  311. for (int32_t i = 0; i < (int32_t)sizeof...(P); i++) {
  312. if (i < p_argcount) {
  313. args[i] = Variant(p_args[i]);
  314. } else {
  315. args[i] = default_values[i - p_argcount + (dvs - missing)];
  316. }
  317. argsp[i] = &args[i];
  318. }
  319. call_with_variant_args_helper(p_instance, p_method, argsp.data(), r_error, BuildIndexSequence<sizeof...(P)>{});
  320. }
  321. template <typename T, typename... P>
  322. 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) {
  323. #ifdef DEBUG_ENABLED
  324. if ((size_t)p_argcount > sizeof...(P)) {
  325. r_error.error = GDEXTENSION_CALL_ERROR_TOO_MANY_ARGUMENTS;
  326. r_error.expected = (int32_t)sizeof...(P);
  327. return;
  328. }
  329. #endif
  330. int32_t missing = (int32_t)sizeof...(P) - (int32_t)p_argcount;
  331. int32_t dvs = (int32_t)default_values.size();
  332. #ifdef DEBUG_ENABLED
  333. if (missing > dvs) {
  334. r_error.error = GDEXTENSION_CALL_ERROR_TOO_FEW_ARGUMENTS;
  335. r_error.expected = (int32_t)sizeof...(P);
  336. return;
  337. }
  338. #endif
  339. Variant args[sizeof...(P) == 0 ? 1 : sizeof...(P)]; // Avoid zero sized array.
  340. std::array<const Variant *, sizeof...(P)> argsp;
  341. for (int32_t i = 0; i < (int32_t)sizeof...(P); i++) {
  342. if (i < p_argcount) {
  343. args[i] = Variant(p_args[i]);
  344. } else {
  345. args[i] = default_values[i - p_argcount + (dvs - missing)];
  346. }
  347. argsp[i] = &args[i];
  348. }
  349. call_with_variant_argsc_helper(p_instance, p_method, argsp.data(), r_error, BuildIndexSequence<sizeof...(P)>{});
  350. }
  351. template <typename T, typename R, typename... P>
  352. 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) {
  353. #ifdef DEBUG_ENABLED
  354. if ((size_t)p_argcount > sizeof...(P)) {
  355. r_error.error = GDEXTENSION_CALL_ERROR_TOO_MANY_ARGUMENTS;
  356. r_error.expected = (int32_t)sizeof...(P);
  357. return;
  358. }
  359. #endif
  360. int32_t missing = (int32_t)sizeof...(P) - (int32_t)p_argcount;
  361. int32_t dvs = (int32_t)default_values.size();
  362. #ifdef DEBUG_ENABLED
  363. if (missing > dvs) {
  364. r_error.error = GDEXTENSION_CALL_ERROR_TOO_FEW_ARGUMENTS;
  365. r_error.expected = (int32_t)sizeof...(P);
  366. return;
  367. }
  368. #endif
  369. Variant args[sizeof...(P) == 0 ? 1 : sizeof...(P)]; // Avoid zero sized array.
  370. std::array<const Variant *, sizeof...(P)> argsp;
  371. for (int32_t i = 0; i < (int32_t)sizeof...(P); i++) {
  372. if (i < p_argcount) {
  373. args[i] = Variant(p_args[i]);
  374. } else {
  375. args[i] = default_values[i - p_argcount + (dvs - missing)];
  376. }
  377. argsp[i] = &args[i];
  378. }
  379. call_with_variant_args_ret_helper(p_instance, p_method, argsp.data(), r_ret, r_error, BuildIndexSequence<sizeof...(P)>{});
  380. }
  381. template <typename T, typename R, typename... P>
  382. 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) {
  383. #ifdef DEBUG_ENABLED
  384. if ((size_t)p_argcount > sizeof...(P)) {
  385. r_error.error = GDEXTENSION_CALL_ERROR_TOO_MANY_ARGUMENTS;
  386. r_error.expected = (int32_t)sizeof...(P);
  387. return;
  388. }
  389. #endif
  390. int32_t missing = (int32_t)sizeof...(P) - (int32_t)p_argcount;
  391. int32_t dvs = (int32_t)default_values.size();
  392. #ifdef DEBUG_ENABLED
  393. if (missing > dvs) {
  394. r_error.error = GDEXTENSION_CALL_ERROR_TOO_FEW_ARGUMENTS;
  395. r_error.expected = (int32_t)sizeof...(P);
  396. return;
  397. }
  398. #endif
  399. Variant args[sizeof...(P) == 0 ? 1 : sizeof...(P)]; // Avoid zero sized array.
  400. std::array<const Variant *, sizeof...(P)> argsp;
  401. for (int32_t i = 0; i < (int32_t)sizeof...(P); i++) {
  402. if (i < p_argcount) {
  403. args[i] = Variant(p_args[i]);
  404. } else {
  405. args[i] = default_values[i - p_argcount + (dvs - missing)];
  406. }
  407. argsp[i] = &args[i];
  408. }
  409. call_with_variant_args_retc_helper(p_instance, p_method, argsp.data(), r_ret, r_error, BuildIndexSequence<sizeof...(P)>{});
  410. }
  411. // GCC raises "parameter 'p_args' set but not used" when P = {},
  412. // it's not clever enough to treat other P values as making this branch valid.
  413. #if defined(DEBUG_METHODS_ENABLED) && defined(__GNUC__) && !defined(__clang__)
  414. #pragma GCC diagnostic push
  415. #pragma GCC diagnostic ignored "-Wunused-but-set-parameter"
  416. #endif
  417. template <typename Q>
  418. void call_get_argument_type_helper(int p_arg, int &index, GDExtensionVariantType &type) {
  419. if (p_arg == index) {
  420. type = GDExtensionVariantType(GetTypeInfo<Q>::VARIANT_TYPE);
  421. }
  422. index++;
  423. }
  424. template <typename... P>
  425. GDExtensionVariantType call_get_argument_type(int p_arg) {
  426. GDExtensionVariantType type = GDEXTENSION_VARIANT_TYPE_NIL;
  427. int index = 0;
  428. // I think rocket science is simpler than modern C++.
  429. using expand_type = int[];
  430. expand_type a{ 0, (call_get_argument_type_helper<P>(p_arg, index, type), 0)... };
  431. (void)a; // Suppress (valid, but unavoidable) -Wunused-variable warning.
  432. (void)index; // Suppress GCC warning.
  433. return type;
  434. }
  435. template <typename Q>
  436. void call_get_argument_type_info_helper(int p_arg, int &index, PropertyInfo &info) {
  437. if (p_arg == index) {
  438. info = GetTypeInfo<Q>::get_class_info();
  439. }
  440. index++;
  441. }
  442. template <typename... P>
  443. void call_get_argument_type_info(int p_arg, PropertyInfo &info) {
  444. int index = 0;
  445. // I think rocket science is simpler than modern C++.
  446. using expand_type = int[];
  447. expand_type a{ 0, (call_get_argument_type_info_helper<P>(p_arg, index, info), 0)... };
  448. (void)a; // Suppress (valid, but unavoidable) -Wunused-variable warning.
  449. (void)index; // Suppress GCC warning.
  450. }
  451. template <typename Q>
  452. void call_get_argument_metadata_helper(int p_arg, int &index, GDExtensionClassMethodArgumentMetadata &md) {
  453. if (p_arg == index) {
  454. md = GetTypeInfo<Q>::METADATA;
  455. }
  456. index++;
  457. }
  458. template <typename... P>
  459. GDExtensionClassMethodArgumentMetadata call_get_argument_metadata(int p_arg) {
  460. GDExtensionClassMethodArgumentMetadata md = GDEXTENSION_METHOD_ARGUMENT_METADATA_NONE;
  461. int index = 0;
  462. // I think rocket science is simpler than modern C++.
  463. using expand_type = int[];
  464. expand_type a{ 0, (call_get_argument_metadata_helper<P>(p_arg, index, md), 0)... };
  465. (void)a; // Suppress (valid, but unavoidable) -Wunused-variable warning.
  466. (void)index;
  467. return md;
  468. }
  469. template <typename... P, size_t... Is>
  470. void call_with_variant_args_static(void (*p_method)(P...), const Variant **p_args, GDExtensionCallError &r_error, IndexSequence<Is...>) {
  471. r_error.error = GDEXTENSION_CALL_OK;
  472. #ifdef DEBUG_METHODS_ENABLED
  473. (p_method)(VariantCasterAndValidate<P>::cast(p_args, Is, r_error)...);
  474. #else
  475. (p_method)(VariantCaster<P>::cast(*p_args[Is])...);
  476. #endif
  477. }
  478. template <typename... P>
  479. 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) {
  480. #ifdef DEBUG_ENABLED
  481. if ((size_t)p_argcount > sizeof...(P)) {
  482. r_error.error = GDEXTENSION_CALL_ERROR_TOO_MANY_ARGUMENTS;
  483. r_error.expected = sizeof...(P);
  484. return;
  485. }
  486. #endif
  487. int32_t missing = (int32_t)sizeof...(P) - (int32_t)p_argcount;
  488. int32_t dvs = default_values.size();
  489. #ifdef DEBUG_ENABLED
  490. if (missing > dvs) {
  491. r_error.error = GDEXTENSION_CALL_ERROR_TOO_FEW_ARGUMENTS;
  492. r_error.expected = sizeof...(P);
  493. return;
  494. }
  495. #endif
  496. Variant args[sizeof...(P) == 0 ? 1 : sizeof...(P)]; // Avoid zero sized array.
  497. std::array<const Variant *, sizeof...(P)> argsp;
  498. for (int32_t i = 0; i < (int32_t)sizeof...(P); i++) {
  499. if (i < p_argcount) {
  500. args[i] = Variant(p_args[i]);
  501. } else {
  502. args[i] = default_values[i - p_argcount + (dvs - missing)];
  503. }
  504. argsp[i] = &args[i];
  505. }
  506. call_with_variant_args_static(p_method, argsp.data(), r_error, BuildIndexSequence<sizeof...(P)>{});
  507. }
  508. template <typename... P, size_t... Is>
  509. void call_with_ptr_args_static_method_helper(void (*p_method)(P...), const GDExtensionConstTypePtr *p_args, IndexSequence<Is...>) {
  510. p_method(PtrToArg<P>::convert(p_args[Is])...);
  511. }
  512. template <typename... P>
  513. void call_with_ptr_args_static_method(void (*p_method)(P...), const GDExtensionConstTypePtr *p_args) {
  514. call_with_ptr_args_static_method_helper<P...>(p_method, p_args, BuildIndexSequence<sizeof...(P)>{});
  515. }
  516. template <typename R, typename... P>
  517. void call_with_variant_args_static_ret(R (*p_method)(P...), const Variant **p_args, int p_argcount, Variant &r_ret, GDExtensionCallError &r_error) {
  518. #ifdef DEBUG_ENABLED
  519. if ((size_t)p_argcount > sizeof...(P)) {
  520. r_error.error = GDEXTENSION_CALL_ERROR_TOO_MANY_ARGUMENTS;
  521. r_error.expected = (int32_t)sizeof...(P);
  522. return;
  523. }
  524. if ((size_t)p_argcount < sizeof...(P)) {
  525. r_error.error = GDEXTENSION_CALL_ERROR_TOO_FEW_ARGUMENTS;
  526. r_error.expected = (int32_t)sizeof...(P);
  527. return;
  528. }
  529. #endif
  530. call_with_variant_args_static_ret<R, P...>(p_method, p_args, r_ret, r_error, BuildIndexSequence<sizeof...(P)>{});
  531. }
  532. template <typename... P>
  533. void call_with_variant_args_static_ret(void (*p_method)(P...), const Variant **p_args, int p_argcount, Variant &r_ret, GDExtensionCallError &r_error) {
  534. #ifdef DEBUG_ENABLED
  535. if ((size_t)p_argcount > sizeof...(P)) {
  536. r_error.error = GDEXTENSION_CALL_ERROR_TOO_MANY_ARGUMENTS;
  537. r_error.expected = (int32_t)sizeof...(P);
  538. return;
  539. }
  540. if ((size_t)p_argcount < sizeof...(P)) {
  541. r_error.error = GDEXTENSION_CALL_ERROR_TOO_FEW_ARGUMENTS;
  542. r_error.expected = (int32_t)sizeof...(P);
  543. return;
  544. }
  545. #endif
  546. call_with_variant_args_static<P...>(p_method, p_args, r_error, BuildIndexSequence<sizeof...(P)>{});
  547. }
  548. template <typename R, typename... P, size_t... Is>
  549. void call_with_variant_args_static_ret(R (*p_method)(P...), const Variant **p_args, Variant &r_ret, GDExtensionCallError &r_error, IndexSequence<Is...>) {
  550. r_error.error = GDEXTENSION_CALL_OK;
  551. #ifdef DEBUG_METHODS_ENABLED
  552. r_ret = (p_method)(VariantCasterAndValidate<P>::cast(p_args, Is, r_error)...);
  553. #else
  554. r_ret = (p_method)(VariantCaster<P>::cast(*p_args[Is])...);
  555. #endif
  556. }
  557. template <typename R, typename... P>
  558. 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) {
  559. #ifdef DEBUG_ENABLED
  560. if ((size_t)p_argcount > sizeof...(P)) {
  561. r_error.error = GDEXTENSION_CALL_ERROR_TOO_MANY_ARGUMENTS;
  562. r_error.expected = sizeof...(P);
  563. return;
  564. }
  565. #endif
  566. int32_t missing = (int32_t)sizeof...(P) - (int32_t)p_argcount;
  567. int32_t dvs = default_values.size();
  568. #ifdef DEBUG_ENABLED
  569. if (missing > dvs) {
  570. r_error.error = GDEXTENSION_CALL_ERROR_TOO_FEW_ARGUMENTS;
  571. r_error.expected = sizeof...(P);
  572. return;
  573. }
  574. #endif
  575. Variant args[sizeof...(P) == 0 ? 1 : sizeof...(P)]; // Avoid zero sized array.
  576. std::array<const Variant *, sizeof...(P)> argsp;
  577. for (int32_t i = 0; i < (int32_t)sizeof...(P); i++) {
  578. if (i < p_argcount) {
  579. args[i] = Variant(p_args[i]);
  580. } else {
  581. args[i] = default_values[i - p_argcount + (dvs - missing)];
  582. }
  583. argsp[i] = &args[i];
  584. }
  585. call_with_variant_args_static_ret(p_method, argsp.data(), r_ret, r_error, BuildIndexSequence<sizeof...(P)>{});
  586. }
  587. template <typename R, typename... P, size_t... Is>
  588. void call_with_ptr_args_static_method_ret_helper(R (*p_method)(P...), const GDExtensionConstTypePtr *p_args, void *r_ret, IndexSequence<Is...>) {
  589. PtrToArg<R>::encode(p_method(PtrToArg<P>::convert(p_args[Is])...), r_ret);
  590. }
  591. template <typename R, typename... P>
  592. void call_with_ptr_args_static_method_ret(R (*p_method)(P...), const GDExtensionConstTypePtr *p_args, void *r_ret) {
  593. call_with_ptr_args_static_method_ret_helper<R, P...>(p_method, p_args, r_ret, BuildIndexSequence<sizeof...(P)>{});
  594. }
  595. #if defined(__GNUC__) && !defined(__clang__)
  596. #pragma GCC diagnostic pop
  597. #endif
  598. } // namespace godot
  599. #include <godot_cpp/classes/global_constants_binds.hpp>
  600. #include <godot_cpp/variant/builtin_binds.hpp>
  601. #endif // GODOT_BINDER_COMMON_HPP