object.h 56 KB

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