callable_bind.cpp 9.2 KB

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