object.h 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  1. /*************************************************************************/
  2. /* object.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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/object/object_id.h"
  33. #include "core/os/rw_lock.h"
  34. #include "core/os/spin_lock.h"
  35. #include "core/templates/hash_map.h"
  36. #include "core/templates/list.h"
  37. #include "core/templates/map.h"
  38. #include "core/templates/safe_refcount.h"
  39. #include "core/templates/set.h"
  40. #include "core/templates/vmap.h"
  41. #include "core/variant/callable_bind.h"
  42. #include "core/variant/variant.h"
  43. #define VARIANT_ARG_LIST const Variant &p_arg1 = Variant(), const Variant &p_arg2 = Variant(), const Variant &p_arg3 = Variant(), const Variant &p_arg4 = Variant(), const Variant &p_arg5 = Variant()
  44. #define VARIANT_ARG_PASS p_arg1, p_arg2, p_arg3, p_arg4, p_arg5
  45. #define VARIANT_ARG_DECLARE const Variant &p_arg1, const Variant &p_arg2, const Variant &p_arg3, const Variant &p_arg4, const Variant &p_arg5
  46. #define VARIANT_ARG_MAX 5
  47. #define VARIANT_ARGPTRS const Variant *argptr[5] = { &p_arg1, &p_arg2, &p_arg3, &p_arg4, &p_arg5 };
  48. #define VARIANT_ARGPTRS_PASS *argptr[0], *argptr[1], *argptr[2], *argptr[3], *argptr[4]
  49. #define VARIANT_ARGS_FROM_ARRAY(m_arr) m_arr[0], m_arr[1], m_arr[2], m_arr[3], m_arr[4]
  50. /**
  51. @author Juan Linietsky <[email protected]>
  52. */
  53. enum PropertyHint {
  54. PROPERTY_HINT_NONE, ///< no hint provided.
  55. PROPERTY_HINT_RANGE, ///< hint_text = "min,max,step,slider; //slider is optional"
  56. PROPERTY_HINT_EXP_RANGE, ///< hint_text = "min,max,step", exponential edit
  57. PROPERTY_HINT_ENUM, ///< hint_text= "val1,val2,val3,etc"
  58. PROPERTY_HINT_EXP_EASING, /// exponential easing function (Math::ease) use "attenuation" hint string to revert (flip h), "full" to also include in/out. (ie: "attenuation,inout")
  59. PROPERTY_HINT_LENGTH, ///< hint_text= "length" (as integer)
  60. PROPERTY_HINT_KEY_ACCEL, ///< hint_text= "length" (as integer)
  61. PROPERTY_HINT_FLAGS, ///< hint_text= "flag1,flag2,etc" (as bit flags)
  62. PROPERTY_HINT_LAYERS_2D_RENDER,
  63. PROPERTY_HINT_LAYERS_2D_PHYSICS,
  64. PROPERTY_HINT_LAYERS_3D_RENDER,
  65. PROPERTY_HINT_LAYERS_3D_PHYSICS,
  66. PROPERTY_HINT_FILE, ///< a file path must be passed, hint_text (optionally) is a filter "*.png,*.wav,*.doc,"
  67. PROPERTY_HINT_DIR, ///< a directory path must be passed
  68. PROPERTY_HINT_GLOBAL_FILE, ///< a file path must be passed, hint_text (optionally) is a filter "*.png,*.wav,*.doc,"
  69. PROPERTY_HINT_GLOBAL_DIR, ///< a directory path must be passed
  70. PROPERTY_HINT_RESOURCE_TYPE, ///< a resource object type
  71. PROPERTY_HINT_MULTILINE_TEXT, ///< used for string properties that can contain multiple lines
  72. PROPERTY_HINT_PLACEHOLDER_TEXT, ///< used to set a placeholder text for string properties
  73. PROPERTY_HINT_COLOR_NO_ALPHA, ///< used for ignoring alpha component when editing a color
  74. PROPERTY_HINT_IMAGE_COMPRESS_LOSSY,
  75. PROPERTY_HINT_IMAGE_COMPRESS_LOSSLESS,
  76. PROPERTY_HINT_OBJECT_ID,
  77. PROPERTY_HINT_TYPE_STRING, ///< a type string, the hint is the base type to choose
  78. PROPERTY_HINT_NODE_PATH_TO_EDITED_NODE, ///< so something else can provide this (used in scripts)
  79. PROPERTY_HINT_METHOD_OF_VARIANT_TYPE, ///< a method of a type
  80. PROPERTY_HINT_METHOD_OF_BASE_TYPE, ///< a method of a base type
  81. PROPERTY_HINT_METHOD_OF_INSTANCE, ///< a method of an instance
  82. PROPERTY_HINT_METHOD_OF_SCRIPT, ///< a method of a script & base
  83. PROPERTY_HINT_PROPERTY_OF_VARIANT_TYPE, ///< a property of a type
  84. PROPERTY_HINT_PROPERTY_OF_BASE_TYPE, ///< a property of a base type
  85. PROPERTY_HINT_PROPERTY_OF_INSTANCE, ///< a property of an instance
  86. PROPERTY_HINT_PROPERTY_OF_SCRIPT, ///< a property of a script & base
  87. PROPERTY_HINT_OBJECT_TOO_BIG, ///< object is too big to send
  88. PROPERTY_HINT_NODE_PATH_VALID_TYPES,
  89. PROPERTY_HINT_SAVE_FILE, ///< a file path must be passed, hint_text (optionally) is a filter "*.png,*.wav,*.doc,". This opens a save dialog
  90. PROPERTY_HINT_INT_IS_OBJECTID,
  91. PROPERTY_HINT_ARRAY_TYPE,
  92. PROPERTY_HINT_MAX,
  93. // When updating PropertyHint, also sync the hardcoded list in VisualScriptEditorVariableEdit
  94. };
  95. enum PropertyUsageFlags {
  96. PROPERTY_USAGE_STORAGE = 1,
  97. PROPERTY_USAGE_EDITOR = 2,
  98. PROPERTY_USAGE_NETWORK = 4,
  99. PROPERTY_USAGE_EDITOR_HELPER = 8,
  100. PROPERTY_USAGE_CHECKABLE = 16, //used for editing global variables
  101. PROPERTY_USAGE_CHECKED = 32, //used for editing global variables
  102. PROPERTY_USAGE_INTERNATIONALIZED = 64, //hint for internationalized strings
  103. PROPERTY_USAGE_GROUP = 128, //used for grouping props in the editor
  104. PROPERTY_USAGE_CATEGORY = 256,
  105. PROPERTY_USAGE_SUBGROUP = 512,
  106. PROPERTY_USAGE_NO_INSTANCE_STATE = 2048,
  107. PROPERTY_USAGE_RESTART_IF_CHANGED = 4096,
  108. PROPERTY_USAGE_SCRIPT_VARIABLE = 8192,
  109. PROPERTY_USAGE_STORE_IF_NULL = 16384,
  110. PROPERTY_USAGE_ANIMATE_AS_TRIGGER = 32768,
  111. PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED = 65536,
  112. PROPERTY_USAGE_SCRIPT_DEFAULT_VALUE = 1 << 17,
  113. PROPERTY_USAGE_CLASS_IS_ENUM = 1 << 18,
  114. PROPERTY_USAGE_NIL_IS_VARIANT = 1 << 19,
  115. PROPERTY_USAGE_INTERNAL = 1 << 20,
  116. PROPERTY_USAGE_DO_NOT_SHARE_ON_DUPLICATE = 1 << 21, // If the object is duplicated also this property will be duplicated
  117. PROPERTY_USAGE_HIGH_END_GFX = 1 << 22,
  118. PROPERTY_USAGE_NODE_PATH_FROM_SCENE_ROOT = 1 << 23,
  119. PROPERTY_USAGE_RESOURCE_NOT_PERSISTENT = 1 << 24,
  120. PROPERTY_USAGE_KEYING_INCREMENTS = 1 << 25, // Used in inspector to increment property when keyed in animation player
  121. PROPERTY_USAGE_DEFERRED_SET_RESOURCE = 1 << 26, // when loading, the resource for this property can be set at the end of loading
  122. PROPERTY_USAGE_EDITOR_INSTANTIATE_OBJECT = 1 << 27, // For Object properties, instantiate them when creating in editor.
  123. PROPERTY_USAGE_EDITOR_BASIC_SETTING = 1 << 28, //for project or editor settings, show when basic settings are selected
  124. PROPERTY_USAGE_DEFAULT = PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_NETWORK,
  125. PROPERTY_USAGE_DEFAULT_INTL = PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_NETWORK | PROPERTY_USAGE_INTERNATIONALIZED,
  126. PROPERTY_USAGE_NOEDITOR = PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_NETWORK,
  127. };
  128. #define ADD_SIGNAL(m_signal) ClassDB::add_signal(get_class_static(), m_signal)
  129. #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))
  130. #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)
  131. #define ADD_PROPERTY_DEFAULT(m_property, m_default) ClassDB::set_property_default_value(get_class_static(), m_property, m_default)
  132. #define ADD_GROUP(m_name, m_prefix) ClassDB::add_property_group(get_class_static(), m_name, m_prefix)
  133. #define ADD_SUBGROUP(m_name, m_prefix) ClassDB::add_property_subgroup(get_class_static(), m_name, m_prefix)
  134. struct PropertyInfo {
  135. Variant::Type type = Variant::NIL;
  136. String name;
  137. StringName class_name; //for classes
  138. PropertyHint hint = PROPERTY_HINT_NONE;
  139. String hint_string;
  140. uint32_t usage = PROPERTY_USAGE_DEFAULT;
  141. _FORCE_INLINE_ PropertyInfo added_usage(int p_fl) const {
  142. PropertyInfo pi = *this;
  143. pi.usage |= p_fl;
  144. return pi;
  145. }
  146. operator Dictionary() const;
  147. static PropertyInfo from_dict(const Dictionary &p_dict);
  148. PropertyInfo() {}
  149. PropertyInfo(Variant::Type p_type, const String p_name, PropertyHint p_hint = PROPERTY_HINT_NONE, const String &p_hint_string = "", uint32_t p_usage = PROPERTY_USAGE_DEFAULT, const StringName &p_class_name = StringName()) :
  150. type(p_type),
  151. name(p_name),
  152. hint(p_hint),
  153. hint_string(p_hint_string),
  154. usage(p_usage) {
  155. if (hint == PROPERTY_HINT_RESOURCE_TYPE) {
  156. class_name = hint_string;
  157. } else {
  158. class_name = p_class_name;
  159. }
  160. }
  161. PropertyInfo(const StringName &p_class_name) :
  162. type(Variant::OBJECT),
  163. class_name(p_class_name) {}
  164. bool operator==(const PropertyInfo &p_info) const {
  165. return ((type == p_info.type) &&
  166. (name == p_info.name) &&
  167. (class_name == p_info.class_name) &&
  168. (hint == p_info.hint) &&
  169. (hint_string == p_info.hint_string) &&
  170. (usage == p_info.usage));
  171. }
  172. bool operator<(const PropertyInfo &p_info) const {
  173. return name < p_info.name;
  174. }
  175. };
  176. Array convert_property_list(const List<PropertyInfo> *p_list);
  177. struct MethodInfo {
  178. String name;
  179. PropertyInfo return_val;
  180. uint32_t flags; // NOLINT - prevent clang-tidy to assign method_bind.h constant here, it should stay in .cpp.
  181. int id = 0;
  182. List<PropertyInfo> arguments;
  183. Vector<Variant> default_arguments;
  184. inline bool operator==(const MethodInfo &p_method) const { return id == p_method.id; }
  185. inline bool operator<(const MethodInfo &p_method) const { return id == p_method.id ? (name < p_method.name) : (id < p_method.id); }
  186. operator Dictionary() const;
  187. static MethodInfo from_dict(const Dictionary &p_dict);
  188. MethodInfo();
  189. MethodInfo(const String &p_name);
  190. MethodInfo(const String &p_name, const PropertyInfo &p_param1);
  191. MethodInfo(const String &p_name, const PropertyInfo &p_param1, const PropertyInfo &p_param2);
  192. MethodInfo(const String &p_name, const PropertyInfo &p_param1, const PropertyInfo &p_param2, const PropertyInfo &p_param3);
  193. MethodInfo(const String &p_name, const PropertyInfo &p_param1, const PropertyInfo &p_param2, const PropertyInfo &p_param3, const PropertyInfo &p_param4);
  194. MethodInfo(const String &p_name, const PropertyInfo &p_param1, const PropertyInfo &p_param2, const PropertyInfo &p_param3, const PropertyInfo &p_param4, const PropertyInfo &p_param5);
  195. MethodInfo(Variant::Type ret);
  196. MethodInfo(Variant::Type ret, const String &p_name);
  197. MethodInfo(Variant::Type ret, const String &p_name, const PropertyInfo &p_param1);
  198. MethodInfo(Variant::Type ret, const String &p_name, const PropertyInfo &p_param1, const PropertyInfo &p_param2);
  199. MethodInfo(Variant::Type ret, const String &p_name, const PropertyInfo &p_param1, const PropertyInfo &p_param2, const PropertyInfo &p_param3);
  200. MethodInfo(Variant::Type ret, const String &p_name, const PropertyInfo &p_param1, const PropertyInfo &p_param2, const PropertyInfo &p_param3, const PropertyInfo &p_param4);
  201. MethodInfo(Variant::Type ret, const String &p_name, const PropertyInfo &p_param1, const PropertyInfo &p_param2, const PropertyInfo &p_param3, const PropertyInfo &p_param4, const PropertyInfo &p_param5);
  202. MethodInfo(const PropertyInfo &p_ret, const String &p_name);
  203. MethodInfo(const PropertyInfo &p_ret, const String &p_name, const PropertyInfo &p_param1);
  204. MethodInfo(const PropertyInfo &p_ret, const String &p_name, const PropertyInfo &p_param1, const PropertyInfo &p_param2);
  205. MethodInfo(const PropertyInfo &p_ret, const String &p_name, const PropertyInfo &p_param1, const PropertyInfo &p_param2, const PropertyInfo &p_param3);
  206. MethodInfo(const PropertyInfo &p_ret, const String &p_name, const PropertyInfo &p_param1, const PropertyInfo &p_param2, const PropertyInfo &p_param3, const PropertyInfo &p_param4);
  207. MethodInfo(const PropertyInfo &p_ret, const String &p_name, const PropertyInfo &p_param1, const PropertyInfo &p_param2, const PropertyInfo &p_param3, const PropertyInfo &p_param4, const PropertyInfo &p_param5);
  208. };
  209. // old cast_to
  210. //if ( is_type(T::get_class_static()) )
  211. //return static_cast<T*>(this);
  212. ////else
  213. //return nullptr;
  214. /*
  215. the following is an incomprehensible blob of hacks and workarounds to compensate for many of the fallencies in C++. As a plus, this macro pretty much alone defines the object model.
  216. */
  217. #define REVERSE_GET_PROPERTY_LIST \
  218. public: \
  219. _FORCE_INLINE_ bool _is_gpl_reversed() const { return true; }; \
  220. \
  221. private:
  222. #define UNREVERSE_GET_PROPERTY_LIST \
  223. public: \
  224. _FORCE_INLINE_ bool _is_gpl_reversed() const { return false; }; \
  225. \
  226. private:
  227. #define GDCLASS(m_class, m_inherits) \
  228. private: \
  229. void operator=(const m_class &p_rval) {} \
  230. mutable StringName _class_name; \
  231. friend class ClassDB; \
  232. \
  233. public: \
  234. virtual String get_class() const override { \
  235. return String(#m_class); \
  236. } \
  237. virtual const StringName *_get_class_namev() const override { \
  238. if (!_class_name) { \
  239. _class_name = get_class_static(); \
  240. } \
  241. return &_class_name; \
  242. } \
  243. static _FORCE_INLINE_ void *get_class_ptr_static() { \
  244. static int ptr; \
  245. return &ptr; \
  246. } \
  247. static _FORCE_INLINE_ String get_class_static() { \
  248. return String(#m_class); \
  249. } \
  250. static _FORCE_INLINE_ String get_parent_class_static() { \
  251. return m_inherits::get_class_static(); \
  252. } \
  253. static void get_inheritance_list_static(List<String> *p_inheritance_list) { \
  254. m_inherits::get_inheritance_list_static(p_inheritance_list); \
  255. p_inheritance_list->push_back(String(#m_class)); \
  256. } \
  257. static String get_category_static() { \
  258. String category = m_inherits::get_category_static(); \
  259. if (_get_category != m_inherits::_get_category) { \
  260. if (category != "") { \
  261. category += "/"; \
  262. } \
  263. category += _get_category(); \
  264. } \
  265. return category; \
  266. } \
  267. static String inherits_static() { \
  268. return String(#m_inherits); \
  269. } \
  270. virtual bool is_class(const String &p_class) const override { return (p_class == (#m_class)) ? true : m_inherits::is_class(p_class); } \
  271. 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); } \
  272. \
  273. static void get_valid_parents_static(List<String> *p_parents) { \
  274. if (m_class::_get_valid_parents_static != m_inherits::_get_valid_parents_static) { \
  275. m_class::_get_valid_parents_static(p_parents); \
  276. } \
  277. \
  278. m_inherits::get_valid_parents_static(p_parents); \
  279. } \
  280. \
  281. protected: \
  282. _FORCE_INLINE_ static void (*_get_bind_methods())() { \
  283. return &m_class::_bind_methods; \
  284. } \
  285. \
  286. public: \
  287. static void initialize_class() { \
  288. static bool initialized = false; \
  289. if (initialized) { \
  290. return; \
  291. } \
  292. m_inherits::initialize_class(); \
  293. ClassDB::_add_class<m_class>(); \
  294. if (m_class::_get_bind_methods() != m_inherits::_get_bind_methods()) { \
  295. _bind_methods(); \
  296. } \
  297. initialized = true; \
  298. } \
  299. \
  300. protected: \
  301. virtual void _initialize_classv() override { \
  302. initialize_class(); \
  303. } \
  304. _FORCE_INLINE_ bool (Object::*_get_get() const)(const StringName &p_name, Variant &) const { \
  305. return (bool (Object::*)(const StringName &, Variant &) const) & m_class::_get; \
  306. } \
  307. virtual bool _getv(const StringName &p_name, Variant &r_ret) const override { \
  308. if (m_class::_get_get() != m_inherits::_get_get()) { \
  309. if (_get(p_name, r_ret)) { \
  310. return true; \
  311. } \
  312. } \
  313. return m_inherits::_getv(p_name, r_ret); \
  314. } \
  315. _FORCE_INLINE_ bool (Object::*_get_set() const)(const StringName &p_name, const Variant &p_property) { \
  316. return (bool (Object::*)(const StringName &, const Variant &)) & m_class::_set; \
  317. } \
  318. virtual bool _setv(const StringName &p_name, const Variant &p_property) override { \
  319. if (m_inherits::_setv(p_name, p_property)) { \
  320. return true; \
  321. } \
  322. if (m_class::_get_set() != m_inherits::_get_set()) { \
  323. return _set(p_name, p_property); \
  324. } \
  325. return false; \
  326. } \
  327. _FORCE_INLINE_ void (Object::*_get_get_property_list() const)(List<PropertyInfo> * p_list) const { \
  328. return (void (Object::*)(List<PropertyInfo> *) const) & m_class::_get_property_list; \
  329. } \
  330. virtual void _get_property_listv(List<PropertyInfo> *p_list, bool p_reversed) const override { \
  331. if (!p_reversed) { \
  332. m_inherits::_get_property_listv(p_list, p_reversed); \
  333. } \
  334. p_list->push_back(PropertyInfo(Variant::NIL, get_class_static(), PROPERTY_HINT_NONE, String(), PROPERTY_USAGE_CATEGORY)); \
  335. if (!_is_gpl_reversed()) { \
  336. ClassDB::get_property_list(#m_class, p_list, true, this); \
  337. } \
  338. if (m_class::_get_get_property_list() != m_inherits::_get_get_property_list()) { \
  339. _get_property_list(p_list); \
  340. } \
  341. if (_is_gpl_reversed()) { \
  342. ClassDB::get_property_list(#m_class, p_list, true, this); \
  343. } \
  344. if (p_reversed) { \
  345. m_inherits::_get_property_listv(p_list, p_reversed); \
  346. } \
  347. } \
  348. _FORCE_INLINE_ void (Object::*_get_notification() const)(int) { \
  349. return (void (Object::*)(int)) & m_class::_notification; \
  350. } \
  351. virtual void _notificationv(int p_notification, bool p_reversed) override { \
  352. if (!p_reversed) { \
  353. m_inherits::_notificationv(p_notification, p_reversed); \
  354. } \
  355. if (m_class::_get_notification() != m_inherits::_get_notification()) { \
  356. _notification(p_notification); \
  357. } \
  358. if (p_reversed) { \
  359. m_inherits::_notificationv(p_notification, p_reversed); \
  360. } \
  361. } \
  362. \
  363. private:
  364. #define OBJ_CATEGORY(m_category) \
  365. protected: \
  366. _FORCE_INLINE_ static String _get_category() { return m_category; } \
  367. \
  368. private:
  369. #define OBJ_SAVE_TYPE(m_class) \
  370. public: \
  371. virtual String get_save_class() const override { return #m_class; } \
  372. \
  373. private:
  374. class ScriptInstance;
  375. class Object {
  376. public:
  377. enum ConnectFlags {
  378. CONNECT_DEFERRED = 1,
  379. CONNECT_PERSIST = 2, // hint for scene to save this connection
  380. CONNECT_ONESHOT = 4,
  381. CONNECT_REFERENCE_COUNTED = 8,
  382. };
  383. struct Connection {
  384. ::Signal signal;
  385. Callable callable;
  386. uint32_t flags = 0;
  387. Vector<Variant> binds;
  388. bool operator<(const Connection &p_conn) const;
  389. operator Variant() const;
  390. Connection() {}
  391. Connection(const Variant &p_variant);
  392. };
  393. private:
  394. enum {
  395. MAX_SCRIPT_INSTANCE_BINDINGS = 8
  396. };
  397. #ifdef DEBUG_ENABLED
  398. friend struct _ObjectDebugLock;
  399. #endif
  400. friend bool predelete_handler(Object *);
  401. friend void postinitialize_handler(Object *);
  402. struct SignalData {
  403. struct Slot {
  404. int reference_count = 0;
  405. Connection conn;
  406. List<Connection>::Element *cE = nullptr;
  407. };
  408. MethodInfo user;
  409. VMap<Callable, Slot> slot_map;
  410. };
  411. HashMap<StringName, SignalData> signal_map;
  412. List<Connection> connections;
  413. #ifdef DEBUG_ENABLED
  414. SafeRefCount _lock_index;
  415. #endif
  416. bool _block_signals = false;
  417. int _predelete_ok = 0;
  418. ObjectID _instance_id;
  419. bool _predelete();
  420. void _postinitialize();
  421. bool _can_translate = true;
  422. bool _emitting = false;
  423. #ifdef TOOLS_ENABLED
  424. bool _edited = false;
  425. uint32_t _edited_version = 0;
  426. Set<String> editor_section_folding;
  427. #endif
  428. ScriptInstance *script_instance = nullptr;
  429. Variant script; //reference does not yet exist, store it in a
  430. Dictionary metadata;
  431. mutable StringName _class_name;
  432. mutable const StringName *_class_ptr = nullptr;
  433. void _add_user_signal(const String &p_name, const Array &p_args = Array());
  434. bool _has_user_signal(const StringName &p_name) const;
  435. Variant _emit_signal(const Variant **p_args, int p_argcount, Callable::CallError &r_error);
  436. Array _get_signal_list() const;
  437. Array _get_signal_connection_list(const String &p_signal) const;
  438. Array _get_incoming_connections() const;
  439. void _set_bind(const String &p_set, const Variant &p_value);
  440. Variant _get_bind(const String &p_name) const;
  441. void _set_indexed_bind(const NodePath &p_name, const Variant &p_value);
  442. Variant _get_indexed_bind(const NodePath &p_name) const;
  443. _FORCE_INLINE_ void _construct_object(bool p_reference);
  444. friend class Reference;
  445. bool type_is_reference = false;
  446. SafeNumeric<uint32_t> instance_binding_count;
  447. void *_script_instance_bindings[MAX_SCRIPT_INSTANCE_BINDINGS];
  448. Object(bool p_reference);
  449. protected:
  450. virtual void _initialize_classv() { initialize_class(); }
  451. virtual bool _setv(const StringName &p_name, const Variant &p_property) { return false; };
  452. virtual bool _getv(const StringName &p_name, Variant &r_property) const { return false; };
  453. virtual void _get_property_listv(List<PropertyInfo> *p_list, bool p_reversed) const {};
  454. virtual void _notificationv(int p_notification, bool p_reversed) {}
  455. static String _get_category() { return ""; }
  456. static void _bind_methods();
  457. bool _set(const StringName &p_name, const Variant &p_property) { return false; };
  458. bool _get(const StringName &p_name, Variant &r_property) const { return false; };
  459. void _get_property_list(List<PropertyInfo> *p_list) const {};
  460. void _notification(int p_notification) {}
  461. _FORCE_INLINE_ static void (*_get_bind_methods())() {
  462. return &Object::_bind_methods;
  463. }
  464. _FORCE_INLINE_ bool (Object::*_get_get() const)(const StringName &p_name, Variant &r_ret) const {
  465. return &Object::_get;
  466. }
  467. _FORCE_INLINE_ bool (Object::*_get_set() const)(const StringName &p_name, const Variant &p_property) {
  468. return &Object::_set;
  469. }
  470. _FORCE_INLINE_ void (Object::*_get_get_property_list() const)(List<PropertyInfo> *p_list) const {
  471. return &Object::_get_property_list;
  472. }
  473. _FORCE_INLINE_ void (Object::*_get_notification() const)(int) {
  474. return &Object::_notification;
  475. }
  476. static void get_valid_parents_static(List<String> *p_parents);
  477. static void _get_valid_parents_static(List<String> *p_parents);
  478. //Variant _call_bind(const StringName& p_name, const Variant& p_arg1 = Variant(), const Variant& p_arg2 = Variant(), const Variant& p_arg3 = Variant(), const Variant& p_arg4 = Variant());
  479. //void _call_deferred_bind(const StringName& p_name, const Variant& p_arg1 = Variant(), const Variant& p_arg2 = Variant(), const Variant& p_arg3 = Variant(), const Variant& p_arg4 = Variant());
  480. Variant _call_bind(const Variant **p_args, int p_argcount, Callable::CallError &r_error);
  481. Variant _call_deferred_bind(const Variant **p_args, int p_argcount, Callable::CallError &r_error);
  482. virtual const StringName *_get_class_namev() const {
  483. if (!_class_name) {
  484. _class_name = get_class_static();
  485. }
  486. return &_class_name;
  487. }
  488. Vector<String> _get_meta_list_bind() const;
  489. Array _get_property_list_bind() const;
  490. Array _get_method_list_bind() const;
  491. void _clear_internal_resource_paths(const Variant &p_var);
  492. friend class ClassDB;
  493. virtual void _validate_property(PropertyInfo &property) const;
  494. void _disconnect(const StringName &p_signal, const Callable &p_callable, bool p_force = false);
  495. public: //should be protected, but bug in clang++
  496. static void initialize_class();
  497. _FORCE_INLINE_ static void register_custom_data_to_otdb() {}
  498. public:
  499. void notify_property_list_changed();
  500. static void *get_class_ptr_static() {
  501. static int ptr;
  502. return &ptr;
  503. }
  504. bool _is_gpl_reversed() const { return false; }
  505. _FORCE_INLINE_ ObjectID get_instance_id() const { return _instance_id; }
  506. template <class T>
  507. static T *cast_to(Object *p_object) {
  508. #ifndef NO_SAFE_CAST
  509. return dynamic_cast<T *>(p_object);
  510. #else
  511. if (!p_object) {
  512. return nullptr;
  513. }
  514. if (p_object->is_class_ptr(T::get_class_ptr_static())) {
  515. return static_cast<T *>(p_object);
  516. } else {
  517. return nullptr;
  518. }
  519. #endif
  520. }
  521. template <class T>
  522. static const T *cast_to(const Object *p_object) {
  523. #ifndef NO_SAFE_CAST
  524. return dynamic_cast<const T *>(p_object);
  525. #else
  526. if (!p_object) {
  527. return nullptr;
  528. }
  529. if (p_object->is_class_ptr(T::get_class_ptr_static())) {
  530. return static_cast<const T *>(p_object);
  531. } else {
  532. return nullptr;
  533. }
  534. #endif
  535. }
  536. enum {
  537. NOTIFICATION_POSTINITIALIZE = 0,
  538. NOTIFICATION_PREDELETE = 1
  539. };
  540. /* TYPE API */
  541. static void get_inheritance_list_static(List<String> *p_inheritance_list) { p_inheritance_list->push_back("Object"); }
  542. static String get_class_static() { return "Object"; }
  543. static String get_parent_class_static() { return String(); }
  544. static String get_category_static() { return String(); }
  545. virtual String get_class() const { return "Object"; }
  546. virtual String get_save_class() const { return get_class(); } //class stored when saving
  547. virtual bool is_class(const String &p_class) const { return (p_class == "Object"); }
  548. virtual bool is_class_ptr(void *p_ptr) const { return get_class_ptr_static() == p_ptr; }
  549. _FORCE_INLINE_ const StringName &get_class_name() const {
  550. if (!_class_ptr) {
  551. return *_get_class_namev();
  552. } else {
  553. return *_class_ptr;
  554. }
  555. }
  556. /* IAPI */
  557. //void set(const String& p_name, const Variant& p_value);
  558. //Variant get(const String& p_name) const;
  559. void set(const StringName &p_name, const Variant &p_value, bool *r_valid = nullptr);
  560. Variant get(const StringName &p_name, bool *r_valid = nullptr) const;
  561. void set_indexed(const Vector<StringName> &p_names, const Variant &p_value, bool *r_valid = nullptr);
  562. Variant get_indexed(const Vector<StringName> &p_names, bool *r_valid = nullptr) const;
  563. void get_property_list(List<PropertyInfo> *p_list, bool p_reversed = false) const;
  564. bool has_method(const StringName &p_method) const;
  565. void get_method_list(List<MethodInfo> *p_list) const;
  566. Variant callv(const StringName &p_method, const Array &p_args);
  567. virtual Variant call(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error);
  568. Variant call(const StringName &p_name, VARIANT_ARG_LIST); // C++ helper
  569. void notification(int p_notification, bool p_reversed = false);
  570. virtual String to_string();
  571. //used mainly by script, get and set all INCLUDING string
  572. virtual Variant getvar(const Variant &p_key, bool *r_valid = nullptr) const;
  573. virtual void setvar(const Variant &p_key, const Variant &p_value, bool *r_valid = nullptr);
  574. /* SCRIPT */
  575. void set_script(const Variant &p_script);
  576. Variant get_script() const;
  577. /* SCRIPT */
  578. bool has_meta(const String &p_name) const;
  579. void set_meta(const String &p_name, const Variant &p_value);
  580. void remove_meta(const String &p_name);
  581. Variant get_meta(const String &p_name) const;
  582. void get_meta_list(List<String> *p_list) const;
  583. #ifdef TOOLS_ENABLED
  584. void set_edited(bool p_edited);
  585. bool is_edited() const;
  586. uint32_t get_edited_version() const; //this function is used to check when something changed beyond a point, it's used mainly for generating previews
  587. #endif
  588. void set_script_instance(ScriptInstance *p_instance);
  589. _FORCE_INLINE_ ScriptInstance *get_script_instance() const { return script_instance; }
  590. void set_script_and_instance(const Variant &p_script, ScriptInstance *p_instance); //some script languages can't control instance creation, so this function eases the process
  591. void add_user_signal(const MethodInfo &p_signal);
  592. Error emit_signal(const StringName &p_name, VARIANT_ARG_LIST);
  593. Error emit_signal(const StringName &p_name, const Variant **p_args, int p_argcount);
  594. bool has_signal(const StringName &p_name) const;
  595. void get_signal_list(List<MethodInfo> *p_signals) const;
  596. void get_signal_connection_list(const StringName &p_signal, List<Connection> *p_connections) const;
  597. void get_all_signal_connections(List<Connection> *p_connections) const;
  598. int get_persistent_signal_connection_count() const;
  599. void get_signals_connected_to_this(List<Connection> *p_connections) const;
  600. Error connect(const StringName &p_signal, const Callable &p_callable, const Vector<Variant> &p_binds = Vector<Variant>(), uint32_t p_flags = 0);
  601. void disconnect(const StringName &p_signal, const Callable &p_callable);
  602. bool is_connected(const StringName &p_signal, const Callable &p_callable) const;
  603. void call_deferred(const StringName &p_method, VARIANT_ARG_LIST);
  604. void set_deferred(const StringName &p_property, const Variant &p_value);
  605. void set_block_signals(bool p_block);
  606. bool is_blocking_signals() const;
  607. Variant::Type get_static_property_type(const StringName &p_property, bool *r_valid = nullptr) const;
  608. Variant::Type get_static_property_type_indexed(const Vector<StringName> &p_path, bool *r_valid = nullptr) const;
  609. virtual void get_translatable_strings(List<String> *p_strings) const;
  610. virtual void get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const;
  611. String tr(const StringName &p_message, const StringName &p_context = "") const; // translate message (internationalization)
  612. String tr_n(const StringName &p_message, const StringName &p_message_plural, int p_n, const StringName &p_context = "") const;
  613. bool _is_queued_for_deletion = false; // set to true by SceneTree::queue_delete()
  614. bool is_queued_for_deletion() const;
  615. _FORCE_INLINE_ void set_message_translation(bool p_enable) { _can_translate = p_enable; }
  616. _FORCE_INLINE_ bool can_translate_messages() const { return _can_translate; }
  617. #ifdef TOOLS_ENABLED
  618. void editor_set_section_unfold(const String &p_section, bool p_unfolded);
  619. bool editor_is_section_unfolded(const String &p_section);
  620. const Set<String> &editor_get_section_folding() const { return editor_section_folding; }
  621. void editor_clear_section_folding() { editor_section_folding.clear(); }
  622. #endif
  623. //used by script languages to store binding data
  624. void *get_script_instance_binding(int p_script_language_index);
  625. bool has_script_instance_binding(int p_script_language_index);
  626. void set_script_instance_binding(int p_script_language_index, void *p_data);
  627. void clear_internal_resource_paths();
  628. _ALWAYS_INLINE_ bool is_reference() const { return type_is_reference; }
  629. Object();
  630. virtual ~Object();
  631. };
  632. bool predelete_handler(Object *p_object);
  633. void postinitialize_handler(Object *p_object);
  634. class ObjectDB {
  635. //this needs to add up to 63, 1 bit is for reference
  636. #define OBJECTDB_VALIDATOR_BITS 39
  637. #define OBJECTDB_VALIDATOR_MASK ((uint64_t(1) << OBJECTDB_VALIDATOR_BITS) - 1)
  638. #define OBJECTDB_SLOT_MAX_COUNT_BITS 24
  639. #define OBJECTDB_SLOT_MAX_COUNT_MASK ((uint64_t(1) << OBJECTDB_SLOT_MAX_COUNT_BITS) - 1)
  640. #define OBJECTDB_REFERENCE_BIT (uint64_t(1) << (OBJECTDB_SLOT_MAX_COUNT_BITS + OBJECTDB_VALIDATOR_BITS))
  641. struct ObjectSlot { //128 bits per slot
  642. uint64_t validator : OBJECTDB_VALIDATOR_BITS;
  643. uint64_t next_free : OBJECTDB_SLOT_MAX_COUNT_BITS;
  644. uint64_t is_reference : 1;
  645. Object *object;
  646. };
  647. static SpinLock spin_lock;
  648. static uint32_t slot_count;
  649. static uint32_t slot_max;
  650. static ObjectSlot *object_slots;
  651. static uint64_t validator_counter;
  652. friend class Object;
  653. friend void unregister_core_types();
  654. static void cleanup();
  655. static ObjectID add_instance(Object *p_object);
  656. static void remove_instance(Object *p_object);
  657. friend void register_core_types();
  658. static void setup();
  659. public:
  660. typedef void (*DebugFunc)(Object *p_obj);
  661. _ALWAYS_INLINE_ static Object *get_instance(ObjectID p_instance_id) {
  662. uint64_t id = p_instance_id;
  663. uint32_t slot = id & OBJECTDB_SLOT_MAX_COUNT_MASK;
  664. ERR_FAIL_COND_V(slot >= slot_max, nullptr); //this should never happen unless RID is corrupted
  665. spin_lock.lock();
  666. uint64_t validator = (id >> OBJECTDB_SLOT_MAX_COUNT_BITS) & OBJECTDB_VALIDATOR_MASK;
  667. if (unlikely(object_slots[slot].validator != validator)) {
  668. spin_lock.unlock();
  669. return nullptr;
  670. }
  671. Object *object = object_slots[slot].object;
  672. spin_lock.unlock();
  673. return object;
  674. }
  675. static void debug_objects(DebugFunc p_func);
  676. static int get_object_count();
  677. };
  678. #endif // OBJECT_H