object.h 58 KB

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