callable_bind.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. bool CallableCustomBind::is_valid() const {
  66. return callable.is_valid();
  67. }
  68. StringName CallableCustomBind::get_method() const {
  69. return callable.get_method();
  70. }
  71. ObjectID CallableCustomBind::get_object() const {
  72. return callable.get_object_id();
  73. }
  74. const Callable *CallableCustomBind::get_base_comparator() const {
  75. return callable.get_base_comparator();
  76. }
  77. int CallableCustomBind::get_argument_count(bool &r_is_valid) const {
  78. int ret = callable.get_argument_count(&r_is_valid);
  79. if (r_is_valid) {
  80. return ret - binds.size();
  81. }
  82. return 0;
  83. }
  84. int CallableCustomBind::get_bound_arguments_count() const {
  85. return callable.get_bound_arguments_count() + MAX(0, binds.size() - callable.get_unbound_arguments_count());
  86. }
  87. void CallableCustomBind::get_bound_arguments(Vector<Variant> &r_arguments) const {
  88. Vector<Variant> sub_bound_args;
  89. callable.get_bound_arguments_ref(sub_bound_args);
  90. int sub_bound_count = sub_bound_args.size();
  91. int sub_unbound_count = callable.get_unbound_arguments_count();
  92. if (sub_bound_count == 0 && sub_unbound_count == 0) {
  93. r_arguments = binds;
  94. return;
  95. }
  96. int added_count = MAX(0, binds.size() - sub_unbound_count);
  97. int new_count = sub_bound_count + added_count;
  98. if (added_count <= 0) {
  99. // All added arguments are consumed by `sub_unbound_count`.
  100. r_arguments = sub_bound_args;
  101. return;
  102. }
  103. r_arguments.resize(new_count);
  104. Variant *args = r_arguments.ptrw();
  105. for (int i = 0; i < added_count; i++) {
  106. args[i] = binds[i];
  107. }
  108. for (int i = 0; i < sub_bound_count; i++) {
  109. args[i + added_count] = sub_bound_args[i];
  110. }
  111. }
  112. int CallableCustomBind::get_unbound_arguments_count() const {
  113. return MAX(0, callable.get_unbound_arguments_count() - binds.size());
  114. }
  115. void CallableCustomBind::call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const {
  116. const Variant **args = (const Variant **)alloca(sizeof(Variant *) * (binds.size() + p_argcount));
  117. for (int i = 0; i < p_argcount; i++) {
  118. args[i] = (const Variant *)p_arguments[i];
  119. }
  120. for (int i = 0; i < binds.size(); i++) {
  121. args[i + p_argcount] = (const Variant *)&binds[i];
  122. }
  123. callable.callp(args, p_argcount + binds.size(), r_return_value, r_call_error);
  124. }
  125. Error CallableCustomBind::rpc(int p_peer_id, const Variant **p_arguments, int p_argcount, Callable::CallError &r_call_error) const {
  126. const Variant **args = (const Variant **)alloca(sizeof(Variant *) * (binds.size() + p_argcount));
  127. for (int i = 0; i < p_argcount; i++) {
  128. args[i] = (const Variant *)p_arguments[i];
  129. }
  130. for (int i = 0; i < binds.size(); i++) {
  131. args[i + p_argcount] = (const Variant *)&binds[i];
  132. }
  133. return callable.rpcp(p_peer_id, args, p_argcount + binds.size(), r_call_error);
  134. }
  135. CallableCustomBind::CallableCustomBind(const Callable &p_callable, const Vector<Variant> &p_binds) {
  136. callable = p_callable;
  137. binds = p_binds;
  138. }
  139. //////////////////////////////////
  140. uint32_t CallableCustomUnbind::hash() const {
  141. return callable.hash();
  142. }
  143. String CallableCustomUnbind::get_as_text() const {
  144. return callable.operator String();
  145. }
  146. bool CallableCustomUnbind::_equal_func(const CallableCustom *p_a, const CallableCustom *p_b) {
  147. const CallableCustomUnbind *a = static_cast<const CallableCustomUnbind *>(p_a);
  148. const CallableCustomUnbind *b = static_cast<const CallableCustomUnbind *>(p_b);
  149. if (a->callable != b->callable) {
  150. return false;
  151. }
  152. if (a->argcount != b->argcount) {
  153. return false;
  154. }
  155. return true;
  156. }
  157. bool CallableCustomUnbind::_less_func(const CallableCustom *p_a, const CallableCustom *p_b) {
  158. const CallableCustomUnbind *a = static_cast<const CallableCustomUnbind *>(p_a);
  159. const CallableCustomUnbind *b = static_cast<const CallableCustomUnbind *>(p_b);
  160. if (a->callable < b->callable) {
  161. return true;
  162. } else if (b->callable < a->callable) {
  163. return false;
  164. }
  165. return a->argcount < b->argcount;
  166. }
  167. CallableCustom::CompareEqualFunc CallableCustomUnbind::get_compare_equal_func() const {
  168. return _equal_func;
  169. }
  170. CallableCustom::CompareLessFunc CallableCustomUnbind::get_compare_less_func() const {
  171. return _less_func;
  172. }
  173. bool CallableCustomUnbind::is_valid() const {
  174. return callable.is_valid();
  175. }
  176. StringName CallableCustomUnbind::get_method() const {
  177. return callable.get_method();
  178. }
  179. ObjectID CallableCustomUnbind::get_object() const {
  180. return callable.get_object_id();
  181. }
  182. const Callable *CallableCustomUnbind::get_base_comparator() const {
  183. return callable.get_base_comparator();
  184. }
  185. int CallableCustomUnbind::get_argument_count(bool &r_is_valid) const {
  186. int ret = callable.get_argument_count(&r_is_valid);
  187. if (r_is_valid) {
  188. return ret + argcount;
  189. }
  190. return 0;
  191. }
  192. int CallableCustomUnbind::get_bound_arguments_count() const {
  193. return callable.get_bound_arguments_count();
  194. }
  195. void CallableCustomUnbind::get_bound_arguments(Vector<Variant> &r_arguments) const {
  196. callable.get_bound_arguments_ref(r_arguments);
  197. }
  198. int CallableCustomUnbind::get_unbound_arguments_count() const {
  199. return callable.get_unbound_arguments_count() + argcount;
  200. }
  201. void CallableCustomUnbind::call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const {
  202. if (p_argcount < argcount) {
  203. r_call_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  204. r_call_error.expected = argcount;
  205. return;
  206. }
  207. callable.callp(p_arguments, p_argcount - argcount, r_return_value, r_call_error);
  208. }
  209. Error CallableCustomUnbind::rpc(int p_peer_id, const Variant **p_arguments, int p_argcount, Callable::CallError &r_call_error) const {
  210. if (p_argcount < argcount) {
  211. r_call_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  212. r_call_error.expected = argcount;
  213. return ERR_UNCONFIGURED;
  214. }
  215. return callable.rpcp(p_peer_id, p_arguments, p_argcount - argcount, r_call_error);
  216. }
  217. CallableCustomUnbind::CallableCustomUnbind(const Callable &p_callable, int p_argcount) {
  218. callable = p_callable;
  219. argcount = p_argcount;
  220. }