Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/core/variant/callable_bind.cpp
20917 views
1
/**************************************************************************/
2
/* callable_bind.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 "callable_bind.h"
32
33
//////////////////////////////////
34
35
uint32_t CallableCustomBind::hash() const {
36
return callable.hash();
37
}
38
String CallableCustomBind::get_as_text() const {
39
return callable.operator String();
40
}
41
42
bool CallableCustomBind::_equal_func(const CallableCustom *p_a, const CallableCustom *p_b) {
43
const CallableCustomBind *a = static_cast<const CallableCustomBind *>(p_a);
44
const CallableCustomBind *b = static_cast<const CallableCustomBind *>(p_b);
45
46
if (a->callable != b->callable) {
47
return false;
48
}
49
50
if (a->binds.size() != b->binds.size()) {
51
return false;
52
}
53
54
return true;
55
}
56
57
bool CallableCustomBind::_less_func(const CallableCustom *p_a, const CallableCustom *p_b) {
58
const CallableCustomBind *a = static_cast<const CallableCustomBind *>(p_a);
59
const CallableCustomBind *b = static_cast<const CallableCustomBind *>(p_b);
60
61
if (a->callable < b->callable) {
62
return true;
63
} else if (b->callable < a->callable) {
64
return false;
65
}
66
67
return a->binds.size() < b->binds.size();
68
}
69
70
CallableCustom::CompareEqualFunc CallableCustomBind::get_compare_equal_func() const {
71
return _equal_func;
72
}
73
74
CallableCustom::CompareLessFunc CallableCustomBind::get_compare_less_func() const {
75
return _less_func;
76
}
77
78
bool CallableCustomBind::is_valid() const {
79
return callable.is_valid();
80
}
81
82
StringName CallableCustomBind::get_method() const {
83
return callable.get_method();
84
}
85
86
ObjectID CallableCustomBind::get_object() const {
87
return callable.get_object_id();
88
}
89
90
const Callable *CallableCustomBind::get_base_comparator() const {
91
return callable.get_base_comparator();
92
}
93
94
int CallableCustomBind::get_argument_count(bool &r_is_valid) const {
95
int ret = callable.get_argument_count(&r_is_valid);
96
if (r_is_valid) {
97
return ret - binds.size();
98
}
99
return 0;
100
}
101
102
int CallableCustomBind::get_bound_arguments_count() const {
103
return callable.get_bound_arguments_count() + MAX(0, binds.size() - callable.get_unbound_arguments_count());
104
}
105
106
void CallableCustomBind::get_bound_arguments(Vector<Variant> &r_arguments) const {
107
Vector<Variant> sub_bound_args;
108
callable.get_bound_arguments_ref(sub_bound_args);
109
int sub_bound_count = sub_bound_args.size();
110
111
int sub_unbound_count = callable.get_unbound_arguments_count();
112
113
if (sub_bound_count == 0 && sub_unbound_count == 0) {
114
r_arguments = binds;
115
return;
116
}
117
118
int added_count = MAX(0, binds.size() - sub_unbound_count);
119
int new_count = sub_bound_count + added_count;
120
121
if (added_count <= 0) {
122
// All added arguments are consumed by `sub_unbound_count`.
123
r_arguments = sub_bound_args;
124
return;
125
}
126
127
r_arguments.resize(new_count);
128
Variant *args = r_arguments.ptrw();
129
for (int i = 0; i < added_count; i++) {
130
args[i] = binds[i];
131
}
132
for (int i = 0; i < sub_bound_count; i++) {
133
args[i + added_count] = sub_bound_args[i];
134
}
135
}
136
137
int CallableCustomBind::get_unbound_arguments_count() const {
138
return MAX(0, callable.get_unbound_arguments_count() - binds.size());
139
}
140
141
void CallableCustomBind::call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const {
142
const Variant **args = (const Variant **)alloca(sizeof(Variant *) * (binds.size() + p_argcount));
143
for (int i = 0; i < p_argcount; i++) {
144
args[i] = (const Variant *)p_arguments[i];
145
}
146
for (int i = 0; i < binds.size(); i++) {
147
args[i + p_argcount] = (const Variant *)&binds[i];
148
}
149
150
callable.callp(args, p_argcount + binds.size(), r_return_value, r_call_error);
151
}
152
153
Error CallableCustomBind::rpc(int p_peer_id, const Variant **p_arguments, int p_argcount, Callable::CallError &r_call_error) const {
154
const Variant **args = (const Variant **)alloca(sizeof(Variant *) * (binds.size() + p_argcount));
155
for (int i = 0; i < p_argcount; i++) {
156
args[i] = (const Variant *)p_arguments[i];
157
}
158
for (int i = 0; i < binds.size(); i++) {
159
args[i + p_argcount] = (const Variant *)&binds[i];
160
}
161
162
return callable.rpcp(p_peer_id, args, p_argcount + binds.size(), r_call_error);
163
}
164
165
CallableCustomBind::CallableCustomBind(const Callable &p_callable, const Vector<Variant> &p_binds) {
166
callable = p_callable;
167
binds = p_binds;
168
}
169
170
//////////////////////////////////
171
172
uint32_t CallableCustomUnbind::hash() const {
173
return callable.hash();
174
}
175
String CallableCustomUnbind::get_as_text() const {
176
return callable.operator String();
177
}
178
179
bool CallableCustomUnbind::_equal_func(const CallableCustom *p_a, const CallableCustom *p_b) {
180
const CallableCustomUnbind *a = static_cast<const CallableCustomUnbind *>(p_a);
181
const CallableCustomUnbind *b = static_cast<const CallableCustomUnbind *>(p_b);
182
183
if (a->callable != b->callable) {
184
return false;
185
}
186
187
if (a->argcount != b->argcount) {
188
return false;
189
}
190
191
return true;
192
}
193
194
bool CallableCustomUnbind::_less_func(const CallableCustom *p_a, const CallableCustom *p_b) {
195
const CallableCustomUnbind *a = static_cast<const CallableCustomUnbind *>(p_a);
196
const CallableCustomUnbind *b = static_cast<const CallableCustomUnbind *>(p_b);
197
198
if (a->callable < b->callable) {
199
return true;
200
} else if (b->callable < a->callable) {
201
return false;
202
}
203
204
return a->argcount < b->argcount;
205
}
206
207
CallableCustom::CompareEqualFunc CallableCustomUnbind::get_compare_equal_func() const {
208
return _equal_func;
209
}
210
211
CallableCustom::CompareLessFunc CallableCustomUnbind::get_compare_less_func() const {
212
return _less_func;
213
}
214
215
bool CallableCustomUnbind::is_valid() const {
216
return callable.is_valid();
217
}
218
219
StringName CallableCustomUnbind::get_method() const {
220
return callable.get_method();
221
}
222
223
ObjectID CallableCustomUnbind::get_object() const {
224
return callable.get_object_id();
225
}
226
227
const Callable *CallableCustomUnbind::get_base_comparator() const {
228
return callable.get_base_comparator();
229
}
230
231
int CallableCustomUnbind::get_argument_count(bool &r_is_valid) const {
232
int ret = callable.get_argument_count(&r_is_valid);
233
if (r_is_valid) {
234
return ret + argcount;
235
}
236
return 0;
237
}
238
239
int CallableCustomUnbind::get_bound_arguments_count() const {
240
return callable.get_bound_arguments_count();
241
}
242
243
void CallableCustomUnbind::get_bound_arguments(Vector<Variant> &r_arguments) const {
244
callable.get_bound_arguments_ref(r_arguments);
245
}
246
247
int CallableCustomUnbind::get_unbound_arguments_count() const {
248
return callable.get_unbound_arguments_count() + argcount;
249
}
250
251
void CallableCustomUnbind::call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const {
252
if (p_argcount < argcount) {
253
r_call_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
254
r_call_error.expected = argcount;
255
return;
256
}
257
callable.callp(p_arguments, p_argcount - argcount, r_return_value, r_call_error);
258
}
259
260
Error CallableCustomUnbind::rpc(int p_peer_id, const Variant **p_arguments, int p_argcount, Callable::CallError &r_call_error) const {
261
if (p_argcount < argcount) {
262
r_call_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
263
r_call_error.expected = argcount;
264
return ERR_UNCONFIGURED;
265
}
266
return callable.rpcp(p_peer_id, p_arguments, p_argcount - argcount, r_call_error);
267
}
268
269
CallableCustomUnbind::CallableCustomUnbind(const Callable &p_callable, int p_argcount) {
270
callable = p_callable;
271
argcount = p_argcount;
272
}
273
274