engine_ptrcall.hpp 4.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*************************************************************************/
  2. /* engine_ptrcall.hpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef GODOT_ENGINE_PTRCALL_HPP
  31. #define GODOT_ENGINE_PTRCALL_HPP
  32. #include <godot/gdextension_interface.h>
  33. #include <godot_cpp/core/binder_common.hpp>
  34. #include <godot_cpp/core/object.hpp>
  35. #include <godot_cpp/godot.hpp>
  36. #include <array>
  37. namespace godot {
  38. namespace internal {
  39. template <class O, class... Args>
  40. O *_call_native_mb_ret_obj(const GDExtensionMethodBindPtr mb, void *instance, const Args &...args) {
  41. GodotObject *ret = nullptr;
  42. std::array<GDExtensionConstTypePtr, sizeof...(Args)> mb_args = { { (GDExtensionConstTypePtr)args... } };
  43. internal::gde_interface->object_method_bind_ptrcall(mb, instance, mb_args.data(), &ret);
  44. if (ret == nullptr) {
  45. return nullptr;
  46. }
  47. return reinterpret_cast<O *>(internal::gde_interface->object_get_instance_binding(ret, internal::token, &O::___binding_callbacks));
  48. }
  49. template <class R, class... Args>
  50. R _call_native_mb_ret(const GDExtensionMethodBindPtr mb, void *instance, const Args &...args) {
  51. R ret;
  52. std::array<GDExtensionConstTypePtr, sizeof...(Args)> mb_args = { { (GDExtensionConstTypePtr)args... } };
  53. internal::gde_interface->object_method_bind_ptrcall(mb, instance, mb_args.data(), &ret);
  54. return ret;
  55. }
  56. template <class... Args>
  57. void _call_native_mb_no_ret(const GDExtensionMethodBindPtr mb, void *instance, const Args &...args) {
  58. std::array<GDExtensionConstTypePtr, sizeof...(Args)> mb_args = { { (GDExtensionConstTypePtr)args... } };
  59. internal::gde_interface->object_method_bind_ptrcall(mb, instance, mb_args.data(), nullptr);
  60. }
  61. template <class R, class... Args>
  62. R _call_utility_ret(GDExtensionPtrUtilityFunction func, const Args &...args) {
  63. R ret;
  64. std::array<GDExtensionConstTypePtr, sizeof...(Args)> mb_args = { { (GDExtensionConstTypePtr)args... } };
  65. func(&ret, mb_args.data(), mb_args.size());
  66. return ret;
  67. }
  68. template <class... Args>
  69. Object *_call_utility_ret_obj(const GDExtensionPtrUtilityFunction func, void *instance, const Args &...args) {
  70. GodotObject *ret = nullptr;
  71. std::array<GDExtensionConstTypePtr, sizeof...(Args)> mb_args = { { (GDExtensionConstTypePtr)args... } };
  72. func(&ret, mb_args.data(), mb_args.size());
  73. return (Object *)internal::gde_interface->object_get_instance_binding(ret, internal::token, &Object::___binding_callbacks);
  74. }
  75. template <class... Args>
  76. void _call_utility_no_ret(const GDExtensionPtrUtilityFunction func, const Args &...args) {
  77. std::array<GDExtensionConstTypePtr, sizeof...(Args)> mb_args = { { (GDExtensionConstTypePtr)args... } };
  78. func(nullptr, mb_args.data(), mb_args.size());
  79. }
  80. } // namespace internal
  81. } // namespace godot
  82. #endif // GODOT_ENGINE_PTRCALL_HPP