object.h 58 KB

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