callable_bind.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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::get_bound_arguments(Vector<Variant> &r_arguments, int &r_argcount) const {
  78. Vector<Variant> sub_args;
  79. int sub_count;
  80. callable.get_bound_arguments_ref(sub_args, sub_count);
  81. if (sub_count == 0) {
  82. r_arguments = binds;
  83. r_argcount = binds.size();
  84. return;
  85. }
  86. int new_count = sub_count + binds.size();
  87. r_argcount = new_count;
  88. if (new_count <= 0) {
  89. // Removed more arguments than it adds.
  90. r_arguments = Vector<Variant>();
  91. return;
  92. }
  93. r_arguments.resize(new_count);
  94. if (sub_count > 0) {
  95. for (int i = 0; i < sub_count; i++) {
  96. r_arguments.write[i] = sub_args[i];
  97. }
  98. for (int i = 0; i < binds.size(); i++) {
  99. r_arguments.write[i + sub_count] = binds[i];
  100. }
  101. r_argcount = new_count;
  102. } else {
  103. for (int i = 0; i < binds.size() + sub_count; i++) {
  104. r_arguments.write[i] = binds[i - sub_count];
  105. }
  106. }
  107. }
  108. void CallableCustomBind::call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const {
  109. const Variant **args = (const Variant **)alloca(sizeof(const Variant **) * (binds.size() + p_argcount));
  110. for (int i = 0; i < p_argcount; i++) {
  111. args[i] = (const Variant *)p_arguments[i];
  112. }
  113. for (int i = 0; i < binds.size(); i++) {
  114. args[i + p_argcount] = (const Variant *)&binds[i];
  115. }
  116. callable.callp(args, p_argcount + binds.size(), r_return_value, r_call_error);
  117. }
  118. CallableCustomBind::CallableCustomBind(const Callable &p_callable, const Vector<Variant> &p_binds) {
  119. callable = p_callable;
  120. binds = p_binds;
  121. }
  122. CallableCustomBind::~CallableCustomBind() {
  123. }
  124. //////////////////////////////////
  125. uint32_t CallableCustomUnbind::hash() const {
  126. return callable.hash();
  127. }
  128. String CallableCustomUnbind::get_as_text() const {
  129. return callable.operator String();
  130. }
  131. bool CallableCustomUnbind::_equal_func(const CallableCustom *p_a, const CallableCustom *p_b) {
  132. const CallableCustomUnbind *a = static_cast<const CallableCustomUnbind *>(p_a);
  133. const CallableCustomUnbind *b = static_cast<const CallableCustomUnbind *>(p_b);
  134. if (!(a->callable != b->callable)) {
  135. return false;
  136. }
  137. if (a->argcount != b->argcount) {
  138. return false;
  139. }
  140. return true;
  141. }
  142. bool CallableCustomUnbind::_less_func(const CallableCustom *p_a, const CallableCustom *p_b) {
  143. const CallableCustomUnbind *a = static_cast<const CallableCustomUnbind *>(p_a);
  144. const CallableCustomUnbind *b = static_cast<const CallableCustomUnbind *>(p_b);
  145. if (a->callable < b->callable) {
  146. return true;
  147. } else if (b->callable < a->callable) {
  148. return false;
  149. }
  150. return a->argcount < b->argcount;
  151. }
  152. CallableCustom::CompareEqualFunc CallableCustomUnbind::get_compare_equal_func() const {
  153. return _equal_func;
  154. }
  155. CallableCustom::CompareLessFunc CallableCustomUnbind::get_compare_less_func() const {
  156. return _less_func;
  157. }
  158. StringName CallableCustomUnbind::get_method() const {
  159. return callable.get_method();
  160. }
  161. ObjectID CallableCustomUnbind::get_object() const {
  162. return callable.get_object_id();
  163. }
  164. const Callable *CallableCustomUnbind::get_base_comparator() const {
  165. return &callable;
  166. }
  167. int CallableCustomUnbind::get_bound_arguments_count() const {
  168. return callable.get_bound_arguments_count() - argcount;
  169. }
  170. void CallableCustomUnbind::get_bound_arguments(Vector<Variant> &r_arguments, int &r_argcount) const {
  171. Vector<Variant> sub_args;
  172. int sub_count;
  173. callable.get_bound_arguments_ref(sub_args, sub_count);
  174. r_argcount = sub_args.size() - argcount;
  175. if (argcount >= sub_args.size()) {
  176. r_arguments = Vector<Variant>();
  177. } else {
  178. sub_args.resize(sub_args.size() - argcount);
  179. r_arguments = sub_args;
  180. }
  181. }
  182. void CallableCustomUnbind::call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const {
  183. if (argcount > p_argcount) {
  184. r_call_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  185. r_call_error.argument = 0;
  186. r_call_error.expected = argcount;
  187. return;
  188. }
  189. callable.callp(p_arguments, p_argcount - argcount, r_return_value, r_call_error);
  190. }
  191. CallableCustomUnbind::CallableCustomUnbind(const Callable &p_callable, int p_argcount) {
  192. callable = p_callable;
  193. argcount = p_argcount;
  194. }
  195. CallableCustomUnbind::~CallableCustomUnbind() {
  196. }