callable.cpp 12 KB

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