callable.cpp 12 KB

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