0.2.0-dev Download ZIP
168 lines ·6.1 KB Raw
| 1 | /**************************************************************************/ |
| 2 | /* signal_watcher.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 | |
| 31 | #include "signal_watcher.h" |
| 32 | |
| 33 | #include "core/object/callable_mp.h" |
| 34 | #include "core/object/class_db.h" |
| 35 | |
| 36 | void SignalWatcher::_add_signal_entry(const Array &p_args, const String &p_name) { |
| 37 | if (!_signals.has(p_name)) { |
| 38 | _signals[p_name] = Array(); |
| 39 | } |
| 40 | _signals[p_name].push_back(p_args); |
| 41 | } |
| 42 | |
| 43 | void SignalWatcher::_signal_callback_zero(const String &p_name) { |
| 44 | Array args; |
| 45 | _add_signal_entry(args, p_name); |
| 46 | } |
| 47 | |
| 48 | void SignalWatcher::_signal_callback_one(Variant p_arg1, const String &p_name) { |
| 49 | Array args = { p_arg1 }; |
| 50 | _add_signal_entry(args, p_name); |
| 51 | } |
| 52 | |
| 53 | void SignalWatcher::_signal_callback_two(Variant p_arg1, Variant p_arg2, const String &p_name) { |
| 54 | Array args = { p_arg1, p_arg2 }; |
| 55 | _add_signal_entry(args, p_name); |
| 56 | } |
| 57 | |
| 58 | void SignalWatcher::_signal_callback_three(Variant p_arg1, Variant p_arg2, Variant p_arg3, const String &p_name) { |
| 59 | Array args = { p_arg1, p_arg2, p_arg3 }; |
| 60 | _add_signal_entry(args, p_name); |
| 61 | } |
| 62 | |
| 63 | void SignalWatcher::watch_signal(Object *p_object, const String &p_signal) { |
| 64 | MethodInfo method_info; |
| 65 | ClassDB::get_signal(p_object->get_class(), p_signal, &method_info); |
| 66 | switch (method_info.arguments.size()) { |
| 67 | case 0: { |
| 68 | p_object->connect(p_signal, callable_mp(this, &SignalWatcher::_signal_callback_zero).bind(p_signal)); |
| 69 | } break; |
| 70 | case 1: { |
| 71 | p_object->connect(p_signal, callable_mp(this, &SignalWatcher::_signal_callback_one).bind(p_signal)); |
| 72 | } break; |
| 73 | case 2: { |
| 74 | p_object->connect(p_signal, callable_mp(this, &SignalWatcher::_signal_callback_two).bind(p_signal)); |
| 75 | } break; |
| 76 | case 3: { |
| 77 | p_object->connect(p_signal, callable_mp(this, &SignalWatcher::_signal_callback_three).bind(p_signal)); |
| 78 | } break; |
| 79 | default: { |
| 80 | MESSAGE("Signal ", p_signal, " arg count not supported."); |
| 81 | } break; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | void SignalWatcher::unwatch_signal(Object *p_object, const String &p_signal) { |
| 86 | MethodInfo method_info; |
| 87 | ClassDB::get_signal(p_object->get_class(), p_signal, &method_info); |
| 88 | switch (method_info.arguments.size()) { |
| 89 | case 0: { |
| 90 | p_object->disconnect(p_signal, callable_mp(this, &SignalWatcher::_signal_callback_zero)); |
| 91 | } break; |
| 92 | case 1: { |
| 93 | p_object->disconnect(p_signal, callable_mp(this, &SignalWatcher::_signal_callback_one)); |
| 94 | } break; |
| 95 | case 2: { |
| 96 | p_object->disconnect(p_signal, callable_mp(this, &SignalWatcher::_signal_callback_two)); |
| 97 | } break; |
| 98 | case 3: { |
| 99 | p_object->disconnect(p_signal, callable_mp(this, &SignalWatcher::_signal_callback_three)); |
| 100 | } break; |
| 101 | default: { |
| 102 | MESSAGE("Signal ", p_signal, " arg count not supported."); |
| 103 | } break; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | bool SignalWatcher::check(const String &p_name, const Array &p_args) { |
| 108 | if (!_signals.has(p_name)) { |
| 109 | MESSAGE("Signal ", p_name, " not emitted"); |
| 110 | return false; |
| 111 | } |
| 112 | |
| 113 | if (p_args.size() != _signals[p_name].size()) { |
| 114 | MESSAGE("Signal has " << _signals[p_name] << " expected " << p_args); |
| 115 | discard_signal(p_name); |
| 116 | return false; |
| 117 | } |
| 118 | |
| 119 | bool match = true; |
| 120 | for (int i = 0; i < p_args.size(); i++) { |
| 121 | if (((Array)p_args[i]).size() != ((Array)_signals[p_name][i]).size()) { |
| 122 | MESSAGE("Signal has " << _signals[p_name][i] << " expected " << p_args[i]); |
| 123 | match = false; |
| 124 | continue; |
| 125 | } |
| 126 | |
| 127 | for (int j = 0; j < ((Array)p_args[i]).size(); j++) { |
| 128 | if (((Array)p_args[i])[j] != ((Array)_signals[p_name][i])[j]) { |
| 129 | MESSAGE("Signal has " << _signals[p_name][i] << " expected " << p_args[i]); |
| 130 | match = false; |
| 131 | break; |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | discard_signal(p_name); |
| 137 | return match; |
| 138 | } |
| 139 | |
| 140 | bool SignalWatcher::check_false(const String &p_name) { |
| 141 | bool has = _signals.has(p_name); |
| 142 | if (has) { |
| 143 | MESSAGE("Signal has " << _signals[p_name] << " expected none."); |
| 144 | } |
| 145 | discard_signal(p_name); |
| 146 | return !has; |
| 147 | } |
| 148 | |
| 149 | void SignalWatcher::discard_signal(const String &p_name) { |
| 150 | if (_signals.has(p_name)) { |
| 151 | _signals.erase(p_name); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | void SignalWatcher::_clear_signals() { |
| 156 | _signals.clear(); |
| 157 | } |
| 158 | |
| 159 | SignalWatcher::SignalWatcher() { |
| 160 | ERR_FAIL_COND_MSG(singleton, "Singleton in SignalWatcher already exists."); |
| 161 | singleton = this; |
| 162 | } |
| 163 | |
| 164 | SignalWatcher::~SignalWatcher() { |
| 165 | if (this == singleton) { |
| 166 | singleton = nullptr; |
| 167 | } |
| 168 | } |