2
0

object.h 47 KB

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