callable.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. /*************************************************************************/
  2. /* callable.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.h"
  31. #include "callable_bind.h"
  32. #include "core/object/message_queue.h"
  33. #include "core/object/object.h"
  34. #include "core/object/ref_counted.h"
  35. #include "core/object/script_language.h"
  36. void Callable::call_deferredp(const Variant **p_arguments, int p_argcount) const {
  37. MessageQueue::get_singleton()->push_callablep(*this, p_arguments, p_argcount);
  38. }
  39. void Callable::callp(const Variant **p_arguments, int p_argcount, Variant &r_return_value, CallError &r_call_error) const {
  40. if (is_null()) {
  41. r_call_error.error = CallError::CALL_ERROR_INSTANCE_IS_NULL;
  42. r_call_error.argument = 0;
  43. r_call_error.expected = 0;
  44. r_return_value = Variant();
  45. } else if (is_custom()) {
  46. custom->call(p_arguments, p_argcount, r_return_value, r_call_error);
  47. } else {
  48. Object *obj = ObjectDB::get_instance(ObjectID(object));
  49. #ifdef DEBUG_ENABLED
  50. if (!obj) {
  51. r_call_error.error = CallError::CALL_ERROR_INSTANCE_IS_NULL;
  52. r_call_error.argument = 0;
  53. r_call_error.expected = 0;
  54. r_return_value = Variant();
  55. return;
  56. }
  57. #endif
  58. r_return_value = obj->callp(method, p_arguments, p_argcount, r_call_error);
  59. }
  60. }
  61. Variant Callable::callv(const Array &p_arguments) const {
  62. int argcount = p_arguments.size();
  63. const Variant **argptrs = nullptr;
  64. if (argcount) {
  65. argptrs = (const Variant **)alloca(sizeof(Variant *) * argcount);
  66. for (int i = 0; i < argcount; i++) {
  67. argptrs[i] = &p_arguments[i];
  68. }
  69. }
  70. CallError ce;
  71. Variant ret;
  72. callp(argptrs, argcount, ret, ce);
  73. return ret;
  74. }
  75. Error Callable::rpcp(int p_id, const Variant **p_arguments, int p_argcount, CallError &r_call_error) const {
  76. if (is_null()) {
  77. r_call_error.error = CallError::CALL_ERROR_INSTANCE_IS_NULL;
  78. r_call_error.argument = 0;
  79. r_call_error.expected = 0;
  80. return ERR_UNCONFIGURED;
  81. } else if (!is_custom()) {
  82. r_call_error.error = CallError::CALL_ERROR_INVALID_METHOD;
  83. r_call_error.argument = 0;
  84. r_call_error.expected = 0;
  85. return ERR_UNCONFIGURED;
  86. } else {
  87. return custom->rpc(p_id, p_arguments, p_argcount, r_call_error);
  88. }
  89. }
  90. Callable Callable::bindp(const Variant **p_arguments, int p_argcount) const {
  91. Vector<Variant> args;
  92. args.resize(p_argcount);
  93. for (int i = 0; i < p_argcount; i++) {
  94. args.write[i] = *p_arguments[i];
  95. }
  96. return Callable(memnew(CallableCustomBind(*this, args)));
  97. }
  98. Callable Callable::unbind(int p_argcount) const {
  99. return Callable(memnew(CallableCustomUnbind(*this, p_argcount)));
  100. }
  101. bool Callable::is_valid() const {
  102. return get_object() && (is_custom() || get_object()->has_method(get_method()));
  103. }
  104. Object *Callable::get_object() const {
  105. if (is_null()) {
  106. return nullptr;
  107. } else if (is_custom()) {
  108. return ObjectDB::get_instance(custom->get_object());
  109. } else {
  110. return ObjectDB::get_instance(ObjectID(object));
  111. }
  112. }
  113. ObjectID Callable::get_object_id() const {
  114. if (is_null()) {
  115. return ObjectID();
  116. } else if (is_custom()) {
  117. return custom->get_object();
  118. } else {
  119. return ObjectID(object);
  120. }
  121. }
  122. StringName Callable::get_method() const {
  123. if (is_custom()) {
  124. return get_custom()->get_method();
  125. }
  126. return method;
  127. }
  128. CallableCustom *Callable::get_custom() const {
  129. ERR_FAIL_COND_V_MSG(!is_custom(), nullptr,
  130. vformat("Can't get custom on non-CallableCustom \"%s\".", operator String()));
  131. return custom;
  132. }
  133. const Callable *Callable::get_base_comparator() const {
  134. const Callable *comparator = nullptr;
  135. if (is_custom()) {
  136. comparator = custom->get_base_comparator();
  137. }
  138. if (comparator) {
  139. return comparator;
  140. } else {
  141. return this;
  142. }
  143. }
  144. uint32_t Callable::hash() const {
  145. if (is_custom()) {
  146. return custom->hash();
  147. } else {
  148. uint32_t hash = method.hash();
  149. hash = hash_murmur3_one_64(object, hash);
  150. return hash_fmix32(hash);
  151. }
  152. }
  153. bool Callable::operator==(const Callable &p_callable) const {
  154. bool custom_a = is_custom();
  155. bool custom_b = p_callable.is_custom();
  156. if (custom_a == custom_b) {
  157. if (custom_a) {
  158. if (custom == p_callable.custom) {
  159. return true; //same pointer, don't even compare
  160. }
  161. CallableCustom::CompareEqualFunc eq_a = custom->get_compare_equal_func();
  162. CallableCustom::CompareEqualFunc eq_b = p_callable.custom->get_compare_equal_func();
  163. if (eq_a == eq_b) {
  164. return eq_a(custom, p_callable.custom);
  165. } else {
  166. return false;
  167. }
  168. } else {
  169. return object == p_callable.object && method == p_callable.method;
  170. }
  171. } else {
  172. return false;
  173. }
  174. }
  175. bool Callable::operator!=(const Callable &p_callable) const {
  176. return !(*this == p_callable);
  177. }
  178. bool Callable::operator<(const Callable &p_callable) const {
  179. bool custom_a = is_custom();
  180. bool custom_b = p_callable.is_custom();
  181. if (custom_a == custom_b) {
  182. if (custom_a) {
  183. if (custom == p_callable.custom) {
  184. return false; //same pointer, don't even compare
  185. }
  186. CallableCustom::CompareLessFunc less_a = custom->get_compare_less_func();
  187. CallableCustom::CompareLessFunc less_b = p_callable.custom->get_compare_less_func();
  188. if (less_a == less_b) {
  189. return less_a(custom, p_callable.custom);
  190. } else {
  191. return less_a < less_b; //it's something..
  192. }
  193. } else {
  194. if (object == p_callable.object) {
  195. return method < p_callable.method;
  196. } else {
  197. return object < p_callable.object;
  198. }
  199. }
  200. } else {
  201. return int(custom_a ? 1 : 0) < int(custom_b ? 1 : 0);
  202. }
  203. }
  204. void Callable::operator=(const Callable &p_callable) {
  205. if (is_custom()) {
  206. if (p_callable.is_custom()) {
  207. if (custom == p_callable.custom) {
  208. return;
  209. }
  210. }
  211. if (custom->ref_count.unref()) {
  212. memdelete(custom);
  213. }
  214. }
  215. if (p_callable.is_custom()) {
  216. method = StringName();
  217. if (!p_callable.custom->ref_count.ref()) {
  218. object = 0;
  219. } else {
  220. object = 0;
  221. custom = p_callable.custom;
  222. }
  223. } else {
  224. method = p_callable.method;
  225. object = p_callable.object;
  226. }
  227. }
  228. Callable::operator String() const {
  229. if (is_custom()) {
  230. return custom->get_as_text();
  231. } else {
  232. if (is_null()) {
  233. return "null::null";
  234. }
  235. Object *base = get_object();
  236. if (base) {
  237. String class_name = base->get_class();
  238. Ref<Script> script = base->get_script();
  239. if (script.is_valid() && script->get_path().is_resource_file()) {
  240. class_name += "(" + script->get_path().get_file() + ")";
  241. }
  242. return class_name + "::" + String(method);
  243. } else {
  244. return "null::" + String(method);
  245. }
  246. }
  247. }
  248. Callable::Callable(const Object *p_object, const StringName &p_method) {
  249. if (p_method == StringName()) {
  250. object = 0;
  251. ERR_FAIL_MSG("Method argument to Callable constructor must be a non-empty string");
  252. }
  253. if (p_object == nullptr) {
  254. object = 0;
  255. ERR_FAIL_MSG("Object argument to Callable constructor must be non-null");
  256. }
  257. object = p_object->get_instance_id();
  258. method = p_method;
  259. }
  260. Callable::Callable(ObjectID p_object, const StringName &p_method) {
  261. if (p_method == StringName()) {
  262. object = 0;
  263. ERR_FAIL_MSG("Method argument to Callable constructor must be a non-empty string");
  264. }
  265. object = p_object;
  266. method = p_method;
  267. }
  268. Callable::Callable(CallableCustom *p_custom) {
  269. if (p_custom->referenced) {
  270. object = 0;
  271. ERR_FAIL_MSG("Callable custom is already referenced");
  272. }
  273. p_custom->referenced = true;
  274. object = 0; //ensure object is all zero, since pointer may be 32 bits
  275. custom = p_custom;
  276. }
  277. Callable::Callable(const Callable &p_callable) {
  278. if (p_callable.is_custom()) {
  279. if (!p_callable.custom->ref_count.ref()) {
  280. object = 0;
  281. } else {
  282. object = 0;
  283. custom = p_callable.custom;
  284. }
  285. } else {
  286. method = p_callable.method;
  287. object = p_callable.object;
  288. }
  289. }
  290. Callable::~Callable() {
  291. if (is_custom()) {
  292. if (custom->ref_count.unref()) {
  293. memdelete(custom);
  294. }
  295. }
  296. }
  297. StringName CallableCustom::get_method() const {
  298. ERR_FAIL_V_MSG(StringName(), vformat("Can't get method on CallableCustom \"%s\".", get_as_text()));
  299. }
  300. Error CallableCustom::rpc(int p_peer_id, const Variant **p_arguments, int p_argcount, Callable::CallError &r_call_error) const {
  301. r_call_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
  302. r_call_error.argument = 0;
  303. r_call_error.expected = 0;
  304. return ERR_UNCONFIGURED;
  305. }
  306. const Callable *CallableCustom::get_base_comparator() const {
  307. return nullptr;
  308. }
  309. CallableCustom::CallableCustom() {
  310. ref_count.init();
  311. }
  312. //////////////////////////////////
  313. Object *Signal::get_object() const {
  314. return ObjectDB::get_instance(object);
  315. }
  316. ObjectID Signal::get_object_id() const {
  317. return object;
  318. }
  319. StringName Signal::get_name() const {
  320. return name;
  321. }
  322. bool Signal::operator==(const Signal &p_signal) const {
  323. return object == p_signal.object && name == p_signal.name;
  324. }
  325. bool Signal::operator!=(const Signal &p_signal) const {
  326. return object != p_signal.object || name != p_signal.name;
  327. }
  328. bool Signal::operator<(const Signal &p_signal) const {
  329. if (object == p_signal.object) {
  330. return name < p_signal.name;
  331. } else {
  332. return object < p_signal.object;
  333. }
  334. }
  335. Signal::operator String() const {
  336. Object *base = get_object();
  337. if (base) {
  338. String class_name = base->get_class();
  339. Ref<Script> script = base->get_script();
  340. if (script.is_valid() && script->get_path().is_resource_file()) {
  341. class_name += "(" + script->get_path().get_file() + ")";
  342. }
  343. return class_name + "::[signal]" + String(name);
  344. } else {
  345. return "null::[signal]" + String(name);
  346. }
  347. }
  348. Error Signal::emit(const Variant **p_arguments, int p_argcount) const {
  349. Object *obj = ObjectDB::get_instance(object);
  350. if (!obj) {
  351. return ERR_INVALID_DATA;
  352. }
  353. return obj->emit_signalp(name, p_arguments, p_argcount);
  354. }
  355. Error Signal::connect(const Callable &p_callable, uint32_t p_flags) {
  356. Object *object = get_object();
  357. ERR_FAIL_COND_V(!object, ERR_UNCONFIGURED);
  358. return object->connect(name, p_callable, p_flags);
  359. }
  360. void Signal::disconnect(const Callable &p_callable) {
  361. Object *object = get_object();
  362. ERR_FAIL_COND(!object);
  363. object->disconnect(name, p_callable);
  364. }
  365. bool Signal::is_connected(const Callable &p_callable) const {
  366. Object *object = get_object();
  367. ERR_FAIL_COND_V(!object, false);
  368. return object->is_connected(name, p_callable);
  369. }
  370. Array Signal::get_connections() const {
  371. Object *object = get_object();
  372. if (!object) {
  373. return Array();
  374. }
  375. List<Object::Connection> connections;
  376. object->get_signal_connection_list(name, &connections);
  377. Array arr;
  378. for (const Object::Connection &E : connections) {
  379. arr.push_back(E);
  380. }
  381. return arr;
  382. }
  383. Signal::Signal(const Object *p_object, const StringName &p_name) {
  384. ERR_FAIL_COND_MSG(p_object == nullptr, "Object argument to Signal constructor must be non-null");
  385. object = p_object->get_instance_id();
  386. name = p_name;
  387. }
  388. Signal::Signal(ObjectID p_object, const StringName &p_name) {
  389. object = p_object;
  390. name = p_name;
  391. }
  392. bool CallableComparator::operator()(const Variant &p_l, const Variant &p_r) const {
  393. const Variant *args[2] = { &p_l, &p_r };
  394. Callable::CallError err;
  395. Variant res;
  396. func.callp(args, 2, res, err);
  397. ERR_FAIL_COND_V_MSG(err.error != Callable::CallError::CALL_OK, false,
  398. "Error calling compare method: " + Variant::get_callable_error_text(func, args, 2, err));
  399. return res;
  400. }