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