object.h 45 KB

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