object.h 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994
  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/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_less][,hide_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_OBJECT_ID,
  71. PROPERTY_HINT_TYPE_STRING, ///< a type string, the hint is the base type to choose
  72. PROPERTY_HINT_NODE_PATH_TO_EDITED_NODE, ///< so something else can provide this (used in scripts)
  73. PROPERTY_HINT_OBJECT_TOO_BIG, ///< object is too big to send
  74. PROPERTY_HINT_NODE_PATH_VALID_TYPES,
  75. PROPERTY_HINT_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_GLOBAL_SAVE_FILE, ///< a file path must be passed, hint_text (optionally) is a filter "*.png,*.wav,*.doc,". This opens a save dialog
  77. PROPERTY_HINT_INT_IS_OBJECTID,
  78. PROPERTY_HINT_INT_IS_POINTER,
  79. PROPERTY_HINT_ARRAY_TYPE,
  80. PROPERTY_HINT_LOCALE_ID,
  81. PROPERTY_HINT_LOCALIZABLE_STRING,
  82. PROPERTY_HINT_NODE_TYPE, ///< a node object type
  83. PROPERTY_HINT_HIDE_QUATERNION_EDIT, /// Only Node3D::transform should hide the quaternion editor.
  84. PROPERTY_HINT_PASSWORD,
  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_DEFAULT = PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_EDITOR,
  118. PROPERTY_USAGE_NO_EDITOR = PROPERTY_USAGE_STORAGE,
  119. };
  120. #define ADD_SIGNAL(m_signal) ::ClassDB::add_signal(get_class_static(), m_signal)
  121. #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))
  122. #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)
  123. #define ADD_PROPERTY_DEFAULT(m_property, m_default) ::ClassDB::set_property_default_value(get_class_static(), m_property, m_default)
  124. #define ADD_GROUP(m_name, m_prefix) ::ClassDB::add_property_group(get_class_static(), m_name, m_prefix)
  125. #define ADD_GROUP_INDENT(m_name, m_prefix, m_depth) ::ClassDB::add_property_group(get_class_static(), m_name, m_prefix, m_depth)
  126. #define ADD_SUBGROUP(m_name, m_prefix) ::ClassDB::add_property_subgroup(get_class_static(), m_name, m_prefix)
  127. #define ADD_SUBGROUP_INDENT(m_name, m_prefix, m_depth) ::ClassDB::add_property_subgroup(get_class_static(), m_name, m_prefix, m_depth)
  128. #define ADD_LINKED_PROPERTY(m_property, m_linked_property) ::ClassDB::add_linked_property(get_class_static(), m_property, m_linked_property)
  129. #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)
  130. #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)
  131. #define ADD_ARRAY(m_array_path, m_prefix) ClassDB::add_property_array(get_class_static(), m_array_path, m_prefix)
  132. // Helper macro to use with PROPERTY_HINT_ARRAY_TYPE for arrays of specific resources:
  133. // PropertyInfo(Variant::ARRAY, "fallbacks", PROPERTY_HINT_ARRAY_TYPE, MAKE_RESOURCE_TYPE_HINT("Font")
  134. #define MAKE_RESOURCE_TYPE_HINT(m_type) vformat("%s/%s:%s", Variant::OBJECT, PROPERTY_HINT_RESOURCE_TYPE, m_type)
  135. struct PropertyInfo {
  136. Variant::Type type = Variant::NIL;
  137. String name;
  138. StringName class_name; // For classes
  139. PropertyHint hint = PROPERTY_HINT_NONE;
  140. String hint_string;
  141. uint32_t usage = PROPERTY_USAGE_DEFAULT;
  142. // If you are thinking about adding another member to this class, ask the maintainer (Juan) first.
  143. _FORCE_INLINE_ PropertyInfo added_usage(uint32_t p_fl) const {
  144. PropertyInfo pi = *this;
  145. pi.usage |= p_fl;
  146. return pi;
  147. }
  148. operator Dictionary() const;
  149. static PropertyInfo from_dict(const Dictionary &p_dict);
  150. PropertyInfo() {}
  151. 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()) :
  152. type(p_type),
  153. name(p_name),
  154. hint(p_hint),
  155. hint_string(p_hint_string),
  156. usage(p_usage) {
  157. if (hint == PROPERTY_HINT_RESOURCE_TYPE) {
  158. class_name = hint_string;
  159. } else {
  160. class_name = p_class_name;
  161. }
  162. }
  163. PropertyInfo(const StringName &p_class_name) :
  164. type(Variant::OBJECT),
  165. class_name(p_class_name) {}
  166. explicit PropertyInfo(const GDExtensionPropertyInfo &pinfo) :
  167. type((Variant::Type)pinfo.type),
  168. name(*reinterpret_cast<StringName *>(pinfo.name)),
  169. class_name(*reinterpret_cast<StringName *>(pinfo.class_name)),
  170. hint((PropertyHint)pinfo.hint),
  171. hint_string(*reinterpret_cast<String *>(pinfo.hint_string)),
  172. usage(pinfo.usage) {}
  173. bool operator==(const PropertyInfo &p_info) const {
  174. return ((type == p_info.type) &&
  175. (name == p_info.name) &&
  176. (class_name == p_info.class_name) &&
  177. (hint == p_info.hint) &&
  178. (hint_string == p_info.hint_string) &&
  179. (usage == p_info.usage));
  180. }
  181. bool operator<(const PropertyInfo &p_info) const {
  182. return name < p_info.name;
  183. }
  184. };
  185. TypedArray<Dictionary> convert_property_list(const List<PropertyInfo> *p_list);
  186. enum MethodFlags {
  187. METHOD_FLAG_NORMAL = 1,
  188. METHOD_FLAG_EDITOR = 2,
  189. METHOD_FLAG_CONST = 4,
  190. METHOD_FLAG_VIRTUAL = 8,
  191. METHOD_FLAG_VARARG = 16,
  192. METHOD_FLAG_STATIC = 32,
  193. METHOD_FLAG_OBJECT_CORE = 64,
  194. METHOD_FLAGS_DEFAULT = METHOD_FLAG_NORMAL,
  195. };
  196. struct MethodInfo {
  197. String name;
  198. PropertyInfo return_val;
  199. uint32_t flags = METHOD_FLAGS_DEFAULT;
  200. int id = 0;
  201. List<PropertyInfo> arguments;
  202. Vector<Variant> default_arguments;
  203. int return_val_metadata = 0;
  204. Vector<int> arguments_metadata;
  205. int get_argument_meta(int p_arg) const {
  206. ERR_FAIL_COND_V(p_arg < -1 || p_arg > arguments.size(), 0);
  207. if (p_arg == -1) {
  208. return return_val_metadata;
  209. }
  210. return arguments_metadata.size() > p_arg ? arguments_metadata[p_arg] : 0;
  211. }
  212. inline bool operator==(const MethodInfo &p_method) const { return id == p_method.id; }
  213. inline bool operator<(const MethodInfo &p_method) const { return id == p_method.id ? (name < p_method.name) : (id < p_method.id); }
  214. operator Dictionary() const;
  215. static MethodInfo from_dict(const Dictionary &p_dict);
  216. MethodInfo() {}
  217. explicit MethodInfo(const GDExtensionMethodInfo &pinfo) :
  218. name(*reinterpret_cast<StringName *>(pinfo.name)),
  219. return_val(PropertyInfo(pinfo.return_value)),
  220. flags(pinfo.flags),
  221. id(pinfo.id) {
  222. for (uint32_t j = 0; j < pinfo.argument_count; j++) {
  223. arguments.push_back(PropertyInfo(pinfo.arguments[j]));
  224. }
  225. const Variant *def_values = (const Variant *)pinfo.default_arguments;
  226. for (uint32_t j = 0; j < pinfo.default_argument_count; j++) {
  227. default_arguments.push_back(def_values[j]);
  228. }
  229. }
  230. void _push_params(const PropertyInfo &p_param) {
  231. arguments.push_back(p_param);
  232. }
  233. template <typename... VarArgs>
  234. void _push_params(const PropertyInfo &p_param, VarArgs... p_params) {
  235. arguments.push_back(p_param);
  236. _push_params(p_params...);
  237. }
  238. MethodInfo(const String &p_name) { name = p_name; }
  239. template <typename... VarArgs>
  240. MethodInfo(const String &p_name, VarArgs... p_params) {
  241. name = p_name;
  242. _push_params(p_params...);
  243. }
  244. MethodInfo(Variant::Type ret) { return_val.type = ret; }
  245. MethodInfo(Variant::Type ret, const String &p_name) {
  246. return_val.type = ret;
  247. name = p_name;
  248. }
  249. template <typename... VarArgs>
  250. MethodInfo(Variant::Type ret, const String &p_name, VarArgs... p_params) {
  251. name = p_name;
  252. return_val.type = ret;
  253. _push_params(p_params...);
  254. }
  255. MethodInfo(const PropertyInfo &p_ret, const String &p_name) {
  256. return_val = p_ret;
  257. name = p_name;
  258. }
  259. template <typename... VarArgs>
  260. MethodInfo(const PropertyInfo &p_ret, const String &p_name, VarArgs... p_params) {
  261. return_val = p_ret;
  262. name = p_name;
  263. _push_params(p_params...);
  264. }
  265. };
  266. // API used to extend in GDExtension and other C compatible compiled languages.
  267. class MethodBind;
  268. struct ObjectGDExtension {
  269. ObjectGDExtension *parent = nullptr;
  270. List<ObjectGDExtension *> children;
  271. StringName parent_class_name;
  272. StringName class_name;
  273. bool editor_class = false;
  274. bool is_virtual = false;
  275. bool is_abstract = false;
  276. GDExtensionClassSet set;
  277. GDExtensionClassGet get;
  278. GDExtensionClassGetPropertyList get_property_list;
  279. GDExtensionClassFreePropertyList free_property_list;
  280. GDExtensionClassPropertyCanRevert property_can_revert;
  281. GDExtensionClassPropertyGetRevert property_get_revert;
  282. GDExtensionClassNotification notification;
  283. GDExtensionClassToString to_string;
  284. GDExtensionClassReference reference;
  285. GDExtensionClassReference unreference;
  286. GDExtensionClassGetRID get_rid;
  287. _FORCE_INLINE_ bool is_class(const String &p_class) const {
  288. const ObjectGDExtension *e = this;
  289. while (e) {
  290. if (p_class == e->class_name.operator String()) {
  291. return true;
  292. }
  293. e = e->parent;
  294. }
  295. return false;
  296. }
  297. void *class_userdata = nullptr;
  298. GDExtensionClassCreateInstance create_instance;
  299. GDExtensionClassFreeInstance free_instance;
  300. GDExtensionClassGetVirtual get_virtual;
  301. };
  302. #define GDVIRTUAL_CALL(m_name, ...) _gdvirtual_##m_name##_call<false>(__VA_ARGS__)
  303. #define GDVIRTUAL_CALL_PTR(m_obj, m_name, ...) m_obj->_gdvirtual_##m_name##_call<false>(__VA_ARGS__)
  304. #define GDVIRTUAL_REQUIRED_CALL(m_name, ...) _gdvirtual_##m_name##_call<true>(__VA_ARGS__)
  305. #define GDVIRTUAL_REQUIRED_CALL_PTR(m_obj, m_name, ...) m_obj->_gdvirtual_##m_name##_call<true>(__VA_ARGS__)
  306. #ifdef DEBUG_METHODS_ENABLED
  307. #define GDVIRTUAL_BIND(m_name, ...) ::ClassDB::add_virtual_method(get_class_static(), _gdvirtual_##m_name##_get_method_info(), true, sarray(__VA_ARGS__));
  308. #else
  309. #define GDVIRTUAL_BIND(m_name, ...)
  310. #endif
  311. #define GDVIRTUAL_IS_OVERRIDDEN(m_name) _gdvirtual_##m_name##_overridden()
  312. #define GDVIRTUAL_IS_OVERRIDDEN_PTR(m_obj, m_name) m_obj->_gdvirtual_##m_name##_overridden()
  313. /*
  314. * The following is an incomprehensible blob of hacks and workarounds to
  315. * compensate for many of the fallacies in C++. As a plus, this macro pretty
  316. * much alone defines the object model.
  317. */
  318. #define REVERSE_GET_PROPERTY_LIST \
  319. public: \
  320. _FORCE_INLINE_ bool _is_gpl_reversed() const { return true; }; \
  321. \
  322. private:
  323. #define UNREVERSE_GET_PROPERTY_LIST \
  324. public: \
  325. _FORCE_INLINE_ bool _is_gpl_reversed() const { return false; }; \
  326. \
  327. private:
  328. #define GDCLASS(m_class, m_inherits) \
  329. private: \
  330. void operator=(const m_class &p_rval) {} \
  331. mutable StringName _class_name; \
  332. friend class ::ClassDB; \
  333. \
  334. public: \
  335. static constexpr bool _class_is_enabled = !bool(GD_IS_DEFINED(ClassDB_Disable_##m_class)) && m_inherits::_class_is_enabled; \
  336. virtual String get_class() const override { \
  337. if (_get_extension()) { \
  338. return _get_extension()->class_name.operator String(); \
  339. } \
  340. return String(#m_class); \
  341. } \
  342. virtual const StringName *_get_class_namev() const override { \
  343. if (_get_extension()) { \
  344. return &_get_extension()->class_name; \
  345. } \
  346. if (!_class_name) { \
  347. _class_name = get_class_static(); \
  348. } \
  349. return &_class_name; \
  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. VMap<Callable, Slot> 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 StringName _class_name;
  558. mutable const StringName *_class_ptr = nullptr;
  559. void _add_user_signal(const String &p_name, const Array &p_args = Array());
  560. bool _has_user_signal(const StringName &p_name) const;
  561. Error _emit_signal(const Variant **p_args, int p_argcount, Callable::CallError &r_error);
  562. TypedArray<Dictionary> _get_signal_list() const;
  563. TypedArray<Dictionary> _get_signal_connection_list(const StringName &p_signal) const;
  564. TypedArray<Dictionary> _get_incoming_connections() const;
  565. void _set_bind(const StringName &p_set, const Variant &p_value);
  566. Variant _get_bind(const StringName &p_name) const;
  567. void _set_indexed_bind(const NodePath &p_name, const Variant &p_value);
  568. Variant _get_indexed_bind(const NodePath &p_name) const;
  569. _FORCE_INLINE_ void _construct_object(bool p_reference);
  570. friend class RefCounted;
  571. bool type_is_reference = false;
  572. std::mutex _instance_binding_mutex;
  573. struct InstanceBinding {
  574. void *binding = nullptr;
  575. void *token = nullptr;
  576. GDExtensionInstanceBindingFreeCallback free_callback = nullptr;
  577. GDExtensionInstanceBindingReferenceCallback reference_callback = nullptr;
  578. };
  579. InstanceBinding *_instance_bindings = nullptr;
  580. uint32_t _instance_binding_count = 0;
  581. Object(bool p_reference);
  582. protected:
  583. _FORCE_INLINE_ bool _instance_binding_reference(bool p_reference) {
  584. bool can_die = true;
  585. if (_instance_bindings) {
  586. _instance_binding_mutex.lock();
  587. for (uint32_t i = 0; i < _instance_binding_count; i++) {
  588. if (_instance_bindings[i].reference_callback) {
  589. if (!_instance_bindings[i].reference_callback(_instance_bindings[i].token, _instance_bindings[i].binding, p_reference)) {
  590. can_die = false;
  591. }
  592. }
  593. }
  594. _instance_binding_mutex.unlock();
  595. }
  596. return can_die;
  597. }
  598. friend class GDExtensionMethodBind;
  599. _ALWAYS_INLINE_ const ObjectGDExtension *_get_extension() const { return _extension; }
  600. _ALWAYS_INLINE_ GDExtensionClassInstancePtr _get_extension_instance() const { return _extension_instance; }
  601. virtual void _initialize_classv() { initialize_class(); }
  602. virtual bool _setv(const StringName &p_name, const Variant &p_property) { return false; };
  603. virtual bool _getv(const StringName &p_name, Variant &r_property) const { return false; };
  604. virtual void _get_property_listv(List<PropertyInfo> *p_list, bool p_reversed) const {};
  605. virtual void _validate_propertyv(PropertyInfo &p_property) const {};
  606. virtual bool _property_can_revertv(const StringName &p_name) const { return false; };
  607. virtual bool _property_get_revertv(const StringName &p_name, Variant &r_property) const { return false; };
  608. virtual void _notificationv(int p_notification, bool p_reversed) {}
  609. static void _bind_methods();
  610. bool _set(const StringName &p_name, const Variant &p_property) { return false; };
  611. bool _get(const StringName &p_name, Variant &r_property) const { return false; };
  612. void _get_property_list(List<PropertyInfo> *p_list) const {};
  613. void _validate_property(PropertyInfo &p_property) const {};
  614. bool _property_can_revert(const StringName &p_name) const { return false; };
  615. bool _property_get_revert(const StringName &p_name, Variant &r_property) const { return false; };
  616. void _notification(int p_notification) {}
  617. _FORCE_INLINE_ static void (*_get_bind_methods())() {
  618. return &Object::_bind_methods;
  619. }
  620. _FORCE_INLINE_ bool (Object::*_get_get() const)(const StringName &p_name, Variant &r_ret) const {
  621. return &Object::_get;
  622. }
  623. _FORCE_INLINE_ bool (Object::*_get_set() const)(const StringName &p_name, const Variant &p_property) {
  624. return &Object::_set;
  625. }
  626. _FORCE_INLINE_ void (Object::*_get_get_property_list() const)(List<PropertyInfo> *p_list) const {
  627. return &Object::_get_property_list;
  628. }
  629. _FORCE_INLINE_ void (Object::*_get_validate_property() const)(PropertyInfo &p_property) const {
  630. return &Object::_validate_property;
  631. }
  632. _FORCE_INLINE_ bool (Object::*_get_property_can_revert() const)(const StringName &p_name) const {
  633. return &Object::_property_can_revert;
  634. }
  635. _FORCE_INLINE_ bool (Object::*_get_property_get_revert() const)(const StringName &p_name, Variant &) const {
  636. return &Object::_property_get_revert;
  637. }
  638. _FORCE_INLINE_ void (Object::*_get_notification() const)(int) {
  639. return &Object::_notification;
  640. }
  641. static void get_valid_parents_static(List<String> *p_parents);
  642. static void _get_valid_parents_static(List<String> *p_parents);
  643. Variant _call_bind(const Variant **p_args, int p_argcount, Callable::CallError &r_error);
  644. Variant _call_deferred_bind(const Variant **p_args, int p_argcount, Callable::CallError &r_error);
  645. virtual const StringName *_get_class_namev() const {
  646. if (!_class_name) {
  647. _class_name = get_class_static();
  648. }
  649. return &_class_name;
  650. }
  651. Vector<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. void _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. return _extension->class_name;
  703. }
  704. if (!_class_ptr) {
  705. return *_get_class_namev();
  706. } else {
  707. return *_class_ptr;
  708. }
  709. }
  710. /* IAPI */
  711. void set(const StringName &p_name, const Variant &p_value, bool *r_valid = nullptr);
  712. Variant get(const StringName &p_name, bool *r_valid = nullptr) const;
  713. void set_indexed(const Vector<StringName> &p_names, const Variant &p_value, bool *r_valid = nullptr);
  714. Variant get_indexed(const Vector<StringName> &p_names, bool *r_valid = nullptr) const;
  715. void get_property_list(List<PropertyInfo> *p_list, bool p_reversed = false) const;
  716. void validate_property(PropertyInfo &p_property) const;
  717. bool property_can_revert(const StringName &p_name) const;
  718. Variant property_get_revert(const StringName &p_name) const;
  719. bool has_method(const StringName &p_method) const;
  720. void get_method_list(List<MethodInfo> *p_list) const;
  721. Variant callv(const StringName &p_method, const Array &p_args);
  722. virtual Variant callp(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error);
  723. virtual Variant call_const(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error);
  724. template <typename... VarArgs>
  725. Variant call(const StringName &p_method, VarArgs... p_args) {
  726. Variant args[sizeof...(p_args) + 1] = { p_args..., Variant() }; // +1 makes sure zero sized arrays are also supported.
  727. const Variant *argptrs[sizeof...(p_args) + 1];
  728. for (uint32_t i = 0; i < sizeof...(p_args); i++) {
  729. argptrs[i] = &args[i];
  730. }
  731. Callable::CallError cerr;
  732. return callp(p_method, sizeof...(p_args) == 0 ? nullptr : (const Variant **)argptrs, sizeof...(p_args), cerr);
  733. }
  734. void notification(int p_notification, bool p_reversed = false);
  735. virtual String to_string();
  736. // Used mainly by script, get and set all INCLUDING string.
  737. virtual Variant getvar(const Variant &p_key, bool *r_valid = nullptr) const;
  738. virtual void setvar(const Variant &p_key, const Variant &p_value, bool *r_valid = nullptr);
  739. /* SCRIPT */
  740. void set_script(const Variant &p_script);
  741. Variant get_script() const;
  742. bool has_meta(const StringName &p_name) const;
  743. void set_meta(const StringName &p_name, const Variant &p_value);
  744. void remove_meta(const StringName &p_name);
  745. Variant get_meta(const StringName &p_name, const Variant &p_default = Variant()) const;
  746. void get_meta_list(List<StringName> *p_list) const;
  747. #ifdef TOOLS_ENABLED
  748. void set_edited(bool p_edited);
  749. bool is_edited() const;
  750. // This function is used to check when something changed beyond a point, it's used mainly for generating previews.
  751. uint32_t get_edited_version() const;
  752. #endif
  753. void set_script_instance(ScriptInstance *p_instance);
  754. _FORCE_INLINE_ ScriptInstance *get_script_instance() const { return script_instance; }
  755. // Some script languages can't control instance creation, so this function eases the process.
  756. void set_script_and_instance(const Variant &p_script, ScriptInstance *p_instance);
  757. void add_user_signal(const MethodInfo &p_signal);
  758. template <typename... VarArgs>
  759. Error emit_signal(const StringName &p_name, VarArgs... p_args) {
  760. Variant args[sizeof...(p_args) + 1] = { p_args..., Variant() }; // +1 makes sure zero sized arrays are also supported.
  761. const Variant *argptrs[sizeof...(p_args) + 1];
  762. for (uint32_t i = 0; i < sizeof...(p_args); i++) {
  763. argptrs[i] = &args[i];
  764. }
  765. return emit_signalp(p_name, sizeof...(p_args) == 0 ? nullptr : (const Variant **)argptrs, sizeof...(p_args));
  766. }
  767. Error emit_signalp(const StringName &p_name, const Variant **p_args, int p_argcount);
  768. bool has_signal(const StringName &p_name) const;
  769. void get_signal_list(List<MethodInfo> *p_signals) const;
  770. void get_signal_connection_list(const StringName &p_signal, List<Connection> *p_connections) const;
  771. void get_all_signal_connections(List<Connection> *p_connections) const;
  772. int get_persistent_signal_connection_count() const;
  773. void get_signals_connected_to_this(List<Connection> *p_connections) const;
  774. Error connect(const StringName &p_signal, const Callable &p_callable, uint32_t p_flags = 0);
  775. void disconnect(const StringName &p_signal, const Callable &p_callable);
  776. bool is_connected(const StringName &p_signal, const Callable &p_callable) const;
  777. template <typename... VarArgs>
  778. void call_deferred(const StringName &p_name, VarArgs... p_args) {
  779. MessageQueue::get_singleton()->push_call(this, p_name, p_args...);
  780. }
  781. void set_deferred(const StringName &p_property, const Variant &p_value);
  782. void set_block_signals(bool p_block);
  783. bool is_blocking_signals() const;
  784. Variant::Type get_static_property_type(const StringName &p_property, bool *r_valid = nullptr) const;
  785. Variant::Type get_static_property_type_indexed(const Vector<StringName> &p_path, bool *r_valid = nullptr) const;
  786. virtual void get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const;
  787. // Translate message (internationalization).
  788. String tr(const StringName &p_message, const StringName &p_context = "") const;
  789. String tr_n(const StringName &p_message, const StringName &p_message_plural, int p_n, const StringName &p_context = "") const;
  790. bool _is_queued_for_deletion = false; // Set to true by SceneTree::queue_delete().
  791. bool is_queued_for_deletion() const;
  792. _FORCE_INLINE_ void set_message_translation(bool p_enable) { _can_translate = p_enable; }
  793. _FORCE_INLINE_ bool can_translate_messages() const { return _can_translate; }
  794. #ifdef TOOLS_ENABLED
  795. void editor_set_section_unfold(const String &p_section, bool p_unfolded);
  796. bool editor_is_section_unfolded(const String &p_section);
  797. const HashSet<String> &editor_get_section_folding() const { return editor_section_folding; }
  798. void editor_clear_section_folding() { editor_section_folding.clear(); }
  799. #endif
  800. // Used by script languages to store binding data.
  801. void *get_instance_binding(void *p_token, const GDExtensionInstanceBindingCallbacks *p_callbacks);
  802. // Used on creation by binding only.
  803. void set_instance_binding(void *p_token, void *p_binding, const GDExtensionInstanceBindingCallbacks *p_callbacks);
  804. bool has_instance_binding(void *p_token);
  805. void clear_internal_resource_paths();
  806. _ALWAYS_INLINE_ bool is_ref_counted() const { return type_is_reference; }
  807. Object();
  808. virtual ~Object();
  809. };
  810. bool predelete_handler(Object *p_object);
  811. void postinitialize_handler(Object *p_object);
  812. class ObjectDB {
  813. // This needs to add up to 63, 1 bit is for reference.
  814. #define OBJECTDB_VALIDATOR_BITS 39
  815. #define OBJECTDB_VALIDATOR_MASK ((uint64_t(1) << OBJECTDB_VALIDATOR_BITS) - 1)
  816. #define OBJECTDB_SLOT_MAX_COUNT_BITS 24
  817. #define OBJECTDB_SLOT_MAX_COUNT_MASK ((uint64_t(1) << OBJECTDB_SLOT_MAX_COUNT_BITS) - 1)
  818. #define OBJECTDB_REFERENCE_BIT (uint64_t(1) << (OBJECTDB_SLOT_MAX_COUNT_BITS + OBJECTDB_VALIDATOR_BITS))
  819. struct ObjectSlot { // 128 bits per slot.
  820. uint64_t validator : OBJECTDB_VALIDATOR_BITS;
  821. uint64_t next_free : OBJECTDB_SLOT_MAX_COUNT_BITS;
  822. uint64_t is_ref_counted : 1;
  823. Object *object = nullptr;
  824. };
  825. static SpinLock spin_lock;
  826. static uint32_t slot_count;
  827. static uint32_t slot_max;
  828. static ObjectSlot *object_slots;
  829. static uint64_t validator_counter;
  830. friend class Object;
  831. friend void unregister_core_types();
  832. static void cleanup();
  833. static ObjectID add_instance(Object *p_object);
  834. static void remove_instance(Object *p_object);
  835. friend void register_core_types();
  836. static void setup();
  837. public:
  838. typedef void (*DebugFunc)(Object *p_obj);
  839. _ALWAYS_INLINE_ static Object *get_instance(ObjectID p_instance_id) {
  840. uint64_t id = p_instance_id;
  841. uint32_t slot = id & OBJECTDB_SLOT_MAX_COUNT_MASK;
  842. ERR_FAIL_COND_V(slot >= slot_max, nullptr); // This should never happen unless RID is corrupted.
  843. spin_lock.lock();
  844. uint64_t validator = (id >> OBJECTDB_SLOT_MAX_COUNT_BITS) & OBJECTDB_VALIDATOR_MASK;
  845. if (unlikely(object_slots[slot].validator != validator)) {
  846. spin_lock.unlock();
  847. return nullptr;
  848. }
  849. Object *object = object_slots[slot].object;
  850. spin_lock.unlock();
  851. return object;
  852. }
  853. static void debug_objects(DebugFunc p_func);
  854. static int get_object_count();
  855. };
  856. #endif // OBJECT_H