object.h 59 KB

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