callable.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /*************************************************************************/
  2. /* callable.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 "core/script_language.h"
  32. #include "message_queue.h"
  33. #include "object.h"
  34. #include "reference.h"
  35. void Callable::call_deferred(const Variant **p_arguments, int p_argcount) const {
  36. MessageQueue::get_singleton()->push_callable(*this, p_arguments, p_argcount);
  37. }
  38. void Callable::call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, CallError &r_call_error) const {
  39. if (is_null()) {
  40. r_call_error.error = CallError::CALL_ERROR_INSTANCE_IS_NULL;
  41. r_call_error.argument = 0;
  42. r_call_error.expected = 0;
  43. r_return_value = Variant();
  44. } else if (is_custom()) {
  45. custom->call(p_arguments, p_argcount, r_return_value, r_call_error);
  46. } else {
  47. Object *obj = ObjectDB::get_instance(ObjectID(object));
  48. r_return_value = obj->call(method, p_arguments, p_argcount, r_call_error);
  49. }
  50. }
  51. Object *Callable::get_object() const {
  52. if (is_null()) {
  53. return nullptr;
  54. } else if (is_custom()) {
  55. return ObjectDB::get_instance(custom->get_object());
  56. } else {
  57. return ObjectDB::get_instance(ObjectID(object));
  58. }
  59. }
  60. ObjectID Callable::get_object_id() const {
  61. if (is_null()) {
  62. return ObjectID();
  63. } else if (is_custom()) {
  64. return custom->get_object();
  65. } else {
  66. return ObjectID(object);
  67. }
  68. }
  69. StringName Callable::get_method() const {
  70. ERR_FAIL_COND_V_MSG(is_custom(), StringName(),
  71. vformat("Can't get method on CallableCustom \"%s\".", operator String()));
  72. return method;
  73. }
  74. CallableCustom *Callable::get_custom() const {
  75. ERR_FAIL_COND_V_MSG(!is_custom(), nullptr,
  76. vformat("Can't get custom on non-CallableCustom \"%s\".", operator String()));
  77. return custom;
  78. }
  79. uint32_t Callable::hash() const {
  80. if (is_custom()) {
  81. return custom->hash();
  82. } else {
  83. uint32_t hash = method.hash();
  84. return hash_djb2_one_64(object, hash);
  85. }
  86. }
  87. bool Callable::operator==(const Callable &p_callable) const {
  88. bool custom_a = is_custom();
  89. bool custom_b = p_callable.is_custom();
  90. if (custom_a == custom_b) {
  91. if (custom_a) {
  92. if (custom == p_callable.custom) {
  93. return true; //same pointer, dont even compare
  94. }
  95. CallableCustom::CompareEqualFunc eq_a = custom->get_compare_equal_func();
  96. CallableCustom::CompareEqualFunc eq_b = p_callable.custom->get_compare_equal_func();
  97. if (eq_a == eq_b) {
  98. return eq_a(custom, p_callable.custom);
  99. } else {
  100. return false;
  101. }
  102. } else {
  103. return object == p_callable.object && method == p_callable.method;
  104. }
  105. } else {
  106. return false;
  107. }
  108. }
  109. bool Callable::operator!=(const Callable &p_callable) const {
  110. return !(*this == p_callable);
  111. }
  112. bool Callable::operator<(const Callable &p_callable) const {
  113. bool custom_a = is_custom();
  114. bool custom_b = p_callable.is_custom();
  115. if (custom_a == custom_b) {
  116. if (custom_a) {
  117. if (custom == p_callable.custom) {
  118. return false; //same pointer, dont even compare
  119. }
  120. CallableCustom::CompareLessFunc less_a = custom->get_compare_less_func();
  121. CallableCustom::CompareLessFunc less_b = p_callable.custom->get_compare_less_func();
  122. if (less_a == less_b) {
  123. return less_a(custom, p_callable.custom);
  124. } else {
  125. return less_a < less_b; //it's something..
  126. }
  127. } else {
  128. if (object == p_callable.object) {
  129. return method < p_callable.method;
  130. } else {
  131. return object < p_callable.object;
  132. }
  133. }
  134. } else {
  135. return int(custom_a ? 1 : 0) < int(custom_b ? 1 : 0);
  136. }
  137. }
  138. void Callable::operator=(const Callable &p_callable) {
  139. if (is_custom()) {
  140. if (p_callable.is_custom()) {
  141. if (custom == p_callable.custom) {
  142. return;
  143. }
  144. }
  145. if (custom->ref_count.unref()) {
  146. memdelete(custom);
  147. }
  148. }
  149. if (p_callable.is_custom()) {
  150. method = StringName();
  151. if (!p_callable.custom->ref_count.ref()) {
  152. object = 0;
  153. } else {
  154. object = 0;
  155. custom = p_callable.custom;
  156. }
  157. } else {
  158. method = p_callable.method;
  159. object = p_callable.object;
  160. }
  161. }
  162. Callable::operator String() const {
  163. if (is_custom()) {
  164. return custom->get_as_text();
  165. } else {
  166. if (is_null()) {
  167. return "null::null";
  168. }
  169. Object *base = get_object();
  170. if (base) {
  171. String class_name = base->get_class();
  172. Ref<Script> script = base->get_script();
  173. if (script.is_valid() && script->get_path().is_resource_file()) {
  174. class_name += "(" + script->get_path().get_file() + ")";
  175. }
  176. return class_name + "::" + String(method);
  177. } else {
  178. return "null::" + String(method);
  179. }
  180. }
  181. }
  182. Callable::Callable(const Object *p_object, const StringName &p_method) {
  183. if (p_method == StringName()) {
  184. object = 0;
  185. ERR_FAIL_MSG("Method argument to Callable constructor must be a non-empty string");
  186. }
  187. if (p_object == nullptr) {
  188. object = 0;
  189. ERR_FAIL_MSG("Object argument to Callable constructor must be non-null");
  190. }
  191. object = p_object->get_instance_id();
  192. method = p_method;
  193. }
  194. Callable::Callable(ObjectID p_object, const StringName &p_method) {
  195. if (p_method == StringName()) {
  196. object = 0;
  197. ERR_FAIL_MSG("Method argument to Callable constructor must be a non-empty string");
  198. }
  199. object = p_object;
  200. method = p_method;
  201. }
  202. Callable::Callable(CallableCustom *p_custom) {
  203. if (p_custom->referenced) {
  204. object = 0;
  205. ERR_FAIL_MSG("Callable custom is already referenced");
  206. }
  207. p_custom->referenced = true;
  208. object = 0; //ensure object is all zero, since pointer may be 32 bits
  209. custom = p_custom;
  210. }
  211. Callable::Callable(const Callable &p_callable) {
  212. if (p_callable.is_custom()) {
  213. if (!p_callable.custom->ref_count.ref()) {
  214. object = 0;
  215. } else {
  216. object = 0;
  217. custom = p_callable.custom;
  218. }
  219. } else {
  220. method = p_callable.method;
  221. object = p_callable.object;
  222. }
  223. }
  224. Callable::~Callable() {
  225. if (is_custom()) {
  226. if (custom->ref_count.unref()) {
  227. memdelete(custom);
  228. }
  229. }
  230. }
  231. CallableCustom::CallableCustom() {
  232. ref_count.init();
  233. }
  234. //////////////////////////////////
  235. Object *Signal::get_object() const {
  236. return ObjectDB::get_instance(object);
  237. }
  238. ObjectID Signal::get_object_id() const {
  239. return object;
  240. }
  241. StringName Signal::get_name() const {
  242. return name;
  243. }
  244. bool Signal::operator==(const Signal &p_signal) const {
  245. return object == p_signal.object && name == p_signal.name;
  246. }
  247. bool Signal::operator!=(const Signal &p_signal) const {
  248. return object != p_signal.object || name != p_signal.name;
  249. }
  250. bool Signal::operator<(const Signal &p_signal) const {
  251. if (object == p_signal.object) {
  252. return name < p_signal.name;
  253. } else {
  254. return object < p_signal.object;
  255. }
  256. }
  257. Signal::operator String() const {
  258. Object *base = get_object();
  259. if (base) {
  260. String class_name = base->get_class();
  261. Ref<Script> script = base->get_script();
  262. if (script.is_valid() && script->get_path().is_resource_file()) {
  263. class_name += "(" + script->get_path().get_file() + ")";
  264. }
  265. return class_name + "::[signal]" + String(name);
  266. } else {
  267. return "null::[signal]" + String(name);
  268. }
  269. }
  270. Error Signal::emit(const Variant **p_arguments, int p_argcount) const {
  271. Object *obj = ObjectDB::get_instance(object);
  272. if (!obj) {
  273. return ERR_INVALID_DATA;
  274. }
  275. return obj->emit_signal(name, p_arguments, p_argcount);
  276. }
  277. Error Signal::connect(const Callable &p_callable, const Vector<Variant> &p_binds, uint32_t p_flags) {
  278. Object *object = get_object();
  279. ERR_FAIL_COND_V(!object, ERR_UNCONFIGURED);
  280. return object->connect(name, p_callable, p_binds, p_flags);
  281. }
  282. void Signal::disconnect(const Callable &p_callable) {
  283. Object *object = get_object();
  284. ERR_FAIL_COND(!object);
  285. object->disconnect(name, p_callable);
  286. }
  287. bool Signal::is_connected(const Callable &p_callable) const {
  288. Object *object = get_object();
  289. ERR_FAIL_COND_V(!object, false);
  290. return object->is_connected(name, p_callable);
  291. }
  292. Array Signal::get_connections() const {
  293. Object *object = get_object();
  294. if (!object) {
  295. return Array();
  296. }
  297. List<Object::Connection> connections;
  298. object->get_signal_connection_list(name, &connections);
  299. Array arr;
  300. for (List<Object::Connection>::Element *E = connections.front(); E; E = E->next()) {
  301. arr.push_back(E->get());
  302. }
  303. return arr;
  304. }
  305. Signal::Signal(const Object *p_object, const StringName &p_name) {
  306. ERR_FAIL_COND_MSG(p_object == nullptr, "Object argument to Signal constructor must be non-null");
  307. object = p_object->get_instance_id();
  308. name = p_name;
  309. }
  310. Signal::Signal(ObjectID p_object, const StringName &p_name) {
  311. object = p_object;
  312. name = p_name;
  313. }