method_bind.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*************************************************************************/
  2. /* method_bind.cpp */
  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. #include <godot_cpp/core/method_bind.hpp>
  31. namespace godot {
  32. const char *MethodBind::get_name() const {
  33. return name;
  34. }
  35. void MethodBind::set_name(const char *p_name) {
  36. name = p_name;
  37. }
  38. void MethodBind::set_argument_count(int p_count) {
  39. argument_count = p_count;
  40. }
  41. void MethodBind::set_const(bool p_const) {
  42. _is_const = p_const;
  43. }
  44. void MethodBind::set_return(bool p_return) {
  45. _has_return = p_return;
  46. }
  47. void MethodBind::set_argument_names(const std::vector<std::string> &p_names) {
  48. argument_names = p_names;
  49. }
  50. std::vector<std::string> MethodBind::get_argument_names() const {
  51. return argument_names;
  52. }
  53. void MethodBind::generate_argument_types(int p_count) {
  54. set_argument_count(p_count);
  55. if (argument_types != nullptr) {
  56. memdelete_arr(argument_types);
  57. }
  58. argument_types = memnew_arr(GDNativeVariantType, p_count + 1);
  59. // -1 means return type.
  60. for (int i = -1; i < p_count; i++) {
  61. argument_types[i + 1] = gen_argument_type(i);
  62. }
  63. }
  64. GDNativePropertyInfo MethodBind::get_argument_info(int p_argument) const {
  65. GDNativePropertyInfo info = gen_argument_type_info(p_argument);
  66. if (p_argument >= 0) {
  67. info.name = p_argument < (int)argument_names.size() ? argument_names[p_argument].c_str() : "";
  68. }
  69. return info;
  70. }
  71. GDNativeVariantType MethodBind::bind_get_argument_type(void *p_method_userdata, int32_t p_argument) {
  72. const MethodBind *bind = reinterpret_cast<const MethodBind *>(p_method_userdata);
  73. return bind->get_argument_type(p_argument);
  74. }
  75. void MethodBind::bind_get_argument_info(void *p_method_userdata, int32_t p_argument, GDNativePropertyInfo *r_info) {
  76. const MethodBind *bind = reinterpret_cast<const MethodBind *>(p_method_userdata);
  77. *r_info = bind->get_argument_info(p_argument);
  78. }
  79. GDNativeExtensionClassMethodArgumentMetadata MethodBind::bind_get_argument_metadata(void *p_method_userdata, int32_t p_argument) {
  80. const MethodBind *bind = reinterpret_cast<const MethodBind *>(p_method_userdata);
  81. return bind->get_argument_metadata(p_argument);
  82. }
  83. void MethodBind::bind_call(void *p_method_userdata, GDExtensionClassInstancePtr p_instance, const GDNativeVariantPtr *p_args, const GDNativeInt p_argument_count, GDNativeVariantPtr r_return, GDNativeCallError *r_error) {
  84. const MethodBind *bind = reinterpret_cast<const MethodBind *>(p_method_userdata);
  85. Variant ret = bind->call(p_instance, p_args, p_argument_count, *r_error);
  86. // This assumes the return value is an empty Variant, so it doesn't need to call the destructor first.
  87. // Since only NativeExtensionMethodBind calls this from the Godot side, it should always be the case.
  88. internal::gdn_interface->variant_new_copy(r_return, ret._native_ptr());
  89. }
  90. void MethodBind::bind_ptrcall(void *p_method_userdata, GDExtensionClassInstancePtr p_instance, const GDNativeTypePtr *p_args, GDNativeTypePtr r_return) {
  91. const MethodBind *bind = reinterpret_cast<const MethodBind *>(p_method_userdata);
  92. bind->ptrcall(p_instance, p_args, r_return);
  93. }
  94. MethodBind::~MethodBind() {
  95. if (argument_types) {
  96. memdelete_arr(argument_types);
  97. }
  98. }
  99. } // namespace godot