callable.cpp 14 KB

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