callable.cpp 14 KB

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