Debian Codegraph @

core/object/method_bind.h

raw symbols SCIP
1 /**************************************************************************/
2 /* method_bind.h */
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 #pragma once
32
33 #include "core/variant/binder_common.h"
34
35 VARIANT_BITFIELD_CAST(MethodFlags)
36
37 // some helpers
38
39 class MethodBind {
40 int method_id;
41 uint32_t hint_flags = METHOD_FLAGS_DEFAULT;
42 StringName name;
43 StringName instance_class;
44 Vector<Variant> default_arguments;
45 int default_argument_count = 0;
46 int argument_count = 0;
47
48 bool _static = false;
49 bool _const = false;
50 bool _returns = false;
51 bool _returns_raw_obj_ptr = false;
52
53 protected:
54 Variant::Type *argument_types = nullptr;
55 #ifdef DEBUG_ENABLED
56 Vector<StringName> arg_names;
57 #endif // DEBUG_ENABLED
58 void _set_const(bool p_const);
59 void _set_static(bool p_static);
60 void _set_returns(bool p_returns);
61 virtual Variant::Type _gen_argument_type(int p_arg) const = 0;
62 virtual PropertyInfo _gen_argument_type_info(int p_arg) const = 0;
63 void _generate_argument_types(int p_count);
64
65 void set_argument_count(int p_count) { argument_count = p_count; }
66
67 public:
68 _FORCE_INLINE_ const Vector<Variant> &get_default_arguments() const { return default_arguments; }
69 _FORCE_INLINE_ int get_default_argument_count() const { return default_argument_count; }
70
71 _FORCE_INLINE_ Variant has_default_argument(int p_arg) const {
72 int idx = p_arg - (argument_count - default_arguments.size());
73
74 if (idx < 0 || idx >= default_arguments.size()) {
75 return false;
76 } else {
77 return true;
78 }
79 }
80
81 _FORCE_INLINE_ Variant get_default_argument(int p_arg) const {
82 int idx = p_arg - (argument_count - default_arguments.size());
83
84 if (idx < 0 || idx >= default_arguments.size()) {
85 return Variant();
86 } else {
87 return default_arguments[idx];
88 }
89 }
90
91 _FORCE_INLINE_ Variant::Type get_argument_type(int p_argument) const {
92 ERR_FAIL_COND_V(p_argument < -1 || p_argument >= argument_count, Variant::NIL);
93 return argument_types[p_argument + 1];
94 }
95
96 PropertyInfo get_argument_info(int p_argument) const;
97 PropertyInfo get_return_info() const;
98
99 #ifdef DEBUG_ENABLED
100 void set_argument_names(const Vector<StringName> &p_names); // Set by ClassDB, can't be inferred otherwise.
101 Vector<StringName> get_argument_names() const;
102
103 virtual GodotTypeInfo::Metadata get_argument_meta(int p_arg) const = 0;
104 #endif // DEBUG_ENABLED
105
106 void set_hint_flags(uint32_t p_hint) { hint_flags = p_hint; }
107 uint32_t get_hint_flags() const { return hint_flags | (is_const() ? METHOD_FLAG_CONST : 0) | (is_vararg() ? METHOD_FLAG_VARARG : 0) | (is_static() ? METHOD_FLAG_STATIC : 0); }
108 _FORCE_INLINE_ StringName get_instance_class() const { return instance_class; }
109 _FORCE_INLINE_ void set_instance_class(const StringName &p_class) { instance_class = p_class; }
110
111 _FORCE_INLINE_ int get_argument_count() const { return argument_count; }
112
113 #ifdef TOOLS_ENABLED
114 virtual bool is_valid() const { return true; }
115 #endif
116
117 virtual Variant call(Object *p_object, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) const = 0;
118 virtual void validated_call(Object *p_object, const Variant **p_args, Variant *r_ret) const = 0;
119
120 virtual void ptrcall(Object *p_object, const void **p_args, void *r_ret) const = 0;
121
122 StringName get_name() const;
123 void set_name(const StringName &p_name);
124 _FORCE_INLINE_ int get_method_id() const { return method_id; }
125 _FORCE_INLINE_ bool is_const() const { return _const; }
126 _FORCE_INLINE_ bool is_static() const { return _static; }
127 _FORCE_INLINE_ bool has_return() const { return _returns; }
128 virtual bool is_vararg() const { return false; }
129
130 _FORCE_INLINE_ bool is_return_type_raw_object_ptr() { return _returns_raw_obj_ptr; }
131 _FORCE_INLINE_ void set_return_type_is_raw_object_ptr(bool p_returns_raw_obj) { _returns_raw_obj_ptr = p_returns_raw_obj; }
132
133 void set_default_arguments(const Vector<Variant> &p_defargs);
134
135 uint32_t get_hash() const;
136
137 MethodBind();
138 virtual ~MethodBind();
139 };
140
141 // MethodBindVarArg base CRTP
142 template <typename Derived, typename T, typename R, bool should_returns>
143 class MethodBindVarArgBase : public MethodBind {
144 protected:
145 R (T::*method)(const Variant **, int, Callable::CallError &);
146 MethodInfo method_info;
147
148 public:
149 virtual PropertyInfo _gen_argument_type_info(int p_arg) const override {
150 if (p_arg < 0) {
151 return _gen_return_type_info();
152 } else if (p_arg < method_info.arguments.size()) {
153 return method_info.arguments[p_arg];
154 } else {
155 return PropertyInfo(Variant::NIL, "arg_" + itos(p_arg), PROPERTY_HINT_NONE, String(), PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_NIL_IS_VARIANT);
156 }
157 }
158
159 virtual Variant::Type _gen_argument_type(int p_arg) const override {
160 return _gen_argument_type_info(p_arg).type;
161 }
162
163 #ifdef DEBUG_ENABLED
164 virtual GodotTypeInfo::Metadata get_argument_meta(int) const override {
165 return GodotTypeInfo::METADATA_NONE;
166 }
167 #endif // DEBUG_ENABLED
168
169 virtual void validated_call(Object *p_object, const Variant **p_args, Variant *r_ret) const override {
170 ERR_FAIL_MSG("Validated call can't be used with vararg methods. This is a bug.");
171 }
172
173 virtual void ptrcall(Object *p_object, const void **p_args, void *r_ret) const override {
174 ERR_FAIL_MSG("ptrcall can't be used with vararg methods. This is a bug.");
175 }
176
177 virtual bool is_const() const { return false; }
178
179 virtual bool is_vararg() const override { return true; }
180
181 MethodBindVarArgBase(
182 R (T::*p_method)(const Variant **, int, Callable::CallError &),
183 const MethodInfo &p_method_info,
184 bool p_return_nil_is_variant) :
185 method(p_method), method_info(p_method_info) {
186 set_argument_count(method_info.arguments.size());
187 Variant::Type *at = memnew_arr(Variant::Type, method_info.arguments.size() + 1);
188 at[0] = _gen_return_type_info().type;
189 if (method_info.arguments.size()) {
190 #ifdef DEBUG_ENABLED
191 Vector<StringName> names;
192 names.resize(method_info.arguments.size());
193 #endif // DEBUG_ENABLED
194 for (int64_t i = 0; i < method_info.arguments.size(); ++i) {
195 at[i + 1] = method_info.arguments[i].type;
196 #ifdef DEBUG_ENABLED
197 names.write[i] = method_info.arguments[i].name;
198 #endif // DEBUG_ENABLED
199 }
200
201 #ifdef DEBUG_ENABLED
202 set_argument_names(names);
203 #endif // DEBUG_ENABLED
204 }
205 argument_types = at;
206 if (p_return_nil_is_variant) {
207 method_info.return_val.usage |= PROPERTY_USAGE_NIL_IS_VARIANT;
208 }
209
210 _set_returns(should_returns);
211 }
212
213 private:
214 PropertyInfo _gen_return_type_info() const {
215 return Derived::_gen_return_type_info_impl();
216 }
217 };
218
219 // variadic, no return
220 template <typename T>
221 class MethodBindVarArgT : public MethodBindVarArgBase<MethodBindVarArgT<T>, T, void, false> {
222 friend class MethodBindVarArgBase<MethodBindVarArgT<T>, T, void, false>;
223
224 public:
225 virtual Variant call(Object *p_object, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) const override {
226 #ifdef TOOLS_ENABLED
227 ERR_FAIL_COND_V_MSG(p_object && p_object->is_extension_placeholder() && p_object->get_class_name() == MethodBind::get_instance_class(), Variant(), vformat("Cannot call method bind '%s' on placeholder instance.", MethodBind::get_name()));
228 #endif
229 (static_cast<T *>(p_object)->*MethodBindVarArgBase<MethodBindVarArgT<T>, T, void, false>::method)(p_args, p_arg_count, r_error);
230 return {};
231 }
232
233 MethodBindVarArgT(
234 void (T::*p_method)(const Variant **, int, Callable::CallError &),
235 const MethodInfo &p_method_info,
236 bool p_return_nil_is_variant) :
237 MethodBindVarArgBase<MethodBindVarArgT<T>, T, void, false>(p_method, p_method_info, p_return_nil_is_variant) {
238 }
239
240 private:
241 static PropertyInfo _gen_return_type_info_impl() {
242 return {};
243 }
244 };
245
246 template <typename T>
247 MethodBind *create_vararg_method_bind(void (T::*p_method)(const Variant **, int, Callable::CallError &), const MethodInfo &p_info, bool p_return_nil_is_variant) {
248 MethodBind *a = memnew((MethodBindVarArgT<T>)(p_method, p_info, p_return_nil_is_variant));
249 a->set_instance_class(T::get_class_static());
250 return a;
251 }
252
253 // variadic, return
254 template <typename T, typename R>
255 class MethodBindVarArgTR : public MethodBindVarArgBase<MethodBindVarArgTR<T, R>, T, R, true> {
256 friend class MethodBindVarArgBase<MethodBindVarArgTR<T, R>, T, R, true>;
257
258 public:
259 GODOT_GCC_WARNING_PUSH_AND_IGNORE("-Wmaybe-uninitialized") // Workaround GH-66343 raised only with UBSAN, seems to be a false positive.
260
261 virtual Variant call(Object *p_object, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) const override {
262 #ifdef TOOLS_ENABLED
263 ERR_FAIL_COND_V_MSG(p_object && p_object->is_extension_placeholder() && p_object->get_class_name() == MethodBind::get_instance_class(), Variant(), vformat("Cannot call method bind '%s' on placeholder instance.", MethodBind::get_name()));
264 #endif
265 return (static_cast<T *>(p_object)->*MethodBindVarArgBase<MethodBindVarArgTR<T, R>, T, R, true>::method)(p_args, p_arg_count, r_error);
266 }
267
268 GODOT_GCC_WARNING_POP
269
270 MethodBindVarArgTR(
271 R (T::*p_method)(const Variant **, int, Callable::CallError &),
272 const MethodInfo &p_info,
273 bool p_return_nil_is_variant) :
274 MethodBindVarArgBase<MethodBindVarArgTR<T, R>, T, R, true>(p_method, p_info, p_return_nil_is_variant) {
275 }
276
277 private:
278 static PropertyInfo _gen_return_type_info_impl() {
279 return GetTypeInfo<R>::get_class_info();
280 }
281 };
282
283 template <typename T, typename R>
284 MethodBind *create_vararg_method_bind(R (T::*p_method)(const Variant **, int, Callable::CallError &), const MethodInfo &p_info, bool p_return_nil_is_variant) {
285 MethodBind *a = memnew((MethodBindVarArgTR<T, R>)(p_method, p_info, p_return_nil_is_variant));
286 a->set_instance_class(T::get_class_static());
287 return a;
288 }
289
290 /**** VARIADIC TEMPLATES ****/
291
292 #ifndef TYPED_METHOD_BIND
293 class __UnexistingClass;
294 #define MB_T __UnexistingClass
295 #else
296 #define MB_T T
297 #endif
298
299 // no return, not const
300 #ifdef TYPED_METHOD_BIND
301 template <typename T, typename... P>
302 #else
303 template <typename... P>
304 #endif
305 class MethodBindT : public MethodBind {
306 void (MB_T::*method)(P...);
307
308 protected:
309 virtual Variant::Type _gen_argument_type(int p_arg) const override {
310 if (p_arg >= 0 && p_arg < (int)sizeof...(P)) {
311 return call_get_argument_type<P...>(p_arg);
312 } else {
313 return Variant::NIL;
314 }
315 }
316
317 virtual PropertyInfo _gen_argument_type_info(int p_arg) const override {
318 PropertyInfo pi;
319 call_get_argument_type_info<P...>(p_arg, pi);
320 return pi;
321 }
322
323 public:
324 #ifdef DEBUG_ENABLED
325 virtual GodotTypeInfo::Metadata get_argument_meta(int p_arg) const override {
326 return call_get_argument_metadata<P...>(p_arg);
327 }
328
329 #endif // DEBUG_ENABLED
330 virtual Variant call(Object *p_object, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) const override {
331 #ifdef TOOLS_ENABLED
332 ERR_FAIL_COND_V_MSG(p_object && p_object->is_extension_placeholder() && p_object->get_class_name() == get_instance_class(), Variant(), vformat("Cannot call method bind '%s' on placeholder instance.", MethodBind::get_name()));
333 #endif
334 #ifdef TYPED_METHOD_BIND
335 call_with_variant_args_dv(static_cast<T *>(p_object), method, p_args, p_arg_count, r_error, get_default_arguments());
336 #else
337 call_with_variant_args_dv(reinterpret_cast<MB_T *>(p_object), method, p_args, p_arg_count, r_error, get_default_arguments());
338 #endif
339 return Variant();
340 }
341
342 virtual void validated_call(Object *p_object, const Variant **p_args, Variant *r_ret) const override {
343 #ifdef TOOLS_ENABLED
344 ERR_FAIL_COND_MSG(p_object && p_object->is_extension_placeholder() && p_object->get_class_name() == get_instance_class(), vformat("Cannot call method bind '%s' on placeholder instance.", MethodBind::get_name()));
345 #endif
346 #ifdef TYPED_METHOD_BIND
347 call_with_validated_object_instance_args(static_cast<T *>(p_object), method, p_args);
348 #else
349 call_with_validated_object_instance_args(reinterpret_cast<MB_T *>(p_object), method, p_args);
350 #endif
351 }
352
353 virtual void ptrcall(Object *p_object, const void **p_args, void *r_ret) const override {
354 #ifdef TOOLS_ENABLED
355 ERR_FAIL_COND_MSG(p_object && p_object->is_extension_placeholder() && p_object->get_class_name() == get_instance_class(), vformat("Cannot call method bind '%s' on placeholder instance.", MethodBind::get_name()));
356 #endif
357 #ifdef TYPED_METHOD_BIND
358 call_with_ptr_args<T, P...>(static_cast<T *>(p_object), method, p_args);
359 #else
360 call_with_ptr_args<MB_T, P...>(reinterpret_cast<MB_T *>(p_object), method, p_args);
361 #endif
362 }
363
364 MethodBindT(void (MB_T::*p_method)(P...)) {
365 method = p_method;
366 _generate_argument_types(sizeof...(P));
367 set_argument_count(sizeof...(P));
368 }
369 };
370
371 template <typename T, typename... P>
372 MethodBind *create_method_bind(void (T::*p_method)(P...)) {
373 #ifdef TYPED_METHOD_BIND
374 MethodBind *a = memnew((MethodBindT<T, P...>)(p_method));
375 #else
376 MethodBind *a = memnew((MethodBindT<P...>)(reinterpret_cast<void (MB_T::*)(P...)>(p_method)));
377 #endif
378 a->set_instance_class(T::get_class_static());
379 return a;
380 }
381
382 // no return, const
383
384 #ifdef TYPED_METHOD_BIND
385 template <typename T, typename... P>
386 #else
387 template <typename... P>
388 #endif
389 class MethodBindTC : public MethodBind {
390 void (MB_T::*method)(P...) const;
391
392 protected:
393 virtual Variant::Type _gen_argument_type(int p_arg) const override {
394 if (p_arg >= 0 && p_arg < (int)sizeof...(P)) {
395 return call_get_argument_type<P...>(p_arg);
396 } else {
397 return Variant::NIL;
398 }
399 }
400
401 virtual PropertyInfo _gen_argument_type_info(int p_arg) const override {
402 PropertyInfo pi;
403 call_get_argument_type_info<P...>(p_arg, pi);
404 return pi;
405 }
406
407 public:
408 #ifdef DEBUG_ENABLED
409 virtual GodotTypeInfo::Metadata get_argument_meta(int p_arg) const override {
410 return call_get_argument_metadata<P...>(p_arg);
411 }
412
413 #endif // DEBUG_ENABLED
414 virtual Variant call(Object *p_object, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) const override {
415 #ifdef TOOLS_ENABLED
416 ERR_FAIL_COND_V_MSG(p_object && p_object->is_extension_placeholder() && p_object->get_class_name() == get_instance_class(), Variant(), vformat("Cannot call method bind '%s' on placeholder instance.", MethodBind::get_name()));
417 #endif
418 #ifdef TYPED_METHOD_BIND
419 call_with_variant_argsc_dv(static_cast<T *>(p_object), method, p_args, p_arg_count, r_error, get_default_arguments());
420 #else
421 call_with_variant_argsc_dv(reinterpret_cast<MB_T *>(p_object), method, p_args, p_arg_count, r_error, get_default_arguments());
422 #endif
423 return Variant();
424 }
425
426 virtual void validated_call(Object *p_object, const Variant **p_args, Variant *r_ret) const override {
427 #ifdef TOOLS_ENABLED
428 ERR_FAIL_COND_MSG(p_object && p_object->is_extension_placeholder() && p_object->get_class_name() == get_instance_class(), vformat("Cannot call method bind '%s' on placeholder instance.", MethodBind::get_name()));
429 #endif
430 #ifdef TYPED_METHOD_BIND
431 call_with_validated_object_instance_argsc(static_cast<T *>(p_object), method, p_args);
432 #else
433 call_with_validated_object_instance_argsc(reinterpret_cast<MB_T *>(p_object), method, p_args);
434 #endif
435 }
436
437 virtual void ptrcall(Object *p_object, const void **p_args, void *r_ret) const override {
438 #ifdef TOOLS_ENABLED
439 ERR_FAIL_COND_MSG(p_object && p_object->is_extension_placeholder() && p_object->get_class_name() == get_instance_class(), vformat("Cannot call method bind '%s' on placeholder instance.", MethodBind::get_name()));
440 #endif
441 #ifdef TYPED_METHOD_BIND
442 call_with_ptr_argsc<T, P...>(static_cast<T *>(p_object), method, p_args);
443 #else
444 call_with_ptr_argsc<MB_T, P...>(reinterpret_cast<MB_T *>(p_object), method, p_args);
445 #endif
446 }
447
448 MethodBindTC(void (MB_T::*p_method)(P...) const) {
449 method = p_method;
450 _set_const(true);
451 _generate_argument_types(sizeof...(P));
452 set_argument_count(sizeof...(P));
453 }
454 };
455
456 template <typename T, typename... P>
457 MethodBind *create_method_bind(void (T::*p_method)(P...) const) {
458 #ifdef TYPED_METHOD_BIND
459 MethodBind *a = memnew((MethodBindTC<T, P...>)(p_method));
460 #else
461 MethodBind *a = memnew((MethodBindTC<P...>)(reinterpret_cast<void (MB_T::*)(P...) const>(p_method)));
462 #endif
463 a->set_instance_class(T::get_class_static());
464 return a;
465 }
466
467 // return, not const
468
469 #ifdef TYPED_METHOD_BIND
470 template <typename T, typename R, typename... P>
471 #else
472 template <typename R, typename... P>
473 #endif
474 class MethodBindTR : public MethodBind {
475 R (MB_T::*method)(P...);
476
477 protected:
478 virtual Variant::Type _gen_argument_type(int p_arg) const override {
479 if (p_arg >= 0 && p_arg < (int)sizeof...(P)) {
480 return call_get_argument_type<P...>(p_arg);
481 } else {
482 return GetTypeInfo<R>::VARIANT_TYPE;
483 }
484 }
485
486 virtual PropertyInfo _gen_argument_type_info(int p_arg) const override {
487 if (p_arg >= 0 && p_arg < (int)sizeof...(P)) {
488 PropertyInfo pi;
489 call_get_argument_type_info<P...>(p_arg, pi);
490 return pi;
491 } else {
492 return GetTypeInfo<R>::get_class_info();
493 }
494 }
495
496 public:
497 #ifdef DEBUG_ENABLED
498 virtual GodotTypeInfo::Metadata get_argument_meta(int p_arg) const override {
499 if (p_arg >= 0) {
500 return call_get_argument_metadata<P...>(p_arg);
501 } else {
502 return GetTypeInfo<R>::METADATA;
503 }
504 }
505 #endif // DEBUG_ENABLED
506
507 virtual Variant call(Object *p_object, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) const override {
508 Variant ret;
509 #ifdef TOOLS_ENABLED
510 ERR_FAIL_COND_V_MSG(p_object && p_object->is_extension_placeholder() && p_object->get_class_name() == get_instance_class(), ret, vformat("Cannot call method bind '%s' on placeholder instance.", MethodBind::get_name()));
511 #endif
512 #ifdef TYPED_METHOD_BIND
513 call_with_variant_args_ret_dv(static_cast<T *>(p_object), method, p_args, p_arg_count, ret, r_error, get_default_arguments());
514 #else
515 call_with_variant_args_ret_dv(reinterpret_cast<MB_T *>(p_object), method, p_args, p_arg_count, ret, r_error, get_default_arguments());
516 #endif
517 return ret;
518 }
519
520 virtual void validated_call(Object *p_object, const Variant **p_args, Variant *r_ret) const override {
521 #ifdef TOOLS_ENABLED
522 ERR_FAIL_COND_MSG(p_object && p_object->is_extension_placeholder() && p_object->get_class_name() == get_instance_class(), vformat("Cannot call method bind '%s' on placeholder instance.", MethodBind::get_name()));
523 #endif
524 #ifdef TYPED_METHOD_BIND
525 call_with_validated_object_instance_args_ret(static_cast<T *>(p_object), method, p_args, r_ret);
526 #else
527 call_with_validated_object_instance_args_ret(reinterpret_cast<MB_T *>(p_object), method, p_args, r_ret);
528 #endif
529 }
530
531 virtual void ptrcall(Object *p_object, const void **p_args, void *r_ret) const override {
532 #ifdef TOOLS_ENABLED
533 ERR_FAIL_COND_MSG(p_object && p_object->is_extension_placeholder() && p_object->get_class_name() == get_instance_class(), vformat("Cannot call method bind '%s' on placeholder instance.", MethodBind::get_name()));
534 #endif
535 #ifdef TYPED_METHOD_BIND
536 call_with_ptr_args_ret<T, R, P...>(static_cast<T *>(p_object), method, p_args, r_ret);
537 #else
538 call_with_ptr_args_ret<MB_T, R, P...>(reinterpret_cast<MB_T *>(p_object), method, p_args, r_ret);
539 #endif
540 }
541
542 MethodBindTR(R (MB_T::*p_method)(P...)) {
543 method = p_method;
544 _set_returns(true);
545 _generate_argument_types(sizeof...(P));
546 set_argument_count(sizeof...(P));
547 }
548 };
549
550 template <typename T, typename R, typename... P>
551 MethodBind *create_method_bind(R (T::*p_method)(P...)) {
552 #ifdef TYPED_METHOD_BIND
553 MethodBind *a = memnew((MethodBindTR<T, R, P...>)(p_method));
554 #else
555 MethodBind *a = memnew((MethodBindTR<R, P...>)(reinterpret_cast<R (MB_T::*)(P...)>(p_method)));
556 #endif
557
558 a->set_instance_class(T::get_class_static());
559 return a;
560 }
561
562 // return, const
563
564 #ifdef TYPED_METHOD_BIND
565 template <typename T, typename R, typename... P>
566 #else
567 template <typename R, typename... P>
568 #endif
569 class MethodBindTRC : public MethodBind {
570 R (MB_T::*method)(P...) const;
571
572 protected:
573 virtual Variant::Type _gen_argument_type(int p_arg) const override {
574 if (p_arg >= 0 && p_arg < (int)sizeof...(P)) {
575 return call_get_argument_type<P...>(p_arg);
576 } else {
577 return GetTypeInfo<R>::VARIANT_TYPE;
578 }
579 }
580
581 virtual PropertyInfo _gen_argument_type_info(int p_arg) const override {
582 if (p_arg >= 0 && p_arg < (int)sizeof...(P)) {
583 PropertyInfo pi;
584 call_get_argument_type_info<P...>(p_arg, pi);
585 return pi;
586 } else {
587 return GetTypeInfo<R>::get_class_info();
588 }
589 }
590
591 public:
592 #ifdef DEBUG_ENABLED
593 virtual GodotTypeInfo::Metadata get_argument_meta(int p_arg) const override {
594 if (p_arg >= 0) {
595 return call_get_argument_metadata<P...>(p_arg);
596 } else {
597 return GetTypeInfo<R>::METADATA;
598 }
599 }
600 #endif // DEBUG_ENABLED
601
602 virtual Variant call(Object *p_object, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) const override {
603 Variant ret;
604 #ifdef TOOLS_ENABLED
605 ERR_FAIL_COND_V_MSG(p_object && p_object->is_extension_placeholder() && p_object->get_class_name() == get_instance_class(), ret, vformat("Cannot call method bind '%s' on placeholder instance.", MethodBind::get_name()));
606 #endif
607 #ifdef TYPED_METHOD_BIND
608 call_with_variant_args_retc_dv(static_cast<T *>(p_object), method, p_args, p_arg_count, ret, r_error, get_default_arguments());
609 #else
610 call_with_variant_args_retc_dv(reinterpret_cast<MB_T *>(p_object), method, p_args, p_arg_count, ret, r_error, get_default_arguments());
611 #endif
612 return ret;
613 }
614
615 virtual void validated_call(Object *p_object, const Variant **p_args, Variant *r_ret) const override {
616 #ifdef TOOLS_ENABLED
617 ERR_FAIL_COND_MSG(p_object && p_object->is_extension_placeholder() && p_object->get_class_name() == get_instance_class(), vformat("Cannot call method bind '%s' on placeholder instance.", MethodBind::get_name()));
618 #endif
619 #ifdef TYPED_METHOD_BIND
620 call_with_validated_object_instance_args_retc(static_cast<T *>(p_object), method, p_args, r_ret);
621 #else
622 call_with_validated_object_instance_args_retc(reinterpret_cast<MB_T *>(p_object), method, p_args, r_ret);
623 #endif
624 }
625
626 virtual void ptrcall(Object *p_object, const void **p_args, void *r_ret) const override {
627 #ifdef TOOLS_ENABLED
628 ERR_FAIL_COND_MSG(p_object && p_object->is_extension_placeholder() && p_object->get_class_name() == get_instance_class(), vformat("Cannot call method bind '%s' on placeholder instance.", MethodBind::get_name()));
629 #endif
630 #ifdef TYPED_METHOD_BIND
631 call_with_ptr_args_retc<T, R, P...>(static_cast<T *>(p_object), method, p_args, r_ret);
632 #else
633 call_with_ptr_args_retc<MB_T, R, P...>(reinterpret_cast<MB_T *>(p_object), method, p_args, r_ret);
634 #endif
635 }
636
637 MethodBindTRC(R (MB_T::*p_method)(P...) const) {
638 method = p_method;
639 _set_returns(true);
640 _set_const(true);
641 _generate_argument_types(sizeof...(P));
642 set_argument_count(sizeof...(P));
643 }
644 };
645
646 template <typename T, typename R, typename... P>
647 MethodBind *create_method_bind(R (T::*p_method)(P...) const) {
648 #ifdef TYPED_METHOD_BIND
649 MethodBind *a = memnew((MethodBindTRC<T, R, P...>)(p_method));
650 #else
651 MethodBind *a = memnew((MethodBindTRC<R, P...>)(reinterpret_cast<R (MB_T::*)(P...) const>(p_method)));
652 #endif
653 a->set_instance_class(T::get_class_static());
654 return a;
655 }
656
657 /* STATIC BINDS */
658
659 // no return
660
661 template <typename... P>
662 class MethodBindTS : public MethodBind {
663 void (*function)(P...);
664
665 protected:
666 virtual Variant::Type _gen_argument_type(int p_arg) const override {
667 if (p_arg >= 0 && p_arg < (int)sizeof...(P)) {
668 return call_get_argument_type<P...>(p_arg);
669 } else {
670 return Variant::NIL;
671 }
672 }
673
674 virtual PropertyInfo _gen_argument_type_info(int p_arg) const override {
675 PropertyInfo pi;
676 call_get_argument_type_info<P...>(p_arg, pi);
677 return pi;
678 }
679
680 public:
681 #ifdef DEBUG_ENABLED
682 virtual GodotTypeInfo::Metadata get_argument_meta(int p_arg) const override {
683 return call_get_argument_metadata<P...>(p_arg);
684 }
685
686 #endif // DEBUG_ENABLED
687 virtual Variant call(Object *p_object, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) const override {
688 (void)p_object; // unused
689 call_with_variant_args_static_dv(function, p_args, p_arg_count, r_error, get_default_arguments());
690 return Variant();
691 }
692
693 virtual void validated_call(Object *p_object, const Variant **p_args, Variant *r_ret) const override {
694 call_with_validated_variant_args_static_method(function, p_args);
695 }
696
697 virtual void ptrcall(Object *p_object, const void **p_args, void *r_ret) const override {
698 (void)p_object;
699 (void)r_ret;
700 call_with_ptr_args_static_method(function, p_args);
701 }
702
703 MethodBindTS(void (*p_function)(P...)) {
704 function = p_function;
705 _generate_argument_types(sizeof...(P));
706 set_argument_count(sizeof...(P));
707 _set_static(true);
708 }
709 };
710
711 template <typename... P>
712 MethodBind *create_static_method_bind(void (*p_method)(P...)) {
713 MethodBind *a = memnew((MethodBindTS<P...>)(p_method));
714 return a;
715 }
716
717 // return
718
719 template <typename R, typename... P>
720 class MethodBindTRS : public MethodBind {
721 R (*function)(P...);
722
723 protected:
724 virtual Variant::Type _gen_argument_type(int p_arg) const override {
725 if (p_arg >= 0 && p_arg < (int)sizeof...(P)) {
726 return call_get_argument_type<P...>(p_arg);
727 } else {
728 return GetTypeInfo<R>::VARIANT_TYPE;
729 }
730 }
731
732 virtual PropertyInfo _gen_argument_type_info(int p_arg) const override {
733 if (p_arg >= 0 && p_arg < (int)sizeof...(P)) {
734 PropertyInfo pi;
735 call_get_argument_type_info<P...>(p_arg, pi);
736 return pi;
737 } else {
738 return GetTypeInfo<R>::get_class_info();
739 }
740 }
741
742 public:
743 #ifdef DEBUG_ENABLED
744 virtual GodotTypeInfo::Metadata get_argument_meta(int p_arg) const override {
745 if (p_arg >= 0) {
746 return call_get_argument_metadata<P...>(p_arg);
747 } else {
748 return GetTypeInfo<R>::METADATA;
749 }
750 }
751
752 #endif // DEBUG_ENABLED
753 virtual Variant call(Object *p_object, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) const override {
754 Variant ret;
755 call_with_variant_args_static_ret_dv(function, p_args, p_arg_count, ret, r_error, get_default_arguments());
756 return ret;
757 }
758
759 virtual void validated_call(Object *p_object, const Variant **p_args, Variant *r_ret) const override {
760 call_with_validated_variant_args_static_method_ret(function, p_args, r_ret);
761 }
762
763 virtual void ptrcall(Object *p_object, const void **p_args, void *r_ret) const override {
764 (void)p_object;
765 call_with_ptr_args_static_method_ret(function, p_args, r_ret);
766 }
767
768 MethodBindTRS(R (*p_function)(P...)) {
769 function = p_function;
770 _generate_argument_types(sizeof...(P));
771 set_argument_count(sizeof...(P));
772 _set_static(true);
773 _set_returns(true);
774 }
775 };
776
777 template <typename R, typename... P>
778 MethodBind *create_static_method_bind(R (*p_method)(P...)) {
779 MethodBind *a = memnew((MethodBindTRS<R, P...>)(p_method));
780 return a;
781 }