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