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