callable_bind.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /**************************************************************************/
  2. /* callable_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 "callable_bind.h"
  31. //////////////////////////////////
  32. uint32_t CallableCustomBind::hash() const {
  33. return callable.hash();
  34. }
  35. String CallableCustomBind::get_as_text() const {
  36. return callable.operator String();
  37. }
  38. bool CallableCustomBind::_equal_func(const CallableCustom *p_a, const CallableCustom *p_b) {
  39. const CallableCustomBind *a = static_cast<const CallableCustomBind *>(p_a);
  40. const CallableCustomBind *b = static_cast<const CallableCustomBind *>(p_b);
  41. if (!(a->callable != b->callable)) {
  42. return false;
  43. }
  44. if (a->binds.size() != b->binds.size()) {
  45. return false;
  46. }
  47. return true;
  48. }
  49. bool CallableCustomBind::_less_func(const CallableCustom *p_a, const CallableCustom *p_b) {
  50. const CallableCustomBind *a = static_cast<const CallableCustomBind *>(p_a);
  51. const CallableCustomBind *b = static_cast<const CallableCustomBind *>(p_b);
  52. if (a->callable < b->callable) {
  53. return true;
  54. } else if (b->callable < a->callable) {
  55. return false;
  56. }
  57. return a->binds.size() < b->binds.size();
  58. }
  59. CallableCustom::CompareEqualFunc CallableCustomBind::get_compare_equal_func() const {
  60. return _equal_func;
  61. }
  62. CallableCustom::CompareLessFunc CallableCustomBind::get_compare_less_func() const {
  63. return _less_func;
  64. }
  65. StringName CallableCustomBind::get_method() const {
  66. return callable.get_method();
  67. }
  68. ObjectID CallableCustomBind::get_object() const {
  69. return callable.get_object_id();
  70. }
  71. const Callable *CallableCustomBind::get_base_comparator() const {
  72. return &callable;
  73. }
  74. int CallableCustomBind::get_bound_arguments_count() const {
  75. return callable.get_bound_arguments_count() + binds.size();
  76. }
  77. void CallableCustomBind::call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const {
  78. const Variant **args = (const Variant **)alloca(sizeof(const Variant **) * (binds.size() + p_argcount));
  79. for (int i = 0; i < p_argcount; i++) {
  80. args[i] = (const Variant *)p_arguments[i];
  81. }
  82. for (int i = 0; i < binds.size(); i++) {
  83. args[i + p_argcount] = (const Variant *)&binds[i];
  84. }
  85. callable.callp(args, p_argcount + binds.size(), r_return_value, r_call_error);
  86. }
  87. CallableCustomBind::CallableCustomBind(const Callable &p_callable, const Vector<Variant> &p_binds) {
  88. callable = p_callable;
  89. binds = p_binds;
  90. }
  91. CallableCustomBind::~CallableCustomBind() {
  92. }
  93. //////////////////////////////////
  94. uint32_t CallableCustomUnbind::hash() const {
  95. return callable.hash();
  96. }
  97. String CallableCustomUnbind::get_as_text() const {
  98. return callable.operator String();
  99. }
  100. bool CallableCustomUnbind::_equal_func(const CallableCustom *p_a, const CallableCustom *p_b) {
  101. const CallableCustomUnbind *a = static_cast<const CallableCustomUnbind *>(p_a);
  102. const CallableCustomUnbind *b = static_cast<const CallableCustomUnbind *>(p_b);
  103. if (!(a->callable != b->callable)) {
  104. return false;
  105. }
  106. if (a->argcount != b->argcount) {
  107. return false;
  108. }
  109. return true;
  110. }
  111. bool CallableCustomUnbind::_less_func(const CallableCustom *p_a, const CallableCustom *p_b) {
  112. const CallableCustomUnbind *a = static_cast<const CallableCustomUnbind *>(p_a);
  113. const CallableCustomUnbind *b = static_cast<const CallableCustomUnbind *>(p_b);
  114. if (a->callable < b->callable) {
  115. return true;
  116. } else if (b->callable < a->callable) {
  117. return false;
  118. }
  119. return a->argcount < b->argcount;
  120. }
  121. CallableCustom::CompareEqualFunc CallableCustomUnbind::get_compare_equal_func() const {
  122. return _equal_func;
  123. }
  124. CallableCustom::CompareLessFunc CallableCustomUnbind::get_compare_less_func() const {
  125. return _less_func;
  126. }
  127. StringName CallableCustomUnbind::get_method() const {
  128. return callable.get_method();
  129. }
  130. ObjectID CallableCustomUnbind::get_object() const {
  131. return callable.get_object_id();
  132. }
  133. const Callable *CallableCustomUnbind::get_base_comparator() const {
  134. return &callable;
  135. }
  136. int CallableCustomUnbind::get_bound_arguments_count() const {
  137. return callable.get_bound_arguments_count() - argcount;
  138. }
  139. void CallableCustomUnbind::call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const {
  140. if (argcount > p_argcount) {
  141. r_call_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  142. r_call_error.argument = 0;
  143. r_call_error.expected = argcount;
  144. return;
  145. }
  146. callable.callp(p_arguments, p_argcount - argcount, r_return_value, r_call_error);
  147. }
  148. CallableCustomUnbind::CallableCustomUnbind(const Callable &p_callable, int p_argcount) {
  149. callable = p_callable;
  150. argcount = p_argcount;
  151. }
  152. CallableCustomUnbind::~CallableCustomUnbind() {
  153. }