object.h 57 KB

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