object.h 58 KB

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