engine_ptrcall.hpp 4.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*************************************************************************/
  2. /* engine_ptrcall.hpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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_CPP_ENGINE_PTRCALL_HPP
  31. #define GODOT_CPP_ENGINE_PTRCALL_HPP
  32. #include <godot/gdnative_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 GDNativeMethodBindPtr mb, void *instance, const Args &...args) {
  41. GodotObject *ret = nullptr;
  42. std::array<const GDNativeTypePtr, sizeof...(Args)> mb_args = { { (const GDNativeTypePtr)args... } };
  43. internal::gdn_interface->object_method_bind_ptrcall(mb, instance, mb_args.data(), &ret);
  44. return reinterpret_cast<O *>(internal::gdn_interface->object_get_instance_binding(ret, internal::token, &O::___binding_callbacks));
  45. }
  46. template <class R, class... Args>
  47. R _call_native_mb_ret(const GDNativeMethodBindPtr mb, void *instance, const Args &...args) {
  48. R ret;
  49. std::array<const GDNativeTypePtr, sizeof...(Args)> mb_args = { { (const GDNativeTypePtr)args... } };
  50. internal::gdn_interface->object_method_bind_ptrcall(mb, instance, mb_args.data(), &ret);
  51. return ret;
  52. }
  53. template <class... Args>
  54. void _call_native_mb_no_ret(const GDNativeMethodBindPtr mb, void *instance, const Args &...args) {
  55. std::array<const GDNativeTypePtr, sizeof...(Args)> mb_args = { { (const GDNativeTypePtr)args... } };
  56. internal::gdn_interface->object_method_bind_ptrcall(mb, instance, mb_args.data(), nullptr);
  57. }
  58. template <class R, class... Args>
  59. R _call_utility_ret(GDNativePtrUtilityFunction func, const Args &...args) {
  60. R ret;
  61. std::array<const GDNativeTypePtr, sizeof...(Args)> mb_args = { { (const GDNativeTypePtr)args... } };
  62. func(&ret, mb_args.data(), mb_args.size());
  63. return ret;
  64. }
  65. template <class... Args>
  66. Object *_call_utility_ret_obj(const GDNativePtrUtilityFunction func, void *instance, const Args &...args) {
  67. GodotObject *ret = nullptr;
  68. std::array<const GDNativeTypePtr, sizeof...(Args)> mb_args = { { (const GDNativeTypePtr)args... } };
  69. func(&ret, mb_args.data(), mb_args.size());
  70. return (Object *)internal::gdn_interface->object_get_instance_binding(ret, internal::token, &Object::___binding_callbacks);
  71. }
  72. template <class... Args>
  73. void _call_utility_no_ret(const GDNativePtrUtilityFunction func, const Args &...args) {
  74. std::array<const GDNativeTypePtr, sizeof...(Args)> mb_args = { { (const GDNativeTypePtr)args... } };
  75. func(nullptr, mb_args.data(), mb_args.size());
  76. }
  77. } // namespace internal
  78. } // namespace godot
  79. #endif // ! GODOT_CPP_ENGINE_PTRCALL_HPP