object.h 58 KB

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