callable.cpp 15 KB

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