object.h 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083
  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. #pragma once
  31. #include "core/disabled_classes.gen.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. template <typename T>
  47. class Ref;
  48. enum PropertyHint {
  49. PROPERTY_HINT_NONE, ///< no hint provided.
  50. PROPERTY_HINT_RANGE, ///< hint_text = "min,max[,step][,or_greater][,or_less][,hide_slider][,radians_as_degrees][,degrees][,exp][,suffix:<keyword>] range.
  51. PROPERTY_HINT_ENUM, ///< hint_text= "val1,val2,val3,etc"
  52. PROPERTY_HINT_ENUM_SUGGESTION, ///< hint_text= "val1,val2,val3,etc"
  53. 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")
  54. PROPERTY_HINT_LINK,
  55. PROPERTY_HINT_FLAGS, ///< hint_text= "flag1,flag2,etc" (as bit flags)
  56. PROPERTY_HINT_LAYERS_2D_RENDER,
  57. PROPERTY_HINT_LAYERS_2D_PHYSICS,
  58. PROPERTY_HINT_LAYERS_2D_NAVIGATION,
  59. PROPERTY_HINT_LAYERS_3D_RENDER,
  60. PROPERTY_HINT_LAYERS_3D_PHYSICS,
  61. PROPERTY_HINT_LAYERS_3D_NAVIGATION,
  62. PROPERTY_HINT_FILE, ///< a file path must be passed, hint_text (optionally) is a filter "*.png,*.wav,*.doc,"
  63. PROPERTY_HINT_DIR, ///< a directory path must be passed
  64. PROPERTY_HINT_GLOBAL_FILE, ///< a file path must be passed, hint_text (optionally) is a filter "*.png,*.wav,*.doc,"
  65. PROPERTY_HINT_GLOBAL_DIR, ///< a directory path must be passed
  66. PROPERTY_HINT_RESOURCE_TYPE, ///< a comma-separated resource object type, e.g. "NoiseTexture,GradientTexture2D". Subclasses can be excluded with a "-" prefix if placed *after* the base class, e.g. "Texture2D,-MeshTexture".
  67. PROPERTY_HINT_MULTILINE_TEXT, ///< used for string properties that can contain multiple lines
  68. PROPERTY_HINT_EXPRESSION, ///< used for string properties that can contain multiple lines
  69. PROPERTY_HINT_PLACEHOLDER_TEXT, ///< used to set a placeholder text for string properties
  70. PROPERTY_HINT_COLOR_NO_ALPHA, ///< used for ignoring alpha component when editing a color
  71. PROPERTY_HINT_OBJECT_ID,
  72. PROPERTY_HINT_TYPE_STRING, ///< a type string, the hint is the base type to choose
  73. PROPERTY_HINT_NODE_PATH_TO_EDITED_NODE, // Deprecated.
  74. PROPERTY_HINT_OBJECT_TOO_BIG, ///< object is too big to send
  75. PROPERTY_HINT_NODE_PATH_VALID_TYPES,
  76. PROPERTY_HINT_SAVE_FILE, ///< a file path must be passed, hint_text (optionally) is a filter "*.png,*.wav,*.doc,". This opens a save dialog
  77. PROPERTY_HINT_GLOBAL_SAVE_FILE, ///< a file path must be passed, hint_text (optionally) is a filter "*.png,*.wav,*.doc,". This opens a save dialog
  78. PROPERTY_HINT_INT_IS_OBJECTID, // Deprecated.
  79. PROPERTY_HINT_INT_IS_POINTER,
  80. PROPERTY_HINT_ARRAY_TYPE,
  81. PROPERTY_HINT_LOCALE_ID,
  82. PROPERTY_HINT_LOCALIZABLE_STRING,
  83. PROPERTY_HINT_NODE_TYPE, ///< a node object type
  84. PROPERTY_HINT_HIDE_QUATERNION_EDIT, /// Only Node3D::transform should hide the quaternion editor.
  85. PROPERTY_HINT_PASSWORD,
  86. PROPERTY_HINT_LAYERS_AVOIDANCE,
  87. PROPERTY_HINT_DICTIONARY_TYPE,
  88. PROPERTY_HINT_TOOL_BUTTON,
  89. PROPERTY_HINT_ONESHOT, ///< the property will be changed by self after setting, such as AudioStreamPlayer.playing, Particles.emitting.
  90. PROPERTY_HINT_NO_NODEPATH, /// < this property will not contain a NodePath, regardless of type (Array, Dictionary, List, etc.). Needed for SceneTreeDock.
  91. PROPERTY_HINT_GROUP_ENABLE, ///< used to make the property's group checkable. Only use for boolean types.
  92. PROPERTY_HINT_INPUT_NAME,
  93. PROPERTY_HINT_FILE_PATH,
  94. PROPERTY_HINT_MAX,
  95. };
  96. enum PropertyUsageFlags {
  97. PROPERTY_USAGE_NONE = 0,
  98. PROPERTY_USAGE_STORAGE = 1 << 1,
  99. PROPERTY_USAGE_EDITOR = 1 << 2,
  100. PROPERTY_USAGE_INTERNAL = 1 << 3,
  101. PROPERTY_USAGE_CHECKABLE = 1 << 4, // Used for editing global variables.
  102. PROPERTY_USAGE_CHECKED = 1 << 5, // Used for editing global variables.
  103. PROPERTY_USAGE_GROUP = 1 << 6, // Used for grouping props in the editor.
  104. PROPERTY_USAGE_CATEGORY = 1 << 7,
  105. PROPERTY_USAGE_SUBGROUP = 1 << 8,
  106. PROPERTY_USAGE_CLASS_IS_BITFIELD = 1 << 9,
  107. PROPERTY_USAGE_NO_INSTANCE_STATE = 1 << 10,
  108. PROPERTY_USAGE_RESTART_IF_CHANGED = 1 << 11,
  109. PROPERTY_USAGE_SCRIPT_VARIABLE = 1 << 12,
  110. PROPERTY_USAGE_STORE_IF_NULL = 1 << 13,
  111. PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED = 1 << 14,
  112. PROPERTY_USAGE_SCRIPT_DEFAULT_VALUE = 1 << 15, // Deprecated.
  113. PROPERTY_USAGE_CLASS_IS_ENUM = 1 << 16,
  114. PROPERTY_USAGE_NIL_IS_VARIANT = 1 << 17,
  115. PROPERTY_USAGE_ARRAY = 1 << 18, // Used in the inspector to group properties as elements of an array.
  116. PROPERTY_USAGE_ALWAYS_DUPLICATE = 1 << 19, // When duplicating a resource, always duplicate, even with subresource duplication disabled.
  117. PROPERTY_USAGE_NEVER_DUPLICATE = 1 << 20, // When duplicating a resource, never duplicate, even with subresource duplication enabled.
  118. PROPERTY_USAGE_HIGH_END_GFX = 1 << 21,
  119. PROPERTY_USAGE_NODE_PATH_FROM_SCENE_ROOT = 1 << 22,
  120. PROPERTY_USAGE_RESOURCE_NOT_PERSISTENT = 1 << 23,
  121. PROPERTY_USAGE_KEYING_INCREMENTS = 1 << 24, // Used in inspector to increment property when keyed in animation player.
  122. PROPERTY_USAGE_DEFERRED_SET_RESOURCE = 1 << 25, // Deprecated.
  123. PROPERTY_USAGE_EDITOR_INSTANTIATE_OBJECT = 1 << 26, // For Object properties, instantiate them when creating in editor.
  124. PROPERTY_USAGE_EDITOR_BASIC_SETTING = 1 << 27, //for project or editor settings, show when basic settings are selected.
  125. PROPERTY_USAGE_READ_ONLY = 1 << 28, // Mark a property as read-only in the inspector.
  126. PROPERTY_USAGE_SECRET = 1 << 29, // Export preset credentials that should be stored separately from the rest of the export config.
  127. PROPERTY_USAGE_DEFAULT = PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_EDITOR,
  128. PROPERTY_USAGE_NO_EDITOR = PROPERTY_USAGE_STORAGE,
  129. };
  130. // Respective values are defined by disabled_classes.gen.h
  131. #define GD_IS_CLASS_ENABLED(m_class) m_class::_class_is_enabled
  132. #define ADD_SIGNAL(m_signal) ::ClassDB::add_signal(get_class_static(), m_signal)
  133. #define ADD_PROPERTY(m_property, m_setter, m_getter) ::ClassDB::add_property(get_class_static(), m_property, StringName(m_setter), StringName(m_getter))
  134. #define ADD_PROPERTYI(m_property, m_setter, m_getter, m_index) ::ClassDB::add_property(get_class_static(), m_property, StringName(m_setter), StringName(m_getter), m_index)
  135. #define ADD_PROPERTY_DEFAULT(m_property, m_default) ::ClassDB::set_property_default_value(get_class_static(), m_property, m_default)
  136. #define ADD_GROUP(m_name, m_prefix) ::ClassDB::add_property_group(get_class_static(), m_name, m_prefix)
  137. #define ADD_GROUP_INDENT(m_name, m_prefix, m_depth) ::ClassDB::add_property_group(get_class_static(), m_name, m_prefix, m_depth)
  138. #define ADD_SUBGROUP(m_name, m_prefix) ::ClassDB::add_property_subgroup(get_class_static(), m_name, m_prefix)
  139. #define ADD_SUBGROUP_INDENT(m_name, m_prefix, m_depth) ::ClassDB::add_property_subgroup(get_class_static(), m_name, m_prefix, m_depth)
  140. #define ADD_LINKED_PROPERTY(m_property, m_linked_property) ::ClassDB::add_linked_property(get_class_static(), m_property, m_linked_property)
  141. #ifdef TOOLS_ENABLED
  142. #define ADD_CLASS_DEPENDENCY(m_class) ::ClassDB::add_class_dependency(get_class_static(), m_class)
  143. #else
  144. #define ADD_CLASS_DEPENDENCY(m_class)
  145. #endif
  146. #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, StringName(m_count_property_setter), StringName(m_count_property_getter), m_prefix)
  147. #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, StringName(m_count_property_setter), StringName(m_count_property_getter), m_prefix, m_property_usage_flags)
  148. #define ADD_ARRAY(m_array_path, m_prefix) ClassDB::add_property_array(get_class_static(), m_array_path, m_prefix)
  149. // Helper macro to use with PROPERTY_HINT_ARRAY_TYPE for arrays of specific resources:
  150. // PropertyInfo(Variant::ARRAY, "fallbacks", PROPERTY_HINT_ARRAY_TYPE, MAKE_RESOURCE_TYPE_HINT("Font")
  151. #define MAKE_RESOURCE_TYPE_HINT(m_type) vformat("%s/%s:%s", Variant::OBJECT, PROPERTY_HINT_RESOURCE_TYPE, m_type)
  152. struct PropertyInfo {
  153. Variant::Type type = Variant::NIL;
  154. String name;
  155. StringName class_name; // For classes
  156. PropertyHint hint = PROPERTY_HINT_NONE;
  157. String hint_string;
  158. uint32_t usage = PROPERTY_USAGE_DEFAULT;
  159. // If you are thinking about adding another member to this class, ask the maintainer (Juan) first.
  160. _FORCE_INLINE_ PropertyInfo added_usage(uint32_t p_fl) const {
  161. PropertyInfo pi = *this;
  162. pi.usage |= p_fl;
  163. return pi;
  164. }
  165. operator Dictionary() const;
  166. static PropertyInfo from_dict(const Dictionary &p_dict);
  167. PropertyInfo() {}
  168. 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()) :
  169. type(p_type),
  170. name(p_name),
  171. hint(p_hint),
  172. hint_string(p_hint_string),
  173. usage(p_usage) {
  174. if (hint == PROPERTY_HINT_RESOURCE_TYPE) {
  175. class_name = hint_string;
  176. } else {
  177. class_name = p_class_name;
  178. }
  179. }
  180. PropertyInfo(const StringName &p_class_name) :
  181. type(Variant::OBJECT),
  182. class_name(p_class_name) {}
  183. explicit PropertyInfo(const GDExtensionPropertyInfo &pinfo) :
  184. type((Variant::Type)pinfo.type),
  185. name(*reinterpret_cast<StringName *>(pinfo.name)),
  186. class_name(*reinterpret_cast<StringName *>(pinfo.class_name)),
  187. hint((PropertyHint)pinfo.hint),
  188. hint_string(*reinterpret_cast<String *>(pinfo.hint_string)),
  189. usage(pinfo.usage) {}
  190. bool operator==(const PropertyInfo &p_info) const {
  191. return ((type == p_info.type) &&
  192. (name == p_info.name) &&
  193. (class_name == p_info.class_name) &&
  194. (hint == p_info.hint) &&
  195. (hint_string == p_info.hint_string) &&
  196. (usage == p_info.usage));
  197. }
  198. bool operator<(const PropertyInfo &p_info) const {
  199. return name < p_info.name;
  200. }
  201. };
  202. TypedArray<Dictionary> convert_property_list(const List<PropertyInfo> *p_list);
  203. TypedArray<Dictionary> convert_property_list(const Vector<PropertyInfo> &p_vector);
  204. enum MethodFlags {
  205. METHOD_FLAG_NORMAL = 1,
  206. METHOD_FLAG_EDITOR = 2,
  207. METHOD_FLAG_CONST = 4,
  208. METHOD_FLAG_VIRTUAL = 8,
  209. METHOD_FLAG_VARARG = 16,
  210. METHOD_FLAG_STATIC = 32,
  211. METHOD_FLAG_OBJECT_CORE = 64,
  212. METHOD_FLAG_VIRTUAL_REQUIRED = 128,
  213. METHOD_FLAGS_DEFAULT = METHOD_FLAG_NORMAL,
  214. };
  215. struct MethodInfo {
  216. String name;
  217. PropertyInfo return_val;
  218. uint32_t flags = METHOD_FLAGS_DEFAULT;
  219. int id = 0;
  220. Vector<PropertyInfo> arguments;
  221. Vector<Variant> default_arguments;
  222. int return_val_metadata = 0;
  223. Vector<int> arguments_metadata;
  224. int get_argument_meta(int p_arg) const {
  225. ERR_FAIL_COND_V(p_arg < -1 || p_arg > arguments.size(), 0);
  226. if (p_arg == -1) {
  227. return return_val_metadata;
  228. }
  229. return arguments_metadata.size() > p_arg ? arguments_metadata[p_arg] : 0;
  230. }
  231. inline bool operator==(const MethodInfo &p_method) const { return id == p_method.id && name == p_method.name; }
  232. inline bool operator<(const MethodInfo &p_method) const { return id == p_method.id ? (name < p_method.name) : (id < p_method.id); }
  233. operator Dictionary() const;
  234. static MethodInfo from_dict(const Dictionary &p_dict);
  235. uint32_t get_compatibility_hash() const;
  236. MethodInfo() {}
  237. explicit MethodInfo(const GDExtensionMethodInfo &pinfo) :
  238. name(*reinterpret_cast<StringName *>(pinfo.name)),
  239. return_val(PropertyInfo(pinfo.return_value)),
  240. flags(pinfo.flags),
  241. id(pinfo.id) {
  242. for (uint32_t i = 0; i < pinfo.argument_count; i++) {
  243. arguments.push_back(PropertyInfo(pinfo.arguments[i]));
  244. }
  245. const Variant *def_values = (const Variant *)pinfo.default_arguments;
  246. for (uint32_t j = 0; j < pinfo.default_argument_count; j++) {
  247. default_arguments.push_back(def_values[j]);
  248. }
  249. }
  250. MethodInfo(const String &p_name) { name = p_name; }
  251. template <typename... VarArgs>
  252. MethodInfo(const String &p_name, VarArgs... p_params) {
  253. name = p_name;
  254. arguments = Vector<PropertyInfo>{ p_params... };
  255. }
  256. MethodInfo(Variant::Type ret) { return_val.type = ret; }
  257. MethodInfo(Variant::Type ret, const String &p_name) {
  258. return_val.type = ret;
  259. name = p_name;
  260. }
  261. template <typename... VarArgs>
  262. MethodInfo(Variant::Type ret, const String &p_name, VarArgs... p_params) {
  263. name = p_name;
  264. return_val.type = ret;
  265. arguments = Vector<PropertyInfo>{ p_params... };
  266. }
  267. MethodInfo(const PropertyInfo &p_ret, const String &p_name) {
  268. return_val = p_ret;
  269. name = p_name;
  270. }
  271. template <typename... VarArgs>
  272. MethodInfo(const PropertyInfo &p_ret, const String &p_name, VarArgs... p_params) {
  273. return_val = p_ret;
  274. name = p_name;
  275. arguments = Vector<PropertyInfo>{ p_params... };
  276. }
  277. };
  278. // API used to extend in GDExtension and other C compatible compiled languages.
  279. class MethodBind;
  280. class GDExtension;
  281. struct ObjectGDExtension {
  282. GDExtension *library = nullptr;
  283. ObjectGDExtension *parent = nullptr;
  284. List<ObjectGDExtension *> children;
  285. StringName parent_class_name;
  286. StringName class_name;
  287. bool editor_class = false;
  288. bool reloadable = false;
  289. bool is_virtual = false;
  290. bool is_abstract = false;
  291. bool is_exposed = true;
  292. #ifdef TOOLS_ENABLED
  293. bool is_runtime = false;
  294. bool is_placeholder = false;
  295. #endif
  296. GDExtensionClassSet set;
  297. GDExtensionClassGet get;
  298. GDExtensionClassGetPropertyList get_property_list;
  299. GDExtensionClassFreePropertyList2 free_property_list2;
  300. GDExtensionClassPropertyCanRevert property_can_revert;
  301. GDExtensionClassPropertyGetRevert property_get_revert;
  302. GDExtensionClassValidateProperty validate_property;
  303. #ifndef DISABLE_DEPRECATED
  304. GDExtensionClassNotification notification;
  305. GDExtensionClassFreePropertyList free_property_list;
  306. #endif // DISABLE_DEPRECATED
  307. GDExtensionClassNotification2 notification2;
  308. GDExtensionClassToString to_string;
  309. GDExtensionClassReference reference;
  310. GDExtensionClassReference unreference;
  311. GDExtensionClassGetRID get_rid;
  312. _FORCE_INLINE_ bool is_class(const String &p_class) const {
  313. const ObjectGDExtension *e = this;
  314. while (e) {
  315. if (p_class == e->class_name.operator String()) {
  316. return true;
  317. }
  318. e = e->parent;
  319. }
  320. return false;
  321. }
  322. void *class_userdata = nullptr;
  323. #ifndef DISABLE_DEPRECATED
  324. GDExtensionClassCreateInstance create_instance;
  325. #endif // DISABLE_DEPRECATED
  326. GDExtensionClassCreateInstance2 create_instance2;
  327. GDExtensionClassFreeInstance free_instance;
  328. #ifndef DISABLE_DEPRECATED
  329. GDExtensionClassGetVirtual get_virtual;
  330. GDExtensionClassGetVirtualCallData get_virtual_call_data;
  331. #endif // DISABLE_DEPRECATED
  332. GDExtensionClassGetVirtual2 get_virtual2;
  333. GDExtensionClassGetVirtualCallData2 get_virtual_call_data2;
  334. GDExtensionClassCallVirtualWithData call_virtual_with_data;
  335. GDExtensionClassRecreateInstance recreate_instance;
  336. #ifdef TOOLS_ENABLED
  337. void *tracking_userdata = nullptr;
  338. void (*track_instance)(void *p_userdata, void *p_instance) = nullptr;
  339. void (*untrack_instance)(void *p_userdata, void *p_instance) = nullptr;
  340. #endif
  341. };
  342. #define GDVIRTUAL_CALL(m_name, ...) _gdvirtual_##m_name##_call(__VA_ARGS__)
  343. #define GDVIRTUAL_CALL_PTR(m_obj, m_name, ...) m_obj->_gdvirtual_##m_name##_call(__VA_ARGS__)
  344. #ifdef DEBUG_ENABLED
  345. #define GDVIRTUAL_BIND(m_name, ...) ::ClassDB::add_virtual_method(get_class_static(), _gdvirtual_##m_name##_get_method_info(), true, sarray(__VA_ARGS__));
  346. #else
  347. #define GDVIRTUAL_BIND(m_name, ...)
  348. #endif // DEBUG_ENABLED
  349. #define GDVIRTUAL_BIND_COMPAT(m_alias, ...) ::ClassDB::add_virtual_compatibility_method(get_class_static(), _gdvirtual_##m_alias##_get_method_info(), true, sarray(__VA_ARGS__));
  350. #define GDVIRTUAL_IS_OVERRIDDEN(m_name) _gdvirtual_##m_name##_overridden()
  351. #define GDVIRTUAL_IS_OVERRIDDEN_PTR(m_obj, m_name) m_obj->_gdvirtual_##m_name##_overridden()
  352. /*
  353. * The following is an incomprehensible blob of hacks and workarounds to
  354. * compensate for many of the fallacies in C++. As a plus, this macro pretty
  355. * much alone defines the object model.
  356. */
  357. /// `GDSOFTCLASS` provides `Object` functionality, such as being able to use `Object::cast_to()`.
  358. /// Use this for `Object` subclasses that are not registered in `ClassDB` (use `GDCLASS` otherwise).
  359. #define GDSOFTCLASS(m_class, m_inherits) \
  360. public: \
  361. using self_type = m_class; \
  362. using super_type = m_inherits; \
  363. static _FORCE_INLINE_ void *get_class_ptr_static() { \
  364. static int ptr; \
  365. return &ptr; \
  366. } \
  367. virtual bool is_class_ptr(void *p_ptr) const override { \
  368. return (p_ptr == get_class_ptr_static()) || m_inherits::is_class_ptr(p_ptr); \
  369. } \
  370. \
  371. protected: \
  372. _FORCE_INLINE_ bool (Object::*_get_get() const)(const StringName &p_name, Variant &) const { \
  373. return (bool (Object::*)(const StringName &, Variant &) const) & m_class::_get; \
  374. } \
  375. virtual bool _getv(const StringName &p_name, Variant &r_ret) const override { \
  376. if (m_class::_get_get() != m_inherits::_get_get()) { \
  377. if (_get(p_name, r_ret)) { \
  378. return true; \
  379. } \
  380. } \
  381. return m_inherits::_getv(p_name, r_ret); \
  382. } \
  383. _FORCE_INLINE_ bool (Object::*_get_set() const)(const StringName &p_name, const Variant &p_property) { \
  384. return (bool (Object::*)(const StringName &, const Variant &)) & m_class::_set; \
  385. } \
  386. virtual bool _setv(const StringName &p_name, const Variant &p_property) override { \
  387. if (m_inherits::_setv(p_name, p_property)) { \
  388. return true; \
  389. } \
  390. if (m_class::_get_set() != m_inherits::_get_set()) { \
  391. return _set(p_name, p_property); \
  392. } \
  393. return false; \
  394. } \
  395. _FORCE_INLINE_ void (Object::*_get_validate_property() const)(PropertyInfo & p_property) const { \
  396. return (void (Object::*)(PropertyInfo &) const) & m_class::_validate_property; \
  397. } \
  398. virtual void _validate_propertyv(PropertyInfo &p_property) const override { \
  399. m_inherits::_validate_propertyv(p_property); \
  400. if (m_class::_get_validate_property() != m_inherits::_get_validate_property()) { \
  401. _validate_property(p_property); \
  402. } \
  403. } \
  404. _FORCE_INLINE_ bool (Object::*_get_property_can_revert() const)(const StringName &p_name) const { \
  405. return (bool (Object::*)(const StringName &) const) & m_class::_property_can_revert; \
  406. } \
  407. virtual bool _property_can_revertv(const StringName &p_name) const override { \
  408. if (m_class::_get_property_can_revert() != m_inherits::_get_property_can_revert()) { \
  409. if (_property_can_revert(p_name)) { \
  410. return true; \
  411. } \
  412. } \
  413. return m_inherits::_property_can_revertv(p_name); \
  414. } \
  415. _FORCE_INLINE_ bool (Object::*_get_property_get_revert() const)(const StringName &p_name, Variant &) const { \
  416. return (bool (Object::*)(const StringName &, Variant &) const) & m_class::_property_get_revert; \
  417. } \
  418. virtual bool _property_get_revertv(const StringName &p_name, Variant &r_ret) const override { \
  419. if (m_class::_get_property_get_revert() != m_inherits::_get_property_get_revert()) { \
  420. if (_property_get_revert(p_name, r_ret)) { \
  421. return true; \
  422. } \
  423. } \
  424. return m_inherits::_property_get_revertv(p_name, r_ret); \
  425. } \
  426. _FORCE_INLINE_ void (Object::*_get_notification() const)(int) { \
  427. return (void (Object::*)(int)) & m_class::_notification; \
  428. } \
  429. virtual void _notification_forwardv(int p_notification) override { \
  430. m_inherits::_notification_forwardv(p_notification); \
  431. if (m_class::_get_notification() != m_inherits::_get_notification()) { \
  432. _notification(p_notification); \
  433. } \
  434. } \
  435. virtual void _notification_backwardv(int p_notification) override { \
  436. if (m_class::_get_notification() != m_inherits::_get_notification()) { \
  437. _notification(p_notification); \
  438. } \
  439. m_inherits::_notification_backwardv(p_notification); \
  440. } \
  441. \
  442. private:
  443. /// `GDSOFTCLASS` provides `Object` functionality, such as being able to use `Object::cast_to()`.
  444. /// Use this for `Object` subclasses that are registered in `ObjectDB` (use `GDSOFTCLASS` otherwise).
  445. #define GDCLASS(m_class, m_inherits) \
  446. GDSOFTCLASS(m_class, m_inherits) \
  447. private: \
  448. void operator=(const m_class &p_rval) {} \
  449. friend class ::ClassDB; \
  450. \
  451. public: \
  452. static constexpr bool _class_is_enabled = !bool(GD_IS_DEFINED(ClassDB_Disable_##m_class)) && m_inherits::_class_is_enabled; \
  453. virtual const StringName *_get_class_namev() const override { \
  454. return &get_class_static(); \
  455. } \
  456. static const StringName &get_class_static() { \
  457. static StringName _class_name_static; \
  458. if (unlikely(!_class_name_static)) { \
  459. assign_class_name_static(#m_class, _class_name_static); \
  460. } \
  461. return _class_name_static; \
  462. } \
  463. virtual bool is_class(const String &p_class) const override { \
  464. if (_get_extension() && _get_extension()->is_class(p_class)) { \
  465. return true; \
  466. } \
  467. return (p_class == (#m_class)) ? true : m_inherits::is_class(p_class); \
  468. } \
  469. \
  470. protected: \
  471. _FORCE_INLINE_ static void (*_get_bind_methods())() { \
  472. return &m_class::_bind_methods; \
  473. } \
  474. _FORCE_INLINE_ static void (*_get_bind_compatibility_methods())() { \
  475. return &m_class::_bind_compatibility_methods; \
  476. } \
  477. \
  478. public: \
  479. static void initialize_class() { \
  480. static bool initialized = false; \
  481. if (initialized) { \
  482. return; \
  483. } \
  484. m_inherits::initialize_class(); \
  485. _add_class_to_classdb(get_class_static(), super_type::get_class_static()); \
  486. if (m_class::_get_bind_methods() != m_inherits::_get_bind_methods()) { \
  487. _bind_methods(); \
  488. } \
  489. if (m_class::_get_bind_compatibility_methods() != m_inherits::_get_bind_compatibility_methods()) { \
  490. _bind_compatibility_methods(); \
  491. } \
  492. initialized = true; \
  493. } \
  494. \
  495. protected: \
  496. virtual void _initialize_classv() override { \
  497. initialize_class(); \
  498. } \
  499. _FORCE_INLINE_ void (Object::*_get_get_property_list() const)(List<PropertyInfo> * p_list) const { \
  500. return (void (Object::*)(List<PropertyInfo> *) const) & m_class::_get_property_list; \
  501. } \
  502. virtual void _get_property_listv(List<PropertyInfo> *p_list, bool p_reversed) const override { \
  503. if (!p_reversed) { \
  504. m_inherits::_get_property_listv(p_list, p_reversed); \
  505. } \
  506. p_list->push_back(PropertyInfo(Variant::NIL, get_class_static(), PROPERTY_HINT_NONE, get_class_static(), PROPERTY_USAGE_CATEGORY)); \
  507. _get_property_list_from_classdb(#m_class, p_list, true, this); \
  508. if (m_class::_get_get_property_list() != m_inherits::_get_get_property_list()) { \
  509. _get_property_list(p_list); \
  510. } \
  511. if (p_reversed) { \
  512. m_inherits::_get_property_listv(p_list, p_reversed); \
  513. } \
  514. } \
  515. \
  516. private:
  517. #define OBJ_SAVE_TYPE(m_class) \
  518. public: \
  519. virtual String get_save_class() const override { \
  520. return #m_class; \
  521. } \
  522. \
  523. private:
  524. class ScriptInstance;
  525. class Object {
  526. public:
  527. typedef Object self_type;
  528. enum ConnectFlags {
  529. CONNECT_DEFERRED = 1,
  530. CONNECT_PERSIST = 2, // Hint for scene to save this connection.
  531. CONNECT_ONE_SHOT = 4,
  532. CONNECT_REFERENCE_COUNTED = 8,
  533. CONNECT_APPEND_SOURCE_OBJECT = 16,
  534. CONNECT_INHERITED = 32, // Used in editor builds.
  535. };
  536. struct Connection {
  537. ::Signal signal;
  538. Callable callable;
  539. uint32_t flags = 0;
  540. bool operator<(const Connection &p_conn) const;
  541. operator Variant() const;
  542. Connection() {}
  543. Connection(const Variant &p_variant);
  544. };
  545. private:
  546. #ifdef DEBUG_ENABLED
  547. friend struct _ObjectDebugLock;
  548. #endif // DEBUG_ENABLED
  549. friend bool predelete_handler(Object *);
  550. friend void postinitialize_handler(Object *);
  551. ObjectGDExtension *_extension = nullptr;
  552. GDExtensionClassInstancePtr _extension_instance = nullptr;
  553. struct SignalData {
  554. struct Slot {
  555. int reference_count = 0;
  556. Connection conn;
  557. List<Connection>::Element *cE = nullptr;
  558. };
  559. MethodInfo user;
  560. HashMap<Callable, Slot, HashableHasher<Callable>> slot_map;
  561. bool removable = false;
  562. };
  563. friend struct _ObjectSignalLock;
  564. mutable Mutex *signal_mutex = nullptr;
  565. HashMap<StringName, SignalData> signal_map;
  566. List<Connection> connections;
  567. #ifdef DEBUG_ENABLED
  568. SafeRefCount _lock_index;
  569. #endif // DEBUG_ENABLED
  570. bool _block_signals = false;
  571. int _predelete_ok = 0;
  572. ObjectID _instance_id;
  573. bool _predelete();
  574. void _initialize();
  575. void _postinitialize();
  576. bool _can_translate = true;
  577. bool _emitting = false;
  578. #ifdef TOOLS_ENABLED
  579. bool _edited = false;
  580. uint32_t _edited_version = 0;
  581. HashSet<String> editor_section_folding;
  582. #endif
  583. ScriptInstance *script_instance = nullptr;
  584. Variant script; // Reference does not exist yet, store it in a Variant.
  585. HashMap<StringName, Variant> metadata;
  586. HashMap<StringName, Variant *> metadata_properties;
  587. mutable const StringName *_class_name_ptr = nullptr;
  588. void _add_user_signal(const String &p_name, const Array &p_args = Array());
  589. bool _has_user_signal(const StringName &p_name) const;
  590. void _remove_user_signal(const StringName &p_name);
  591. Error _emit_signal(const Variant **p_args, int p_argcount, Callable::CallError &r_error);
  592. TypedArray<Dictionary> _get_signal_list() const;
  593. TypedArray<Dictionary> _get_signal_connection_list(const StringName &p_signal) const;
  594. TypedArray<Dictionary> _get_incoming_connections() const;
  595. void _set_bind(const StringName &p_set, const Variant &p_value);
  596. Variant _get_bind(const StringName &p_name) const;
  597. void _set_indexed_bind(const NodePath &p_name, const Variant &p_value);
  598. Variant _get_indexed_bind(const NodePath &p_name) const;
  599. int _get_method_argument_count_bind(const StringName &p_name) const;
  600. _FORCE_INLINE_ void _construct_object(bool p_reference);
  601. friend class RefCounted;
  602. bool type_is_reference = false;
  603. BinaryMutex _instance_binding_mutex;
  604. struct InstanceBinding {
  605. void *binding = nullptr;
  606. void *token = nullptr;
  607. GDExtensionInstanceBindingFreeCallback free_callback = nullptr;
  608. GDExtensionInstanceBindingReferenceCallback reference_callback = nullptr;
  609. };
  610. InstanceBinding *_instance_bindings = nullptr;
  611. uint32_t _instance_binding_count = 0;
  612. Object(bool p_reference);
  613. protected:
  614. StringName _translation_domain;
  615. _FORCE_INLINE_ bool _instance_binding_reference(bool p_reference) {
  616. bool can_die = true;
  617. if (_instance_bindings) {
  618. MutexLock instance_binding_lock(_instance_binding_mutex);
  619. for (uint32_t i = 0; i < _instance_binding_count; i++) {
  620. if (_instance_bindings[i].reference_callback) {
  621. if (!_instance_bindings[i].reference_callback(_instance_bindings[i].token, _instance_bindings[i].binding, p_reference)) {
  622. can_die = false;
  623. }
  624. }
  625. }
  626. }
  627. return can_die;
  628. }
  629. friend class GDExtensionMethodBind;
  630. _ALWAYS_INLINE_ const ObjectGDExtension *_get_extension() const { return _extension; }
  631. _ALWAYS_INLINE_ GDExtensionClassInstancePtr _get_extension_instance() const { return _extension_instance; }
  632. virtual void _initialize_classv() { initialize_class(); }
  633. virtual bool _setv(const StringName &p_name, const Variant &p_property) { return false; }
  634. virtual bool _getv(const StringName &p_name, Variant &r_property) const { return false; }
  635. virtual void _get_property_listv(List<PropertyInfo> *p_list, bool p_reversed) const {}
  636. virtual void _validate_propertyv(PropertyInfo &p_property) const {}
  637. virtual bool _property_can_revertv(const StringName &p_name) const { return false; }
  638. virtual bool _property_get_revertv(const StringName &p_name, Variant &r_property) const { return false; }
  639. void _notification_forward(int p_notification);
  640. void _notification_backward(int p_notification);
  641. virtual void _notification_forwardv(int p_notification) {}
  642. virtual void _notification_backwardv(int p_notification) {}
  643. static void _bind_methods();
  644. static void _bind_compatibility_methods() {}
  645. bool _set(const StringName &p_name, const Variant &p_property) { return false; }
  646. bool _get(const StringName &p_name, Variant &r_property) const { return false; }
  647. void _get_property_list(List<PropertyInfo> *p_list) const {}
  648. void _validate_property(PropertyInfo &p_property) const {}
  649. bool _property_can_revert(const StringName &p_name) const { return false; }
  650. bool _property_get_revert(const StringName &p_name, Variant &r_property) const { return false; }
  651. void _notification(int p_notification) {}
  652. _FORCE_INLINE_ static void (*_get_bind_methods())() {
  653. return &Object::_bind_methods;
  654. }
  655. _FORCE_INLINE_ static void (*_get_bind_compatibility_methods())() {
  656. return &Object::_bind_compatibility_methods;
  657. }
  658. _FORCE_INLINE_ bool (Object::*_get_get() const)(const StringName &p_name, Variant &r_ret) const {
  659. return &Object::_get;
  660. }
  661. _FORCE_INLINE_ bool (Object::*_get_set() const)(const StringName &p_name, const Variant &p_property) {
  662. return &Object::_set;
  663. }
  664. _FORCE_INLINE_ void (Object::*_get_get_property_list() const)(List<PropertyInfo> *p_list) const {
  665. return &Object::_get_property_list;
  666. }
  667. _FORCE_INLINE_ void (Object::*_get_validate_property() const)(PropertyInfo &p_property) const {
  668. return &Object::_validate_property;
  669. }
  670. _FORCE_INLINE_ bool (Object::*_get_property_can_revert() const)(const StringName &p_name) const {
  671. return &Object::_property_can_revert;
  672. }
  673. _FORCE_INLINE_ bool (Object::*_get_property_get_revert() const)(const StringName &p_name, Variant &) const {
  674. return &Object::_property_get_revert;
  675. }
  676. _FORCE_INLINE_ void (Object::*_get_notification() const)(int) {
  677. return &Object::_notification;
  678. }
  679. Variant _call_bind(const Variant **p_args, int p_argcount, Callable::CallError &r_error);
  680. Variant _call_deferred_bind(const Variant **p_args, int p_argcount, Callable::CallError &r_error);
  681. virtual const StringName *_get_class_namev() const {
  682. return &get_class_static();
  683. }
  684. TypedArray<StringName> _get_meta_list_bind() const;
  685. TypedArray<Dictionary> _get_property_list_bind() const;
  686. TypedArray<Dictionary> _get_method_list_bind() const;
  687. void _clear_internal_resource_paths(const Variant &p_var);
  688. friend class ClassDB;
  689. friend class PlaceholderExtensionInstance;
  690. static void _add_class_to_classdb(const StringName &p_class, const StringName &p_inherits);
  691. static void _get_property_list_from_classdb(const StringName &p_class, List<PropertyInfo> *p_list, bool p_no_inheritance, const Object *p_validator);
  692. bool _disconnect(const StringName &p_signal, const Callable &p_callable, bool p_force = false);
  693. virtual bool _uses_signal_mutex() const;
  694. #ifdef TOOLS_ENABLED
  695. struct VirtualMethodTracker {
  696. void **method;
  697. VirtualMethodTracker *next;
  698. };
  699. mutable VirtualMethodTracker *virtual_method_list = nullptr;
  700. #endif
  701. public: // Should be protected, but bug in clang++.
  702. static void initialize_class();
  703. _FORCE_INLINE_ static void register_custom_data_to_otdb() {}
  704. public:
  705. static constexpr bool _class_is_enabled = true;
  706. void notify_property_list_changed();
  707. static void *get_class_ptr_static() {
  708. static int ptr;
  709. return &ptr;
  710. }
  711. void detach_from_objectdb();
  712. _FORCE_INLINE_ ObjectID get_instance_id() const { return _instance_id; }
  713. template <typename T>
  714. static T *cast_to(Object *p_object) {
  715. // This is like dynamic_cast, but faster.
  716. // The reason is that we can assume no virtual and multiple inheritance.
  717. static_assert(std::is_base_of_v<Object, T>, "T must be derived from Object");
  718. static_assert(std::is_same_v<std::decay_t<T>, typename T::self_type>, "T must use GDCLASS or GDSOFTCLASS");
  719. return p_object && p_object->is_class_ptr(T::get_class_ptr_static()) ? static_cast<T *>(p_object) : nullptr;
  720. }
  721. template <typename T>
  722. static const T *cast_to(const Object *p_object) {
  723. static_assert(std::is_base_of_v<Object, T>, "T must be derived from Object");
  724. static_assert(std::is_same_v<std::decay_t<T>, typename T::self_type>, "T must use GDCLASS or GDSOFTCLASS");
  725. return p_object && p_object->is_class_ptr(T::get_class_ptr_static()) ? static_cast<const T *>(p_object) : nullptr;
  726. }
  727. enum {
  728. NOTIFICATION_POSTINITIALIZE = 0,
  729. NOTIFICATION_PREDELETE = 1,
  730. NOTIFICATION_EXTENSION_RELOADED = 2,
  731. // Internal notification to send after NOTIFICATION_PREDELETE, not bound to scripting.
  732. NOTIFICATION_PREDELETE_CLEANUP = 3,
  733. };
  734. /* TYPE API */
  735. static void assign_class_name_static(const Span<char> &p_name, StringName &r_target);
  736. static const StringName &get_class_static() {
  737. static StringName _class_name_static;
  738. if (unlikely(!_class_name_static)) {
  739. assign_class_name_static("Object", _class_name_static);
  740. }
  741. return _class_name_static;
  742. }
  743. _FORCE_INLINE_ String get_class() const { return get_class_name(); }
  744. virtual String get_save_class() const { return get_class(); } //class stored when saving
  745. virtual bool is_class(const String &p_class) const {
  746. if (_extension && _extension->is_class(p_class)) {
  747. return true;
  748. }
  749. return (p_class == "Object");
  750. }
  751. virtual bool is_class_ptr(void *p_ptr) const { return get_class_ptr_static() == p_ptr; }
  752. const StringName &get_class_name() const;
  753. StringName get_class_name_for_extension(const GDExtension *p_library) const;
  754. /* IAPI */
  755. void set(const StringName &p_name, const Variant &p_value, bool *r_valid = nullptr);
  756. Variant get(const StringName &p_name, bool *r_valid = nullptr) const;
  757. void set_indexed(const Vector<StringName> &p_names, const Variant &p_value, bool *r_valid = nullptr);
  758. Variant get_indexed(const Vector<StringName> &p_names, bool *r_valid = nullptr) const;
  759. void get_property_list(List<PropertyInfo> *p_list, bool p_reversed = false) const;
  760. void validate_property(PropertyInfo &p_property) const;
  761. bool property_can_revert(const StringName &p_name) const;
  762. Variant property_get_revert(const StringName &p_name) const;
  763. bool has_method(const StringName &p_method) const;
  764. int get_method_argument_count(const StringName &p_method, bool *r_is_valid = nullptr) const;
  765. void get_method_list(List<MethodInfo> *p_list) const;
  766. Variant callv(const StringName &p_method, const Array &p_args);
  767. virtual Variant callp(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error);
  768. virtual Variant call_const(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error);
  769. template <typename... VarArgs>
  770. Variant call(const StringName &p_method, VarArgs... p_args) {
  771. Variant args[sizeof...(p_args) + 1] = { p_args..., Variant() }; // +1 makes sure zero sized arrays are also supported.
  772. const Variant *argptrs[sizeof...(p_args) + 1];
  773. for (uint32_t i = 0; i < sizeof...(p_args); i++) {
  774. argptrs[i] = &args[i];
  775. }
  776. Callable::CallError cerr;
  777. const Variant ret = callp(p_method, sizeof...(p_args) == 0 ? nullptr : (const Variant **)argptrs, sizeof...(p_args), cerr);
  778. return (cerr.error == Callable::CallError::CALL_OK) ? ret : Variant();
  779. }
  780. // Depending on the boolean, we call either the virtual function _notification_backward or _notification_forward.
  781. // - Forward calls subclasses in descending order (e.g. Object -> Node -> Node3D -> extension -> script).
  782. // Backward calls subclasses in descending order (e.g. script -> extension -> Node3D -> Node -> Object).
  783. _FORCE_INLINE_ void notification(int p_notification, bool p_reversed = false) {
  784. if (p_reversed) {
  785. _notification_backward(p_notification);
  786. } else {
  787. _notification_forward(p_notification);
  788. }
  789. }
  790. virtual String to_string();
  791. // Used mainly by script, get and set all INCLUDING string.
  792. virtual Variant getvar(const Variant &p_key, bool *r_valid = nullptr) const;
  793. virtual void setvar(const Variant &p_key, const Variant &p_value, bool *r_valid = nullptr);
  794. /* SCRIPT */
  795. // When in debug, some non-virtual functions can be overridden for multithreaded guards.
  796. #ifdef DEBUG_ENABLED
  797. #define MTVIRTUAL virtual
  798. #else
  799. #define MTVIRTUAL
  800. #endif // DEBUG_ENABLED
  801. MTVIRTUAL void set_script(const Variant &p_script);
  802. MTVIRTUAL Variant get_script() const;
  803. MTVIRTUAL bool has_meta(const StringName &p_name) const;
  804. MTVIRTUAL void set_meta(const StringName &p_name, const Variant &p_value);
  805. MTVIRTUAL void remove_meta(const StringName &p_name);
  806. MTVIRTUAL Variant get_meta(const StringName &p_name, const Variant &p_default = Variant()) const;
  807. MTVIRTUAL void get_meta_list(List<StringName> *p_list) const;
  808. MTVIRTUAL void merge_meta_from(const Object *p_src);
  809. #ifdef TOOLS_ENABLED
  810. void set_edited(bool p_edited);
  811. bool is_edited() const;
  812. // This function is used to check when something changed beyond a point, it's used mainly for generating previews.
  813. uint32_t get_edited_version() const;
  814. #endif
  815. void set_script_instance(ScriptInstance *p_instance);
  816. _FORCE_INLINE_ ScriptInstance *get_script_instance() const { return script_instance; }
  817. // Some script languages can't control instance creation, so this function eases the process.
  818. void set_script_and_instance(const Variant &p_script, ScriptInstance *p_instance);
  819. void add_user_signal(const MethodInfo &p_signal);
  820. template <typename... VarArgs>
  821. Error emit_signal(const StringName &p_name, VarArgs... p_args) {
  822. Variant args[sizeof...(p_args) + 1] = { p_args..., Variant() }; // +1 makes sure zero sized arrays are also supported.
  823. const Variant *argptrs[sizeof...(p_args) + 1];
  824. for (uint32_t i = 0; i < sizeof...(p_args); i++) {
  825. argptrs[i] = &args[i];
  826. }
  827. return emit_signalp(p_name, sizeof...(p_args) == 0 ? nullptr : (const Variant **)argptrs, sizeof...(p_args));
  828. }
  829. MTVIRTUAL Error emit_signalp(const StringName &p_name, const Variant **p_args, int p_argcount);
  830. MTVIRTUAL bool has_signal(const StringName &p_name) const;
  831. MTVIRTUAL void get_signal_list(List<MethodInfo> *p_signals) const;
  832. MTVIRTUAL void get_signal_connection_list(const StringName &p_signal, List<Connection> *p_connections) const;
  833. MTVIRTUAL void get_all_signal_connections(List<Connection> *p_connections) const;
  834. MTVIRTUAL int get_persistent_signal_connection_count() const;
  835. MTVIRTUAL void get_signals_connected_to_this(List<Connection> *p_connections) const;
  836. MTVIRTUAL Error connect(const StringName &p_signal, const Callable &p_callable, uint32_t p_flags = 0);
  837. MTVIRTUAL void disconnect(const StringName &p_signal, const Callable &p_callable);
  838. MTVIRTUAL bool is_connected(const StringName &p_signal, const Callable &p_callable) const;
  839. MTVIRTUAL bool has_connections(const StringName &p_signal) const;
  840. template <typename... VarArgs>
  841. void call_deferred(const StringName &p_name, VarArgs... p_args) {
  842. MessageQueue::get_singleton()->push_call(this, p_name, p_args...);
  843. }
  844. void set_deferred(const StringName &p_property, const Variant &p_value);
  845. void set_block_signals(bool p_block);
  846. bool is_blocking_signals() const;
  847. Variant::Type get_static_property_type(const StringName &p_property, bool *r_valid = nullptr) const;
  848. Variant::Type get_static_property_type_indexed(const Vector<StringName> &p_path, bool *r_valid = nullptr) const;
  849. // Translate message (internationalization).
  850. String tr(const StringName &p_message, const StringName &p_context = "") const;
  851. String tr_n(const StringName &p_message, const StringName &p_message_plural, int p_n, const StringName &p_context = "") const;
  852. bool _is_queued_for_deletion = false; // Set to true by SceneTree::queue_delete().
  853. bool is_queued_for_deletion() const;
  854. _FORCE_INLINE_ void set_message_translation(bool p_enable) { _can_translate = p_enable; }
  855. _FORCE_INLINE_ bool can_translate_messages() const { return _can_translate; }
  856. virtual StringName get_translation_domain() const;
  857. virtual void set_translation_domain(const StringName &p_domain);
  858. #ifdef TOOLS_ENABLED
  859. virtual void get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const;
  860. void editor_set_section_unfold(const String &p_section, bool p_unfolded, bool p_initializing = false);
  861. bool editor_is_section_unfolded(const String &p_section);
  862. const HashSet<String> &editor_get_section_folding() const { return editor_section_folding; }
  863. void editor_clear_section_folding() { editor_section_folding.clear(); }
  864. #endif
  865. // Used by script languages to store binding data.
  866. void *get_instance_binding(void *p_token, const GDExtensionInstanceBindingCallbacks *p_callbacks);
  867. // Used on creation by binding only.
  868. void set_instance_binding(void *p_token, void *p_binding, const GDExtensionInstanceBindingCallbacks *p_callbacks);
  869. bool has_instance_binding(void *p_token);
  870. void free_instance_binding(void *p_token);
  871. #ifdef TOOLS_ENABLED
  872. void clear_internal_extension();
  873. void reset_internal_extension(ObjectGDExtension *p_extension);
  874. bool is_extension_placeholder() const { return _extension && _extension->is_placeholder; }
  875. #endif
  876. void clear_internal_resource_paths();
  877. _ALWAYS_INLINE_ bool is_ref_counted() const { return type_is_reference; }
  878. void cancel_free();
  879. Object();
  880. virtual ~Object();
  881. };
  882. bool predelete_handler(Object *p_object);
  883. void postinitialize_handler(Object *p_object);
  884. class ObjectDB {
  885. // This needs to add up to 63, 1 bit is for reference.
  886. #define OBJECTDB_VALIDATOR_BITS 39
  887. #define OBJECTDB_VALIDATOR_MASK ((uint64_t(1) << OBJECTDB_VALIDATOR_BITS) - 1)
  888. #define OBJECTDB_SLOT_MAX_COUNT_BITS 24
  889. #define OBJECTDB_SLOT_MAX_COUNT_MASK ((uint64_t(1) << OBJECTDB_SLOT_MAX_COUNT_BITS) - 1)
  890. #define OBJECTDB_REFERENCE_BIT (uint64_t(1) << (OBJECTDB_SLOT_MAX_COUNT_BITS + OBJECTDB_VALIDATOR_BITS))
  891. struct ObjectSlot { // 128 bits per slot.
  892. uint64_t validator : OBJECTDB_VALIDATOR_BITS;
  893. uint64_t next_free : OBJECTDB_SLOT_MAX_COUNT_BITS;
  894. uint64_t is_ref_counted : 1;
  895. Object *object = nullptr;
  896. };
  897. static SpinLock spin_lock;
  898. static uint32_t slot_count;
  899. static uint32_t slot_max;
  900. static ObjectSlot *object_slots;
  901. static uint64_t validator_counter;
  902. friend class Object;
  903. friend void unregister_core_types();
  904. static void cleanup();
  905. static ObjectID add_instance(Object *p_object);
  906. static void remove_instance(Object *p_object);
  907. friend void register_core_types();
  908. static void setup();
  909. public:
  910. typedef void (*DebugFunc)(Object *p_obj);
  911. _ALWAYS_INLINE_ static Object *get_instance(ObjectID p_instance_id) {
  912. uint64_t id = p_instance_id;
  913. uint32_t slot = id & OBJECTDB_SLOT_MAX_COUNT_MASK;
  914. ERR_FAIL_COND_V(slot >= slot_max, nullptr); // This should never happen unless RID is corrupted.
  915. spin_lock.lock();
  916. uint64_t validator = (id >> OBJECTDB_SLOT_MAX_COUNT_BITS) & OBJECTDB_VALIDATOR_MASK;
  917. if (unlikely(object_slots[slot].validator != validator)) {
  918. spin_lock.unlock();
  919. return nullptr;
  920. }
  921. Object *object = object_slots[slot].object;
  922. spin_lock.unlock();
  923. return object;
  924. }
  925. template <typename T>
  926. _ALWAYS_INLINE_ static T *get_instance(ObjectID p_instance_id) {
  927. return Object::cast_to<T>(get_instance(p_instance_id));
  928. }
  929. template <typename T>
  930. _ALWAYS_INLINE_ static Ref<T> get_ref(ObjectID p_instance_id); // Defined in ref_counted.h
  931. static void debug_objects(DebugFunc p_func);
  932. static int get_object_count();
  933. };