method_bind.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /**************************************************************************/
  2. /* method_bind.cpp */
  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. #include <godot_cpp/core/method_bind.hpp>
  31. namespace godot {
  32. StringName MethodBind::get_name() const {
  33. return name;
  34. }
  35. void MethodBind::set_name(const StringName &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_static(bool p_static) {
  48. _static = p_static;
  49. }
  50. void MethodBind::set_vararg(bool p_vararg) {
  51. _vararg = p_vararg;
  52. }
  53. void MethodBind::set_argument_names(const std::vector<StringName> &p_names) {
  54. argument_names = p_names;
  55. }
  56. std::vector<StringName> MethodBind::get_argument_names() const {
  57. return argument_names;
  58. }
  59. void MethodBind::generate_argument_types(int p_count) {
  60. set_argument_count(p_count);
  61. if (argument_types != nullptr) {
  62. memdelete_arr(argument_types);
  63. }
  64. argument_types = memnew_arr(GDExtensionVariantType, p_count + 1);
  65. // -1 means return type.
  66. for (int i = -1; i < p_count; i++) {
  67. argument_types[i + 1] = gen_argument_type(i);
  68. }
  69. }
  70. PropertyInfo MethodBind::get_argument_info(int p_argument) const {
  71. PropertyInfo info = gen_argument_type_info(p_argument);
  72. if (p_argument >= 0) {
  73. info.name = p_argument < (int)argument_names.size() ? argument_names[p_argument] : "";
  74. }
  75. return info;
  76. }
  77. void MethodBind::bind_call(void *p_method_userdata, GDExtensionClassInstancePtr p_instance, const GDExtensionConstVariantPtr *p_args, GDExtensionInt p_argument_count, GDExtensionVariantPtr r_return, GDExtensionCallError *r_error) {
  78. const MethodBind *bind = reinterpret_cast<const MethodBind *>(p_method_userdata);
  79. Variant ret = bind->call(p_instance, p_args, p_argument_count, *r_error);
  80. // This assumes the return value is an empty Variant, so it doesn't need to call the destructor first.
  81. // Since only GDExtensionMethodBind calls this from the Godot side, it should always be the case.
  82. internal::gde_interface->variant_new_copy(r_return, ret._native_ptr());
  83. }
  84. void MethodBind::bind_ptrcall(void *p_method_userdata, GDExtensionClassInstancePtr p_instance, const GDExtensionConstTypePtr *p_args, GDExtensionTypePtr r_return) {
  85. const MethodBind *bind = reinterpret_cast<const MethodBind *>(p_method_userdata);
  86. bind->ptrcall(p_instance, p_args, r_return);
  87. }
  88. MethodBind::~MethodBind() {
  89. if (argument_types) {
  90. memdelete_arr(argument_types);
  91. }
  92. }
  93. } // namespace godot