2
0

object.h 59 KB

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