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