Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/core/object/object.h
20844 views
1
/**************************************************************************/
2
/* object.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/extension/gdextension_interface.gen.h"
34
#include "core/object/gdtype.h"
35
#include "core/object/message_queue.h"
36
#include "core/object/object_id.h"
37
#include "core/os/spin_lock.h"
38
#include "core/templates/hash_map.h"
39
#include "core/templates/hash_set.h"
40
#include "core/templates/list.h"
41
#include "core/templates/safe_refcount.h"
42
#include "core/variant/variant.h"
43
44
template <typename T>
45
class TypedArray;
46
47
template <typename T>
48
class Ref;
49
50
enum PropertyHint {
51
PROPERTY_HINT_NONE, ///< no hint provided.
52
PROPERTY_HINT_RANGE, ///< hint_text = "min,max[,step][,or_greater][,or_less][,prefer_slider][,hide_control][,radians_as_degrees][,degrees][,exp][,suffix:<keyword>] range.
53
PROPERTY_HINT_ENUM, ///< hint_text= "val1,val2,val3,etc"
54
PROPERTY_HINT_ENUM_SUGGESTION, ///< hint_text= "val1,val2,val3,etc"
55
PROPERTY_HINT_EXP_EASING, /// exponential easing function (Math::ease) use "attenuation" hint string to revert (flip h), "positive_only" to exclude in-out and out-in. (ie: "attenuation,positive_only")
56
PROPERTY_HINT_LINK,
57
PROPERTY_HINT_FLAGS, ///< hint_text= "flag1,flag2,etc" (as bit flags)
58
PROPERTY_HINT_LAYERS_2D_RENDER,
59
PROPERTY_HINT_LAYERS_2D_PHYSICS,
60
PROPERTY_HINT_LAYERS_2D_NAVIGATION,
61
PROPERTY_HINT_LAYERS_3D_RENDER,
62
PROPERTY_HINT_LAYERS_3D_PHYSICS,
63
PROPERTY_HINT_LAYERS_3D_NAVIGATION,
64
PROPERTY_HINT_FILE, ///< a file path must be passed, hint_text (optionally) is a filter "*.png,*.wav,*.doc,"
65
PROPERTY_HINT_DIR, ///< a directory path must be passed
66
PROPERTY_HINT_GLOBAL_FILE, ///< a file path must be passed, hint_text (optionally) is a filter "*.png,*.wav,*.doc,"
67
PROPERTY_HINT_GLOBAL_DIR, ///< a directory path must be passed
68
PROPERTY_HINT_RESOURCE_TYPE, ///< a comma-separated resource object type, e.g. "NoiseTexture,GradientTexture2D". Subclasses can be excluded with a "-" prefix if placed *after* the base class, e.g. "Texture2D,-MeshTexture".
69
PROPERTY_HINT_MULTILINE_TEXT, ///< used for string properties that can contain multiple lines
70
PROPERTY_HINT_EXPRESSION, ///< used for string properties that can contain multiple lines
71
PROPERTY_HINT_PLACEHOLDER_TEXT, ///< used to set a placeholder text for string properties
72
PROPERTY_HINT_COLOR_NO_ALPHA, ///< used for ignoring alpha component when editing a color
73
PROPERTY_HINT_OBJECT_ID,
74
PROPERTY_HINT_TYPE_STRING, ///< a type string, the hint is the base type to choose
75
PROPERTY_HINT_NODE_PATH_TO_EDITED_NODE, // Deprecated.
76
PROPERTY_HINT_OBJECT_TOO_BIG, ///< object is too big to send
77
PROPERTY_HINT_NODE_PATH_VALID_TYPES,
78
PROPERTY_HINT_SAVE_FILE, ///< a file path must be passed, hint_text (optionally) is a filter "*.png,*.wav,*.doc,". This opens a save dialog
79
PROPERTY_HINT_GLOBAL_SAVE_FILE, ///< a file path must be passed, hint_text (optionally) is a filter "*.png,*.wav,*.doc,". This opens a save dialog
80
PROPERTY_HINT_INT_IS_OBJECTID, // Deprecated.
81
PROPERTY_HINT_INT_IS_POINTER,
82
PROPERTY_HINT_ARRAY_TYPE,
83
PROPERTY_HINT_LOCALE_ID,
84
PROPERTY_HINT_LOCALIZABLE_STRING,
85
PROPERTY_HINT_NODE_TYPE, ///< a node object type
86
PROPERTY_HINT_HIDE_QUATERNION_EDIT, /// Only Node3D::transform should hide the quaternion editor.
87
PROPERTY_HINT_PASSWORD,
88
PROPERTY_HINT_LAYERS_AVOIDANCE,
89
PROPERTY_HINT_DICTIONARY_TYPE,
90
PROPERTY_HINT_TOOL_BUTTON,
91
PROPERTY_HINT_ONESHOT, ///< the property will be changed by self after setting, such as AudioStreamPlayer.playing, Particles.emitting.
92
PROPERTY_HINT_NO_NODEPATH, /// < this property will not contain a NodePath, regardless of type (Array, Dictionary, List, etc.). Needed for SceneTreeDock.
93
PROPERTY_HINT_GROUP_ENABLE, ///< used to make the property's group checkable. Only use for boolean types.
94
PROPERTY_HINT_INPUT_NAME,
95
PROPERTY_HINT_FILE_PATH,
96
PROPERTY_HINT_MAX,
97
};
98
99
enum PropertyUsageFlags {
100
PROPERTY_USAGE_NONE = 0,
101
PROPERTY_USAGE_STORAGE = 1 << 1,
102
PROPERTY_USAGE_EDITOR = 1 << 2,
103
PROPERTY_USAGE_INTERNAL = 1 << 3,
104
PROPERTY_USAGE_CHECKABLE = 1 << 4, // Used for editing global variables.
105
PROPERTY_USAGE_CHECKED = 1 << 5, // Used for editing global variables.
106
PROPERTY_USAGE_GROUP = 1 << 6, // Used for grouping props in the editor.
107
PROPERTY_USAGE_CATEGORY = 1 << 7,
108
PROPERTY_USAGE_SUBGROUP = 1 << 8,
109
PROPERTY_USAGE_CLASS_IS_BITFIELD = 1 << 9,
110
PROPERTY_USAGE_NO_INSTANCE_STATE = 1 << 10,
111
PROPERTY_USAGE_RESTART_IF_CHANGED = 1 << 11,
112
PROPERTY_USAGE_SCRIPT_VARIABLE = 1 << 12,
113
PROPERTY_USAGE_STORE_IF_NULL = 1 << 13,
114
PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED = 1 << 14,
115
PROPERTY_USAGE_SCRIPT_DEFAULT_VALUE = 1 << 15, // Deprecated.
116
PROPERTY_USAGE_CLASS_IS_ENUM = 1 << 16,
117
PROPERTY_USAGE_NIL_IS_VARIANT = 1 << 17,
118
PROPERTY_USAGE_ARRAY = 1 << 18, // Used in the inspector to group properties as elements of an array.
119
PROPERTY_USAGE_ALWAYS_DUPLICATE = 1 << 19, // When duplicating a resource, always duplicate, even with subresource duplication disabled.
120
PROPERTY_USAGE_NEVER_DUPLICATE = 1 << 20, // When duplicating a resource, never duplicate, even with subresource duplication enabled.
121
PROPERTY_USAGE_HIGH_END_GFX = 1 << 21,
122
PROPERTY_USAGE_NODE_PATH_FROM_SCENE_ROOT = 1 << 22,
123
PROPERTY_USAGE_RESOURCE_NOT_PERSISTENT = 1 << 23,
124
PROPERTY_USAGE_KEYING_INCREMENTS = 1 << 24, // Used in inspector to increment property when keyed in animation player.
125
PROPERTY_USAGE_DEFERRED_SET_RESOURCE = 1 << 25, // Deprecated.
126
PROPERTY_USAGE_EDITOR_INSTANTIATE_OBJECT = 1 << 26, // For Object properties, instantiate them when creating in editor.
127
PROPERTY_USAGE_EDITOR_BASIC_SETTING = 1 << 27, //for project or editor settings, show when basic settings are selected.
128
PROPERTY_USAGE_READ_ONLY = 1 << 28, // Mark a property as read-only in the inspector.
129
PROPERTY_USAGE_SECRET = 1 << 29, // Export preset credentials that should be stored separately from the rest of the export config.
130
131
PROPERTY_USAGE_DEFAULT = PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_EDITOR,
132
PROPERTY_USAGE_NO_EDITOR = PROPERTY_USAGE_STORAGE,
133
};
134
135
#define ADD_SIGNAL(m_signal) ::ClassDB::add_signal(get_class_static(), m_signal)
136
#define ADD_PROPERTY(m_property, m_setter, m_getter) ::ClassDB::add_property(get_class_static(), m_property, StringName(m_setter), StringName(m_getter))
137
#define ADD_PROPERTYI(m_property, m_setter, m_getter, m_index) ::ClassDB::add_property(get_class_static(), m_property, StringName(m_setter), StringName(m_getter), m_index)
138
#define ADD_PROPERTY_DEFAULT(m_property, m_default) ::ClassDB::set_property_default_value(get_class_static(), m_property, m_default)
139
#define ADD_GROUP(m_name, m_prefix) ::ClassDB::add_property_group(get_class_static(), m_name, m_prefix)
140
#define ADD_GROUP_INDENT(m_name, m_prefix, m_depth) ::ClassDB::add_property_group(get_class_static(), m_name, m_prefix, m_depth)
141
#define ADD_SUBGROUP(m_name, m_prefix) ::ClassDB::add_property_subgroup(get_class_static(), m_name, m_prefix)
142
#define ADD_SUBGROUP_INDENT(m_name, m_prefix, m_depth) ::ClassDB::add_property_subgroup(get_class_static(), m_name, m_prefix, m_depth)
143
#define ADD_LINKED_PROPERTY(m_property, m_linked_property) ::ClassDB::add_linked_property(get_class_static(), m_property, m_linked_property)
144
145
#ifdef TOOLS_ENABLED
146
#define ADD_CLASS_DEPENDENCY(m_class) ::ClassDB::add_class_dependency(get_class_static(), m_class)
147
#else
148
#define ADD_CLASS_DEPENDENCY(m_class)
149
#endif
150
151
#define ADD_ARRAY_COUNT(m_label, m_count_property, m_count_property_setter, m_count_property_getter, m_prefix) ClassDB::add_property_array_count(get_class_static(), m_label, m_count_property, StringName(m_count_property_setter), StringName(m_count_property_getter), m_prefix)
152
#define ADD_ARRAY_COUNT_WITH_USAGE_FLAGS(m_label, m_count_property, m_count_property_setter, m_count_property_getter, m_prefix, m_property_usage_flags) ClassDB::add_property_array_count(get_class_static(), m_label, m_count_property, StringName(m_count_property_setter), StringName(m_count_property_getter), m_prefix, m_property_usage_flags)
153
#define ADD_ARRAY(m_array_path, m_prefix) ClassDB::add_property_array(get_class_static(), m_array_path, m_prefix)
154
155
// Helper macro to use with PROPERTY_HINT_ARRAY_TYPE for arrays of specific resources:
156
// PropertyInfo(Variant::ARRAY, "fallbacks", PROPERTY_HINT_ARRAY_TYPE, MAKE_RESOURCE_TYPE_HINT("Font")
157
#define MAKE_RESOURCE_TYPE_HINT(m_type) vformat("%s/%s:%s", Variant::OBJECT, PROPERTY_HINT_RESOURCE_TYPE, m_type)
158
159
struct PropertyInfo {
160
Variant::Type type = Variant::NIL;
161
String name;
162
StringName class_name; // For classes
163
PropertyHint hint = PROPERTY_HINT_NONE;
164
String hint_string;
165
uint32_t usage = PROPERTY_USAGE_DEFAULT;
166
167
// If you are thinking about adding another member to this class, ask the maintainer (Juan) first.
168
169
_FORCE_INLINE_ PropertyInfo added_usage(uint32_t p_fl) const {
170
PropertyInfo pi = *this;
171
pi.usage |= p_fl;
172
return pi;
173
}
174
175
operator Dictionary() const;
176
177
static PropertyInfo from_dict(const Dictionary &p_dict);
178
179
PropertyInfo() {}
180
181
PropertyInfo(const Variant::Type p_type, const String &p_name, const PropertyHint p_hint = PROPERTY_HINT_NONE, const String &p_hint_string = "", const uint32_t p_usage = PROPERTY_USAGE_DEFAULT, const StringName &p_class_name = StringName()) :
182
type(p_type),
183
name(p_name),
184
hint(p_hint),
185
hint_string(p_hint_string),
186
usage(p_usage) {
187
if (hint == PROPERTY_HINT_RESOURCE_TYPE) {
188
class_name = hint_string;
189
} else {
190
class_name = p_class_name;
191
}
192
}
193
194
PropertyInfo(const StringName &p_class_name) :
195
type(Variant::OBJECT),
196
class_name(p_class_name) {}
197
198
explicit PropertyInfo(const GDExtensionPropertyInfo &pinfo) :
199
type((Variant::Type)pinfo.type),
200
name(*reinterpret_cast<StringName *>(pinfo.name)),
201
class_name(*reinterpret_cast<StringName *>(pinfo.class_name)),
202
hint((PropertyHint)pinfo.hint),
203
hint_string(*reinterpret_cast<String *>(pinfo.hint_string)),
204
usage(pinfo.usage) {}
205
206
bool operator==(const PropertyInfo &p_info) const {
207
return ((type == p_info.type) &&
208
(name == p_info.name) &&
209
(class_name == p_info.class_name) &&
210
(hint == p_info.hint) &&
211
(hint_string == p_info.hint_string) &&
212
(usage == p_info.usage));
213
}
214
215
bool operator<(const PropertyInfo &p_info) const {
216
return name < p_info.name;
217
}
218
};
219
220
TypedArray<Dictionary> convert_property_list(const List<PropertyInfo> *p_list);
221
TypedArray<Dictionary> convert_property_list(const Vector<PropertyInfo> &p_vector);
222
223
enum MethodFlags {
224
METHOD_FLAG_NORMAL = 1,
225
METHOD_FLAG_EDITOR = 2,
226
METHOD_FLAG_CONST = 4,
227
METHOD_FLAG_VIRTUAL = 8,
228
METHOD_FLAG_VARARG = 16,
229
METHOD_FLAG_STATIC = 32,
230
METHOD_FLAG_OBJECT_CORE = 64,
231
METHOD_FLAG_VIRTUAL_REQUIRED = 128,
232
METHOD_FLAGS_DEFAULT = METHOD_FLAG_NORMAL,
233
};
234
235
struct MethodInfo {
236
String name;
237
PropertyInfo return_val;
238
uint32_t flags = METHOD_FLAGS_DEFAULT;
239
int id = 0;
240
Vector<PropertyInfo> arguments;
241
Vector<Variant> default_arguments;
242
int return_val_metadata = 0;
243
Vector<int> arguments_metadata;
244
245
int get_argument_meta(int p_arg) const {
246
ERR_FAIL_COND_V(p_arg < -1 || p_arg > arguments.size(), 0);
247
if (p_arg == -1) {
248
return return_val_metadata;
249
}
250
return arguments_metadata.size() > p_arg ? arguments_metadata[p_arg] : 0;
251
}
252
253
inline bool operator==(const MethodInfo &p_method) const { return id == p_method.id && name == p_method.name; }
254
inline bool operator<(const MethodInfo &p_method) const { return id == p_method.id ? (name < p_method.name) : (id < p_method.id); }
255
256
operator Dictionary() const;
257
258
static MethodInfo from_dict(const Dictionary &p_dict);
259
260
uint32_t get_compatibility_hash() const;
261
262
MethodInfo() {}
263
264
explicit MethodInfo(const GDExtensionMethodInfo &pinfo) :
265
name(*reinterpret_cast<StringName *>(pinfo.name)),
266
return_val(PropertyInfo(pinfo.return_value)),
267
flags(pinfo.flags),
268
id(pinfo.id) {
269
for (uint32_t i = 0; i < pinfo.argument_count; i++) {
270
arguments.push_back(PropertyInfo(pinfo.arguments[i]));
271
}
272
const Variant *def_values = (const Variant *)pinfo.default_arguments;
273
for (uint32_t j = 0; j < pinfo.default_argument_count; j++) {
274
default_arguments.push_back(def_values[j]);
275
}
276
}
277
278
MethodInfo(const String &p_name) { name = p_name; }
279
280
template <typename... VarArgs>
281
MethodInfo(const String &p_name, VarArgs... p_params) {
282
name = p_name;
283
arguments = Vector<PropertyInfo>{ p_params... };
284
}
285
286
MethodInfo(Variant::Type ret) { return_val.type = ret; }
287
MethodInfo(Variant::Type ret, const String &p_name) {
288
return_val.type = ret;
289
name = p_name;
290
}
291
292
template <typename... VarArgs>
293
MethodInfo(Variant::Type ret, const String &p_name, VarArgs... p_params) {
294
name = p_name;
295
return_val.type = ret;
296
arguments = Vector<PropertyInfo>{ p_params... };
297
}
298
299
MethodInfo(const PropertyInfo &p_ret, const String &p_name) {
300
return_val = p_ret;
301
name = p_name;
302
}
303
304
template <typename... VarArgs>
305
MethodInfo(const PropertyInfo &p_ret, const String &p_name, VarArgs... p_params) {
306
return_val = p_ret;
307
name = p_name;
308
arguments = Vector<PropertyInfo>{ p_params... };
309
}
310
};
311
312
// API used to extend in GDExtension and other C compatible compiled languages.
313
class MethodBind;
314
class GDExtension;
315
316
struct ObjectGDExtension {
317
GDExtension *library = nullptr;
318
ObjectGDExtension *parent = nullptr;
319
List<ObjectGDExtension *> children;
320
StringName parent_class_name;
321
StringName class_name;
322
bool editor_class = false;
323
bool reloadable = false;
324
bool is_virtual = false;
325
bool is_abstract = false;
326
bool is_exposed = true;
327
#ifdef TOOLS_ENABLED
328
bool is_runtime = false;
329
bool is_placeholder = false;
330
#endif
331
#ifndef DISABLE_DEPRECATED
332
bool legacy_unexposed_class = false;
333
#endif // DISABLE_DEPRECATED
334
GDExtensionClassSet set;
335
GDExtensionClassGet get;
336
GDExtensionClassGetPropertyList get_property_list;
337
GDExtensionClassFreePropertyList2 free_property_list2;
338
GDExtensionClassPropertyCanRevert property_can_revert;
339
GDExtensionClassPropertyGetRevert property_get_revert;
340
GDExtensionClassValidateProperty validate_property;
341
#ifndef DISABLE_DEPRECATED
342
GDExtensionClassNotification notification;
343
GDExtensionClassFreePropertyList free_property_list;
344
#endif // DISABLE_DEPRECATED
345
GDExtensionClassNotification2 notification2;
346
GDExtensionClassToString to_string;
347
GDExtensionClassReference reference;
348
GDExtensionClassReference unreference;
349
GDExtensionClassGetRID get_rid;
350
351
void *class_userdata = nullptr;
352
353
#ifndef DISABLE_DEPRECATED
354
GDExtensionClassCreateInstance create_instance;
355
#endif // DISABLE_DEPRECATED
356
GDExtensionClassCreateInstance2 create_instance2;
357
GDExtensionClassFreeInstance free_instance;
358
#ifndef DISABLE_DEPRECATED
359
GDExtensionClassGetVirtual get_virtual;
360
GDExtensionClassGetVirtualCallData get_virtual_call_data;
361
#endif // DISABLE_DEPRECATED
362
GDExtensionClassGetVirtual2 get_virtual2;
363
GDExtensionClassGetVirtualCallData2 get_virtual_call_data2;
364
GDExtensionClassCallVirtualWithData call_virtual_with_data;
365
GDExtensionClassRecreateInstance recreate_instance;
366
367
#ifdef TOOLS_ENABLED
368
void *tracking_userdata = nullptr;
369
void (*track_instance)(void *p_userdata, void *p_instance) = nullptr;
370
void (*untrack_instance)(void *p_userdata, void *p_instance) = nullptr;
371
#endif
372
373
/// A type for this Object extension.
374
/// This is not exposed through the GDExtension API (yet) so it is inferred from above parameters.
375
const GDType *gdtype;
376
void create_gdtype();
377
void destroy_gdtype();
378
379
~ObjectGDExtension();
380
};
381
382
#define GDVIRTUAL_CALL(m_name, ...) _gdvirtual_##m_name##_call(__VA_ARGS__)
383
#define GDVIRTUAL_CALL_PTR(m_obj, m_name, ...) m_obj->_gdvirtual_##m_name##_call(__VA_ARGS__)
384
385
#ifdef DEBUG_ENABLED
386
#define GDVIRTUAL_BIND(m_name, ...) ::ClassDB::add_virtual_method(get_class_static(), _gdvirtual_##m_name##_get_method_info(), true, sarray(__VA_ARGS__));
387
#else
388
#define GDVIRTUAL_BIND(m_name, ...)
389
#endif // DEBUG_ENABLED
390
#define GDVIRTUAL_BIND_COMPAT(m_alias, ...) ::ClassDB::add_virtual_compatibility_method(get_class_static(), _gdvirtual_##m_alias##_get_method_info(), true, sarray(__VA_ARGS__));
391
#define GDVIRTUAL_IS_OVERRIDDEN(m_name) _gdvirtual_##m_name##_overridden()
392
#define GDVIRTUAL_IS_OVERRIDDEN_PTR(m_obj, m_name) m_obj->_gdvirtual_##m_name##_overridden()
393
394
/*
395
* The following is an incomprehensible blob of hacks and workarounds to
396
* compensate for many of the fallacies in C++. As a plus, this macro pretty
397
* much alone defines the object model.
398
*/
399
400
/// `GDSOFTCLASS` provides `Object` functionality, such as being able to use `Object::cast_to()`.
401
/// Use this for `Object` subclasses that are not registered in `ClassDB` (use `GDCLASS` otherwise).
402
#define GDSOFTCLASS(m_class, m_inherits) \
403
public: \
404
using self_type = m_class; \
405
using super_type = m_inherits; \
406
static _FORCE_INLINE_ void *get_class_ptr_static() { \
407
static int ptr; \
408
return &ptr; \
409
} \
410
virtual bool is_class_ptr(void *p_ptr) const override { \
411
return (p_ptr == get_class_ptr_static()) || m_inherits::is_class_ptr(p_ptr); \
412
} \
413
\
414
protected: \
415
_FORCE_INLINE_ bool (Object::*_get_get() const)(const StringName &p_name, Variant &) const { \
416
return (bool (Object::*)(const StringName &, Variant &) const) & m_class::_get; \
417
} \
418
virtual bool _getv(const StringName &p_name, Variant &r_ret) const override { \
419
if (m_class::_get_get() != m_inherits::_get_get()) { \
420
if (_get(p_name, r_ret)) { \
421
return true; \
422
} \
423
} \
424
return m_inherits::_getv(p_name, r_ret); \
425
} \
426
_FORCE_INLINE_ bool (Object::*_get_set() const)(const StringName &p_name, const Variant &p_property) { \
427
return (bool (Object::*)(const StringName &, const Variant &)) & m_class::_set; \
428
} \
429
virtual bool _setv(const StringName &p_name, const Variant &p_property) override { \
430
if (m_inherits::_setv(p_name, p_property)) { \
431
return true; \
432
} \
433
if (m_class::_get_set() != m_inherits::_get_set()) { \
434
return _set(p_name, p_property); \
435
} \
436
return false; \
437
} \
438
_FORCE_INLINE_ void (Object::*_get_validate_property() const)(PropertyInfo & p_property) const { \
439
return (void (Object::*)(PropertyInfo &) const) & m_class::_validate_property; \
440
} \
441
virtual void _validate_propertyv(PropertyInfo &p_property) const override { \
442
m_inherits::_validate_propertyv(p_property); \
443
if (m_class::_get_validate_property() != m_inherits::_get_validate_property()) { \
444
_validate_property(p_property); \
445
} \
446
} \
447
_FORCE_INLINE_ bool (Object::*_get_property_can_revert() const)(const StringName &p_name) const { \
448
return (bool (Object::*)(const StringName &) const) & m_class::_property_can_revert; \
449
} \
450
virtual bool _property_can_revertv(const StringName &p_name) const override { \
451
if (m_class::_get_property_can_revert() != m_inherits::_get_property_can_revert()) { \
452
if (_property_can_revert(p_name)) { \
453
return true; \
454
} \
455
} \
456
return m_inherits::_property_can_revertv(p_name); \
457
} \
458
_FORCE_INLINE_ bool (Object::*_get_property_get_revert() const)(const StringName &p_name, Variant &) const { \
459
return (bool (Object::*)(const StringName &, Variant &) const) & m_class::_property_get_revert; \
460
} \
461
virtual bool _property_get_revertv(const StringName &p_name, Variant &r_ret) const override { \
462
if (m_class::_get_property_get_revert() != m_inherits::_get_property_get_revert()) { \
463
if (_property_get_revert(p_name, r_ret)) { \
464
return true; \
465
} \
466
} \
467
return m_inherits::_property_get_revertv(p_name, r_ret); \
468
} \
469
_FORCE_INLINE_ void (Object::*_get_notification() const)(int) { \
470
return (void (Object::*)(int)) & m_class::_notification; \
471
} \
472
virtual void _notification_forwardv(int p_notification) override { \
473
m_inherits::_notification_forwardv(p_notification); \
474
if (m_class::_get_notification() != m_inherits::_get_notification()) { \
475
_notification(p_notification); \
476
} \
477
} \
478
virtual void _notification_backwardv(int p_notification) override { \
479
if (m_class::_get_notification() != m_inherits::_get_notification()) { \
480
_notification(p_notification); \
481
} \
482
m_inherits::_notification_backwardv(p_notification); \
483
} \
484
\
485
private:
486
487
/// `GDSOFTCLASS` provides `Object` functionality, such as being able to use `Object::cast_to()`.
488
/// Use this for `Object` subclasses that are registered in `ObjectDB` (use `GDSOFTCLASS` otherwise).
489
#define GDCLASS(m_class, m_inherits) \
490
GDSOFTCLASS(m_class, m_inherits) \
491
private: \
492
void operator=(const m_class &p_rval) {} \
493
friend class ::ClassDB; \
494
\
495
public: \
496
virtual const GDType &_get_typev() const override { \
497
return get_gdtype_static(); \
498
} \
499
static const GDType &get_gdtype_static() { \
500
static GDType *_class_static; \
501
if (unlikely(!_class_static)) { \
502
assign_type_static(&_class_static, #m_class, &super_type::get_gdtype_static()); \
503
} \
504
return *_class_static; \
505
} \
506
static const StringName &get_class_static() { \
507
return get_gdtype_static().get_name(); \
508
} \
509
\
510
protected: \
511
_FORCE_INLINE_ static void (*_get_bind_methods())() { \
512
return &m_class::_bind_methods; \
513
} \
514
_FORCE_INLINE_ static void (*_get_bind_compatibility_methods())() { \
515
return &m_class::_bind_compatibility_methods; \
516
} \
517
\
518
public: \
519
static void initialize_class() { \
520
static bool initialized = false; \
521
if (initialized) { \
522
return; \
523
} \
524
m_inherits::initialize_class(); \
525
_add_class_to_classdb(get_gdtype_static(), &super_type::get_gdtype_static()); \
526
if (m_class::_get_bind_methods() != m_inherits::_get_bind_methods()) { \
527
_bind_methods(); \
528
} \
529
if (m_class::_get_bind_compatibility_methods() != m_inherits::_get_bind_compatibility_methods()) { \
530
_bind_compatibility_methods(); \
531
} \
532
initialized = true; \
533
} \
534
\
535
protected: \
536
virtual void _initialize_classv() override { \
537
initialize_class(); \
538
} \
539
_FORCE_INLINE_ void (Object::*_get_get_property_list() const)(List<PropertyInfo> * p_list) const { \
540
return (void (Object::*)(List<PropertyInfo> *) const) & m_class::_get_property_list; \
541
} \
542
virtual void _get_property_listv(List<PropertyInfo> *p_list, bool p_reversed) const override { \
543
if (!p_reversed) { \
544
m_inherits::_get_property_listv(p_list, p_reversed); \
545
} \
546
p_list->push_back(PropertyInfo(Variant::NIL, get_class_static(), PROPERTY_HINT_NONE, get_class_static(), PROPERTY_USAGE_CATEGORY)); \
547
_get_property_list_from_classdb(#m_class, p_list, true, this); \
548
if (m_class::_get_get_property_list() != m_inherits::_get_get_property_list()) { \
549
_get_property_list(p_list); \
550
} \
551
if (p_reversed) { \
552
m_inherits::_get_property_listv(p_list, p_reversed); \
553
} \
554
} \
555
\
556
private:
557
558
#define OBJ_SAVE_TYPE(m_class) \
559
public: \
560
virtual String get_save_class() const override { \
561
return #m_class; \
562
} \
563
\
564
private:
565
566
class ClassDB;
567
class ScriptInstance;
568
569
class Object {
570
public:
571
typedef Object self_type;
572
573
enum ConnectFlags {
574
CONNECT_DEFERRED = 1,
575
CONNECT_PERSIST = 2, // Hint for scene to save this connection.
576
CONNECT_ONE_SHOT = 4,
577
CONNECT_REFERENCE_COUNTED = 8,
578
CONNECT_APPEND_SOURCE_OBJECT = 16,
579
CONNECT_INHERITED = 32, // Used in editor builds.
580
};
581
582
// Store on each object a bitfield to quickly test whether it is derived from some "key" classes
583
// that are commonly tested in performance sensitive code.
584
// Ensure unsigned to bitpack.
585
enum class AncestralClass : unsigned int {
586
REF_COUNTED = 1 << 0,
587
NODE = 1 << 1,
588
RESOURCE = 1 << 2,
589
SCRIPT = 1 << 3,
590
591
CANVAS_ITEM = 1 << 4,
592
CONTROL = 1 << 5,
593
NODE_2D = 1 << 6,
594
COLLISION_OBJECT_2D = 1 << 7,
595
AREA_2D = 1 << 8,
596
597
NODE_3D = 1 << 9,
598
VISUAL_INSTANCE_3D = 1 << 10,
599
GEOMETRY_INSTANCE_3D = 1 << 11,
600
COLLISION_OBJECT_3D = 1 << 12,
601
PHYSICS_BODY_3D = 1 << 13,
602
MESH_INSTANCE_3D = 1 << 14,
603
};
604
605
static constexpr AncestralClass static_ancestral_class = (AncestralClass)0;
606
607
struct Connection {
608
::Signal signal;
609
Callable callable;
610
611
uint32_t flags = 0;
612
bool operator<(const Connection &p_conn) const;
613
614
operator Variant() const;
615
616
Connection() {}
617
Connection(const Variant &p_variant);
618
};
619
620
private:
621
#ifdef DEBUG_ENABLED
622
friend struct _ObjectDebugLock;
623
#endif // DEBUG_ENABLED
624
friend bool predelete_handler(Object *);
625
friend void postinitialize_handler(Object *);
626
627
ObjectGDExtension *_extension = nullptr;
628
GDExtensionClassInstancePtr _extension_instance = nullptr;
629
630
struct SignalData {
631
struct Slot {
632
int reference_count = 0;
633
Connection conn;
634
List<Connection>::Element *cE = nullptr;
635
};
636
637
MethodInfo user;
638
HashMap<Callable, Slot> slot_map;
639
bool removable = false;
640
};
641
friend struct _ObjectSignalLock;
642
mutable Mutex *signal_mutex = nullptr;
643
HashMap<StringName, SignalData> signal_map;
644
List<Connection> connections;
645
#ifdef DEBUG_ENABLED
646
SafeRefCount _lock_index;
647
#endif // DEBUG_ENABLED
648
ObjectID _instance_id;
649
bool _predelete();
650
void _initialize();
651
void _postinitialize();
652
653
uint32_t _ancestry : 15;
654
655
bool _block_signals : 1;
656
bool _can_translate : 1;
657
bool _emitting : 1;
658
bool _predelete_ok : 1;
659
660
public:
661
bool _is_queued_for_deletion : 1; // Set to true by SceneTree::queue_delete().
662
663
private:
664
#ifdef TOOLS_ENABLED
665
bool _edited : 1;
666
uint32_t _edited_version = 0;
667
HashSet<String> editor_section_folding;
668
#endif
669
ScriptInstance *script_instance = nullptr;
670
HashMap<StringName, Variant> metadata;
671
HashMap<StringName, Variant *> metadata_properties;
672
mutable const GDType *_gdtype_ptr = nullptr;
673
void _reset_gdtype() const;
674
675
void _add_user_signal(const String &p_name, const Array &p_args = Array());
676
bool _has_user_signal(const StringName &p_name) const;
677
void _remove_user_signal(const StringName &p_name);
678
Error _emit_signal(const Variant **p_args, int p_argcount, Callable::CallError &r_error);
679
TypedArray<Dictionary> _get_signal_list() const;
680
TypedArray<Dictionary> _get_signal_connection_list(const StringName &p_signal) const;
681
TypedArray<Dictionary> _get_incoming_connections() const;
682
void _set_bind(const StringName &p_set, const Variant &p_value);
683
Variant _get_bind(const StringName &p_name) const;
684
void _set_indexed_bind(const NodePath &p_name, const Variant &p_value);
685
Variant _get_indexed_bind(const NodePath &p_name) const;
686
int _get_method_argument_count_bind(const StringName &p_name) const;
687
688
_FORCE_INLINE_ void _construct_object(bool p_reference);
689
690
friend class RefCounted;
691
692
BinaryMutex _instance_binding_mutex;
693
struct InstanceBinding {
694
void *binding = nullptr;
695
void *token = nullptr;
696
GDExtensionInstanceBindingFreeCallback free_callback = nullptr;
697
GDExtensionInstanceBindingReferenceCallback reference_callback = nullptr;
698
};
699
InstanceBinding *_instance_bindings = nullptr;
700
uint32_t _instance_binding_count = 0;
701
702
Object(bool p_reference);
703
704
protected:
705
StringName _translation_domain;
706
707
_FORCE_INLINE_ bool _instance_binding_reference(bool p_reference) {
708
bool can_die = true;
709
if (_instance_bindings) {
710
MutexLock instance_binding_lock(_instance_binding_mutex);
711
for (uint32_t i = 0; i < _instance_binding_count; i++) {
712
if (_instance_bindings[i].reference_callback) {
713
if (!_instance_bindings[i].reference_callback(_instance_bindings[i].token, _instance_bindings[i].binding, p_reference)) {
714
can_die = false;
715
}
716
}
717
}
718
}
719
return can_die;
720
}
721
722
// Used in gdvirtual.gen.h
723
void _gdvirtual_init_method_ptr(uint32_t p_compat_hash, void *&r_fn_ptr, const StringName &p_fn_name, bool p_compat) const;
724
725
friend class GDExtensionMethodBind;
726
_ALWAYS_INLINE_ const ObjectGDExtension *_get_extension() const { return _extension; }
727
_ALWAYS_INLINE_ GDExtensionClassInstancePtr _get_extension_instance() const { return _extension_instance; }
728
virtual void _initialize_classv() { initialize_class(); }
729
virtual bool _setv(const StringName &p_name, const Variant &p_property) { return false; }
730
virtual bool _getv(const StringName &p_name, Variant &r_property) const { return false; }
731
virtual void _get_property_listv(List<PropertyInfo> *p_list, bool p_reversed) const {}
732
virtual void _validate_propertyv(PropertyInfo &p_property) const {}
733
virtual bool _property_can_revertv(const StringName &p_name) const { return false; }
734
virtual bool _property_get_revertv(const StringName &p_name, Variant &r_property) const { return false; }
735
736
void _notification_forward(int p_notification);
737
void _notification_backward(int p_notification);
738
virtual void _notification_forwardv(int p_notification) {}
739
virtual void _notification_backwardv(int p_notification) {}
740
virtual String _to_string();
741
742
static void _bind_methods();
743
static void _bind_compatibility_methods() {}
744
bool _set(const StringName &p_name, const Variant &p_property) { return false; }
745
bool _get(const StringName &p_name, Variant &r_property) const { return false; }
746
void _get_property_list(List<PropertyInfo> *p_list) const {}
747
void _validate_property(PropertyInfo &p_property) const {}
748
bool _property_can_revert(const StringName &p_name) const { return false; }
749
bool _property_get_revert(const StringName &p_name, Variant &r_property) const { return false; }
750
void _notification(int p_notification) {}
751
752
_FORCE_INLINE_ static void (*_get_bind_methods())() {
753
return &Object::_bind_methods;
754
}
755
_FORCE_INLINE_ static void (*_get_bind_compatibility_methods())() {
756
return &Object::_bind_compatibility_methods;
757
}
758
_FORCE_INLINE_ bool (Object::*_get_get() const)(const StringName &p_name, Variant &r_ret) const {
759
return &Object::_get;
760
}
761
_FORCE_INLINE_ bool (Object::*_get_set() const)(const StringName &p_name, const Variant &p_property) {
762
return &Object::_set;
763
}
764
_FORCE_INLINE_ void (Object::*_get_get_property_list() const)(List<PropertyInfo> *p_list) const {
765
return &Object::_get_property_list;
766
}
767
_FORCE_INLINE_ void (Object::*_get_validate_property() const)(PropertyInfo &p_property) const {
768
return &Object::_validate_property;
769
}
770
_FORCE_INLINE_ bool (Object::*_get_property_can_revert() const)(const StringName &p_name) const {
771
return &Object::_property_can_revert;
772
}
773
_FORCE_INLINE_ bool (Object::*_get_property_get_revert() const)(const StringName &p_name, Variant &) const {
774
return &Object::_property_get_revert;
775
}
776
_FORCE_INLINE_ void (Object::*_get_notification() const)(int) {
777
return &Object::_notification;
778
}
779
780
Variant _call_bind(const Variant **p_args, int p_argcount, Callable::CallError &r_error);
781
Variant _call_deferred_bind(const Variant **p_args, int p_argcount, Callable::CallError &r_error);
782
783
virtual const GDType &_get_typev() const { return get_gdtype_static(); }
784
785
TypedArray<StringName> _get_meta_list_bind() const;
786
TypedArray<Dictionary> _get_property_list_bind() const;
787
TypedArray<Dictionary> _get_method_list_bind() const;
788
789
void _clear_internal_resource_paths(const Variant &p_var);
790
791
friend class ::ClassDB;
792
friend class PlaceholderExtensionInstance;
793
794
static void _add_class_to_classdb(const GDType &p_class, const GDType *p_inherits);
795
static void _get_property_list_from_classdb(const StringName &p_class, List<PropertyInfo> *p_list, bool p_no_inheritance, const Object *p_validator);
796
797
bool _disconnect(const StringName &p_signal, const Callable &p_callable, bool p_force = false);
798
void _define_ancestry(AncestralClass p_class) { _ancestry |= (uint32_t)p_class; }
799
// Prefer using derives_from.
800
bool _has_ancestry(AncestralClass p_class) const { return _ancestry & (uint32_t)p_class; }
801
802
virtual bool _uses_signal_mutex() const;
803
804
// Internal helper to get the current locale, taking into account the translation domain.
805
String _get_locale() const;
806
807
#ifdef TOOLS_ENABLED
808
struct VirtualMethodTracker {
809
void **method;
810
VirtualMethodTracker *next;
811
};
812
813
mutable VirtualMethodTracker *virtual_method_list = nullptr;
814
#endif
815
816
public: // Should be protected, but bug in clang++.
817
static void initialize_class();
818
_FORCE_INLINE_ static void register_custom_data_to_otdb() {}
819
820
public:
821
void notify_property_list_changed();
822
823
static void *get_class_ptr_static() {
824
static int ptr;
825
return &ptr;
826
}
827
828
void detach_from_objectdb();
829
_FORCE_INLINE_ ObjectID get_instance_id() const { return _instance_id; }
830
831
template <typename T, typename O>
832
static T *cast_to(O *p_object) {
833
// This is like dynamic_cast, but faster.
834
// The reason is that we can assume no virtual and multiple inheritance.
835
return p_object && p_object->template derives_from<T, O>() ? static_cast<T *>(p_object) : nullptr;
836
}
837
838
template <typename T, typename O>
839
static const T *cast_to(const O *p_object) {
840
return p_object && p_object->template derives_from<T, O>() ? static_cast<const T *>(p_object) : nullptr;
841
}
842
843
// cast_to versions for types that implicitly convert to Object.
844
template <typename T>
845
static T *cast_to(Object *p_object) {
846
return p_object && p_object->template derives_from<T, Object>() ? static_cast<T *>(p_object) : nullptr;
847
}
848
849
template <typename T>
850
static const T *cast_to(const Object *p_object) {
851
return p_object && p_object->template derives_from<T, Object>() ? static_cast<const T *>(p_object) : nullptr;
852
}
853
854
enum {
855
NOTIFICATION_POSTINITIALIZE = 0,
856
NOTIFICATION_PREDELETE = 1,
857
NOTIFICATION_EXTENSION_RELOADED = 2,
858
// Internal notification to send after NOTIFICATION_PREDELETE, not bound to scripting.
859
NOTIFICATION_PREDELETE_CLEANUP = 3,
860
};
861
862
/* TYPE API */
863
static void assign_type_static(GDType **type_ptr, const char *p_name, const GDType *super_type);
864
865
static const GDType &get_gdtype_static() {
866
static GDType *_class_static;
867
if (unlikely(!_class_static)) {
868
assign_type_static(&_class_static, "Object", nullptr);
869
}
870
return *_class_static;
871
}
872
873
const GDType &get_gdtype() const;
874
875
static const StringName &get_class_static() { return get_gdtype_static().get_name(); }
876
877
_FORCE_INLINE_ String get_class() const { return get_class_name(); }
878
879
virtual String get_save_class() const { return get_class(); } //class stored when saving
880
881
bool is_class(const String &p_class) const;
882
virtual bool is_class_ptr(void *p_ptr) const { return get_class_ptr_static() == p_ptr; }
883
884
template <typename T, typename O>
885
bool derives_from() const;
886
887
const StringName &get_class_name() const;
888
889
StringName get_class_name_for_extension(const GDExtension *p_library) const;
890
891
/* IAPI */
892
893
void set(const StringName &p_name, const Variant &p_value, bool *r_valid = nullptr);
894
Variant get(const StringName &p_name, bool *r_valid = nullptr) const;
895
void set_indexed(const Vector<StringName> &p_names, const Variant &p_value, bool *r_valid = nullptr);
896
Variant get_indexed(const Vector<StringName> &p_names, bool *r_valid = nullptr) const;
897
898
void get_property_list(List<PropertyInfo> *p_list, bool p_reversed = false) const;
899
void validate_property(PropertyInfo &p_property) const;
900
bool property_can_revert(const StringName &p_name) const;
901
Variant property_get_revert(const StringName &p_name) const;
902
903
bool has_method(const StringName &p_method) const;
904
int get_method_argument_count(const StringName &p_method, bool *r_is_valid = nullptr) const;
905
void get_method_list(List<MethodInfo> *p_list) const;
906
Variant callv(const StringName &p_method, const Array &p_args);
907
virtual Variant callp(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error);
908
virtual Variant call_const(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error);
909
910
template <typename... VarArgs>
911
Variant call(const StringName &p_method, VarArgs... p_args) {
912
Variant args[sizeof...(p_args) + 1] = { p_args..., Variant() }; // +1 makes sure zero sized arrays are also supported.
913
const Variant *argptrs[sizeof...(p_args) + 1];
914
for (uint32_t i = 0; i < sizeof...(p_args); i++) {
915
argptrs[i] = &args[i];
916
}
917
Callable::CallError cerr;
918
const Variant ret = callp(p_method, sizeof...(p_args) == 0 ? nullptr : (const Variant **)argptrs, sizeof...(p_args), cerr);
919
return (cerr.error == Callable::CallError::CALL_OK) ? ret : Variant();
920
}
921
922
// Depending on the boolean, we call either the virtual function _notification_backward or _notification_forward.
923
// - Forward calls subclasses in descending order (e.g. Object -> Node -> Node3D -> extension -> script).
924
// Backward calls subclasses in descending order (e.g. script -> extension -> Node3D -> Node -> Object).
925
_FORCE_INLINE_ void notification(int p_notification, bool p_reversed = false) {
926
if (p_reversed) {
927
_notification_backward(p_notification);
928
} else {
929
_notification_forward(p_notification);
930
}
931
}
932
933
String to_string();
934
935
// Used mainly by script, get and set all INCLUDING string.
936
virtual Variant getvar(const Variant &p_key, bool *r_valid = nullptr) const;
937
virtual void setvar(const Variant &p_key, const Variant &p_value, bool *r_valid = nullptr);
938
939
/* SCRIPT */
940
941
// When in debug, some non-virtual functions can be overridden.
942
#ifdef DEBUG_ENABLED
943
#define DEBUG_VIRTUAL virtual
944
#else
945
#define DEBUG_VIRTUAL
946
#endif // DEBUG_ENABLED
947
948
DEBUG_VIRTUAL void set_script(const Variant &p_script);
949
DEBUG_VIRTUAL Variant get_script() const;
950
951
DEBUG_VIRTUAL bool has_meta(const StringName &p_name) const;
952
DEBUG_VIRTUAL void set_meta(const StringName &p_name, const Variant &p_value);
953
DEBUG_VIRTUAL void remove_meta(const StringName &p_name);
954
DEBUG_VIRTUAL Variant get_meta(const StringName &p_name, const Variant &p_default = Variant()) const;
955
DEBUG_VIRTUAL void get_meta_list(List<StringName> *p_list) const;
956
DEBUG_VIRTUAL void merge_meta_from(const Object *p_src);
957
958
#ifdef TOOLS_ENABLED
959
void set_edited(bool p_edited);
960
bool is_edited() const;
961
// This function is used to check when something changed beyond a point, it's used mainly for generating previews.
962
uint32_t get_edited_version() const;
963
#endif
964
965
void set_script_instance(ScriptInstance *p_instance);
966
_FORCE_INLINE_ ScriptInstance *get_script_instance() const { return script_instance; }
967
968
void add_user_signal(const MethodInfo &p_signal);
969
970
template <typename... VarArgs>
971
Error emit_signal(const StringName &p_name, VarArgs... p_args) {
972
Variant args[sizeof...(p_args) + 1] = { p_args..., Variant() }; // +1 makes sure zero sized arrays are also supported.
973
const Variant *argptrs[sizeof...(p_args) + 1];
974
for (uint32_t i = 0; i < sizeof...(p_args); i++) {
975
argptrs[i] = &args[i];
976
}
977
return emit_signalp(p_name, sizeof...(p_args) == 0 ? nullptr : (const Variant **)argptrs, sizeof...(p_args));
978
}
979
980
DEBUG_VIRTUAL Error emit_signalp(const StringName &p_name, const Variant **p_args, int p_argcount);
981
DEBUG_VIRTUAL bool has_signal(const StringName &p_name) const;
982
DEBUG_VIRTUAL void get_signal_list(List<MethodInfo> *p_signals) const;
983
DEBUG_VIRTUAL void get_signal_connection_list(const StringName &p_signal, List<Connection> *p_connections) const;
984
DEBUG_VIRTUAL void get_all_signal_connections(List<Connection> *p_connections) const;
985
DEBUG_VIRTUAL int get_persistent_signal_connection_count() const;
986
DEBUG_VIRTUAL uint32_t get_signal_connection_flags(const StringName &p_name, const Callable &p_callable) const;
987
DEBUG_VIRTUAL void get_signals_connected_to_this(List<Connection> *p_connections) const;
988
989
DEBUG_VIRTUAL Error connect(const StringName &p_signal, const Callable &p_callable, uint32_t p_flags = 0);
990
DEBUG_VIRTUAL void disconnect(const StringName &p_signal, const Callable &p_callable);
991
DEBUG_VIRTUAL bool is_connected(const StringName &p_signal, const Callable &p_callable) const;
992
DEBUG_VIRTUAL bool has_connections(const StringName &p_signal) const;
993
994
template <typename... VarArgs>
995
void call_deferred(const StringName &p_name, VarArgs... p_args) {
996
MessageQueue::get_singleton()->push_call(this, p_name, p_args...);
997
}
998
999
void set_deferred(const StringName &p_property, const Variant &p_value);
1000
1001
void set_block_signals(bool p_block);
1002
bool is_blocking_signals() const;
1003
1004
Variant::Type get_static_property_type(const StringName &p_property, bool *r_valid = nullptr) const;
1005
Variant::Type get_static_property_type_indexed(const Vector<StringName> &p_path, bool *r_valid = nullptr) const;
1006
1007
// Translate message (internationalization).
1008
String tr(const StringName &p_message, const StringName &p_context = "") const;
1009
String tr_n(const StringName &p_message, const StringName &p_message_plural, int p_n, const StringName &p_context = "") const;
1010
1011
bool is_queued_for_deletion() const;
1012
1013
_FORCE_INLINE_ void set_message_translation(bool p_enable) { _can_translate = p_enable; }
1014
_FORCE_INLINE_ bool can_translate_messages() const { return _can_translate; }
1015
1016
virtual StringName get_translation_domain() const;
1017
virtual void set_translation_domain(const StringName &p_domain);
1018
1019
#ifdef TOOLS_ENABLED
1020
virtual void get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const;
1021
void editor_set_section_unfold(const String &p_section, bool p_unfolded, bool p_initializing = false);
1022
bool editor_is_section_unfolded(const String &p_section);
1023
const HashSet<String> &editor_get_section_folding() const { return editor_section_folding; }
1024
void editor_clear_section_folding() { editor_section_folding.clear(); }
1025
#endif
1026
1027
// Used by script languages to store binding data.
1028
void *get_instance_binding(void *p_token, const GDExtensionInstanceBindingCallbacks *p_callbacks);
1029
// Used on creation by binding only.
1030
void set_instance_binding(void *p_token, void *p_binding, const GDExtensionInstanceBindingCallbacks *p_callbacks);
1031
bool has_instance_binding(void *p_token);
1032
void free_instance_binding(void *p_token);
1033
1034
#ifdef TOOLS_ENABLED
1035
void clear_internal_extension();
1036
void reset_internal_extension(ObjectGDExtension *p_extension);
1037
bool is_extension_placeholder() const { return _extension && _extension->is_placeholder; }
1038
#endif
1039
1040
void clear_internal_resource_paths();
1041
1042
_ALWAYS_INLINE_ bool is_ref_counted() const { return _has_ancestry(AncestralClass::REF_COUNTED); }
1043
1044
void cancel_free();
1045
1046
Object();
1047
virtual ~Object();
1048
};
1049
1050
bool predelete_handler(Object *p_object);
1051
void postinitialize_handler(Object *p_object);
1052
1053
template <typename T, typename O>
1054
bool Object::derives_from() const {
1055
if constexpr (std::is_base_of_v<T, O>) {
1056
// We derive statically from T (or are the same class), so casting to it is trivial.
1057
return true;
1058
} else {
1059
static_assert(std::is_base_of_v<Object, O>, "derives_from can only be used with Object subclasses.");
1060
static_assert(std::is_base_of_v<O, T>, "Cannot cast argument to T because T does not derive from the argument's known class.");
1061
static_assert(std::is_same_v<std::decay_t<T>, typename T::self_type>, "T must use GDCLASS or GDSOFTCLASS.");
1062
1063
// If there is an explicitly set ancestral class on the type, we can use that.
1064
if constexpr (T::static_ancestral_class != T::super_type::static_ancestral_class) {
1065
return _has_ancestry(T::static_ancestral_class);
1066
} else {
1067
return is_class_ptr(T::get_class_ptr_static());
1068
}
1069
}
1070
}
1071
1072
class ObjectDB {
1073
// This needs to add up to 63, 1 bit is for reference.
1074
#define OBJECTDB_VALIDATOR_BITS 39
1075
#define OBJECTDB_VALIDATOR_MASK ((uint64_t(1) << OBJECTDB_VALIDATOR_BITS) - 1)
1076
#define OBJECTDB_SLOT_MAX_COUNT_BITS 24
1077
#define OBJECTDB_SLOT_MAX_COUNT_MASK ((uint64_t(1) << OBJECTDB_SLOT_MAX_COUNT_BITS) - 1)
1078
#define OBJECTDB_REFERENCE_BIT (uint64_t(1) << (OBJECTDB_SLOT_MAX_COUNT_BITS + OBJECTDB_VALIDATOR_BITS))
1079
1080
struct ObjectSlot { // 128 bits per slot.
1081
uint64_t validator : OBJECTDB_VALIDATOR_BITS;
1082
uint64_t next_free : OBJECTDB_SLOT_MAX_COUNT_BITS;
1083
uint64_t is_ref_counted : 1;
1084
Object *object = nullptr;
1085
};
1086
1087
static SpinLock spin_lock;
1088
static uint32_t slot_count;
1089
static uint32_t slot_max;
1090
static ObjectSlot *object_slots;
1091
static uint64_t validator_counter;
1092
1093
friend class Object;
1094
friend void unregister_core_types();
1095
static void cleanup();
1096
1097
static ObjectID add_instance(Object *p_object);
1098
static void remove_instance(Object *p_object);
1099
1100
friend void register_core_types();
1101
static void setup();
1102
1103
public:
1104
typedef void (*DebugFunc)(Object *p_obj, void *p_user_data);
1105
1106
_ALWAYS_INLINE_ static Object *get_instance(ObjectID p_instance_id) {
1107
uint64_t id = p_instance_id;
1108
uint32_t slot = id & OBJECTDB_SLOT_MAX_COUNT_MASK;
1109
1110
ERR_FAIL_COND_V(slot >= slot_max, nullptr); // This should never happen unless RID is corrupted.
1111
1112
spin_lock.lock();
1113
1114
uint64_t validator = (id >> OBJECTDB_SLOT_MAX_COUNT_BITS) & OBJECTDB_VALIDATOR_MASK;
1115
1116
if (unlikely(object_slots[slot].validator != validator)) {
1117
spin_lock.unlock();
1118
return nullptr;
1119
}
1120
1121
Object *object = object_slots[slot].object;
1122
1123
spin_lock.unlock();
1124
1125
return object;
1126
}
1127
1128
template <typename T>
1129
_ALWAYS_INLINE_ static T *get_instance(ObjectID p_instance_id) {
1130
return Object::cast_to<T>(get_instance(p_instance_id));
1131
}
1132
1133
template <typename T>
1134
_ALWAYS_INLINE_ static Ref<T> get_ref(ObjectID p_instance_id); // Defined in ref_counted.h
1135
1136
static void debug_objects(DebugFunc p_func, void *p_user_data);
1137
static int get_object_count();
1138
};
1139
1140
// Using `RequiredResult<T>` as the return type indicates that null will only be returned in the case of an error.
1141
// This allows GDExtension language bindings to use the appropriate error handling mechanism for that language
1142
// when null is returned (for example, throwing an exception), rather than simply returning the value.
1143
template <typename T>
1144
class RequiredResult {
1145
static_assert(!is_fully_defined_v<T> || std::is_base_of_v<Object, T>, "T must be an Object subtype");
1146
1147
public:
1148
using element_type = T;
1149
using ptr_type = std::conditional_t<std::is_base_of_v<RefCounted, T>, Ref<T>, T *>;
1150
1151
private:
1152
ptr_type _value = ptr_type();
1153
1154
public:
1155
_FORCE_INLINE_ RequiredResult() = default;
1156
1157
RequiredResult(const RequiredResult &p_other) = default;
1158
RequiredResult(RequiredResult &&p_other) = default;
1159
RequiredResult &operator=(const RequiredResult &p_other) = default;
1160
RequiredResult &operator=(RequiredResult &&p_other) = default;
1161
1162
_FORCE_INLINE_ RequiredResult(std::nullptr_t) :
1163
RequiredResult() {}
1164
_FORCE_INLINE_ RequiredResult &operator=(std::nullptr_t) { _value = nullptr; }
1165
1166
// These functions should not be called directly, they are only for internal use.
1167
_FORCE_INLINE_ ptr_type _internal_ptr_dont_use() const { return _value; }
1168
_FORCE_INLINE_ static RequiredResult<T> _err_return_dont_use() { return RequiredResult<T>(); }
1169
1170
template <typename T_Other, std::enable_if_t<std::is_base_of_v<T, T_Other>, int> = 0>
1171
_FORCE_INLINE_ RequiredResult(const RequiredResult<T_Other> &p_other) :
1172
_value(p_other._value) {}
1173
template <typename T_Other, std::enable_if_t<std::is_base_of_v<T, T_Other>, int> = 0>
1174
_FORCE_INLINE_ RequiredResult &operator=(const RequiredResult<T_Other> &p_other) {
1175
_value = p_other._value;
1176
return *this;
1177
}
1178
1179
template <typename T_Other, std::enable_if_t<std::is_base_of_v<T, T_Other>, int> = 0>
1180
_FORCE_INLINE_ RequiredResult(T_Other *p_ptr) :
1181
_value(p_ptr) {}
1182
template <typename T_Other, std::enable_if_t<std::is_base_of_v<T, T_Other>, int> = 0>
1183
_FORCE_INLINE_ RequiredResult &operator=(T_Other *p_ptr) {
1184
_value = p_ptr;
1185
return *this;
1186
}
1187
1188
template <typename T_Other, std::enable_if_t<std::is_base_of_v<T, T_Other>, int> = 0>
1189
_FORCE_INLINE_ RequiredResult(const Ref<T_Other> &p_ref) :
1190
_value(p_ref) {}
1191
template <typename T_Other, std::enable_if_t<std::is_base_of_v<T, T_Other>, int> = 0>
1192
_FORCE_INLINE_ RequiredResult &operator=(const Ref<T_Other> &p_ref) {
1193
_value = p_ref;
1194
return *this;
1195
}
1196
1197
template <typename T_Other, std::enable_if_t<std::is_base_of_v<T, T_Other>, int> = 0>
1198
_FORCE_INLINE_ RequiredResult(Ref<T_Other> &&p_ref) :
1199
_value(std::move(p_ref)) {}
1200
template <typename T_Other, std::enable_if_t<std::is_base_of_v<T, T_Other>, int> = 0>
1201
_FORCE_INLINE_ RequiredResult &operator=(Ref<T_Other> &&p_ref) {
1202
_value = std::move(p_ref);
1203
return &this;
1204
}
1205
1206
template <typename U = T, std::enable_if_t<std::is_base_of_v<RefCounted, U>, int> = 0>
1207
_FORCE_INLINE_ RequiredResult(const Variant &p_variant) :
1208
_value(Object::cast_to<T>(p_variant.get_validated_object())) {}
1209
template <typename U = T, std::enable_if_t<std::is_base_of_v<RefCounted, U>, int> = 0>
1210
_FORCE_INLINE_ RequiredResult &operator=(const Variant &p_variant) {
1211
_value = Object::cast_to<T>(p_variant.get_validated_object());
1212
return *this;
1213
}
1214
1215
template <typename U = T, std::enable_if_t<!std::is_base_of_v<RefCounted, U>, int> = 0>
1216
_FORCE_INLINE_ RequiredResult(const Variant &p_variant) :
1217
_value(Object::cast_to<T>(p_variant.operator Object *())) {}
1218
template <typename U = T, std::enable_if_t<!std::is_base_of_v<RefCounted, U>, int> = 0>
1219
_FORCE_INLINE_ RequiredResult &operator=(const Variant &p_variant) {
1220
_value = Object::cast_to<T>(p_variant.operator Object *());
1221
return *this;
1222
}
1223
1224
template <typename U = T, std::enable_if_t<std::is_base_of_v<RefCounted, U>, int> = 0>
1225
_FORCE_INLINE_ element_type *ptr() const {
1226
return *_value;
1227
}
1228
1229
template <typename U = T, std::enable_if_t<!std::is_base_of_v<RefCounted, U>, int> = 0>
1230
_FORCE_INLINE_ element_type *ptr() const {
1231
return _value;
1232
}
1233
1234
_FORCE_INLINE_ operator ptr_type() const {
1235
return _value;
1236
}
1237
1238
template <typename U = T, typename T_Other, std::enable_if_t<std::is_base_of_v<RefCounted, U> && std::is_base_of_v<U, T_Other>, int> = 0>
1239
_FORCE_INLINE_ operator Ref<T_Other>() const {
1240
return Ref<T_Other>(_value);
1241
}
1242
1243
_FORCE_INLINE_ element_type *operator*() const {
1244
return ptr();
1245
}
1246
1247
_FORCE_INLINE_ element_type *operator->() const {
1248
return ptr();
1249
}
1250
};
1251
1252
// Using `RequiredParam<T>` as an argument type indicates that passing null as that parameter is an error,
1253
// that will prevent the method from doing its intended function.
1254
// This allows GDExtension bindings to use language-specific mechanisms to prevent users from passing null,
1255
// because it is never valid to do so.
1256
template <typename T>
1257
class RequiredParam {
1258
static_assert(!is_fully_defined_v<T> || std::is_base_of_v<Object, T>, "T must be an Object subtype");
1259
1260
public:
1261
static constexpr bool is_ref = std::is_base_of_v<RefCounted, T>;
1262
1263
using element_type = T;
1264
using extracted_type = std::conditional_t<is_ref, const Ref<T> &, T *>;
1265
using persistent_type = std::conditional_t<is_ref, Ref<T>, T *>;
1266
1267
private:
1268
T *_value = nullptr;
1269
1270
_FORCE_INLINE_ RequiredParam() = default;
1271
1272
public:
1273
// These functions should not be called directly, they are only for internal use.
1274
_FORCE_INLINE_ extracted_type _internal_ptr_dont_use() const {
1275
if constexpr (is_ref) {
1276
// Pretend _value is a Ref, for ease of use with existing `const Ref &` accepting APIs.
1277
// This only works as long as Ref is internally T *.
1278
// The double indirection should be optimized away by the compiler.
1279
static_assert(sizeof(Ref<T>) == sizeof(T *));
1280
return *((const Ref<T> *)&_value);
1281
} else {
1282
return _value;
1283
}
1284
}
1285
_FORCE_INLINE_ bool _is_null_dont_use() const { return _value == nullptr; }
1286
_FORCE_INLINE_ static RequiredParam<T> _err_return_dont_use() { return RequiredParam<T>(); }
1287
1288
// Prevent erroneously assigning null values by explicitly removing nullptr constructor/assignment.
1289
RequiredParam(std::nullptr_t) = delete;
1290
RequiredParam &operator=(std::nullptr_t) = delete;
1291
1292
RequiredParam(const RequiredParam &p_other) = default;
1293
RequiredParam(RequiredParam &&p_other) = default;
1294
RequiredParam &operator=(const RequiredParam &p_other) = default;
1295
RequiredParam &operator=(RequiredParam &&p_other) = default;
1296
1297
template <typename T_Other, std::enable_if_t<std::is_base_of_v<T, T_Other>, int> = 0>
1298
_FORCE_INLINE_ RequiredParam(const RequiredParam<T_Other> &p_other) :
1299
_value(p_other._internal_ptr_dont_use()) {}
1300
template <typename T_Other, std::enable_if_t<std::is_base_of_v<T, T_Other>, int> = 0>
1301
_FORCE_INLINE_ RequiredParam &operator=(const RequiredParam<T_Other> &p_other) {
1302
_value = p_other._internal_ptr_dont_use();
1303
return *this;
1304
}
1305
1306
template <typename T_Other, std::enable_if_t<std::is_base_of_v<T, T_Other>, int> = 0>
1307
_FORCE_INLINE_ RequiredParam(T_Other *p_ptr) :
1308
_value(p_ptr) {}
1309
template <typename T_Other, std::enable_if_t<std::is_base_of_v<T, T_Other>, int> = 0>
1310
_FORCE_INLINE_ RequiredParam &operator=(T_Other *p_ptr) {
1311
_value = p_ptr;
1312
return *this;
1313
}
1314
1315
template <typename T_Other, std::enable_if_t<std::is_base_of_v<T, T_Other>, int> = 0>
1316
_FORCE_INLINE_ RequiredParam(const Ref<T_Other> &p_ref) :
1317
_value(*p_ref) {}
1318
template <typename T_Other, std::enable_if_t<std::is_base_of_v<T, T_Other>, int> = 0>
1319
_FORCE_INLINE_ RequiredParam &operator=(const Ref<T_Other> &p_ref) {
1320
_value = *p_ref;
1321
return *this;
1322
}
1323
1324
template <typename U = T, std::enable_if_t<std::is_base_of_v<RefCounted, U>, int> = 0>
1325
_FORCE_INLINE_ RequiredParam(const Variant &p_variant) :
1326
_value(Object::cast_to<T>(p_variant.get_validated_object())) {}
1327
template <typename U = T, std::enable_if_t<std::is_base_of_v<RefCounted, U>, int> = 0>
1328
_FORCE_INLINE_ RequiredParam &operator=(const Variant &p_variant) {
1329
_value = Object::cast_to<T>(p_variant.get_validated_object());
1330
return *this;
1331
}
1332
1333
template <typename U = T, std::enable_if_t<!std::is_base_of_v<RefCounted, U>, int> = 0>
1334
_FORCE_INLINE_ RequiredParam(const Variant &p_variant) :
1335
_value(Object::cast_to<T>(p_variant.operator Object *())) {}
1336
template <typename U = T, std::enable_if_t<!std::is_base_of_v<RefCounted, U>, int> = 0>
1337
_FORCE_INLINE_ RequiredParam &operator=(const Variant &p_variant) {
1338
_value = Object::cast_to<T>(p_variant.operator Object *());
1339
return *this;
1340
}
1341
};
1342
1343
#define TMPL_EXTRACT_PARAM_OR_FAIL(m_name, m_param, m_retval, m_msg, m_editor) \
1344
if (unlikely(m_param._is_null_dont_use())) { \
1345
_err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Required object \"" _STR(m_param) "\" is null.", m_msg, m_editor); \
1346
return m_retval; \
1347
} \
1348
typename std::decay_t<decltype(m_param)>::extracted_type m_name = m_param._internal_ptr_dont_use(); \
1349
static_assert(true)
1350
1351
// These macros are equivalent to the ERR_FAIL_NULL*() family of macros, only for RequiredParam<T> instead of raw pointers.
1352
#define EXTRACT_PARAM_OR_FAIL(m_name, m_param) TMPL_EXTRACT_PARAM_OR_FAIL(m_name, m_param, void(), "", false)
1353
#define EXTRACT_PARAM_OR_FAIL_MSG(m_name, m_param, m_msg) TMPL_EXTRACT_PARAM_OR_FAIL(m_name, m_param, void(), m_msg, false)
1354
#define EXTRACT_PARAM_OR_FAIL_EDMSG(m_name, m_param, m_msg) TMPL_EXTRACT_PARAM_OR_FAIL(m_name, m_param, void(), m_msg, true)
1355
#define EXTRACT_PARAM_OR_FAIL_V(m_name, m_param, m_retval) TMPL_EXTRACT_PARAM_OR_FAIL(m_name, m_param, m_retval, "", false)
1356
#define EXTRACT_PARAM_OR_FAIL_V_MSG(m_name, m_param, m_retval, m_msg) TMPL_EXTRACT_PARAM_OR_FAIL(m_name, m_param, m_retval, m_msg, false)
1357
#define EXTRACT_PARAM_OR_FAIL_V_EDMSG(m_name, m_param, m_retval, m_msg) TMPL_EXTRACT_PARAM_OR_FAIL(m_name, m_param, m_retval, m_msg, true)
1358
1359