object.h 58 KB

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