callable_bind.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. CallableCustomBind::~CallableCustomBind() {
  140. }
  141. //////////////////////////////////
  142. uint32_t CallableCustomUnbind::hash() const {
  143. return callable.hash();
  144. }
  145. String CallableCustomUnbind::get_as_text() const {
  146. return callable.operator String();
  147. }
  148. bool CallableCustomUnbind::_equal_func(const CallableCustom *p_a, const CallableCustom *p_b) {
  149. const CallableCustomUnbind *a = static_cast<const CallableCustomUnbind *>(p_a);
  150. const CallableCustomUnbind *b = static_cast<const CallableCustomUnbind *>(p_b);
  151. if (a->callable != b->callable) {
  152. return false;
  153. }
  154. if (a->argcount != b->argcount) {
  155. return false;
  156. }
  157. return true;
  158. }
  159. bool CallableCustomUnbind::_less_func(const CallableCustom *p_a, const CallableCustom *p_b) {
  160. const CallableCustomUnbind *a = static_cast<const CallableCustomUnbind *>(p_a);
  161. const CallableCustomUnbind *b = static_cast<const CallableCustomUnbind *>(p_b);
  162. if (a->callable < b->callable) {
  163. return true;
  164. } else if (b->callable < a->callable) {
  165. return false;
  166. }
  167. return a->argcount < b->argcount;
  168. }
  169. CallableCustom::CompareEqualFunc CallableCustomUnbind::get_compare_equal_func() const {
  170. return _equal_func;
  171. }
  172. CallableCustom::CompareLessFunc CallableCustomUnbind::get_compare_less_func() const {
  173. return _less_func;
  174. }
  175. bool CallableCustomUnbind::is_valid() const {
  176. return callable.is_valid();
  177. }
  178. StringName CallableCustomUnbind::get_method() const {
  179. return callable.get_method();
  180. }
  181. ObjectID CallableCustomUnbind::get_object() const {
  182. return callable.get_object_id();
  183. }
  184. const Callable *CallableCustomUnbind::get_base_comparator() const {
  185. return callable.get_base_comparator();
  186. }
  187. int CallableCustomUnbind::get_argument_count(bool &r_is_valid) const {
  188. int ret = callable.get_argument_count(&r_is_valid);
  189. if (r_is_valid) {
  190. return ret + argcount;
  191. }
  192. return 0;
  193. }
  194. int CallableCustomUnbind::get_bound_arguments_count() const {
  195. return callable.get_bound_arguments_count();
  196. }
  197. void CallableCustomUnbind::get_bound_arguments(Vector<Variant> &r_arguments) const {
  198. callable.get_bound_arguments_ref(r_arguments);
  199. }
  200. int CallableCustomUnbind::get_unbound_arguments_count() const {
  201. return callable.get_unbound_arguments_count() + argcount;
  202. }
  203. void CallableCustomUnbind::call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const {
  204. if (p_argcount < argcount) {
  205. r_call_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  206. r_call_error.expected = argcount;
  207. return;
  208. }
  209. callable.callp(p_arguments, p_argcount - argcount, r_return_value, r_call_error);
  210. }
  211. Error CallableCustomUnbind::rpc(int p_peer_id, const Variant **p_arguments, int p_argcount, Callable::CallError &r_call_error) const {
  212. if (p_argcount < argcount) {
  213. r_call_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  214. r_call_error.expected = argcount;
  215. return ERR_UNCONFIGURED;
  216. }
  217. return callable.rpcp(p_peer_id, p_arguments, p_argcount - argcount, r_call_error);
  218. }
  219. CallableCustomUnbind::CallableCustomUnbind(const Callable &p_callable, int p_argcount) {
  220. callable = p_callable;
  221. argcount = p_argcount;
  222. }
  223. CallableCustomUnbind::~CallableCustomUnbind() {
  224. }