callable.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. /**************************************************************************/
  2. /* callable.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.h"
  31. #include "core/object/object.h"
  32. #include "core/object/ref_counted.h"
  33. #include "core/object/script_language.h"
  34. #include "core/variant/callable_bind.h"
  35. #include "core/variant/variant_callable.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, true);
  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. if (!is_valid()) {
  47. r_call_error.error = CallError::CALL_ERROR_INSTANCE_IS_NULL;
  48. r_call_error.argument = 0;
  49. r_call_error.expected = 0;
  50. r_return_value = Variant();
  51. return;
  52. }
  53. custom->call(p_arguments, p_argcount, r_return_value, r_call_error);
  54. } else {
  55. Object *obj = ObjectDB::get_instance(ObjectID(object));
  56. #ifdef DEBUG_ENABLED
  57. if (!obj) {
  58. r_call_error.error = CallError::CALL_ERROR_INSTANCE_IS_NULL;
  59. r_call_error.argument = 0;
  60. r_call_error.expected = 0;
  61. r_return_value = Variant();
  62. return;
  63. }
  64. #endif
  65. r_return_value = obj->callp(method, p_arguments, p_argcount, r_call_error);
  66. }
  67. }
  68. Variant Callable::callv(const Array &p_arguments) const {
  69. int argcount = p_arguments.size();
  70. const Variant **argptrs = nullptr;
  71. if (argcount) {
  72. argptrs = (const Variant **)alloca(sizeof(Variant *) * argcount);
  73. for (int i = 0; i < argcount; i++) {
  74. argptrs[i] = &p_arguments[i];
  75. }
  76. }
  77. CallError ce;
  78. Variant ret;
  79. callp(argptrs, argcount, ret, ce);
  80. return ret;
  81. }
  82. Error Callable::rpcp(int p_id, const Variant **p_arguments, int p_argcount, CallError &r_call_error) const {
  83. if (is_null()) {
  84. r_call_error.error = CallError::CALL_ERROR_INSTANCE_IS_NULL;
  85. r_call_error.argument = 0;
  86. r_call_error.expected = 0;
  87. return ERR_UNCONFIGURED;
  88. } else if (!is_custom()) {
  89. Object *obj = ObjectDB::get_instance(ObjectID(object));
  90. #ifdef DEBUG_ENABLED
  91. if (!obj || !obj->is_class("Node")) {
  92. r_call_error.error = CallError::CALL_ERROR_INSTANCE_IS_NULL;
  93. r_call_error.argument = 0;
  94. r_call_error.expected = 0;
  95. return ERR_UNCONFIGURED;
  96. }
  97. #endif
  98. int argcount = p_argcount + 2;
  99. const Variant **argptrs = (const Variant **)alloca(sizeof(Variant *) * argcount);
  100. const Variant args[2] = { p_id, method };
  101. argptrs[0] = &args[0];
  102. argptrs[1] = &args[1];
  103. for (int i = 0; i < p_argcount; ++i) {
  104. argptrs[i + 2] = p_arguments[i];
  105. }
  106. CallError tmp;
  107. Error err = (Error)obj->callp(SNAME("rpc_id"), argptrs, argcount, tmp).operator int64_t();
  108. r_call_error.error = Callable::CallError::CALL_OK;
  109. return err;
  110. } else {
  111. return custom->rpc(p_id, p_arguments, p_argcount, r_call_error);
  112. }
  113. }
  114. Callable Callable::bindp(const Variant **p_arguments, int p_argcount) const {
  115. Vector<Variant> args;
  116. args.resize(p_argcount);
  117. for (int i = 0; i < p_argcount; i++) {
  118. args.write[i] = *p_arguments[i];
  119. }
  120. return Callable(memnew(CallableCustomBind(*this, args)));
  121. }
  122. Callable Callable::bindv(const Array &p_arguments) {
  123. if (p_arguments.is_empty()) {
  124. return *this; // No point in creating a new callable if nothing is bound.
  125. }
  126. Vector<Variant> args;
  127. args.resize(p_arguments.size());
  128. for (int i = 0; i < p_arguments.size(); i++) {
  129. args.write[i] = p_arguments[i];
  130. }
  131. return Callable(memnew(CallableCustomBind(*this, args)));
  132. }
  133. Callable Callable::unbind(int p_argcount) const {
  134. ERR_FAIL_COND_V_MSG(p_argcount <= 0, Callable(*this), "Amount of unbind() arguments must be 1 or greater.");
  135. return Callable(memnew(CallableCustomUnbind(*this, p_argcount)));
  136. }
  137. bool Callable::is_valid() const {
  138. if (is_custom()) {
  139. return get_custom()->is_valid();
  140. } else {
  141. return get_object() && get_object()->has_method(get_method());
  142. }
  143. }
  144. Object *Callable::get_object() const {
  145. if (is_null()) {
  146. return nullptr;
  147. } else if (is_custom()) {
  148. return ObjectDB::get_instance(custom->get_object());
  149. } else {
  150. return ObjectDB::get_instance(ObjectID(object));
  151. }
  152. }
  153. ObjectID Callable::get_object_id() const {
  154. if (is_null()) {
  155. return ObjectID();
  156. } else if (is_custom()) {
  157. return custom->get_object();
  158. } else {
  159. return ObjectID(object);
  160. }
  161. }
  162. StringName Callable::get_method() const {
  163. if (is_custom()) {
  164. return get_custom()->get_method();
  165. }
  166. return method;
  167. }
  168. int Callable::get_argument_count(bool *r_is_valid) const {
  169. if (is_custom()) {
  170. bool valid = false;
  171. return custom->get_argument_count(r_is_valid ? *r_is_valid : valid);
  172. } else if (!is_null()) {
  173. return get_object()->get_method_argument_count(method, r_is_valid);
  174. } else {
  175. if (r_is_valid) {
  176. *r_is_valid = false;
  177. }
  178. return 0;
  179. }
  180. }
  181. int Callable::get_bound_arguments_count() const {
  182. if (!is_null() && is_custom()) {
  183. return custom->get_bound_arguments_count();
  184. } else {
  185. return 0;
  186. }
  187. }
  188. void Callable::get_bound_arguments_ref(Vector<Variant> &r_arguments, int &r_argcount) const {
  189. if (!is_null() && is_custom()) {
  190. custom->get_bound_arguments(r_arguments, r_argcount);
  191. } else {
  192. r_arguments.clear();
  193. r_argcount = 0;
  194. }
  195. }
  196. Array Callable::get_bound_arguments() const {
  197. Vector<Variant> arr;
  198. int ac;
  199. get_bound_arguments_ref(arr, ac);
  200. Array ret;
  201. ret.resize(arr.size());
  202. for (int i = 0; i < arr.size(); i++) {
  203. ret[i] = arr[i];
  204. }
  205. return ret;
  206. }
  207. CallableCustom *Callable::get_custom() const {
  208. ERR_FAIL_COND_V_MSG(!is_custom(), nullptr,
  209. vformat("Can't get custom on non-CallableCustom \"%s\".", operator String()));
  210. return custom;
  211. }
  212. const Callable *Callable::get_base_comparator() const {
  213. const Callable *comparator = nullptr;
  214. if (is_custom()) {
  215. comparator = custom->get_base_comparator();
  216. }
  217. if (comparator) {
  218. return comparator;
  219. } else {
  220. return this;
  221. }
  222. }
  223. uint32_t Callable::hash() const {
  224. if (is_custom()) {
  225. return custom->hash();
  226. } else {
  227. uint32_t hash = method.hash();
  228. hash = hash_murmur3_one_64(object, hash);
  229. return hash_fmix32(hash);
  230. }
  231. }
  232. bool Callable::operator==(const Callable &p_callable) const {
  233. bool custom_a = is_custom();
  234. bool custom_b = p_callable.is_custom();
  235. if (custom_a == custom_b) {
  236. if (custom_a) {
  237. if (custom == p_callable.custom) {
  238. return true; //same pointer, don't even compare
  239. }
  240. CallableCustom::CompareEqualFunc eq_a = custom->get_compare_equal_func();
  241. CallableCustom::CompareEqualFunc eq_b = p_callable.custom->get_compare_equal_func();
  242. if (eq_a == eq_b) {
  243. return eq_a(custom, p_callable.custom);
  244. } else {
  245. return false;
  246. }
  247. } else {
  248. return object == p_callable.object && method == p_callable.method;
  249. }
  250. } else {
  251. return false;
  252. }
  253. }
  254. bool Callable::operator!=(const Callable &p_callable) const {
  255. return !(*this == p_callable);
  256. }
  257. bool Callable::operator<(const Callable &p_callable) const {
  258. bool custom_a = is_custom();
  259. bool custom_b = p_callable.is_custom();
  260. if (custom_a == custom_b) {
  261. if (custom_a) {
  262. if (custom == p_callable.custom) {
  263. return false; //same pointer, don't even compare
  264. }
  265. CallableCustom::CompareLessFunc less_a = custom->get_compare_less_func();
  266. CallableCustom::CompareLessFunc less_b = p_callable.custom->get_compare_less_func();
  267. if (less_a == less_b) {
  268. return less_a(custom, p_callable.custom);
  269. } else {
  270. return less_a < less_b; //it's something..
  271. }
  272. } else {
  273. if (object == p_callable.object) {
  274. return method < p_callable.method;
  275. } else {
  276. return object < p_callable.object;
  277. }
  278. }
  279. } else {
  280. return int(custom_a ? 1 : 0) < int(custom_b ? 1 : 0);
  281. }
  282. }
  283. void Callable::operator=(const Callable &p_callable) {
  284. if (is_custom()) {
  285. if (p_callable.is_custom()) {
  286. if (custom == p_callable.custom) {
  287. return;
  288. }
  289. }
  290. if (custom->ref_count.unref()) {
  291. memdelete(custom);
  292. }
  293. }
  294. if (p_callable.is_custom()) {
  295. method = StringName();
  296. if (!p_callable.custom->ref_count.ref()) {
  297. object = 0;
  298. } else {
  299. object = 0;
  300. custom = p_callable.custom;
  301. }
  302. } else {
  303. method = p_callable.method;
  304. object = p_callable.object;
  305. }
  306. }
  307. Callable::operator String() const {
  308. if (is_custom()) {
  309. return custom->get_as_text();
  310. } else {
  311. if (is_null()) {
  312. return "null::null";
  313. }
  314. Object *base = get_object();
  315. if (base) {
  316. String class_name = base->get_class();
  317. Ref<Script> script = base->get_script();
  318. if (script.is_valid() && script->get_path().is_resource_file()) {
  319. class_name += "(" + script->get_path().get_file() + ")";
  320. }
  321. return class_name + "::" + String(method);
  322. } else {
  323. return "null::" + String(method);
  324. }
  325. }
  326. }
  327. Callable Callable::create(const Variant &p_variant, const StringName &p_method) {
  328. ERR_FAIL_COND_V_MSG(p_method == StringName(), Callable(), "Method argument to Callable::create method must be a non-empty string.");
  329. switch (p_variant.get_type()) {
  330. case Variant::NIL:
  331. return Callable(ObjectID(), p_method);
  332. case Variant::OBJECT:
  333. return Callable(p_variant.operator ObjectID(), p_method);
  334. default:
  335. return Callable(memnew(VariantCallable(p_variant, p_method)));
  336. }
  337. }
  338. Callable::Callable(const Object *p_object, const StringName &p_method) {
  339. if (unlikely(p_method == StringName())) {
  340. object = 0;
  341. ERR_FAIL_MSG("Method argument to Callable constructor must be a non-empty string.");
  342. }
  343. if (unlikely(p_object == nullptr)) {
  344. object = 0;
  345. ERR_FAIL_MSG("Object argument to Callable constructor must be non-null.");
  346. }
  347. object = p_object->get_instance_id();
  348. method = p_method;
  349. }
  350. Callable::Callable(ObjectID p_object, const StringName &p_method) {
  351. if (unlikely(p_method == StringName())) {
  352. object = 0;
  353. ERR_FAIL_MSG("Method argument to Callable constructor must be a non-empty string.");
  354. }
  355. object = p_object;
  356. method = p_method;
  357. }
  358. Callable::Callable(CallableCustom *p_custom) {
  359. if (unlikely(p_custom->referenced)) {
  360. object = 0;
  361. ERR_FAIL_MSG("Callable custom is already referenced.");
  362. }
  363. p_custom->referenced = true;
  364. object = 0; //ensure object is all zero, since pointer may be 32 bits
  365. custom = p_custom;
  366. }
  367. Callable::Callable(const Callable &p_callable) {
  368. if (p_callable.is_custom()) {
  369. if (!p_callable.custom->ref_count.ref()) {
  370. object = 0;
  371. } else {
  372. object = 0;
  373. custom = p_callable.custom;
  374. }
  375. } else {
  376. method = p_callable.method;
  377. object = p_callable.object;
  378. }
  379. }
  380. Callable::~Callable() {
  381. if (is_custom()) {
  382. if (custom->ref_count.unref()) {
  383. memdelete(custom);
  384. }
  385. }
  386. }
  387. bool CallableCustom::is_valid() const {
  388. // Sensible default implementation so most custom callables don't need their own.
  389. return ObjectDB::get_instance(get_object());
  390. }
  391. StringName CallableCustom::get_method() const {
  392. ERR_FAIL_V_MSG(StringName(), vformat("Can't get method on CallableCustom \"%s\".", get_as_text()));
  393. }
  394. Error CallableCustom::rpc(int p_peer_id, const Variant **p_arguments, int p_argcount, Callable::CallError &r_call_error) const {
  395. r_call_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
  396. r_call_error.argument = 0;
  397. r_call_error.expected = 0;
  398. return ERR_UNCONFIGURED;
  399. }
  400. const Callable *CallableCustom::get_base_comparator() const {
  401. return nullptr;
  402. }
  403. int CallableCustom::get_argument_count(bool &r_is_valid) const {
  404. r_is_valid = false;
  405. return 0;
  406. }
  407. int CallableCustom::get_bound_arguments_count() const {
  408. return 0;
  409. }
  410. void CallableCustom::get_bound_arguments(Vector<Variant> &r_arguments, int &r_argcount) const {
  411. r_arguments = Vector<Variant>();
  412. r_argcount = 0;
  413. }
  414. CallableCustom::CallableCustom() {
  415. ref_count.init();
  416. }
  417. //////////////////////////////////
  418. Object *Signal::get_object() const {
  419. return ObjectDB::get_instance(object);
  420. }
  421. ObjectID Signal::get_object_id() const {
  422. return object;
  423. }
  424. StringName Signal::get_name() const {
  425. return name;
  426. }
  427. bool Signal::operator==(const Signal &p_signal) const {
  428. return object == p_signal.object && name == p_signal.name;
  429. }
  430. bool Signal::operator!=(const Signal &p_signal) const {
  431. return object != p_signal.object || name != p_signal.name;
  432. }
  433. bool Signal::operator<(const Signal &p_signal) const {
  434. if (object == p_signal.object) {
  435. return name < p_signal.name;
  436. } else {
  437. return object < p_signal.object;
  438. }
  439. }
  440. Signal::operator String() const {
  441. Object *base = get_object();
  442. if (base) {
  443. String class_name = base->get_class();
  444. Ref<Script> script = base->get_script();
  445. if (script.is_valid() && script->get_path().is_resource_file()) {
  446. class_name += "(" + script->get_path().get_file() + ")";
  447. }
  448. return class_name + "::[signal]" + String(name);
  449. } else {
  450. return "null::[signal]" + String(name);
  451. }
  452. }
  453. Error Signal::emit(const Variant **p_arguments, int p_argcount) const {
  454. Object *obj = ObjectDB::get_instance(object);
  455. if (!obj) {
  456. return ERR_INVALID_DATA;
  457. }
  458. return obj->emit_signalp(name, p_arguments, p_argcount);
  459. }
  460. Error Signal::connect(const Callable &p_callable, uint32_t p_flags) {
  461. Object *obj = get_object();
  462. ERR_FAIL_NULL_V(obj, ERR_UNCONFIGURED);
  463. return obj->connect(name, p_callable, p_flags);
  464. }
  465. void Signal::disconnect(const Callable &p_callable) {
  466. Object *obj = get_object();
  467. ERR_FAIL_NULL(obj);
  468. obj->disconnect(name, p_callable);
  469. }
  470. bool Signal::is_connected(const Callable &p_callable) const {
  471. Object *obj = get_object();
  472. ERR_FAIL_NULL_V(obj, false);
  473. return obj->is_connected(name, p_callable);
  474. }
  475. Array Signal::get_connections() const {
  476. Object *obj = get_object();
  477. if (!obj) {
  478. return Array();
  479. }
  480. List<Object::Connection> connections;
  481. obj->get_signal_connection_list(name, &connections);
  482. Array arr;
  483. for (const Object::Connection &E : connections) {
  484. arr.push_back(E);
  485. }
  486. return arr;
  487. }
  488. Signal::Signal(const Object *p_object, const StringName &p_name) {
  489. ERR_FAIL_NULL_MSG(p_object, "Object argument to Signal constructor must be non-null.");
  490. object = p_object->get_instance_id();
  491. name = p_name;
  492. }
  493. Signal::Signal(ObjectID p_object, const StringName &p_name) {
  494. object = p_object;
  495. name = p_name;
  496. }
  497. bool CallableComparator::operator()(const Variant &p_l, const Variant &p_r) const {
  498. const Variant *args[2] = { &p_l, &p_r };
  499. Callable::CallError err;
  500. Variant res;
  501. func.callp(args, 2, res, err);
  502. ERR_FAIL_COND_V_MSG(err.error != Callable::CallError::CALL_OK, false,
  503. "Error calling compare method: " + Variant::get_callable_error_text(func, args, 2, err));
  504. return res;
  505. }