Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/core/variant/callable_bind.cpp
9903 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
CallableCustomBind::~CallableCustomBind() {
171
}
172
173
//////////////////////////////////
174
175
uint32_t CallableCustomUnbind::hash() const {
176
return callable.hash();
177
}
178
String CallableCustomUnbind::get_as_text() const {
179
return callable.operator String();
180
}
181
182
bool CallableCustomUnbind::_equal_func(const CallableCustom *p_a, const CallableCustom *p_b) {
183
const CallableCustomUnbind *a = static_cast<const CallableCustomUnbind *>(p_a);
184
const CallableCustomUnbind *b = static_cast<const CallableCustomUnbind *>(p_b);
185
186
if (a->callable != b->callable) {
187
return false;
188
}
189
190
if (a->argcount != b->argcount) {
191
return false;
192
}
193
194
return true;
195
}
196
197
bool CallableCustomUnbind::_less_func(const CallableCustom *p_a, const CallableCustom *p_b) {
198
const CallableCustomUnbind *a = static_cast<const CallableCustomUnbind *>(p_a);
199
const CallableCustomUnbind *b = static_cast<const CallableCustomUnbind *>(p_b);
200
201
if (a->callable < b->callable) {
202
return true;
203
} else if (b->callable < a->callable) {
204
return false;
205
}
206
207
return a->argcount < b->argcount;
208
}
209
210
CallableCustom::CompareEqualFunc CallableCustomUnbind::get_compare_equal_func() const {
211
return _equal_func;
212
}
213
214
CallableCustom::CompareLessFunc CallableCustomUnbind::get_compare_less_func() const {
215
return _less_func;
216
}
217
218
bool CallableCustomUnbind::is_valid() const {
219
return callable.is_valid();
220
}
221
222
StringName CallableCustomUnbind::get_method() const {
223
return callable.get_method();
224
}
225
226
ObjectID CallableCustomUnbind::get_object() const {
227
return callable.get_object_id();
228
}
229
230
const Callable *CallableCustomUnbind::get_base_comparator() const {
231
return callable.get_base_comparator();
232
}
233
234
int CallableCustomUnbind::get_argument_count(bool &r_is_valid) const {
235
int ret = callable.get_argument_count(&r_is_valid);
236
if (r_is_valid) {
237
return ret + argcount;
238
}
239
return 0;
240
}
241
242
int CallableCustomUnbind::get_bound_arguments_count() const {
243
return callable.get_bound_arguments_count();
244
}
245
246
void CallableCustomUnbind::get_bound_arguments(Vector<Variant> &r_arguments) const {
247
callable.get_bound_arguments_ref(r_arguments);
248
}
249
250
int CallableCustomUnbind::get_unbound_arguments_count() const {
251
return callable.get_unbound_arguments_count() + argcount;
252
}
253
254
void CallableCustomUnbind::call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const {
255
if (p_argcount < argcount) {
256
r_call_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
257
r_call_error.expected = argcount;
258
return;
259
}
260
callable.callp(p_arguments, p_argcount - argcount, r_return_value, r_call_error);
261
}
262
263
Error CallableCustomUnbind::rpc(int p_peer_id, const Variant **p_arguments, int p_argcount, Callable::CallError &r_call_error) const {
264
if (p_argcount < argcount) {
265
r_call_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
266
r_call_error.expected = argcount;
267
return ERR_UNCONFIGURED;
268
}
269
return callable.rpcp(p_peer_id, p_arguments, p_argcount - argcount, r_call_error);
270
}
271
272
CallableCustomUnbind::CallableCustomUnbind(const Callable &p_callable, int p_argcount) {
273
callable = p_callable;
274
argcount = p_argcount;
275
}
276
277
CallableCustomUnbind::~CallableCustomUnbind() {
278
}
279
280