Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/core/object/object.h
9902 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/disabled_classes.gen.h"
34
#include "core/extension/gdextension_interface.h"
35
#include "core/object/message_queue.h"
36
#include "core/object/object_id.h"
37
#include "core/os/rw_lock.h"
38
#include "core/os/spin_lock.h"
39
#include "core/templates/hash_map.h"
40
#include "core/templates/hash_set.h"
41
#include "core/templates/list.h"
42
#include "core/templates/rb_map.h"
43
#include "core/templates/safe_refcount.h"
44
#include "core/variant/callable_bind.h"
45
#include "core/variant/variant.h"
46
47
template <typename T>
48
class TypedArray;
49
50
template <typename T>
51
class Ref;
52
53
enum PropertyHint {
54
PROPERTY_HINT_NONE, ///< no hint provided.
55
PROPERTY_HINT_RANGE, ///< hint_text = "min,max[,step][,or_greater][,or_less][,hide_slider][,radians_as_degrees][,degrees][,exp][,suffix:<keyword>] range.
56
PROPERTY_HINT_ENUM, ///< hint_text= "val1,val2,val3,etc"
57
PROPERTY_HINT_ENUM_SUGGESTION, ///< hint_text= "val1,val2,val3,etc"
58
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")
59
PROPERTY_HINT_LINK,
60
PROPERTY_HINT_FLAGS, ///< hint_text= "flag1,flag2,etc" (as bit flags)
61
PROPERTY_HINT_LAYERS_2D_RENDER,
62
PROPERTY_HINT_LAYERS_2D_PHYSICS,
63
PROPERTY_HINT_LAYERS_2D_NAVIGATION,
64
PROPERTY_HINT_LAYERS_3D_RENDER,
65
PROPERTY_HINT_LAYERS_3D_PHYSICS,
66
PROPERTY_HINT_LAYERS_3D_NAVIGATION,
67
PROPERTY_HINT_FILE, ///< a file path must be passed, hint_text (optionally) is a filter "*.png,*.wav,*.doc,"
68
PROPERTY_HINT_DIR, ///< a directory path must be passed
69
PROPERTY_HINT_GLOBAL_FILE, ///< a file path must be passed, hint_text (optionally) is a filter "*.png,*.wav,*.doc,"
70
PROPERTY_HINT_GLOBAL_DIR, ///< a directory path must be passed
71
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".
72
PROPERTY_HINT_MULTILINE_TEXT, ///< used for string properties that can contain multiple lines
73
PROPERTY_HINT_EXPRESSION, ///< used for string properties that can contain multiple lines
74
PROPERTY_HINT_PLACEHOLDER_TEXT, ///< used to set a placeholder text for string properties
75
PROPERTY_HINT_COLOR_NO_ALPHA, ///< used for ignoring alpha component when editing a color
76
PROPERTY_HINT_OBJECT_ID,
77
PROPERTY_HINT_TYPE_STRING, ///< a type string, the hint is the base type to choose
78
PROPERTY_HINT_NODE_PATH_TO_EDITED_NODE, // Deprecated.
79
PROPERTY_HINT_OBJECT_TOO_BIG, ///< object is too big to send
80
PROPERTY_HINT_NODE_PATH_VALID_TYPES,
81
PROPERTY_HINT_SAVE_FILE, ///< a file path must be passed, hint_text (optionally) is a filter "*.png,*.wav,*.doc,". This opens a save dialog
82
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
83
PROPERTY_HINT_INT_IS_OBJECTID, // Deprecated.
84
PROPERTY_HINT_INT_IS_POINTER,
85
PROPERTY_HINT_ARRAY_TYPE,
86
PROPERTY_HINT_LOCALE_ID,
87
PROPERTY_HINT_LOCALIZABLE_STRING,
88
PROPERTY_HINT_NODE_TYPE, ///< a node object type
89
PROPERTY_HINT_HIDE_QUATERNION_EDIT, /// Only Node3D::transform should hide the quaternion editor.
90
PROPERTY_HINT_PASSWORD,
91
PROPERTY_HINT_LAYERS_AVOIDANCE,
92
PROPERTY_HINT_DICTIONARY_TYPE,
93
PROPERTY_HINT_TOOL_BUTTON,
94
PROPERTY_HINT_ONESHOT, ///< the property will be changed by self after setting, such as AudioStreamPlayer.playing, Particles.emitting.
95
PROPERTY_HINT_NO_NODEPATH, /// < this property will not contain a NodePath, regardless of type (Array, Dictionary, List, etc.). Needed for SceneTreeDock.
96
PROPERTY_HINT_GROUP_ENABLE, ///< used to make the property's group checkable. Only use for boolean types.
97
PROPERTY_HINT_INPUT_NAME,
98
PROPERTY_HINT_FILE_PATH,
99
PROPERTY_HINT_MAX,
100
};
101
102
enum PropertyUsageFlags {
103
PROPERTY_USAGE_NONE = 0,
104
PROPERTY_USAGE_STORAGE = 1 << 1,
105
PROPERTY_USAGE_EDITOR = 1 << 2,
106
PROPERTY_USAGE_INTERNAL = 1 << 3,
107
PROPERTY_USAGE_CHECKABLE = 1 << 4, // Used for editing global variables.
108
PROPERTY_USAGE_CHECKED = 1 << 5, // Used for editing global variables.
109
PROPERTY_USAGE_GROUP = 1 << 6, // Used for grouping props in the editor.
110
PROPERTY_USAGE_CATEGORY = 1 << 7,
111
PROPERTY_USAGE_SUBGROUP = 1 << 8,
112
PROPERTY_USAGE_CLASS_IS_BITFIELD = 1 << 9,
113
PROPERTY_USAGE_NO_INSTANCE_STATE = 1 << 10,
114
PROPERTY_USAGE_RESTART_IF_CHANGED = 1 << 11,
115
PROPERTY_USAGE_SCRIPT_VARIABLE = 1 << 12,
116
PROPERTY_USAGE_STORE_IF_NULL = 1 << 13,
117
PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED = 1 << 14,
118
PROPERTY_USAGE_SCRIPT_DEFAULT_VALUE = 1 << 15, // Deprecated.
119
PROPERTY_USAGE_CLASS_IS_ENUM = 1 << 16,
120
PROPERTY_USAGE_NIL_IS_VARIANT = 1 << 17,
121
PROPERTY_USAGE_ARRAY = 1 << 18, // Used in the inspector to group properties as elements of an array.
122
PROPERTY_USAGE_ALWAYS_DUPLICATE = 1 << 19, // When duplicating a resource, always duplicate, even with subresource duplication disabled.
123
PROPERTY_USAGE_NEVER_DUPLICATE = 1 << 20, // When duplicating a resource, never duplicate, even with subresource duplication enabled.
124
PROPERTY_USAGE_HIGH_END_GFX = 1 << 21,
125
PROPERTY_USAGE_NODE_PATH_FROM_SCENE_ROOT = 1 << 22,
126
PROPERTY_USAGE_RESOURCE_NOT_PERSISTENT = 1 << 23,
127
PROPERTY_USAGE_KEYING_INCREMENTS = 1 << 24, // Used in inspector to increment property when keyed in animation player.
128
PROPERTY_USAGE_DEFERRED_SET_RESOURCE = 1 << 25, // Deprecated.
129
PROPERTY_USAGE_EDITOR_INSTANTIATE_OBJECT = 1 << 26, // For Object properties, instantiate them when creating in editor.
130
PROPERTY_USAGE_EDITOR_BASIC_SETTING = 1 << 27, //for project or editor settings, show when basic settings are selected.
131
PROPERTY_USAGE_READ_ONLY = 1 << 28, // Mark a property as read-only in the inspector.
132
PROPERTY_USAGE_SECRET = 1 << 29, // Export preset credentials that should be stored separately from the rest of the export config.
133
134
PROPERTY_USAGE_DEFAULT = PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_EDITOR,
135
PROPERTY_USAGE_NO_EDITOR = PROPERTY_USAGE_STORAGE,
136
};
137
138
// Respective values are defined by disabled_classes.gen.h
139
#define GD_IS_CLASS_ENABLED(m_class) m_class::_class_is_enabled
140
141
#define ADD_SIGNAL(m_signal) ::ClassDB::add_signal(get_class_static(), m_signal)
142
#define ADD_PROPERTY(m_property, m_setter, m_getter) ::ClassDB::add_property(get_class_static(), m_property, StringName(m_setter), StringName(m_getter))
143
#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)
144
#define ADD_PROPERTY_DEFAULT(m_property, m_default) ::ClassDB::set_property_default_value(get_class_static(), m_property, m_default)
145
#define ADD_GROUP(m_name, m_prefix) ::ClassDB::add_property_group(get_class_static(), m_name, m_prefix)
146
#define ADD_GROUP_INDENT(m_name, m_prefix, m_depth) ::ClassDB::add_property_group(get_class_static(), m_name, m_prefix, m_depth)
147
#define ADD_SUBGROUP(m_name, m_prefix) ::ClassDB::add_property_subgroup(get_class_static(), m_name, m_prefix)
148
#define ADD_SUBGROUP_INDENT(m_name, m_prefix, m_depth) ::ClassDB::add_property_subgroup(get_class_static(), m_name, m_prefix, m_depth)
149
#define ADD_LINKED_PROPERTY(m_property, m_linked_property) ::ClassDB::add_linked_property(get_class_static(), m_property, m_linked_property)
150
151
#ifdef TOOLS_ENABLED
152
#define ADD_CLASS_DEPENDENCY(m_class) ::ClassDB::add_class_dependency(get_class_static(), m_class)
153
#else
154
#define ADD_CLASS_DEPENDENCY(m_class)
155
#endif
156
157
#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)
158
#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)
159
#define ADD_ARRAY(m_array_path, m_prefix) ClassDB::add_property_array(get_class_static(), m_array_path, m_prefix)
160
161
// Helper macro to use with PROPERTY_HINT_ARRAY_TYPE for arrays of specific resources:
162
// PropertyInfo(Variant::ARRAY, "fallbacks", PROPERTY_HINT_ARRAY_TYPE, MAKE_RESOURCE_TYPE_HINT("Font")
163
#define MAKE_RESOURCE_TYPE_HINT(m_type) vformat("%s/%s:%s", Variant::OBJECT, PROPERTY_HINT_RESOURCE_TYPE, m_type)
164
165
struct PropertyInfo {
166
Variant::Type type = Variant::NIL;
167
String name;
168
StringName class_name; // For classes
169
PropertyHint hint = PROPERTY_HINT_NONE;
170
String hint_string;
171
uint32_t usage = PROPERTY_USAGE_DEFAULT;
172
173
// If you are thinking about adding another member to this class, ask the maintainer (Juan) first.
174
175
_FORCE_INLINE_ PropertyInfo added_usage(uint32_t p_fl) const {
176
PropertyInfo pi = *this;
177
pi.usage |= p_fl;
178
return pi;
179
}
180
181
operator Dictionary() const;
182
183
static PropertyInfo from_dict(const Dictionary &p_dict);
184
185
PropertyInfo() {}
186
187
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()) :
188
type(p_type),
189
name(p_name),
190
hint(p_hint),
191
hint_string(p_hint_string),
192
usage(p_usage) {
193
if (hint == PROPERTY_HINT_RESOURCE_TYPE) {
194
class_name = hint_string;
195
} else {
196
class_name = p_class_name;
197
}
198
}
199
200
PropertyInfo(const StringName &p_class_name) :
201
type(Variant::OBJECT),
202
class_name(p_class_name) {}
203
204
explicit PropertyInfo(const GDExtensionPropertyInfo &pinfo) :
205
type((Variant::Type)pinfo.type),
206
name(*reinterpret_cast<StringName *>(pinfo.name)),
207
class_name(*reinterpret_cast<StringName *>(pinfo.class_name)),
208
hint((PropertyHint)pinfo.hint),
209
hint_string(*reinterpret_cast<String *>(pinfo.hint_string)),
210
usage(pinfo.usage) {}
211
212
bool operator==(const PropertyInfo &p_info) const {
213
return ((type == p_info.type) &&
214
(name == p_info.name) &&
215
(class_name == p_info.class_name) &&
216
(hint == p_info.hint) &&
217
(hint_string == p_info.hint_string) &&
218
(usage == p_info.usage));
219
}
220
221
bool operator<(const PropertyInfo &p_info) const {
222
return name < p_info.name;
223
}
224
};
225
226
TypedArray<Dictionary> convert_property_list(const List<PropertyInfo> *p_list);
227
TypedArray<Dictionary> convert_property_list(const Vector<PropertyInfo> &p_vector);
228
229
enum MethodFlags {
230
METHOD_FLAG_NORMAL = 1,
231
METHOD_FLAG_EDITOR = 2,
232
METHOD_FLAG_CONST = 4,
233
METHOD_FLAG_VIRTUAL = 8,
234
METHOD_FLAG_VARARG = 16,
235
METHOD_FLAG_STATIC = 32,
236
METHOD_FLAG_OBJECT_CORE = 64,
237
METHOD_FLAG_VIRTUAL_REQUIRED = 128,
238
METHOD_FLAGS_DEFAULT = METHOD_FLAG_NORMAL,
239
};
240
241
struct MethodInfo {
242
String name;
243
PropertyInfo return_val;
244
uint32_t flags = METHOD_FLAGS_DEFAULT;
245
int id = 0;
246
Vector<PropertyInfo> arguments;
247
Vector<Variant> default_arguments;
248
int return_val_metadata = 0;
249
Vector<int> arguments_metadata;
250
251
int get_argument_meta(int p_arg) const {
252
ERR_FAIL_COND_V(p_arg < -1 || p_arg > arguments.size(), 0);
253
if (p_arg == -1) {
254
return return_val_metadata;
255
}
256
return arguments_metadata.size() > p_arg ? arguments_metadata[p_arg] : 0;
257
}
258
259
inline bool operator==(const MethodInfo &p_method) const { return id == p_method.id && name == p_method.name; }
260
inline bool operator<(const MethodInfo &p_method) const { return id == p_method.id ? (name < p_method.name) : (id < p_method.id); }
261
262
operator Dictionary() const;
263
264
static MethodInfo from_dict(const Dictionary &p_dict);
265
266
uint32_t get_compatibility_hash() const;
267
268
MethodInfo() {}
269
270
explicit MethodInfo(const GDExtensionMethodInfo &pinfo) :
271
name(*reinterpret_cast<StringName *>(pinfo.name)),
272
return_val(PropertyInfo(pinfo.return_value)),
273
flags(pinfo.flags),
274
id(pinfo.id) {
275
for (uint32_t i = 0; i < pinfo.argument_count; i++) {
276
arguments.push_back(PropertyInfo(pinfo.arguments[i]));
277
}
278
const Variant *def_values = (const Variant *)pinfo.default_arguments;
279
for (uint32_t j = 0; j < pinfo.default_argument_count; j++) {
280
default_arguments.push_back(def_values[j]);
281
}
282
}
283
284
MethodInfo(const String &p_name) { name = p_name; }
285
286
template <typename... VarArgs>
287
MethodInfo(const String &p_name, VarArgs... p_params) {
288
name = p_name;
289
arguments = Vector<PropertyInfo>{ p_params... };
290
}
291
292
MethodInfo(Variant::Type ret) { return_val.type = ret; }
293
MethodInfo(Variant::Type ret, const String &p_name) {
294
return_val.type = ret;
295
name = p_name;
296
}
297
298
template <typename... VarArgs>
299
MethodInfo(Variant::Type ret, const String &p_name, VarArgs... p_params) {
300
name = p_name;
301
return_val.type = ret;
302
arguments = Vector<PropertyInfo>{ p_params... };
303
}
304
305
MethodInfo(const PropertyInfo &p_ret, const String &p_name) {
306
return_val = p_ret;
307
name = p_name;
308
}
309
310
template <typename... VarArgs>
311
MethodInfo(const PropertyInfo &p_ret, const String &p_name, VarArgs... p_params) {
312
return_val = p_ret;
313
name = p_name;
314
arguments = Vector<PropertyInfo>{ p_params... };
315
}
316
};
317
318
// API used to extend in GDExtension and other C compatible compiled languages.
319
class MethodBind;
320
class GDExtension;
321
322
struct ObjectGDExtension {
323
GDExtension *library = nullptr;
324
ObjectGDExtension *parent = nullptr;
325
List<ObjectGDExtension *> children;
326
StringName parent_class_name;
327
StringName class_name;
328
bool editor_class = false;
329
bool reloadable = false;
330
bool is_virtual = false;
331
bool is_abstract = false;
332
bool is_exposed = true;
333
#ifdef TOOLS_ENABLED
334
bool is_runtime = false;
335
bool is_placeholder = false;
336
#endif
337
GDExtensionClassSet set;
338
GDExtensionClassGet get;
339
GDExtensionClassGetPropertyList get_property_list;
340
GDExtensionClassFreePropertyList2 free_property_list2;
341
GDExtensionClassPropertyCanRevert property_can_revert;
342
GDExtensionClassPropertyGetRevert property_get_revert;
343
GDExtensionClassValidateProperty validate_property;
344
#ifndef DISABLE_DEPRECATED
345
GDExtensionClassNotification notification;
346
GDExtensionClassFreePropertyList free_property_list;
347
#endif // DISABLE_DEPRECATED
348
GDExtensionClassNotification2 notification2;
349
GDExtensionClassToString to_string;
350
GDExtensionClassReference reference;
351
GDExtensionClassReference unreference;
352
GDExtensionClassGetRID get_rid;
353
354
_FORCE_INLINE_ bool is_class(const String &p_class) const {
355
const ObjectGDExtension *e = this;
356
while (e) {
357
if (p_class == e->class_name.operator String()) {
358
return true;
359
}
360
e = e->parent;
361
}
362
return false;
363
}
364
void *class_userdata = nullptr;
365
366
#ifndef DISABLE_DEPRECATED
367
GDExtensionClassCreateInstance create_instance;
368
#endif // DISABLE_DEPRECATED
369
GDExtensionClassCreateInstance2 create_instance2;
370
GDExtensionClassFreeInstance free_instance;
371
#ifndef DISABLE_DEPRECATED
372
GDExtensionClassGetVirtual get_virtual;
373
GDExtensionClassGetVirtualCallData get_virtual_call_data;
374
#endif // DISABLE_DEPRECATED
375
GDExtensionClassGetVirtual2 get_virtual2;
376
GDExtensionClassGetVirtualCallData2 get_virtual_call_data2;
377
GDExtensionClassCallVirtualWithData call_virtual_with_data;
378
GDExtensionClassRecreateInstance recreate_instance;
379
380
#ifdef TOOLS_ENABLED
381
void *tracking_userdata = nullptr;
382
void (*track_instance)(void *p_userdata, void *p_instance) = nullptr;
383
void (*untrack_instance)(void *p_userdata, void *p_instance) = nullptr;
384
#endif
385
};
386
387
#define GDVIRTUAL_CALL(m_name, ...) _gdvirtual_##m_name##_call(__VA_ARGS__)
388
#define GDVIRTUAL_CALL_PTR(m_obj, m_name, ...) m_obj->_gdvirtual_##m_name##_call(__VA_ARGS__)
389
390
#ifdef DEBUG_ENABLED
391
#define GDVIRTUAL_BIND(m_name, ...) ::ClassDB::add_virtual_method(get_class_static(), _gdvirtual_##m_name##_get_method_info(), true, sarray(__VA_ARGS__));
392
#else
393
#define GDVIRTUAL_BIND(m_name, ...)
394
#endif // DEBUG_ENABLED
395
#define GDVIRTUAL_BIND_COMPAT(m_alias, ...) ::ClassDB::add_virtual_compatibility_method(get_class_static(), _gdvirtual_##m_alias##_get_method_info(), true, sarray(__VA_ARGS__));
396
#define GDVIRTUAL_IS_OVERRIDDEN(m_name) _gdvirtual_##m_name##_overridden()
397
#define GDVIRTUAL_IS_OVERRIDDEN_PTR(m_obj, m_name) m_obj->_gdvirtual_##m_name##_overridden()
398
399
/*
400
* The following is an incomprehensible blob of hacks and workarounds to
401
* compensate for many of the fallacies in C++. As a plus, this macro pretty
402
* much alone defines the object model.
403
*/
404
405
/// `GDSOFTCLASS` provides `Object` functionality, such as being able to use `Object::cast_to()`.
406
/// Use this for `Object` subclasses that are not registered in `ClassDB` (use `GDCLASS` otherwise).
407
#define GDSOFTCLASS(m_class, m_inherits) \
408
public: \
409
using self_type = m_class; \
410
using super_type = m_inherits; \
411
static _FORCE_INLINE_ void *get_class_ptr_static() { \
412
static int ptr; \
413
return &ptr; \
414
} \
415
virtual bool is_class_ptr(void *p_ptr) const override { \
416
return (p_ptr == get_class_ptr_static()) || m_inherits::is_class_ptr(p_ptr); \
417
} \
418
\
419
protected: \
420
_FORCE_INLINE_ bool (Object::*_get_get() const)(const StringName &p_name, Variant &) const { \
421
return (bool (Object::*)(const StringName &, Variant &) const) & m_class::_get; \
422
} \
423
virtual bool _getv(const StringName &p_name, Variant &r_ret) const override { \
424
if (m_class::_get_get() != m_inherits::_get_get()) { \
425
if (_get(p_name, r_ret)) { \
426
return true; \
427
} \
428
} \
429
return m_inherits::_getv(p_name, r_ret); \
430
} \
431
_FORCE_INLINE_ bool (Object::*_get_set() const)(const StringName &p_name, const Variant &p_property) { \
432
return (bool (Object::*)(const StringName &, const Variant &)) & m_class::_set; \
433
} \
434
virtual bool _setv(const StringName &p_name, const Variant &p_property) override { \
435
if (m_inherits::_setv(p_name, p_property)) { \
436
return true; \
437
} \
438
if (m_class::_get_set() != m_inherits::_get_set()) { \
439
return _set(p_name, p_property); \
440
} \
441
return false; \
442
} \
443
_FORCE_INLINE_ void (Object::*_get_validate_property() const)(PropertyInfo & p_property) const { \
444
return (void (Object::*)(PropertyInfo &) const) & m_class::_validate_property; \
445
} \
446
virtual void _validate_propertyv(PropertyInfo &p_property) const override { \
447
m_inherits::_validate_propertyv(p_property); \
448
if (m_class::_get_validate_property() != m_inherits::_get_validate_property()) { \
449
_validate_property(p_property); \
450
} \
451
} \
452
_FORCE_INLINE_ bool (Object::*_get_property_can_revert() const)(const StringName &p_name) const { \
453
return (bool (Object::*)(const StringName &) const) & m_class::_property_can_revert; \
454
} \
455
virtual bool _property_can_revertv(const StringName &p_name) const override { \
456
if (m_class::_get_property_can_revert() != m_inherits::_get_property_can_revert()) { \
457
if (_property_can_revert(p_name)) { \
458
return true; \
459
} \
460
} \
461
return m_inherits::_property_can_revertv(p_name); \
462
} \
463
_FORCE_INLINE_ bool (Object::*_get_property_get_revert() const)(const StringName &p_name, Variant &) const { \
464
return (bool (Object::*)(const StringName &, Variant &) const) & m_class::_property_get_revert; \
465
} \
466
virtual bool _property_get_revertv(const StringName &p_name, Variant &r_ret) const override { \
467
if (m_class::_get_property_get_revert() != m_inherits::_get_property_get_revert()) { \
468
if (_property_get_revert(p_name, r_ret)) { \
469
return true; \
470
} \
471
} \
472
return m_inherits::_property_get_revertv(p_name, r_ret); \
473
} \
474
_FORCE_INLINE_ void (Object::*_get_notification() const)(int) { \
475
return (void (Object::*)(int)) & m_class::_notification; \
476
} \
477
virtual void _notification_forwardv(int p_notification) override { \
478
m_inherits::_notification_forwardv(p_notification); \
479
if (m_class::_get_notification() != m_inherits::_get_notification()) { \
480
_notification(p_notification); \
481
} \
482
} \
483
virtual void _notification_backwardv(int p_notification) override { \
484
if (m_class::_get_notification() != m_inherits::_get_notification()) { \
485
_notification(p_notification); \
486
} \
487
m_inherits::_notification_backwardv(p_notification); \
488
} \
489
\
490
private:
491
492
/// `GDSOFTCLASS` provides `Object` functionality, such as being able to use `Object::cast_to()`.
493
/// Use this for `Object` subclasses that are registered in `ObjectDB` (use `GDSOFTCLASS` otherwise).
494
#define GDCLASS(m_class, m_inherits) \
495
GDSOFTCLASS(m_class, m_inherits) \
496
private: \
497
void operator=(const m_class &p_rval) {} \
498
friend class ::ClassDB; \
499
\
500
public: \
501
static constexpr bool _class_is_enabled = !bool(GD_IS_DEFINED(ClassDB_Disable_##m_class)) && m_inherits::_class_is_enabled; \
502
virtual const StringName *_get_class_namev() const override { \
503
return &get_class_static(); \
504
} \
505
static const StringName &get_class_static() { \
506
static StringName _class_name_static; \
507
if (unlikely(!_class_name_static)) { \
508
assign_class_name_static(#m_class, _class_name_static); \
509
} \
510
return _class_name_static; \
511
} \
512
virtual bool is_class(const String &p_class) const override { \
513
if (_get_extension() && _get_extension()->is_class(p_class)) { \
514
return true; \
515
} \
516
return (p_class == (#m_class)) ? true : m_inherits::is_class(p_class); \
517
} \
518
\
519
protected: \
520
_FORCE_INLINE_ static void (*_get_bind_methods())() { \
521
return &m_class::_bind_methods; \
522
} \
523
_FORCE_INLINE_ static void (*_get_bind_compatibility_methods())() { \
524
return &m_class::_bind_compatibility_methods; \
525
} \
526
\
527
public: \
528
static void initialize_class() { \
529
static bool initialized = false; \
530
if (initialized) { \
531
return; \
532
} \
533
m_inherits::initialize_class(); \
534
_add_class_to_classdb(get_class_static(), super_type::get_class_static()); \
535
if (m_class::_get_bind_methods() != m_inherits::_get_bind_methods()) { \
536
_bind_methods(); \
537
} \
538
if (m_class::_get_bind_compatibility_methods() != m_inherits::_get_bind_compatibility_methods()) { \
539
_bind_compatibility_methods(); \
540
} \
541
initialized = true; \
542
} \
543
\
544
protected: \
545
virtual void _initialize_classv() override { \
546
initialize_class(); \
547
} \
548
_FORCE_INLINE_ void (Object::*_get_get_property_list() const)(List<PropertyInfo> * p_list) const { \
549
return (void (Object::*)(List<PropertyInfo> *) const) & m_class::_get_property_list; \
550
} \
551
virtual void _get_property_listv(List<PropertyInfo> *p_list, bool p_reversed) const override { \
552
if (!p_reversed) { \
553
m_inherits::_get_property_listv(p_list, p_reversed); \
554
} \
555
p_list->push_back(PropertyInfo(Variant::NIL, get_class_static(), PROPERTY_HINT_NONE, get_class_static(), PROPERTY_USAGE_CATEGORY)); \
556
_get_property_list_from_classdb(#m_class, p_list, true, this); \
557
if (m_class::_get_get_property_list() != m_inherits::_get_get_property_list()) { \
558
_get_property_list(p_list); \
559
} \
560
if (p_reversed) { \
561
m_inherits::_get_property_listv(p_list, p_reversed); \
562
} \
563
} \
564
\
565
private:
566
567
#define OBJ_SAVE_TYPE(m_class) \
568
public: \
569
virtual String get_save_class() const override { \
570
return #m_class; \
571
} \
572
\
573
private:
574
575
class ScriptInstance;
576
577
class Object {
578
public:
579
typedef Object self_type;
580
581
enum ConnectFlags {
582
CONNECT_DEFERRED = 1,
583
CONNECT_PERSIST = 2, // Hint for scene to save this connection.
584
CONNECT_ONE_SHOT = 4,
585
CONNECT_REFERENCE_COUNTED = 8,
586
CONNECT_APPEND_SOURCE_OBJECT = 16,
587
CONNECT_INHERITED = 32, // Used in editor builds.
588
};
589
590
struct Connection {
591
::Signal signal;
592
Callable callable;
593
594
uint32_t flags = 0;
595
bool operator<(const Connection &p_conn) const;
596
597
operator Variant() const;
598
599
Connection() {}
600
Connection(const Variant &p_variant);
601
};
602
603
private:
604
#ifdef DEBUG_ENABLED
605
friend struct _ObjectDebugLock;
606
#endif // DEBUG_ENABLED
607
friend bool predelete_handler(Object *);
608
friend void postinitialize_handler(Object *);
609
610
ObjectGDExtension *_extension = nullptr;
611
GDExtensionClassInstancePtr _extension_instance = nullptr;
612
613
struct SignalData {
614
struct Slot {
615
int reference_count = 0;
616
Connection conn;
617
List<Connection>::Element *cE = nullptr;
618
};
619
620
MethodInfo user;
621
HashMap<Callable, Slot, HashableHasher<Callable>> slot_map;
622
bool removable = false;
623
};
624
friend struct _ObjectSignalLock;
625
mutable Mutex *signal_mutex = nullptr;
626
HashMap<StringName, SignalData> signal_map;
627
List<Connection> connections;
628
#ifdef DEBUG_ENABLED
629
SafeRefCount _lock_index;
630
#endif // DEBUG_ENABLED
631
bool _block_signals = false;
632
int _predelete_ok = 0;
633
ObjectID _instance_id;
634
bool _predelete();
635
void _initialize();
636
void _postinitialize();
637
bool _can_translate = true;
638
bool _emitting = false;
639
#ifdef TOOLS_ENABLED
640
bool _edited = false;
641
uint32_t _edited_version = 0;
642
HashSet<String> editor_section_folding;
643
#endif
644
ScriptInstance *script_instance = nullptr;
645
Variant script; // Reference does not exist yet, store it in a Variant.
646
HashMap<StringName, Variant> metadata;
647
HashMap<StringName, Variant *> metadata_properties;
648
mutable const StringName *_class_name_ptr = nullptr;
649
650
void _add_user_signal(const String &p_name, const Array &p_args = Array());
651
bool _has_user_signal(const StringName &p_name) const;
652
void _remove_user_signal(const StringName &p_name);
653
Error _emit_signal(const Variant **p_args, int p_argcount, Callable::CallError &r_error);
654
TypedArray<Dictionary> _get_signal_list() const;
655
TypedArray<Dictionary> _get_signal_connection_list(const StringName &p_signal) const;
656
TypedArray<Dictionary> _get_incoming_connections() const;
657
void _set_bind(const StringName &p_set, const Variant &p_value);
658
Variant _get_bind(const StringName &p_name) const;
659
void _set_indexed_bind(const NodePath &p_name, const Variant &p_value);
660
Variant _get_indexed_bind(const NodePath &p_name) const;
661
int _get_method_argument_count_bind(const StringName &p_name) const;
662
663
_FORCE_INLINE_ void _construct_object(bool p_reference);
664
665
friend class RefCounted;
666
bool type_is_reference = false;
667
668
BinaryMutex _instance_binding_mutex;
669
struct InstanceBinding {
670
void *binding = nullptr;
671
void *token = nullptr;
672
GDExtensionInstanceBindingFreeCallback free_callback = nullptr;
673
GDExtensionInstanceBindingReferenceCallback reference_callback = nullptr;
674
};
675
InstanceBinding *_instance_bindings = nullptr;
676
uint32_t _instance_binding_count = 0;
677
678
Object(bool p_reference);
679
680
protected:
681
StringName _translation_domain;
682
683
_FORCE_INLINE_ bool _instance_binding_reference(bool p_reference) {
684
bool can_die = true;
685
if (_instance_bindings) {
686
MutexLock instance_binding_lock(_instance_binding_mutex);
687
for (uint32_t i = 0; i < _instance_binding_count; i++) {
688
if (_instance_bindings[i].reference_callback) {
689
if (!_instance_bindings[i].reference_callback(_instance_bindings[i].token, _instance_bindings[i].binding, p_reference)) {
690
can_die = false;
691
}
692
}
693
}
694
}
695
return can_die;
696
}
697
698
friend class GDExtensionMethodBind;
699
_ALWAYS_INLINE_ const ObjectGDExtension *_get_extension() const { return _extension; }
700
_ALWAYS_INLINE_ GDExtensionClassInstancePtr _get_extension_instance() const { return _extension_instance; }
701
virtual void _initialize_classv() { initialize_class(); }
702
virtual bool _setv(const StringName &p_name, const Variant &p_property) { return false; }
703
virtual bool _getv(const StringName &p_name, Variant &r_property) const { return false; }
704
virtual void _get_property_listv(List<PropertyInfo> *p_list, bool p_reversed) const {}
705
virtual void _validate_propertyv(PropertyInfo &p_property) const {}
706
virtual bool _property_can_revertv(const StringName &p_name) const { return false; }
707
virtual bool _property_get_revertv(const StringName &p_name, Variant &r_property) const { return false; }
708
709
void _notification_forward(int p_notification);
710
void _notification_backward(int p_notification);
711
virtual void _notification_forwardv(int p_notification) {}
712
virtual void _notification_backwardv(int p_notification) {}
713
714
static void _bind_methods();
715
static void _bind_compatibility_methods() {}
716
bool _set(const StringName &p_name, const Variant &p_property) { return false; }
717
bool _get(const StringName &p_name, Variant &r_property) const { return false; }
718
void _get_property_list(List<PropertyInfo> *p_list) const {}
719
void _validate_property(PropertyInfo &p_property) const {}
720
bool _property_can_revert(const StringName &p_name) const { return false; }
721
bool _property_get_revert(const StringName &p_name, Variant &r_property) const { return false; }
722
void _notification(int p_notification) {}
723
724
_FORCE_INLINE_ static void (*_get_bind_methods())() {
725
return &Object::_bind_methods;
726
}
727
_FORCE_INLINE_ static void (*_get_bind_compatibility_methods())() {
728
return &Object::_bind_compatibility_methods;
729
}
730
_FORCE_INLINE_ bool (Object::*_get_get() const)(const StringName &p_name, Variant &r_ret) const {
731
return &Object::_get;
732
}
733
_FORCE_INLINE_ bool (Object::*_get_set() const)(const StringName &p_name, const Variant &p_property) {
734
return &Object::_set;
735
}
736
_FORCE_INLINE_ void (Object::*_get_get_property_list() const)(List<PropertyInfo> *p_list) const {
737
return &Object::_get_property_list;
738
}
739
_FORCE_INLINE_ void (Object::*_get_validate_property() const)(PropertyInfo &p_property) const {
740
return &Object::_validate_property;
741
}
742
_FORCE_INLINE_ bool (Object::*_get_property_can_revert() const)(const StringName &p_name) const {
743
return &Object::_property_can_revert;
744
}
745
_FORCE_INLINE_ bool (Object::*_get_property_get_revert() const)(const StringName &p_name, Variant &) const {
746
return &Object::_property_get_revert;
747
}
748
_FORCE_INLINE_ void (Object::*_get_notification() const)(int) {
749
return &Object::_notification;
750
}
751
752
Variant _call_bind(const Variant **p_args, int p_argcount, Callable::CallError &r_error);
753
Variant _call_deferred_bind(const Variant **p_args, int p_argcount, Callable::CallError &r_error);
754
755
virtual const StringName *_get_class_namev() const {
756
return &get_class_static();
757
}
758
759
TypedArray<StringName> _get_meta_list_bind() const;
760
TypedArray<Dictionary> _get_property_list_bind() const;
761
TypedArray<Dictionary> _get_method_list_bind() const;
762
763
void _clear_internal_resource_paths(const Variant &p_var);
764
765
friend class ClassDB;
766
friend class PlaceholderExtensionInstance;
767
768
static void _add_class_to_classdb(const StringName &p_class, const StringName &p_inherits);
769
static void _get_property_list_from_classdb(const StringName &p_class, List<PropertyInfo> *p_list, bool p_no_inheritance, const Object *p_validator);
770
771
bool _disconnect(const StringName &p_signal, const Callable &p_callable, bool p_force = false);
772
773
virtual bool _uses_signal_mutex() const;
774
775
#ifdef TOOLS_ENABLED
776
struct VirtualMethodTracker {
777
void **method;
778
VirtualMethodTracker *next;
779
};
780
781
mutable VirtualMethodTracker *virtual_method_list = nullptr;
782
#endif
783
784
public: // Should be protected, but bug in clang++.
785
static void initialize_class();
786
_FORCE_INLINE_ static void register_custom_data_to_otdb() {}
787
788
public:
789
static constexpr bool _class_is_enabled = true;
790
791
void notify_property_list_changed();
792
793
static void *get_class_ptr_static() {
794
static int ptr;
795
return &ptr;
796
}
797
798
void detach_from_objectdb();
799
_FORCE_INLINE_ ObjectID get_instance_id() const { return _instance_id; }
800
801
template <typename T>
802
static T *cast_to(Object *p_object) {
803
// This is like dynamic_cast, but faster.
804
// The reason is that we can assume no virtual and multiple inheritance.
805
static_assert(std::is_base_of_v<Object, T>, "T must be derived from Object");
806
static_assert(std::is_same_v<std::decay_t<T>, typename T::self_type>, "T must use GDCLASS or GDSOFTCLASS");
807
return p_object && p_object->is_class_ptr(T::get_class_ptr_static()) ? static_cast<T *>(p_object) : nullptr;
808
}
809
810
template <typename T>
811
static const T *cast_to(const Object *p_object) {
812
static_assert(std::is_base_of_v<Object, T>, "T must be derived from Object");
813
static_assert(std::is_same_v<std::decay_t<T>, typename T::self_type>, "T must use GDCLASS or GDSOFTCLASS");
814
return p_object && p_object->is_class_ptr(T::get_class_ptr_static()) ? static_cast<const T *>(p_object) : nullptr;
815
}
816
817
enum {
818
NOTIFICATION_POSTINITIALIZE = 0,
819
NOTIFICATION_PREDELETE = 1,
820
NOTIFICATION_EXTENSION_RELOADED = 2,
821
// Internal notification to send after NOTIFICATION_PREDELETE, not bound to scripting.
822
NOTIFICATION_PREDELETE_CLEANUP = 3,
823
};
824
825
/* TYPE API */
826
static void assign_class_name_static(const Span<char> &p_name, StringName &r_target);
827
828
static const StringName &get_class_static() {
829
static StringName _class_name_static;
830
if (unlikely(!_class_name_static)) {
831
assign_class_name_static("Object", _class_name_static);
832
}
833
return _class_name_static;
834
}
835
836
_FORCE_INLINE_ String get_class() const { return get_class_name(); }
837
838
virtual String get_save_class() const { return get_class(); } //class stored when saving
839
840
virtual bool is_class(const String &p_class) const {
841
if (_extension && _extension->is_class(p_class)) {
842
return true;
843
}
844
return (p_class == "Object");
845
}
846
virtual bool is_class_ptr(void *p_ptr) const { return get_class_ptr_static() == p_ptr; }
847
848
const StringName &get_class_name() const;
849
850
StringName get_class_name_for_extension(const GDExtension *p_library) const;
851
852
/* IAPI */
853
854
void set(const StringName &p_name, const Variant &p_value, bool *r_valid = nullptr);
855
Variant get(const StringName &p_name, bool *r_valid = nullptr) const;
856
void set_indexed(const Vector<StringName> &p_names, const Variant &p_value, bool *r_valid = nullptr);
857
Variant get_indexed(const Vector<StringName> &p_names, bool *r_valid = nullptr) const;
858
859
void get_property_list(List<PropertyInfo> *p_list, bool p_reversed = false) const;
860
void validate_property(PropertyInfo &p_property) const;
861
bool property_can_revert(const StringName &p_name) const;
862
Variant property_get_revert(const StringName &p_name) const;
863
864
bool has_method(const StringName &p_method) const;
865
int get_method_argument_count(const StringName &p_method, bool *r_is_valid = nullptr) const;
866
void get_method_list(List<MethodInfo> *p_list) const;
867
Variant callv(const StringName &p_method, const Array &p_args);
868
virtual Variant callp(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error);
869
virtual Variant call_const(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error);
870
871
template <typename... VarArgs>
872
Variant call(const StringName &p_method, VarArgs... p_args) {
873
Variant args[sizeof...(p_args) + 1] = { p_args..., Variant() }; // +1 makes sure zero sized arrays are also supported.
874
const Variant *argptrs[sizeof...(p_args) + 1];
875
for (uint32_t i = 0; i < sizeof...(p_args); i++) {
876
argptrs[i] = &args[i];
877
}
878
Callable::CallError cerr;
879
const Variant ret = callp(p_method, sizeof...(p_args) == 0 ? nullptr : (const Variant **)argptrs, sizeof...(p_args), cerr);
880
return (cerr.error == Callable::CallError::CALL_OK) ? ret : Variant();
881
}
882
883
// Depending on the boolean, we call either the virtual function _notification_backward or _notification_forward.
884
// - Forward calls subclasses in descending order (e.g. Object -> Node -> Node3D -> extension -> script).
885
// Backward calls subclasses in descending order (e.g. script -> extension -> Node3D -> Node -> Object).
886
_FORCE_INLINE_ void notification(int p_notification, bool p_reversed = false) {
887
if (p_reversed) {
888
_notification_backward(p_notification);
889
} else {
890
_notification_forward(p_notification);
891
}
892
}
893
894
virtual String to_string();
895
896
// Used mainly by script, get and set all INCLUDING string.
897
virtual Variant getvar(const Variant &p_key, bool *r_valid = nullptr) const;
898
virtual void setvar(const Variant &p_key, const Variant &p_value, bool *r_valid = nullptr);
899
900
/* SCRIPT */
901
902
// When in debug, some non-virtual functions can be overridden for multithreaded guards.
903
#ifdef DEBUG_ENABLED
904
#define MTVIRTUAL virtual
905
#else
906
#define MTVIRTUAL
907
#endif // DEBUG_ENABLED
908
909
MTVIRTUAL void set_script(const Variant &p_script);
910
MTVIRTUAL Variant get_script() const;
911
912
MTVIRTUAL bool has_meta(const StringName &p_name) const;
913
MTVIRTUAL void set_meta(const StringName &p_name, const Variant &p_value);
914
MTVIRTUAL void remove_meta(const StringName &p_name);
915
MTVIRTUAL Variant get_meta(const StringName &p_name, const Variant &p_default = Variant()) const;
916
MTVIRTUAL void get_meta_list(List<StringName> *p_list) const;
917
MTVIRTUAL void merge_meta_from(const Object *p_src);
918
919
#ifdef TOOLS_ENABLED
920
void set_edited(bool p_edited);
921
bool is_edited() const;
922
// This function is used to check when something changed beyond a point, it's used mainly for generating previews.
923
uint32_t get_edited_version() const;
924
#endif
925
926
void set_script_instance(ScriptInstance *p_instance);
927
_FORCE_INLINE_ ScriptInstance *get_script_instance() const { return script_instance; }
928
929
// Some script languages can't control instance creation, so this function eases the process.
930
void set_script_and_instance(const Variant &p_script, ScriptInstance *p_instance);
931
932
void add_user_signal(const MethodInfo &p_signal);
933
934
template <typename... VarArgs>
935
Error emit_signal(const StringName &p_name, VarArgs... p_args) {
936
Variant args[sizeof...(p_args) + 1] = { p_args..., Variant() }; // +1 makes sure zero sized arrays are also supported.
937
const Variant *argptrs[sizeof...(p_args) + 1];
938
for (uint32_t i = 0; i < sizeof...(p_args); i++) {
939
argptrs[i] = &args[i];
940
}
941
return emit_signalp(p_name, sizeof...(p_args) == 0 ? nullptr : (const Variant **)argptrs, sizeof...(p_args));
942
}
943
944
MTVIRTUAL Error emit_signalp(const StringName &p_name, const Variant **p_args, int p_argcount);
945
MTVIRTUAL bool has_signal(const StringName &p_name) const;
946
MTVIRTUAL void get_signal_list(List<MethodInfo> *p_signals) const;
947
MTVIRTUAL void get_signal_connection_list(const StringName &p_signal, List<Connection> *p_connections) const;
948
MTVIRTUAL void get_all_signal_connections(List<Connection> *p_connections) const;
949
MTVIRTUAL int get_persistent_signal_connection_count() const;
950
MTVIRTUAL void get_signals_connected_to_this(List<Connection> *p_connections) const;
951
952
MTVIRTUAL Error connect(const StringName &p_signal, const Callable &p_callable, uint32_t p_flags = 0);
953
MTVIRTUAL void disconnect(const StringName &p_signal, const Callable &p_callable);
954
MTVIRTUAL bool is_connected(const StringName &p_signal, const Callable &p_callable) const;
955
MTVIRTUAL bool has_connections(const StringName &p_signal) const;
956
957
template <typename... VarArgs>
958
void call_deferred(const StringName &p_name, VarArgs... p_args) {
959
MessageQueue::get_singleton()->push_call(this, p_name, p_args...);
960
}
961
962
void set_deferred(const StringName &p_property, const Variant &p_value);
963
964
void set_block_signals(bool p_block);
965
bool is_blocking_signals() const;
966
967
Variant::Type get_static_property_type(const StringName &p_property, bool *r_valid = nullptr) const;
968
Variant::Type get_static_property_type_indexed(const Vector<StringName> &p_path, bool *r_valid = nullptr) const;
969
970
// Translate message (internationalization).
971
String tr(const StringName &p_message, const StringName &p_context = "") const;
972
String tr_n(const StringName &p_message, const StringName &p_message_plural, int p_n, const StringName &p_context = "") const;
973
974
bool _is_queued_for_deletion = false; // Set to true by SceneTree::queue_delete().
975
bool is_queued_for_deletion() const;
976
977
_FORCE_INLINE_ void set_message_translation(bool p_enable) { _can_translate = p_enable; }
978
_FORCE_INLINE_ bool can_translate_messages() const { return _can_translate; }
979
980
virtual StringName get_translation_domain() const;
981
virtual void set_translation_domain(const StringName &p_domain);
982
983
#ifdef TOOLS_ENABLED
984
virtual void get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const;
985
void editor_set_section_unfold(const String &p_section, bool p_unfolded, bool p_initializing = false);
986
bool editor_is_section_unfolded(const String &p_section);
987
const HashSet<String> &editor_get_section_folding() const { return editor_section_folding; }
988
void editor_clear_section_folding() { editor_section_folding.clear(); }
989
#endif
990
991
// Used by script languages to store binding data.
992
void *get_instance_binding(void *p_token, const GDExtensionInstanceBindingCallbacks *p_callbacks);
993
// Used on creation by binding only.
994
void set_instance_binding(void *p_token, void *p_binding, const GDExtensionInstanceBindingCallbacks *p_callbacks);
995
bool has_instance_binding(void *p_token);
996
void free_instance_binding(void *p_token);
997
998
#ifdef TOOLS_ENABLED
999
void clear_internal_extension();
1000
void reset_internal_extension(ObjectGDExtension *p_extension);
1001
bool is_extension_placeholder() const { return _extension && _extension->is_placeholder; }
1002
#endif
1003
1004
void clear_internal_resource_paths();
1005
1006
_ALWAYS_INLINE_ bool is_ref_counted() const { return type_is_reference; }
1007
1008
void cancel_free();
1009
1010
Object();
1011
virtual ~Object();
1012
};
1013
1014
bool predelete_handler(Object *p_object);
1015
void postinitialize_handler(Object *p_object);
1016
1017
class ObjectDB {
1018
// This needs to add up to 63, 1 bit is for reference.
1019
#define OBJECTDB_VALIDATOR_BITS 39
1020
#define OBJECTDB_VALIDATOR_MASK ((uint64_t(1) << OBJECTDB_VALIDATOR_BITS) - 1)
1021
#define OBJECTDB_SLOT_MAX_COUNT_BITS 24
1022
#define OBJECTDB_SLOT_MAX_COUNT_MASK ((uint64_t(1) << OBJECTDB_SLOT_MAX_COUNT_BITS) - 1)
1023
#define OBJECTDB_REFERENCE_BIT (uint64_t(1) << (OBJECTDB_SLOT_MAX_COUNT_BITS + OBJECTDB_VALIDATOR_BITS))
1024
1025
struct ObjectSlot { // 128 bits per slot.
1026
uint64_t validator : OBJECTDB_VALIDATOR_BITS;
1027
uint64_t next_free : OBJECTDB_SLOT_MAX_COUNT_BITS;
1028
uint64_t is_ref_counted : 1;
1029
Object *object = nullptr;
1030
};
1031
1032
static SpinLock spin_lock;
1033
static uint32_t slot_count;
1034
static uint32_t slot_max;
1035
static ObjectSlot *object_slots;
1036
static uint64_t validator_counter;
1037
1038
friend class Object;
1039
friend void unregister_core_types();
1040
static void cleanup();
1041
1042
static ObjectID add_instance(Object *p_object);
1043
static void remove_instance(Object *p_object);
1044
1045
friend void register_core_types();
1046
static void setup();
1047
1048
public:
1049
typedef void (*DebugFunc)(Object *p_obj);
1050
1051
_ALWAYS_INLINE_ static Object *get_instance(ObjectID p_instance_id) {
1052
uint64_t id = p_instance_id;
1053
uint32_t slot = id & OBJECTDB_SLOT_MAX_COUNT_MASK;
1054
1055
ERR_FAIL_COND_V(slot >= slot_max, nullptr); // This should never happen unless RID is corrupted.
1056
1057
spin_lock.lock();
1058
1059
uint64_t validator = (id >> OBJECTDB_SLOT_MAX_COUNT_BITS) & OBJECTDB_VALIDATOR_MASK;
1060
1061
if (unlikely(object_slots[slot].validator != validator)) {
1062
spin_lock.unlock();
1063
return nullptr;
1064
}
1065
1066
Object *object = object_slots[slot].object;
1067
1068
spin_lock.unlock();
1069
1070
return object;
1071
}
1072
1073
template <typename T>
1074
_ALWAYS_INLINE_ static T *get_instance(ObjectID p_instance_id) {
1075
return Object::cast_to<T>(get_instance(p_instance_id));
1076
}
1077
1078
template <typename T>
1079
_ALWAYS_INLINE_ static Ref<T> get_ref(ObjectID p_instance_id); // Defined in ref_counted.h
1080
1081
static void debug_objects(DebugFunc p_func);
1082
static int get_object_count();
1083
};
1084
1085