Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/core/variant/callable.h
21207 views
1
/**************************************************************************/
2
/* callable.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/object/object_id.h"
34
#include "core/string/string_name.h"
35
36
class Array;
37
class Object;
38
class Variant;
39
class CallableCustom;
40
41
// This is an abstraction of things that can be called.
42
// It is used for signals and other cases where efficient calling of functions
43
// is required. It is designed for the standard case (object and method)
44
// but can be optimized or customized.
45
46
// Enforce 16 bytes with `alignas` to avoid arch-specific alignment issues on x86 vs armv7.
47
48
class Callable {
49
alignas(8) StringName method;
50
union {
51
uint64_t object = 0;
52
CallableCustom *custom;
53
};
54
55
public:
56
struct CallError {
57
enum Error {
58
CALL_OK,
59
CALL_ERROR_INVALID_METHOD,
60
CALL_ERROR_INVALID_ARGUMENT, // expected is variant type
61
CALL_ERROR_TOO_MANY_ARGUMENTS, // expected is number of arguments
62
CALL_ERROR_TOO_FEW_ARGUMENTS, // expected is number of arguments
63
CALL_ERROR_INSTANCE_IS_NULL,
64
CALL_ERROR_METHOD_NOT_CONST,
65
};
66
Error error = Error::CALL_OK;
67
int argument = 0;
68
int expected = 0;
69
};
70
71
template <typename... VarArgs>
72
Variant call(VarArgs... p_args) const;
73
void callp(const Variant **p_arguments, int p_argcount, Variant &r_return_value, CallError &r_call_error) const;
74
void call_deferredp(const Variant **p_arguments, int p_argcount) const;
75
Variant callv(const Array &p_arguments) const;
76
77
template <typename... VarArgs>
78
void call_deferred(VarArgs... p_args) const {
79
Variant args[sizeof...(p_args) + 1] = { p_args..., 0 }; // +1 makes sure zero sized arrays are also supported.
80
const Variant *argptrs[sizeof...(p_args) + 1];
81
for (uint32_t i = 0; i < sizeof...(p_args); i++) {
82
argptrs[i] = &args[i];
83
}
84
return call_deferredp(sizeof...(p_args) == 0 ? nullptr : (const Variant **)argptrs, sizeof...(p_args));
85
}
86
87
Error rpcp(int p_id, const Variant **p_arguments, int p_argcount, CallError &r_call_error) const;
88
89
_FORCE_INLINE_ bool is_null() const {
90
return method == StringName() && object == 0;
91
}
92
_FORCE_INLINE_ bool is_custom() const {
93
return method == StringName() && custom != nullptr;
94
}
95
_FORCE_INLINE_ bool is_standard() const {
96
return method != StringName();
97
}
98
bool is_valid() const;
99
100
template <typename... VarArgs>
101
Callable bind(VarArgs... p_args) const;
102
Callable bindv(const Array &p_arguments);
103
104
Callable bindp(const Variant **p_arguments, int p_argcount) const;
105
Callable unbind(int p_argcount) const;
106
107
Object *get_object() const;
108
ObjectID get_object_id() const;
109
StringName get_method() const;
110
CallableCustom *get_custom() const;
111
int get_argument_count(bool *r_is_valid = nullptr) const;
112
int get_bound_arguments_count() const;
113
void get_bound_arguments_ref(Vector<Variant> &r_arguments) const; // Internal engine use, the exposed one is below.
114
Array get_bound_arguments() const;
115
int get_unbound_arguments_count() const;
116
117
uint32_t hash() const;
118
119
const Callable *get_base_comparator() const; //used for bind/unbind to do less precise comparisons (ignoring binds) in signal connect/disconnect
120
121
bool operator==(const Callable &p_callable) const;
122
bool operator!=(const Callable &p_callable) const;
123
bool operator<(const Callable &p_callable) const;
124
125
void operator=(const Callable &p_callable);
126
127
explicit operator String() const;
128
129
static Callable create(const Variant &p_variant, const StringName &p_method);
130
131
Callable(const Object *p_object, const StringName &p_method);
132
Callable(ObjectID p_object, const StringName &p_method);
133
Callable(CallableCustom *p_custom);
134
Callable(const Callable &p_callable);
135
Callable() {}
136
~Callable();
137
};
138
139
// Zero-constructing Callable initializes method and object to 0 (and thus empty).
140
template <>
141
struct is_zero_constructible<Callable> : std::true_type {};
142
143
class CallableCustom {
144
friend class Callable;
145
SafeRefCount ref_count;
146
bool referenced = false;
147
148
public:
149
typedef bool (*CompareEqualFunc)(const CallableCustom *p_a, const CallableCustom *p_b);
150
typedef bool (*CompareLessFunc)(const CallableCustom *p_a, const CallableCustom *p_b);
151
152
//for every type that inherits, these must always be the same for this type
153
virtual uint32_t hash() const = 0;
154
virtual String get_as_text() const = 0;
155
virtual CompareEqualFunc get_compare_equal_func() const = 0;
156
virtual CompareLessFunc get_compare_less_func() const = 0;
157
virtual bool is_valid() const;
158
virtual StringName get_method() const;
159
virtual ObjectID get_object() const = 0;
160
virtual void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const = 0;
161
virtual Error rpc(int p_peer_id, const Variant **p_arguments, int p_argcount, Callable::CallError &r_call_error) const;
162
virtual const Callable *get_base_comparator() const;
163
virtual int get_argument_count(bool &r_is_valid) const;
164
virtual int get_bound_arguments_count() const;
165
virtual void get_bound_arguments(Vector<Variant> &r_arguments) const;
166
virtual int get_unbound_arguments_count() const;
167
168
CallableCustom();
169
virtual ~CallableCustom() {}
170
};
171
172
// This is just a proxy object to object signals, its only
173
// allocated on demand by/for scripting languages so it can
174
// be put inside a Variant, but it is not
175
// used by the engine itself.
176
177
// Enforce 16 bytes with `alignas` to avoid arch-specific alignment issues on x86 vs armv7.
178
class Signal {
179
alignas(8) StringName name;
180
ObjectID object;
181
182
public:
183
_FORCE_INLINE_ bool is_null() const {
184
return object.is_null() && name == StringName();
185
}
186
Object *get_object() const;
187
ObjectID get_object_id() const;
188
StringName get_name() const;
189
190
bool operator==(const Signal &p_signal) const;
191
bool operator!=(const Signal &p_signal) const;
192
bool operator<(const Signal &p_signal) const;
193
194
explicit operator String() const;
195
196
Error emit(const Variant **p_arguments, int p_argcount) const;
197
Error connect(const Callable &p_callable, uint32_t p_flags = 0);
198
void disconnect(const Callable &p_callable);
199
bool is_connected(const Callable &p_callable) const;
200
bool has_connections() const;
201
202
Array get_connections() const;
203
Signal(const Object *p_object, const StringName &p_name);
204
Signal(ObjectID p_object, const StringName &p_name);
205
Signal() {}
206
};
207
208
// Zero-constructing Signal initializes name and object to 0 (and thus empty).
209
template <>
210
struct is_zero_constructible<Signal> : std::true_type {};
211
212
struct CallableComparator {
213
const Callable &func;
214
215
bool operator()(const Variant &p_l, const Variant &p_r) const;
216
};
217
218