callable_bind.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*************************************************************************/
  2. /* callable_bind.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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 "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 = (const CallableCustomBind *)p_a;
  40. const CallableCustomBind *b = (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 = (const CallableCustomBind *)p_a;
  51. const CallableCustomBind *b = (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. void CallableCustomBind::call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const {
  75. const Variant **args = (const Variant **)alloca(sizeof(const Variant **) * (binds.size() + p_argcount));
  76. for (int i = 0; i < p_argcount; i++) {
  77. args[i] = (const Variant *)p_arguments[i];
  78. }
  79. for (int i = 0; i < binds.size(); i++) {
  80. args[i + p_argcount] = (const Variant *)&binds[i];
  81. }
  82. callable.call(args, p_argcount + binds.size(), r_return_value, r_call_error);
  83. }
  84. CallableCustomBind::CallableCustomBind(const Callable &p_callable, const Vector<Variant> &p_binds) {
  85. callable = p_callable;
  86. binds = p_binds;
  87. }
  88. CallableCustomBind::~CallableCustomBind() {
  89. }
  90. //////////////////////////////////
  91. uint32_t CallableCustomUnbind::hash() const {
  92. return callable.hash();
  93. }
  94. String CallableCustomUnbind::get_as_text() const {
  95. return callable.operator String();
  96. }
  97. bool CallableCustomUnbind::_equal_func(const CallableCustom *p_a, const CallableCustom *p_b) {
  98. const CallableCustomUnbind *a = (const CallableCustomUnbind *)p_a;
  99. const CallableCustomUnbind *b = (const CallableCustomUnbind *)p_b;
  100. if (!(a->callable != b->callable)) {
  101. return false;
  102. }
  103. if (a->argcount != b->argcount) {
  104. return false;
  105. }
  106. return true;
  107. }
  108. bool CallableCustomUnbind::_less_func(const CallableCustom *p_a, const CallableCustom *p_b) {
  109. const CallableCustomUnbind *a = (const CallableCustomUnbind *)p_a;
  110. const CallableCustomUnbind *b = (const CallableCustomUnbind *)p_b;
  111. if (a->callable < b->callable) {
  112. return true;
  113. } else if (b->callable < a->callable) {
  114. return false;
  115. }
  116. return a->argcount < b->argcount;
  117. }
  118. CallableCustom::CompareEqualFunc CallableCustomUnbind::get_compare_equal_func() const {
  119. return _equal_func;
  120. }
  121. CallableCustom::CompareLessFunc CallableCustomUnbind::get_compare_less_func() const {
  122. return _less_func;
  123. }
  124. StringName CallableCustomUnbind::get_method() const {
  125. return callable.get_method();
  126. }
  127. ObjectID CallableCustomUnbind::get_object() const {
  128. return callable.get_object_id();
  129. }
  130. const Callable *CallableCustomUnbind::get_base_comparator() const {
  131. return &callable;
  132. }
  133. void CallableCustomUnbind::call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const {
  134. if (argcount > p_argcount) {
  135. r_call_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  136. r_call_error.argument = 0;
  137. r_call_error.expected = argcount;
  138. return;
  139. }
  140. callable.call(p_arguments, p_argcount - argcount, r_return_value, r_call_error);
  141. }
  142. CallableCustomUnbind::CallableCustomUnbind(const Callable &p_callable, int p_argcount) {
  143. callable = p_callable;
  144. argcount = p_argcount;
  145. }
  146. CallableCustomUnbind::~CallableCustomUnbind() {
  147. }
  148. Callable callable_bind(const Callable &p_callable, const Variant &p_arg1) {
  149. const Variant *args[1] = { &p_arg1 };
  150. return p_callable.bind(args, 1);
  151. }
  152. Callable callable_bind(const Callable &p_callable, const Variant &p_arg1, const Variant &p_arg2) {
  153. const Variant *args[2] = { &p_arg1, &p_arg2 };
  154. return p_callable.bind(args, 2);
  155. }
  156. Callable callable_bind(const Callable &p_callable, const Variant &p_arg1, const Variant &p_arg2, const Variant &p_arg3) {
  157. const Variant *args[3] = { &p_arg1, &p_arg2, &p_arg3 };
  158. return p_callable.bind(args, 3);
  159. }
  160. Callable callable_bind(const Callable &p_callable, const Variant &p_arg1, const Variant &p_arg2, const Variant &p_arg3, const Variant &p_arg4) {
  161. const Variant *args[4] = { &p_arg1, &p_arg2, &p_arg3, &p_arg4 };
  162. return p_callable.bind(args, 4);
  163. }
  164. Callable callable_bind(const Callable &p_callable, const Variant &p_arg1, const Variant &p_arg2, const Variant &p_arg3, const Variant &p_arg4, const Variant &p_arg5) {
  165. const Variant *args[5] = { &p_arg1, &p_arg2, &p_arg3, &p_arg4, &p_arg5 };
  166. return p_callable.bind(args, 5);
  167. }