method_bind.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. // object.h needs to be the first include *before* method_bind.h
  31. // FIXME: Find out why and fix potential cyclical dependencies.
  32. #include "core/object/object.h"
  33. #include "method_bind.h"
  34. uint32_t MethodBind::get_hash() const {
  35. uint32_t hash = hash_murmur3_one_32(has_return() ? 1 : 0);
  36. hash = hash_murmur3_one_32(get_argument_count(), hash);
  37. for (int i = (has_return() ? -1 : 0); i < get_argument_count(); i++) {
  38. PropertyInfo pi = i == -1 ? get_return_info() : get_argument_info(i);
  39. hash = hash_murmur3_one_32(get_argument_type(i), hash);
  40. if (pi.class_name != StringName()) {
  41. hash = hash_murmur3_one_32(pi.class_name.operator String().hash(), hash);
  42. }
  43. }
  44. hash = hash_murmur3_one_32(get_default_argument_count(), hash);
  45. for (int i = 0; i < get_argument_count(); i++) {
  46. if (has_default_argument(i)) {
  47. Variant v = get_default_argument(i);
  48. hash = hash_murmur3_one_32(v.hash(), hash);
  49. }
  50. }
  51. hash = hash_murmur3_one_32(is_const(), hash);
  52. hash = hash_murmur3_one_32(is_vararg(), hash);
  53. return hash_fmix32(hash);
  54. }
  55. PropertyInfo MethodBind::get_argument_info(int p_argument) const {
  56. ERR_FAIL_INDEX_V(p_argument, get_argument_count(), PropertyInfo());
  57. PropertyInfo info = _gen_argument_type_info(p_argument);
  58. #ifdef DEBUG_METHODS_ENABLED
  59. if (info.name.is_empty()) {
  60. info.name = p_argument < arg_names.size() ? String(arg_names[p_argument]) : String("_unnamed_arg" + itos(p_argument));
  61. }
  62. #endif
  63. return info;
  64. }
  65. PropertyInfo MethodBind::get_return_info() const {
  66. return _gen_argument_type_info(-1);
  67. }
  68. void MethodBind::_set_const(bool p_const) {
  69. _const = p_const;
  70. }
  71. void MethodBind::_set_static(bool p_static) {
  72. _static = p_static;
  73. }
  74. void MethodBind::_set_returns(bool p_returns) {
  75. _returns = p_returns;
  76. }
  77. StringName MethodBind::get_name() const {
  78. return name;
  79. }
  80. void MethodBind::set_name(const StringName &p_name) {
  81. name = p_name;
  82. }
  83. #ifdef DEBUG_METHODS_ENABLED
  84. void MethodBind::set_argument_names(const Vector<StringName> &p_names) {
  85. arg_names = p_names;
  86. }
  87. Vector<StringName> MethodBind::get_argument_names() const {
  88. return arg_names;
  89. }
  90. #endif
  91. void MethodBind::set_default_arguments(const Vector<Variant> &p_defargs) {
  92. default_arguments = p_defargs;
  93. default_argument_count = default_arguments.size();
  94. }
  95. void MethodBind::_generate_argument_types(int p_count) {
  96. set_argument_count(p_count);
  97. Variant::Type *argt = memnew_arr(Variant::Type, p_count + 1);
  98. argt[0] = _gen_argument_type(-1); // return type
  99. for (int i = 0; i < p_count; i++) {
  100. argt[i + 1] = _gen_argument_type(i);
  101. }
  102. argument_types = argt;
  103. }
  104. MethodBind::MethodBind() {
  105. static int last_id = 0;
  106. method_id = last_id++;
  107. }
  108. MethodBind::~MethodBind() {
  109. if (argument_types) {
  110. memdelete_arr(argument_types);
  111. }
  112. }