object.h 56 KB

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