object.h 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  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. static constexpr bool _class_is_enabled = !bool(GD_IS_DEFINED(ClassDB_Disable_##m_class)) && m_inherits::_class_is_enabled; \
  338. virtual String get_class() const override { \
  339. if (_get_extension()) { \
  340. return _get_extension()->class_name.operator String(); \
  341. } \
  342. return String(#m_class); \
  343. } \
  344. virtual const StringName *_get_class_namev() const override { \
  345. static StringName _class_name_static; \
  346. if (unlikely(!_class_name_static)) { \
  347. StringName::assign_static_unique_class_name(&_class_name_static, #m_class); \
  348. } \
  349. return &_class_name_static; \
  350. } \
  351. static _FORCE_INLINE_ void *get_class_ptr_static() { \
  352. static int ptr; \
  353. return &ptr; \
  354. } \
  355. static _FORCE_INLINE_ String get_class_static() { \
  356. return String(#m_class); \
  357. } \
  358. static _FORCE_INLINE_ String get_parent_class_static() { \
  359. return m_inherits::get_class_static(); \
  360. } \
  361. static void get_inheritance_list_static(List<String> *p_inheritance_list) { \
  362. m_inherits::get_inheritance_list_static(p_inheritance_list); \
  363. p_inheritance_list->push_back(String(#m_class)); \
  364. } \
  365. virtual bool is_class(const String &p_class) const override { \
  366. if (_get_extension() && _get_extension()->is_class(p_class)) { \
  367. return true; \
  368. } \
  369. return (p_class == (#m_class)) ? true : m_inherits::is_class(p_class); \
  370. } \
  371. 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); } \
  372. \
  373. static void get_valid_parents_static(List<String> *p_parents) { \
  374. if (m_class::_get_valid_parents_static != m_inherits::_get_valid_parents_static) { \
  375. m_class::_get_valid_parents_static(p_parents); \
  376. } \
  377. \
  378. m_inherits::get_valid_parents_static(p_parents); \
  379. } \
  380. \
  381. protected: \
  382. _FORCE_INLINE_ static void (*_get_bind_methods())() { \
  383. return &m_class::_bind_methods; \
  384. } \
  385. \
  386. public: \
  387. static void initialize_class() { \
  388. static bool initialized = false; \
  389. if (initialized) { \
  390. return; \
  391. } \
  392. m_inherits::initialize_class(); \
  393. ::ClassDB::_add_class<m_class>(); \
  394. if (m_class::_get_bind_methods() != m_inherits::_get_bind_methods()) { \
  395. _bind_methods(); \
  396. } \
  397. initialized = true; \
  398. } \
  399. \
  400. protected: \
  401. virtual void _initialize_classv() override { \
  402. initialize_class(); \
  403. } \
  404. _FORCE_INLINE_ bool (Object::*_get_get() const)(const StringName &p_name, Variant &) const { \
  405. return (bool(Object::*)(const StringName &, Variant &) const) & m_class::_get; \
  406. } \
  407. virtual bool _getv(const StringName &p_name, Variant &r_ret) const override { \
  408. if (m_class::_get_get() != m_inherits::_get_get()) { \
  409. if (_get(p_name, r_ret)) { \
  410. return true; \
  411. } \
  412. } \
  413. return m_inherits::_getv(p_name, r_ret); \
  414. } \
  415. _FORCE_INLINE_ bool (Object::*_get_set() const)(const StringName &p_name, const Variant &p_property) { \
  416. return (bool(Object::*)(const StringName &, const Variant &)) & m_class::_set; \
  417. } \
  418. virtual bool _setv(const StringName &p_name, const Variant &p_property) override { \
  419. if (m_inherits::_setv(p_name, p_property)) { \
  420. return true; \
  421. } \
  422. if (m_class::_get_set() != m_inherits::_get_set()) { \
  423. return _set(p_name, p_property); \
  424. } \
  425. return false; \
  426. } \
  427. _FORCE_INLINE_ void (Object::*_get_get_property_list() const)(List<PropertyInfo> * p_list) const { \
  428. return (void(Object::*)(List<PropertyInfo> *) const) & m_class::_get_property_list; \
  429. } \
  430. virtual void _get_property_listv(List<PropertyInfo> *p_list, bool p_reversed) const override { \
  431. if (!p_reversed) { \
  432. m_inherits::_get_property_listv(p_list, p_reversed); \
  433. } \
  434. p_list->push_back(PropertyInfo(Variant::NIL, get_class_static(), PROPERTY_HINT_NONE, String(), PROPERTY_USAGE_CATEGORY)); \
  435. if (!_is_gpl_reversed()) { \
  436. ::ClassDB::get_property_list(#m_class, p_list, true, this); \
  437. } \
  438. if (m_class::_get_get_property_list() != m_inherits::_get_get_property_list()) { \
  439. _get_property_list(p_list); \
  440. } \
  441. if (_is_gpl_reversed()) { \
  442. ::ClassDB::get_property_list(#m_class, p_list, true, this); \
  443. } \
  444. if (p_reversed) { \
  445. m_inherits::_get_property_listv(p_list, p_reversed); \
  446. } \
  447. } \
  448. _FORCE_INLINE_ void (Object::*_get_validate_property() const)(PropertyInfo & p_property) const { \
  449. return (void(Object::*)(PropertyInfo &) const) & m_class::_validate_property; \
  450. } \
  451. virtual void _validate_propertyv(PropertyInfo &p_property) const override { \
  452. m_inherits::_validate_propertyv(p_property); \
  453. if (m_class::_get_validate_property() != m_inherits::_get_validate_property()) { \
  454. _validate_property(p_property); \
  455. } \
  456. } \
  457. _FORCE_INLINE_ bool (Object::*_get_property_can_revert() const)(const StringName &p_name) const { \
  458. return (bool(Object::*)(const StringName &) const) & m_class::_property_can_revert; \
  459. } \
  460. virtual bool _property_can_revertv(const StringName &p_name) const override { \
  461. if (m_class::_get_property_can_revert() != m_inherits::_get_property_can_revert()) { \
  462. if (_property_can_revert(p_name)) { \
  463. return true; \
  464. } \
  465. } \
  466. return m_inherits::_property_can_revertv(p_name); \
  467. } \
  468. _FORCE_INLINE_ bool (Object::*_get_property_get_revert() const)(const StringName &p_name, Variant &) const { \
  469. return (bool(Object::*)(const StringName &, Variant &) const) & m_class::_property_get_revert; \
  470. } \
  471. virtual bool _property_get_revertv(const StringName &p_name, Variant &r_ret) const override { \
  472. if (m_class::_get_property_get_revert() != m_inherits::_get_property_get_revert()) { \
  473. if (_property_get_revert(p_name, r_ret)) { \
  474. return true; \
  475. } \
  476. } \
  477. return m_inherits::_property_get_revertv(p_name, r_ret); \
  478. } \
  479. _FORCE_INLINE_ void (Object::*_get_notification() const)(int) { \
  480. return (void(Object::*)(int)) & m_class::_notification; \
  481. } \
  482. virtual void _notificationv(int p_notification, bool p_reversed) override { \
  483. if (!p_reversed) { \
  484. m_inherits::_notificationv(p_notification, p_reversed); \
  485. } \
  486. if (m_class::_get_notification() != m_inherits::_get_notification()) { \
  487. _notification(p_notification); \
  488. } \
  489. if (p_reversed) { \
  490. m_inherits::_notificationv(p_notification, p_reversed); \
  491. } \
  492. } \
  493. \
  494. private:
  495. #define OBJ_SAVE_TYPE(m_class) \
  496. public: \
  497. virtual String get_save_class() const override { return #m_class; } \
  498. \
  499. private:
  500. class ScriptInstance;
  501. class Object {
  502. public:
  503. enum ConnectFlags {
  504. CONNECT_DEFERRED = 1,
  505. CONNECT_PERSIST = 2, // hint for scene to save this connection
  506. CONNECT_ONE_SHOT = 4,
  507. CONNECT_REFERENCE_COUNTED = 8,
  508. CONNECT_INHERITED = 16, // Used in editor builds.
  509. };
  510. struct Connection {
  511. ::Signal signal;
  512. Callable callable;
  513. uint32_t flags = 0;
  514. bool operator<(const Connection &p_conn) const;
  515. operator Variant() const;
  516. Connection() {}
  517. Connection(const Variant &p_variant);
  518. };
  519. private:
  520. #ifdef DEBUG_ENABLED
  521. friend struct _ObjectDebugLock;
  522. #endif
  523. friend bool predelete_handler(Object *);
  524. friend void postinitialize_handler(Object *);
  525. ObjectGDExtension *_extension = nullptr;
  526. GDExtensionClassInstancePtr _extension_instance = nullptr;
  527. struct SignalData {
  528. struct Slot {
  529. int reference_count = 0;
  530. Connection conn;
  531. List<Connection>::Element *cE = nullptr;
  532. };
  533. MethodInfo user;
  534. HashMap<Callable, Slot, HashableHasher<Callable>> slot_map;
  535. };
  536. HashMap<StringName, SignalData> signal_map;
  537. List<Connection> connections;
  538. #ifdef DEBUG_ENABLED
  539. SafeRefCount _lock_index;
  540. #endif
  541. bool _block_signals = false;
  542. int _predelete_ok = 0;
  543. ObjectID _instance_id;
  544. bool _predelete();
  545. void _postinitialize();
  546. bool _can_translate = true;
  547. bool _emitting = false;
  548. #ifdef TOOLS_ENABLED
  549. bool _edited = false;
  550. uint32_t _edited_version = 0;
  551. HashSet<String> editor_section_folding;
  552. #endif
  553. ScriptInstance *script_instance = nullptr;
  554. Variant script; // Reference does not exist yet, store it in a Variant.
  555. HashMap<StringName, Variant> metadata;
  556. HashMap<StringName, Variant *> metadata_properties;
  557. mutable const StringName *_class_name_ptr = nullptr;
  558. void _add_user_signal(const String &p_name, const Array &p_args = Array());
  559. bool _has_user_signal(const StringName &p_name) const;
  560. Error _emit_signal(const Variant **p_args, int p_argcount, Callable::CallError &r_error);
  561. TypedArray<Dictionary> _get_signal_list() const;
  562. TypedArray<Dictionary> _get_signal_connection_list(const StringName &p_signal) const;
  563. TypedArray<Dictionary> _get_incoming_connections() const;
  564. void _set_bind(const StringName &p_set, const Variant &p_value);
  565. Variant _get_bind(const StringName &p_name) const;
  566. void _set_indexed_bind(const NodePath &p_name, const Variant &p_value);
  567. Variant _get_indexed_bind(const NodePath &p_name) const;
  568. _FORCE_INLINE_ void _construct_object(bool p_reference);
  569. friend class RefCounted;
  570. bool type_is_reference = false;
  571. std::mutex _instance_binding_mutex;
  572. struct InstanceBinding {
  573. void *binding = nullptr;
  574. void *token = nullptr;
  575. GDExtensionInstanceBindingFreeCallback free_callback = nullptr;
  576. GDExtensionInstanceBindingReferenceCallback reference_callback = nullptr;
  577. };
  578. InstanceBinding *_instance_bindings = nullptr;
  579. uint32_t _instance_binding_count = 0;
  580. Object(bool p_reference);
  581. protected:
  582. _FORCE_INLINE_ bool _instance_binding_reference(bool p_reference) {
  583. bool can_die = true;
  584. if (_instance_bindings) {
  585. _instance_binding_mutex.lock();
  586. for (uint32_t i = 0; i < _instance_binding_count; i++) {
  587. if (_instance_bindings[i].reference_callback) {
  588. if (!_instance_bindings[i].reference_callback(_instance_bindings[i].token, _instance_bindings[i].binding, p_reference)) {
  589. can_die = false;
  590. }
  591. }
  592. }
  593. _instance_binding_mutex.unlock();
  594. }
  595. return can_die;
  596. }
  597. friend class GDExtensionMethodBind;
  598. _ALWAYS_INLINE_ const ObjectGDExtension *_get_extension() const { return _extension; }
  599. _ALWAYS_INLINE_ GDExtensionClassInstancePtr _get_extension_instance() const { return _extension_instance; }
  600. virtual void _initialize_classv() { initialize_class(); }
  601. virtual bool _setv(const StringName &p_name, const Variant &p_property) { return false; };
  602. virtual bool _getv(const StringName &p_name, Variant &r_property) const { return false; };
  603. virtual void _get_property_listv(List<PropertyInfo> *p_list, bool p_reversed) const {};
  604. virtual void _validate_propertyv(PropertyInfo &p_property) const {};
  605. virtual bool _property_can_revertv(const StringName &p_name) const { return false; };
  606. virtual bool _property_get_revertv(const StringName &p_name, Variant &r_property) const { return false; };
  607. virtual void _notificationv(int p_notification, bool p_reversed) {}
  608. static void _bind_methods();
  609. bool _set(const StringName &p_name, const Variant &p_property) { return false; };
  610. bool _get(const StringName &p_name, Variant &r_property) const { return false; };
  611. void _get_property_list(List<PropertyInfo> *p_list) const {};
  612. void _validate_property(PropertyInfo &p_property) const {};
  613. bool _property_can_revert(const StringName &p_name) const { return false; };
  614. bool _property_get_revert(const StringName &p_name, Variant &r_property) const { return false; };
  615. void _notification(int p_notification) {}
  616. _FORCE_INLINE_ static void (*_get_bind_methods())() {
  617. return &Object::_bind_methods;
  618. }
  619. _FORCE_INLINE_ bool (Object::*_get_get() const)(const StringName &p_name, Variant &r_ret) const {
  620. return &Object::_get;
  621. }
  622. _FORCE_INLINE_ bool (Object::*_get_set() const)(const StringName &p_name, const Variant &p_property) {
  623. return &Object::_set;
  624. }
  625. _FORCE_INLINE_ void (Object::*_get_get_property_list() const)(List<PropertyInfo> *p_list) const {
  626. return &Object::_get_property_list;
  627. }
  628. _FORCE_INLINE_ void (Object::*_get_validate_property() const)(PropertyInfo &p_property) const {
  629. return &Object::_validate_property;
  630. }
  631. _FORCE_INLINE_ bool (Object::*_get_property_can_revert() const)(const StringName &p_name) const {
  632. return &Object::_property_can_revert;
  633. }
  634. _FORCE_INLINE_ bool (Object::*_get_property_get_revert() const)(const StringName &p_name, Variant &) const {
  635. return &Object::_property_get_revert;
  636. }
  637. _FORCE_INLINE_ void (Object::*_get_notification() const)(int) {
  638. return &Object::_notification;
  639. }
  640. static void get_valid_parents_static(List<String> *p_parents);
  641. static void _get_valid_parents_static(List<String> *p_parents);
  642. Variant _call_bind(const Variant **p_args, int p_argcount, Callable::CallError &r_error);
  643. Variant _call_deferred_bind(const Variant **p_args, int p_argcount, Callable::CallError &r_error);
  644. virtual const StringName *_get_class_namev() const {
  645. static StringName _class_name_static;
  646. if (unlikely(!_class_name_static)) {
  647. StringName::assign_static_unique_class_name(&_class_name_static, "Object");
  648. }
  649. return &_class_name_static;
  650. }
  651. TypedArray<StringName> _get_meta_list_bind() const;
  652. TypedArray<Dictionary> _get_property_list_bind() const;
  653. TypedArray<Dictionary> _get_method_list_bind() const;
  654. void _clear_internal_resource_paths(const Variant &p_var);
  655. friend class ClassDB;
  656. bool _disconnect(const StringName &p_signal, const Callable &p_callable, bool p_force = false);
  657. public: // Should be protected, but bug in clang++.
  658. static void initialize_class();
  659. _FORCE_INLINE_ static void register_custom_data_to_otdb() {}
  660. public:
  661. static constexpr bool _class_is_enabled = true;
  662. void notify_property_list_changed();
  663. static void *get_class_ptr_static() {
  664. static int ptr;
  665. return &ptr;
  666. }
  667. bool _is_gpl_reversed() const { return false; }
  668. void detach_from_objectdb();
  669. _FORCE_INLINE_ ObjectID get_instance_id() const { return _instance_id; }
  670. template <class T>
  671. static T *cast_to(Object *p_object) {
  672. return dynamic_cast<T *>(p_object);
  673. }
  674. template <class T>
  675. static const T *cast_to(const Object *p_object) {
  676. return dynamic_cast<const T *>(p_object);
  677. }
  678. enum {
  679. NOTIFICATION_POSTINITIALIZE = 0,
  680. NOTIFICATION_PREDELETE = 1
  681. };
  682. /* TYPE API */
  683. static void get_inheritance_list_static(List<String> *p_inheritance_list) { p_inheritance_list->push_back("Object"); }
  684. static String get_class_static() { return "Object"; }
  685. static String get_parent_class_static() { return String(); }
  686. virtual String get_class() const {
  687. if (_extension) {
  688. return _extension->class_name.operator String();
  689. }
  690. return "Object";
  691. }
  692. virtual String get_save_class() const { return get_class(); } //class stored when saving
  693. virtual bool is_class(const String &p_class) const {
  694. if (_extension && _extension->is_class(p_class)) {
  695. return true;
  696. }
  697. return (p_class == "Object");
  698. }
  699. virtual bool is_class_ptr(void *p_ptr) const { return get_class_ptr_static() == p_ptr; }
  700. _FORCE_INLINE_ const StringName &get_class_name() const {
  701. if (_extension) {
  702. // Can't put inside the unlikely as constructor can run it
  703. return _extension->class_name;
  704. }
  705. if (unlikely(!_class_name_ptr)) {
  706. // While class is initializing / deinitializing, constructors and destructurs
  707. // need access to the proper class at the proper stage.
  708. return *_get_class_namev();
  709. }
  710. return *_class_name_ptr;
  711. }
  712. StringName get_class_name_for_extension(const GDExtension *p_library) const;
  713. /* IAPI */
  714. void set(const StringName &p_name, const Variant &p_value, bool *r_valid = nullptr);
  715. Variant get(const StringName &p_name, bool *r_valid = nullptr) const;
  716. void set_indexed(const Vector<StringName> &p_names, const Variant &p_value, bool *r_valid = nullptr);
  717. Variant get_indexed(const Vector<StringName> &p_names, bool *r_valid = nullptr) const;
  718. void get_property_list(List<PropertyInfo> *p_list, bool p_reversed = false) const;
  719. void validate_property(PropertyInfo &p_property) const;
  720. bool property_can_revert(const StringName &p_name) const;
  721. Variant property_get_revert(const StringName &p_name) const;
  722. bool has_method(const StringName &p_method) const;
  723. void get_method_list(List<MethodInfo> *p_list) const;
  724. Variant callv(const StringName &p_method, const Array &p_args);
  725. virtual Variant callp(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error);
  726. virtual Variant call_const(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error);
  727. template <typename... VarArgs>
  728. Variant call(const StringName &p_method, VarArgs... p_args) {
  729. Variant args[sizeof...(p_args) + 1] = { p_args..., Variant() }; // +1 makes sure zero sized arrays are also supported.
  730. const Variant *argptrs[sizeof...(p_args) + 1];
  731. for (uint32_t i = 0; i < sizeof...(p_args); i++) {
  732. argptrs[i] = &args[i];
  733. }
  734. Callable::CallError cerr;
  735. return callp(p_method, sizeof...(p_args) == 0 ? nullptr : (const Variant **)argptrs, sizeof...(p_args), cerr);
  736. }
  737. void notification(int p_notification, bool p_reversed = false);
  738. virtual String to_string();
  739. // Used mainly by script, get and set all INCLUDING string.
  740. virtual Variant getvar(const Variant &p_key, bool *r_valid = nullptr) const;
  741. virtual void setvar(const Variant &p_key, const Variant &p_value, bool *r_valid = nullptr);
  742. /* SCRIPT */
  743. // When in debug, some non-virtual functions can be overridden for multithreaded guards.
  744. #ifdef DEBUG_ENABLED
  745. #define MTVIRTUAL virtual
  746. #else
  747. #define MTVIRTUAL
  748. #endif
  749. MTVIRTUAL void set_script(const Variant &p_script);
  750. MTVIRTUAL Variant get_script() const;
  751. MTVIRTUAL bool has_meta(const StringName &p_name) const;
  752. MTVIRTUAL void set_meta(const StringName &p_name, const Variant &p_value);
  753. MTVIRTUAL void remove_meta(const StringName &p_name);
  754. MTVIRTUAL Variant get_meta(const StringName &p_name, const Variant &p_default = Variant()) const;
  755. MTVIRTUAL void get_meta_list(List<StringName> *p_list) const;
  756. #ifdef TOOLS_ENABLED
  757. void set_edited(bool p_edited);
  758. bool is_edited() const;
  759. // This function is used to check when something changed beyond a point, it's used mainly for generating previews.
  760. uint32_t get_edited_version() const;
  761. #endif
  762. void set_script_instance(ScriptInstance *p_instance);
  763. _FORCE_INLINE_ ScriptInstance *get_script_instance() const { return script_instance; }
  764. // Some script languages can't control instance creation, so this function eases the process.
  765. void set_script_and_instance(const Variant &p_script, ScriptInstance *p_instance);
  766. void add_user_signal(const MethodInfo &p_signal);
  767. template <typename... VarArgs>
  768. Error emit_signal(const StringName &p_name, VarArgs... p_args) {
  769. Variant args[sizeof...(p_args) + 1] = { p_args..., Variant() }; // +1 makes sure zero sized arrays are also supported.
  770. const Variant *argptrs[sizeof...(p_args) + 1];
  771. for (uint32_t i = 0; i < sizeof...(p_args); i++) {
  772. argptrs[i] = &args[i];
  773. }
  774. return emit_signalp(p_name, sizeof...(p_args) == 0 ? nullptr : (const Variant **)argptrs, sizeof...(p_args));
  775. }
  776. MTVIRTUAL Error emit_signalp(const StringName &p_name, const Variant **p_args, int p_argcount);
  777. MTVIRTUAL bool has_signal(const StringName &p_name) const;
  778. MTVIRTUAL void get_signal_list(List<MethodInfo> *p_signals) const;
  779. MTVIRTUAL void get_signal_connection_list(const StringName &p_signal, List<Connection> *p_connections) const;
  780. MTVIRTUAL void get_all_signal_connections(List<Connection> *p_connections) const;
  781. MTVIRTUAL int get_persistent_signal_connection_count() const;
  782. MTVIRTUAL void get_signals_connected_to_this(List<Connection> *p_connections) const;
  783. MTVIRTUAL Error connect(const StringName &p_signal, const Callable &p_callable, uint32_t p_flags = 0);
  784. MTVIRTUAL void disconnect(const StringName &p_signal, const Callable &p_callable);
  785. MTVIRTUAL bool is_connected(const StringName &p_signal, const Callable &p_callable) const;
  786. template <typename... VarArgs>
  787. void call_deferred(const StringName &p_name, VarArgs... p_args) {
  788. MessageQueue::get_singleton()->push_call(this, p_name, p_args...);
  789. }
  790. void set_deferred(const StringName &p_property, const Variant &p_value);
  791. void set_block_signals(bool p_block);
  792. bool is_blocking_signals() const;
  793. Variant::Type get_static_property_type(const StringName &p_property, bool *r_valid = nullptr) const;
  794. Variant::Type get_static_property_type_indexed(const Vector<StringName> &p_path, bool *r_valid = nullptr) const;
  795. virtual void get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const;
  796. // Translate message (internationalization).
  797. String tr(const StringName &p_message, const StringName &p_context = "") const;
  798. String tr_n(const StringName &p_message, const StringName &p_message_plural, int p_n, const StringName &p_context = "") const;
  799. bool _is_queued_for_deletion = false; // Set to true by SceneTree::queue_delete().
  800. bool is_queued_for_deletion() const;
  801. _FORCE_INLINE_ void set_message_translation(bool p_enable) { _can_translate = p_enable; }
  802. _FORCE_INLINE_ bool can_translate_messages() const { return _can_translate; }
  803. #ifdef TOOLS_ENABLED
  804. void editor_set_section_unfold(const String &p_section, bool p_unfolded);
  805. bool editor_is_section_unfolded(const String &p_section);
  806. const HashSet<String> &editor_get_section_folding() const { return editor_section_folding; }
  807. void editor_clear_section_folding() { editor_section_folding.clear(); }
  808. #endif
  809. // Used by script languages to store binding data.
  810. void *get_instance_binding(void *p_token, const GDExtensionInstanceBindingCallbacks *p_callbacks);
  811. // Used on creation by binding only.
  812. void set_instance_binding(void *p_token, void *p_binding, const GDExtensionInstanceBindingCallbacks *p_callbacks);
  813. bool has_instance_binding(void *p_token);
  814. void clear_internal_resource_paths();
  815. _ALWAYS_INLINE_ bool is_ref_counted() const { return type_is_reference; }
  816. void cancel_free();
  817. Object();
  818. virtual ~Object();
  819. };
  820. bool predelete_handler(Object *p_object);
  821. void postinitialize_handler(Object *p_object);
  822. class ObjectDB {
  823. // This needs to add up to 63, 1 bit is for reference.
  824. #define OBJECTDB_VALIDATOR_BITS 39
  825. #define OBJECTDB_VALIDATOR_MASK ((uint64_t(1) << OBJECTDB_VALIDATOR_BITS) - 1)
  826. #define OBJECTDB_SLOT_MAX_COUNT_BITS 24
  827. #define OBJECTDB_SLOT_MAX_COUNT_MASK ((uint64_t(1) << OBJECTDB_SLOT_MAX_COUNT_BITS) - 1)
  828. #define OBJECTDB_REFERENCE_BIT (uint64_t(1) << (OBJECTDB_SLOT_MAX_COUNT_BITS + OBJECTDB_VALIDATOR_BITS))
  829. struct ObjectSlot { // 128 bits per slot.
  830. uint64_t validator : OBJECTDB_VALIDATOR_BITS;
  831. uint64_t next_free : OBJECTDB_SLOT_MAX_COUNT_BITS;
  832. uint64_t is_ref_counted : 1;
  833. Object *object = nullptr;
  834. };
  835. static SpinLock spin_lock;
  836. static uint32_t slot_count;
  837. static uint32_t slot_max;
  838. static ObjectSlot *object_slots;
  839. static uint64_t validator_counter;
  840. friend class Object;
  841. friend void unregister_core_types();
  842. static void cleanup();
  843. static ObjectID add_instance(Object *p_object);
  844. static void remove_instance(Object *p_object);
  845. friend void register_core_types();
  846. static void setup();
  847. public:
  848. typedef void (*DebugFunc)(Object *p_obj);
  849. _ALWAYS_INLINE_ static Object *get_instance(ObjectID p_instance_id) {
  850. uint64_t id = p_instance_id;
  851. uint32_t slot = id & OBJECTDB_SLOT_MAX_COUNT_MASK;
  852. ERR_FAIL_COND_V(slot >= slot_max, nullptr); // This should never happen unless RID is corrupted.
  853. spin_lock.lock();
  854. uint64_t validator = (id >> OBJECTDB_SLOT_MAX_COUNT_BITS) & OBJECTDB_VALIDATOR_MASK;
  855. if (unlikely(object_slots[slot].validator != validator)) {
  856. spin_lock.unlock();
  857. return nullptr;
  858. }
  859. Object *object = object_slots[slot].object;
  860. spin_lock.unlock();
  861. return object;
  862. }
  863. static void debug_objects(DebugFunc p_func);
  864. static int get_object_count();
  865. };
  866. #endif // OBJECT_H