object.h 56 KB

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