callable.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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/reference.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. r_return_value = obj->call(method, p_arguments, p_argcount, r_call_error);
  50. }
  51. }
  52. Callable Callable::bind(const Variant **p_arguments, int p_argcount) const {
  53. Vector<Variant> args;
  54. args.resize(p_argcount);
  55. for (int i = 0; i < p_argcount; i++) {
  56. args.write[i] = *p_arguments[i];
  57. }
  58. return Callable(memnew(CallableCustomBind(*this, args)));
  59. }
  60. Callable Callable::unbind(int p_argcount) const {
  61. return Callable(memnew(CallableCustomUnbind(*this, p_argcount)));
  62. }
  63. Object *Callable::get_object() const {
  64. if (is_null()) {
  65. return nullptr;
  66. } else if (is_custom()) {
  67. return ObjectDB::get_instance(custom->get_object());
  68. } else {
  69. return ObjectDB::get_instance(ObjectID(object));
  70. }
  71. }
  72. ObjectID Callable::get_object_id() const {
  73. if (is_null()) {
  74. return ObjectID();
  75. } else if (is_custom()) {
  76. return custom->get_object();
  77. } else {
  78. return ObjectID(object);
  79. }
  80. }
  81. StringName Callable::get_method() const {
  82. ERR_FAIL_COND_V_MSG(is_custom(), StringName(),
  83. vformat("Can't get method on CallableCustom \"%s\".", operator String()));
  84. return method;
  85. }
  86. CallableCustom *Callable::get_custom() const {
  87. ERR_FAIL_COND_V_MSG(!is_custom(), nullptr,
  88. vformat("Can't get custom on non-CallableCustom \"%s\".", operator String()));
  89. return custom;
  90. }
  91. const Callable *Callable::get_base_comparator() const {
  92. const Callable *comparator = nullptr;
  93. if (is_custom()) {
  94. comparator = custom->get_base_comparator();
  95. }
  96. if (comparator) {
  97. return comparator;
  98. } else {
  99. return this;
  100. }
  101. }
  102. uint32_t Callable::hash() const {
  103. if (is_custom()) {
  104. return custom->hash();
  105. } else {
  106. uint32_t hash = method.hash();
  107. return hash_djb2_one_64(object, hash);
  108. }
  109. }
  110. bool Callable::operator==(const Callable &p_callable) const {
  111. bool custom_a = is_custom();
  112. bool custom_b = p_callable.is_custom();
  113. if (custom_a == custom_b) {
  114. if (custom_a) {
  115. if (custom == p_callable.custom) {
  116. return true; //same pointer, dont even compare
  117. }
  118. CallableCustom::CompareEqualFunc eq_a = custom->get_compare_equal_func();
  119. CallableCustom::CompareEqualFunc eq_b = p_callable.custom->get_compare_equal_func();
  120. if (eq_a == eq_b) {
  121. return eq_a(custom, p_callable.custom);
  122. } else {
  123. return false;
  124. }
  125. } else {
  126. return object == p_callable.object && method == p_callable.method;
  127. }
  128. } else {
  129. return false;
  130. }
  131. }
  132. bool Callable::operator!=(const Callable &p_callable) const {
  133. return !(*this == p_callable);
  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 false; //same pointer, dont even compare
  142. }
  143. CallableCustom::CompareLessFunc less_a = custom->get_compare_less_func();
  144. CallableCustom::CompareLessFunc less_b = p_callable.custom->get_compare_less_func();
  145. if (less_a == less_b) {
  146. return less_a(custom, p_callable.custom);
  147. } else {
  148. return less_a < less_b; //it's something..
  149. }
  150. } else {
  151. if (object == p_callable.object) {
  152. return method < p_callable.method;
  153. } else {
  154. return object < p_callable.object;
  155. }
  156. }
  157. } else {
  158. return int(custom_a ? 1 : 0) < int(custom_b ? 1 : 0);
  159. }
  160. }
  161. void Callable::operator=(const Callable &p_callable) {
  162. if (is_custom()) {
  163. if (p_callable.is_custom()) {
  164. if (custom == p_callable.custom) {
  165. return;
  166. }
  167. }
  168. if (custom->ref_count.unref()) {
  169. memdelete(custom);
  170. }
  171. }
  172. if (p_callable.is_custom()) {
  173. method = StringName();
  174. if (!p_callable.custom->ref_count.ref()) {
  175. object = 0;
  176. } else {
  177. object = 0;
  178. custom = p_callable.custom;
  179. }
  180. } else {
  181. method = p_callable.method;
  182. object = p_callable.object;
  183. }
  184. }
  185. Callable::operator String() const {
  186. if (is_custom()) {
  187. return custom->get_as_text();
  188. } else {
  189. if (is_null()) {
  190. return "null::null";
  191. }
  192. Object *base = get_object();
  193. if (base) {
  194. String class_name = base->get_class();
  195. Ref<Script> script = base->get_script();
  196. if (script.is_valid() && script->get_path().is_resource_file()) {
  197. class_name += "(" + script->get_path().get_file() + ")";
  198. }
  199. return class_name + "::" + String(method);
  200. } else {
  201. return "null::" + String(method);
  202. }
  203. }
  204. }
  205. Callable::Callable(const Object *p_object, const StringName &p_method) {
  206. if (p_method == StringName()) {
  207. object = 0;
  208. ERR_FAIL_MSG("Method argument to Callable constructor must be a non-empty string");
  209. }
  210. if (p_object == nullptr) {
  211. object = 0;
  212. ERR_FAIL_MSG("Object argument to Callable constructor must be non-null");
  213. }
  214. object = p_object->get_instance_id();
  215. method = p_method;
  216. }
  217. Callable::Callable(ObjectID p_object, const StringName &p_method) {
  218. if (p_method == StringName()) {
  219. object = 0;
  220. ERR_FAIL_MSG("Method argument to Callable constructor must be a non-empty string");
  221. }
  222. object = p_object;
  223. method = p_method;
  224. }
  225. Callable::Callable(CallableCustom *p_custom) {
  226. if (p_custom->referenced) {
  227. object = 0;
  228. ERR_FAIL_MSG("Callable custom is already referenced");
  229. }
  230. p_custom->referenced = true;
  231. object = 0; //ensure object is all zero, since pointer may be 32 bits
  232. custom = p_custom;
  233. }
  234. Callable::Callable(const Callable &p_callable) {
  235. if (p_callable.is_custom()) {
  236. if (!p_callable.custom->ref_count.ref()) {
  237. object = 0;
  238. } else {
  239. object = 0;
  240. custom = p_callable.custom;
  241. }
  242. } else {
  243. method = p_callable.method;
  244. object = p_callable.object;
  245. }
  246. }
  247. Callable::~Callable() {
  248. if (is_custom()) {
  249. if (custom->ref_count.unref()) {
  250. memdelete(custom);
  251. }
  252. }
  253. }
  254. const Callable *CallableCustom::get_base_comparator() const {
  255. return nullptr;
  256. }
  257. CallableCustom::CallableCustom() {
  258. ref_count.init();
  259. }
  260. //////////////////////////////////
  261. Object *Signal::get_object() const {
  262. return ObjectDB::get_instance(object);
  263. }
  264. ObjectID Signal::get_object_id() const {
  265. return object;
  266. }
  267. StringName Signal::get_name() const {
  268. return name;
  269. }
  270. bool Signal::operator==(const Signal &p_signal) const {
  271. return object == p_signal.object && name == p_signal.name;
  272. }
  273. bool Signal::operator!=(const Signal &p_signal) const {
  274. return object != p_signal.object || name != p_signal.name;
  275. }
  276. bool Signal::operator<(const Signal &p_signal) const {
  277. if (object == p_signal.object) {
  278. return name < p_signal.name;
  279. } else {
  280. return object < p_signal.object;
  281. }
  282. }
  283. Signal::operator String() const {
  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 + "::[signal]" + String(name);
  292. } else {
  293. return "null::[signal]" + String(name);
  294. }
  295. }
  296. Error Signal::emit(const Variant **p_arguments, int p_argcount) const {
  297. Object *obj = ObjectDB::get_instance(object);
  298. if (!obj) {
  299. return ERR_INVALID_DATA;
  300. }
  301. return obj->emit_signal(name, p_arguments, p_argcount);
  302. }
  303. Error Signal::connect(const Callable &p_callable, const Vector<Variant> &p_binds, uint32_t p_flags) {
  304. Object *object = get_object();
  305. ERR_FAIL_COND_V(!object, ERR_UNCONFIGURED);
  306. return object->connect(name, p_callable, p_binds, p_flags);
  307. }
  308. void Signal::disconnect(const Callable &p_callable) {
  309. Object *object = get_object();
  310. ERR_FAIL_COND(!object);
  311. object->disconnect(name, p_callable);
  312. }
  313. bool Signal::is_connected(const Callable &p_callable) const {
  314. Object *object = get_object();
  315. ERR_FAIL_COND_V(!object, false);
  316. return object->is_connected(name, p_callable);
  317. }
  318. Array Signal::get_connections() const {
  319. Object *object = get_object();
  320. if (!object) {
  321. return Array();
  322. }
  323. List<Object::Connection> connections;
  324. object->get_signal_connection_list(name, &connections);
  325. Array arr;
  326. for (List<Object::Connection>::Element *E = connections.front(); E; E = E->next()) {
  327. arr.push_back(E->get());
  328. }
  329. return arr;
  330. }
  331. Signal::Signal(const Object *p_object, const StringName &p_name) {
  332. ERR_FAIL_COND_MSG(p_object == nullptr, "Object argument to Signal constructor must be non-null");
  333. object = p_object->get_instance_id();
  334. name = p_name;
  335. }
  336. Signal::Signal(ObjectID p_object, const StringName &p_name) {
  337. object = p_object;
  338. name = p_name;
  339. }