binder_common.hpp 27 KB

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