object.h 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  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 "list.h"
  33. #include "map.h"
  34. #include "set.h"
  35. #include "variant.h"
  36. #include "vmap.h"
  37. #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()
  38. #define VARIANT_ARG_PASS p_arg1, p_arg2, p_arg3, p_arg4, p_arg5
  39. #define VARIANT_ARG_DECLARE const Variant &p_arg1, const Variant &p_arg2, const Variant &p_arg3, const Variant &p_arg4, const Variant &p_arg5
  40. #define VARIANT_ARG_MAX 5
  41. #define VARIANT_ARGPTRS const Variant *argptr[5] = { &p_arg1, &p_arg2, &p_arg3, &p_arg4, &p_arg5 };
  42. #define VARIANT_ARGPTRS_PASS *argptr[0], *argptr[1], *argptr[2], *argptr[3], *argptr[4]
  43. #define VARIANT_ARGS_FROM_ARRAY(m_arr) m_arr[0], m_arr[1], m_arr[2], m_arr[3], m_arr[4]
  44. /**
  45. @author Juan Linietsky <reduzio@gmail.com>
  46. */
  47. enum PropertyHint {
  48. PROPERTY_HINT_NONE, ///< no hint provided.
  49. PROPERTY_HINT_RANGE, ///< hint_text = "min,max,step,slider; //slider is optional"
  50. PROPERTY_HINT_EXP_RANGE, ///< hint_text = "min,max,step", exponential edit
  51. PROPERTY_HINT_ENUM, ///< hint_text= "val1,val2,val3,etc"
  52. PROPERTY_HINT_EXP_EASING, /// exponential easing funciton (Math::ease)
  53. PROPERTY_HINT_LENGTH, ///< hint_text= "length" (as integer)
  54. PROPERTY_HINT_SPRITE_FRAME,
  55. PROPERTY_HINT_KEY_ACCEL, ///< hint_text= "length" (as integer)
  56. PROPERTY_HINT_FLAGS, ///< hint_text= "flag1,flag2,etc" (as bit flags)
  57. PROPERTY_HINT_ALL_FLAGS,
  58. PROPERTY_HINT_FILE, ///< a file path must be passed, hint_text (optionally) is a filter "*.png,*.wav,*.doc,"
  59. PROPERTY_HINT_DIR, ///< a directort path must be passed
  60. PROPERTY_HINT_GLOBAL_FILE, ///< a file path must be passed, hint_text (optionally) is a filter "*.png,*.wav,*.doc,"
  61. PROPERTY_HINT_GLOBAL_DIR, ///< a directort path must be passed
  62. PROPERTY_HINT_RESOURCE_TYPE, ///< a resource object type
  63. PROPERTY_HINT_MULTILINE_TEXT, ///< used for string properties that can contain multiple lines
  64. PROPERTY_HINT_COLOR_NO_ALPHA, ///< used for ignoring alpha component when editing a color
  65. PROPERTY_HINT_IMAGE_COMPRESS_LOSSY,
  66. PROPERTY_HINT_IMAGE_COMPRESS_LOSSLESS,
  67. PROPERTY_HINT_OBJECT_ID,
  68. PROPERTY_HINT_MAX,
  69. };
  70. enum PropertyUsageFlags {
  71. PROPERTY_USAGE_STORAGE = 1,
  72. PROPERTY_USAGE_EDITOR = 2,
  73. PROPERTY_USAGE_NETWORK = 4,
  74. PROPERTY_USAGE_EDITOR_HELPER = 8,
  75. PROPERTY_USAGE_CHECKABLE = 16, //used for editing global variables
  76. PROPERTY_USAGE_CHECKED = 32, //used for editing global variables
  77. PROPERTY_USAGE_INTERNATIONALIZED = 64, //hint for internationalized strings
  78. PROPERTY_USAGE_BUNDLE = 128, //used for optimized bundles
  79. PROPERTY_USAGE_CATEGORY = 256,
  80. PROPERTY_USAGE_STORE_IF_NONZERO = 512, //only store if nonzero
  81. PROPERTY_USAGE_STORE_IF_NONONE = 1024, //only store if false
  82. PROPERTY_USAGE_NO_INSTANCE_STATE = 2048,
  83. PROPERTY_USAGE_RESTART_IF_CHANGED = 4096,
  84. PROPERTY_USAGE_SCRIPT_VARIABLE = 8192,
  85. PROPERTY_USAGE_STORE_IF_NULL = 16384,
  86. PROPERTY_USAGE_ANIMATE_AS_TRIGGER = 32768,
  87. PROPERTY_USAGE_SCRIPT_DEFAULT_VALUE = 1 << 17,
  88. PROPERTY_USAGE_DEFAULT = PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_NETWORK,
  89. PROPERTY_USAGE_DEFAULT_INTL = PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_NETWORK | PROPERTY_USAGE_INTERNATIONALIZED,
  90. PROPERTY_USAGE_NOEDITOR = PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_NETWORK,
  91. };
  92. #define ADD_SIGNAL(m_signal) ObjectTypeDB::add_signal(get_type_static(), m_signal)
  93. #define ADD_PROPERTY(m_property, m_setter, m_getter) ObjectTypeDB::add_property(get_type_static(), m_property, m_setter, m_getter)
  94. #define ADD_PROPERTYI(m_property, m_setter, m_getter, m_index) ObjectTypeDB::add_property(get_type_static(), m_property, m_setter, m_getter, m_index)
  95. #define ADD_PROPERTYNZ(m_property, m_setter, m_getter) ObjectTypeDB::add_property(get_type_static(), (m_property).added_usage(PROPERTY_USAGE_STORE_IF_NONZERO), m_setter, m_getter)
  96. #define ADD_PROPERTYINZ(m_property, m_setter, m_getter, m_index) ObjectTypeDB::add_property(get_type_static(), (m_property).added_usage(PROPERTY_USAGE_STORE_IF_NONZERO), m_setter, m_getter, m_index)
  97. #define ADD_PROPERTYNO(m_property, m_setter, m_getter) ObjectTypeDB::add_property(get_type_static(), (m_property).added_usage(PROPERTY_USAGE_STORE_IF_NONONE), m_setter, m_getter)
  98. #define ADD_PROPERTYINO(m_property, m_setter, m_getter, m_index) ObjectTypeDB::add_property(get_type_static(), (m_property).added_usage(PROPERTY_USAGE_STORE_IF_NONONE), m_setter, m_getter, m_index)
  99. struct PropertyInfo {
  100. Variant::Type type;
  101. String name;
  102. PropertyHint hint;
  103. String hint_string;
  104. uint32_t usage;
  105. _FORCE_INLINE_ PropertyInfo added_usage(int p_fl) const {
  106. PropertyInfo pi = *this;
  107. pi.usage |= p_fl;
  108. return pi;
  109. }
  110. PropertyInfo() {
  111. type = Variant::NIL;
  112. hint = PROPERTY_HINT_NONE;
  113. usage = PROPERTY_USAGE_DEFAULT;
  114. }
  115. 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) {
  116. type = p_type;
  117. name = p_name;
  118. hint = p_hint;
  119. hint_string = p_hint_string;
  120. usage = p_usage;
  121. }
  122. bool operator<(const PropertyInfo &p_info) const {
  123. return name < p_info.name;
  124. }
  125. };
  126. Array convert_property_list(const List<PropertyInfo> *p_list);
  127. struct MethodInfo {
  128. String name;
  129. List<PropertyInfo> arguments;
  130. Vector<Variant> default_arguments;
  131. PropertyInfo return_val;
  132. uint32_t flags;
  133. int id;
  134. inline bool operator<(const MethodInfo &p_method) const { return id == p_method.id ? (name < p_method.name) : (id < p_method.id); }
  135. MethodInfo();
  136. MethodInfo(const String &p_name);
  137. MethodInfo(const String &p_name, const PropertyInfo &p_param1);
  138. MethodInfo(const String &p_name, const PropertyInfo &p_param1, const PropertyInfo &p_param2);
  139. MethodInfo(const String &p_name, const PropertyInfo &p_param1, const PropertyInfo &p_param2, const PropertyInfo &p_param3);
  140. MethodInfo(const String &p_name, const PropertyInfo &p_param1, const PropertyInfo &p_param2, const PropertyInfo &p_param3, const PropertyInfo &p_param4);
  141. 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);
  142. MethodInfo(Variant::Type ret);
  143. MethodInfo(Variant::Type ret, const String &p_name);
  144. MethodInfo(Variant::Type ret, const String &p_name, const PropertyInfo &p_param1);
  145. MethodInfo(Variant::Type ret, const String &p_name, const PropertyInfo &p_param1, const PropertyInfo &p_param2);
  146. MethodInfo(Variant::Type ret, const String &p_name, const PropertyInfo &p_param1, const PropertyInfo &p_param2, const PropertyInfo &p_param3);
  147. MethodInfo(Variant::Type ret, const String &p_name, const PropertyInfo &p_param1, const PropertyInfo &p_param2, const PropertyInfo &p_param3, const PropertyInfo &p_param4);
  148. 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);
  149. };
  150. // old cast_to
  151. //if ( is_type(T::get_type_static()) )
  152. //return static_cast<T*>(this);
  153. ////else
  154. //return NULL;
  155. /*
  156. the following is an uncomprehensible 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.
  157. */
  158. #define REVERSE_GET_PROPERTY_LIST \
  159. public: \
  160. _FORCE_INLINE_ bool _is_gpl_reversed() const { return true; }; \
  161. \
  162. private:
  163. #define UNREVERSE_GET_PROPERTY_LIST \
  164. public: \
  165. _FORCE_INLINE_ bool _is_gpl_reversed() const { return false; }; \
  166. \
  167. private:
  168. #define OBJ_TYPE(m_type, m_inherits) \
  169. private: \
  170. void operator=(const m_type &p_rval) {} \
  171. mutable StringName _type_name; \
  172. friend class ObjectTypeDB; \
  173. \
  174. public: \
  175. virtual String get_type() const { \
  176. return String(#m_type); \
  177. } \
  178. virtual const StringName *_get_type_namev() const { \
  179. if (!_type_name) \
  180. _type_name = get_type_static(); \
  181. return &_type_name; \
  182. } \
  183. static _FORCE_INLINE_ void *get_type_ptr_static() { \
  184. static int ptr; \
  185. return &ptr; \
  186. } \
  187. static _FORCE_INLINE_ String get_type_static() { \
  188. return String(#m_type); \
  189. } \
  190. static _FORCE_INLINE_ String get_parent_type_static() { \
  191. return m_inherits::get_type_static(); \
  192. } \
  193. static void get_inheritance_list_static(List<String> *p_inheritance_list) { \
  194. m_inherits::get_inheritance_list_static(p_inheritance_list); \
  195. p_inheritance_list->push_back(String(#m_type)); \
  196. } \
  197. static String get_category_static() { \
  198. String category = m_inherits::get_category_static(); \
  199. if (_get_category != m_inherits::_get_category) { \
  200. if (category != "") \
  201. category += "/"; \
  202. category += _get_category(); \
  203. } \
  204. return category; \
  205. } \
  206. static String inherits_static() { \
  207. return String(#m_inherits); \
  208. } \
  209. virtual bool is_type(const String &p_type) const { return (p_type == (#m_type)) ? true : m_inherits::is_type(p_type); } \
  210. virtual bool is_type_ptr(void *p_ptr) const { return (p_ptr == get_type_ptr_static()) ? true : m_inherits::is_type_ptr(p_ptr); } \
  211. \
  212. static void get_valid_parents_static(List<String> *p_parents) { \
  213. \
  214. if (m_type::_get_valid_parents_static != m_inherits::_get_valid_parents_static) { \
  215. m_type::_get_valid_parents_static(p_parents); \
  216. } \
  217. \
  218. m_inherits::get_valid_parents_static(p_parents); \
  219. } \
  220. \
  221. protected: \
  222. _FORCE_INLINE_ static void (*_get_bind_methods())() { \
  223. return &m_type::_bind_methods; \
  224. } \
  225. \
  226. public: \
  227. static void initialize_type() { \
  228. static bool initialized = false; \
  229. if (initialized) \
  230. return; \
  231. m_inherits::initialize_type(); \
  232. ObjectTypeDB::_add_type<m_type>(); \
  233. if (m_type::_get_bind_methods() != m_inherits::_get_bind_methods()) \
  234. _bind_methods(); \
  235. initialized = true; \
  236. } \
  237. \
  238. protected: \
  239. virtual void _initialize_typev() { \
  240. initialize_type(); \
  241. } \
  242. _FORCE_INLINE_ bool (Object::*(_get_get() const))(const StringName &p_name, Variant &) const { \
  243. return (bool (Object::*)(const StringName &, Variant &) const) & m_type::_get; \
  244. } \
  245. virtual bool _getv(const StringName &p_name, Variant &r_ret) const { \
  246. if (m_type::_get_get() != m_inherits::_get_get()) { \
  247. if (_get(p_name, r_ret)) \
  248. return true; \
  249. } \
  250. return m_inherits::_getv(p_name, r_ret); \
  251. } \
  252. _FORCE_INLINE_ bool (Object::*(_get_set() const))(const StringName &p_name, const Variant &p_property) { \
  253. return (bool (Object::*)(const StringName &, const Variant &)) & m_type::_set; \
  254. } \
  255. virtual bool _setv(const StringName &p_name, const Variant &p_property) { \
  256. if (m_inherits::_setv(p_name, p_property)) return true; \
  257. if (m_type::_get_set() != m_inherits::_get_set()) { \
  258. return _set(p_name, p_property); \
  259. } \
  260. return false; \
  261. } \
  262. _FORCE_INLINE_ void (Object::*(_get_get_property_list() const))(List<PropertyInfo> * p_list) const { \
  263. return (void (Object::*)(List<PropertyInfo> *) const) & m_type::_get_property_list; \
  264. } \
  265. virtual void _get_property_listv(List<PropertyInfo> *p_list, bool p_reversed) const { \
  266. if (!p_reversed) { \
  267. m_inherits::_get_property_listv(p_list, p_reversed); \
  268. } \
  269. p_list->push_back(PropertyInfo(Variant::NIL, get_type_static(), PROPERTY_HINT_NONE, String(), PROPERTY_USAGE_CATEGORY)); \
  270. if (!_is_gpl_reversed()) \
  271. ObjectTypeDB::get_property_list(#m_type, p_list, true, this); \
  272. if (m_type::_get_get_property_list() != m_inherits::_get_get_property_list()) { \
  273. _get_property_list(p_list); \
  274. } \
  275. if (_is_gpl_reversed()) \
  276. ObjectTypeDB::get_property_list(#m_type, p_list, true, this); \
  277. if (p_reversed) { \
  278. m_inherits::_get_property_listv(p_list, p_reversed); \
  279. } \
  280. } \
  281. _FORCE_INLINE_ void (Object::*(_get_notification() const))(int) { \
  282. return (void (Object::*)(int)) & m_type::_notification; \
  283. } \
  284. virtual void _notificationv(int p_notification, bool p_reversed) { \
  285. if (!p_reversed) \
  286. m_inherits::_notificationv(p_notification, p_reversed); \
  287. if (m_type::_get_notification() != m_inherits::_get_notification()) { \
  288. _notification(p_notification); \
  289. } \
  290. if (p_reversed) \
  291. m_inherits::_notificationv(p_notification, p_reversed); \
  292. } \
  293. \
  294. private:
  295. #define OBJ_CATEGORY(m_category) \
  296. protected: \
  297. _FORCE_INLINE_ static String _get_category() { return m_category; } \
  298. \
  299. private:
  300. #define OBJ_SAVE_TYPE(m_type) \
  301. public: \
  302. virtual String get_save_type() const { return #m_type; } \
  303. \
  304. private:
  305. class ScriptInstance;
  306. typedef uint32_t ObjectID;
  307. class Object {
  308. public:
  309. enum ConnectFlags {
  310. CONNECT_DEFERRED = 1,
  311. CONNECT_PERSIST = 2, // hint for scene to save this connection
  312. CONNECT_ONESHOT = 4
  313. };
  314. struct Connection {
  315. Object *source;
  316. StringName signal;
  317. Object *target;
  318. StringName method;
  319. uint32_t flags;
  320. Vector<Variant> binds;
  321. bool operator<(const Connection &p_conn) const;
  322. operator Variant() const;
  323. Connection() {
  324. source = NULL;
  325. target = NULL;
  326. flags = 0;
  327. }
  328. Connection(const Variant &p_variant);
  329. };
  330. private:
  331. #ifdef DEBUG_ENABLED
  332. friend class _ObjectDebugLock;
  333. #endif
  334. friend bool predelete_handler(Object *);
  335. friend void postinitialize_handler(Object *);
  336. struct Signal {
  337. struct Target {
  338. ObjectID _id;
  339. StringName method;
  340. _FORCE_INLINE_ bool operator<(const Target &p_target) const { return (_id == p_target._id) ? (method < p_target.method) : (_id < p_target._id); }
  341. Target(const ObjectID &p_id, const StringName &p_method) {
  342. _id = p_id;
  343. method = p_method;
  344. }
  345. Target() { _id = 0; }
  346. };
  347. struct Slot {
  348. Connection conn;
  349. List<Connection>::Element *cE;
  350. };
  351. MethodInfo user;
  352. VMap<Target, Slot> slot_map;
  353. int lock;
  354. Signal() { lock = 0; }
  355. };
  356. HashMap<StringName, Signal, StringNameHasher> signal_map;
  357. List<Connection> connections;
  358. #ifdef DEBUG_ENABLED
  359. SafeRefCount _lock_index;
  360. #endif
  361. bool _block_signals;
  362. int _predelete_ok;
  363. Set<Object *> change_receptors;
  364. uint32_t _instance_ID;
  365. bool _predelete();
  366. void _postinitialize();
  367. bool _can_translate;
  368. #ifdef TOOLS_ENABLED
  369. bool _edited;
  370. uint32_t _edited_version;
  371. #endif
  372. ScriptInstance *script_instance;
  373. RefPtr script;
  374. Dictionary metadata;
  375. mutable StringName _type_name;
  376. mutable const StringName *_type_ptr;
  377. void _add_user_signal(const String &p_name, const Array &p_pargs = Array());
  378. bool _has_user_signal(const StringName &p_name) const;
  379. Variant _emit_signal(const Variant **p_args, int p_argcount, Variant::CallError &r_error);
  380. Array _get_signal_list() const;
  381. Array _get_signal_connection_list(const String &p_signal) const;
  382. void _set_bind(const String &p_set, const Variant &p_value);
  383. Variant _get_bind(const String &p_name) const;
  384. void property_list_changed_notify();
  385. protected:
  386. virtual bool _use_builtin_script() const { return false; }
  387. virtual void _initialize_typev() { initialize_type(); }
  388. virtual bool _setv(const StringName &p_name, const Variant &p_property) { return false; };
  389. virtual bool _getv(const StringName &p_name, Variant &r_property) const { return false; };
  390. virtual void _get_property_listv(List<PropertyInfo> *p_list, bool p_reversed) const {};
  391. virtual void _notificationv(int p_notification, bool p_reversed){};
  392. static String _get_category() { return ""; }
  393. static void _bind_methods();
  394. bool _set(const StringName &p_name, const Variant &p_property) { return false; };
  395. bool _get(const StringName &p_name, Variant &r_property) const { return false; };
  396. void _get_property_list(List<PropertyInfo> *p_list) const {};
  397. void _notification(int p_notification){};
  398. _FORCE_INLINE_ static void (*_get_bind_methods())() {
  399. return &Object::_bind_methods;
  400. }
  401. _FORCE_INLINE_ bool (Object::*(_get_get() const))(const StringName &p_name, Variant &r_ret) const {
  402. return &Object::_get;
  403. }
  404. _FORCE_INLINE_ bool (Object::*(_get_set() const))(const StringName &p_name, const Variant &p_property) {
  405. return &Object::_set;
  406. }
  407. _FORCE_INLINE_ void (Object::*(_get_get_property_list() const))(List<PropertyInfo> *p_list) const {
  408. return &Object::_get_property_list;
  409. }
  410. _FORCE_INLINE_ void (Object::*(_get_notification() const))(int) {
  411. return &Object::_notification;
  412. }
  413. static void get_valid_parents_static(List<String> *p_parents);
  414. static void _get_valid_parents_static(List<String> *p_parents);
  415. void cancel_delete();
  416. virtual void _changed_callback(Object *p_changed, const char *p_prop);
  417. //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());
  418. //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());
  419. Variant _call_bind(const Variant **p_args, int p_argcount, Variant::CallError &r_error);
  420. Variant _call_deferred_bind(const Variant **p_args, int p_argcount, Variant::CallError &r_error);
  421. virtual const StringName *_get_type_namev() const {
  422. if (!_type_name)
  423. _type_name = get_type_static();
  424. return &_type_name;
  425. }
  426. DVector<String> _get_meta_list_bind() const;
  427. Array _get_property_list_bind() const;
  428. Array _get_method_list_bind() const;
  429. void _clear_internal_resource_paths(const Variant &p_var);
  430. friend class ObjectTypeDB;
  431. virtual void _validate_property(PropertyInfo &property) const;
  432. public: //should be protected, but bug in clang++
  433. static void initialize_type();
  434. _FORCE_INLINE_ static void register_custom_data_to_otdb(){};
  435. public:
  436. #ifdef TOOLS_ENABLED
  437. _FORCE_INLINE_ void _change_notify(const char *p_property = "") {
  438. _edited = true;
  439. for (Set<Object *>::Element *E = change_receptors.front(); E; E = E->next())
  440. ((Object *)(E->get()))->_changed_callback(this, p_property);
  441. }
  442. #else
  443. _FORCE_INLINE_ void _change_notify(const char *p_what = "") {}
  444. #endif
  445. static void *get_type_ptr_static() {
  446. static int ptr;
  447. return &ptr;
  448. }
  449. bool _is_gpl_reversed() const { return false; }
  450. _FORCE_INLINE_ ObjectID get_instance_ID() const { return _instance_ID; }
  451. // this is used for editors
  452. void add_change_receptor(Object *p_receptor);
  453. void remove_change_receptor(Object *p_receptor);
  454. // TODO: ensure 'this' is never NULL since it's UB, but by now, avoid warning flood
  455. #ifdef __clang__
  456. #pragma clang diagnostic push
  457. #pragma clang diagnostic ignored "-Wundefined-bool-conversion"
  458. #endif
  459. template <class T>
  460. T *cast_to() {
  461. #ifndef NO_SAFE_CAST
  462. return SAFE_CAST<T *>(this);
  463. #else
  464. if (!this)
  465. return NULL;
  466. if (is_type_ptr(T::get_type_ptr_static()))
  467. return static_cast<T *>(this);
  468. else
  469. return NULL;
  470. #endif
  471. }
  472. template <class T>
  473. const T *cast_to() const {
  474. #ifndef NO_SAFE_CAST
  475. return SAFE_CAST<const T *>(this);
  476. #else
  477. if (!this)
  478. return NULL;
  479. if (is_type_ptr(T::get_type_ptr_static()))
  480. return static_cast<const T *>(this);
  481. else
  482. return NULL;
  483. #endif
  484. }
  485. #ifdef __clang__
  486. #pragma clang diagnostic pop
  487. #endif
  488. enum {
  489. NOTIFICATION_POSTINITIALIZE = 0,
  490. NOTIFICATION_PREDELETE = 1
  491. };
  492. /* TYPE API */
  493. static void get_inheritance_list_static(List<String> *p_inheritance_list) { p_inheritance_list->push_back("Object"); }
  494. static String get_type_static() { return "Object"; }
  495. static String get_parent_type_static() { return String(); }
  496. static String get_category_static() { return String(); }
  497. virtual String get_type() const { return "Object"; }
  498. virtual String get_save_type() const { return get_type(); } //type stored when saving
  499. virtual bool is_type(const String &p_type) const { return (p_type == "Object"); }
  500. virtual bool is_type_ptr(void *p_ptr) const { return get_type_ptr_static() == p_ptr; }
  501. _FORCE_INLINE_ const StringName &get_type_name() const {
  502. if (!_type_ptr) {
  503. return *_get_type_namev();
  504. } else {
  505. return *_type_ptr;
  506. }
  507. }
  508. /* IAPI */
  509. // void set(const String& p_name, const Variant& p_value);
  510. // Variant get(const String& p_name) const;
  511. void set(const StringName &p_name, const Variant &p_value, bool *r_valid = NULL);
  512. Variant get(const StringName &p_name, bool *r_valid = NULL) const;
  513. void get_property_list(List<PropertyInfo> *p_list, bool p_reversed = false) const;
  514. bool has_method(const StringName &p_method) const;
  515. void get_method_list(List<MethodInfo> *p_list) const;
  516. Variant callv(const StringName &p_method, const Array &p_args);
  517. virtual Variant call(const StringName &p_method, const Variant **p_args, int p_argcount, Variant::CallError &r_error);
  518. virtual void call_multilevel(const StringName &p_method, const Variant **p_args, int p_argcount);
  519. virtual void call_multilevel_reversed(const StringName &p_method, const Variant **p_args, int p_argcount);
  520. Variant call(const StringName &p_name, VARIANT_ARG_LIST); // C++ helper
  521. void call_multilevel(const StringName &p_name, VARIANT_ARG_LIST); // C++ helper
  522. void notification(int p_notification, bool p_reversed = false);
  523. //used mainly by script, get and set all INCLUDING string
  524. virtual Variant getvar(const Variant &p_key, bool *r_valid = NULL) const;
  525. virtual void setvar(const Variant &p_key, const Variant &p_value, bool *r_valid = NULL);
  526. /* SCRIPT */
  527. void set_script(const RefPtr &p_script);
  528. RefPtr get_script() const;
  529. /* SCRIPT */
  530. bool has_meta(const String &p_name) const;
  531. void set_meta(const String &p_name, const Variant &p_value);
  532. Variant get_meta(const String &p_name) const;
  533. void get_meta_list(List<String> *p_list) const;
  534. #ifdef TOOLS_ENABLED
  535. void set_edited(bool p_edited);
  536. bool is_edited() const;
  537. 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
  538. #endif
  539. void set_script_instance(ScriptInstance *p_instance);
  540. _FORCE_INLINE_ ScriptInstance *get_script_instance() const { return script_instance; }
  541. void add_user_signal(const MethodInfo &p_signal);
  542. void emit_signal(const StringName &p_name, VARIANT_ARG_LIST);
  543. void emit_signal(const StringName &p_name, const Variant **p_args, int p_argcount);
  544. void get_signal_list(List<MethodInfo> *p_signals) const;
  545. void get_signal_connection_list(const StringName &p_signal, List<Connection> *p_connections) const;
  546. void get_all_signal_connections(List<Connection> *p_connections) const;
  547. bool has_persistent_signal_connections() const;
  548. void get_signals_connected_to_this(List<Connection> *p_connections) const;
  549. Error connect(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);
  550. void disconnect(const StringName &p_signal, Object *p_to_object, const StringName &p_to_method);
  551. bool is_connected(const StringName &p_signal, Object *p_to_object, const StringName &p_to_method) const;
  552. void call_deferred(const StringName &p_method, VARIANT_ARG_LIST);
  553. void set_block_signals(bool p_block);
  554. bool is_blocking_signals() const;
  555. Variant::Type get_static_property_type(const StringName &p_property, bool *r_valid = NULL) const;
  556. virtual void get_translatable_strings(List<String> *p_strings) const;
  557. virtual void get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const;
  558. StringName XL_MESSAGE(const StringName &p_message) const; //translate message (internationalization)
  559. StringName tr(const StringName &p_message) const; //translate message (alternative)
  560. bool _is_queued_for_deletion; // set to true by SceneTree::queue_delete()
  561. bool is_queued_for_deletion() const;
  562. _FORCE_INLINE_ void set_message_translation(bool p_enable) { _can_translate = p_enable; }
  563. _FORCE_INLINE_ bool can_translate_messages() const { return _can_translate; }
  564. void clear_internal_resource_paths();
  565. Object();
  566. virtual ~Object();
  567. };
  568. bool predelete_handler(Object *p_object);
  569. void postinitialize_handler(Object *p_object);
  570. class ObjectDB {
  571. struct ObjectPtrHash {
  572. static _FORCE_INLINE_ uint32_t hash(const Object *p_obj) {
  573. union {
  574. const Object *p;
  575. unsigned long i;
  576. } u;
  577. u.p = p_obj;
  578. return HashMapHasherDefault::hash((uint64_t)u.i);
  579. }
  580. };
  581. static HashMap<uint32_t, Object *> instances;
  582. static HashMap<Object *, ObjectID, ObjectPtrHash> instance_checks;
  583. static uint32_t instance_counter;
  584. friend class Object;
  585. friend void unregister_core_types();
  586. static void cleanup();
  587. static uint32_t add_instance(Object *p_object);
  588. static void remove_instance(Object *p_object);
  589. public:
  590. typedef void (*DebugFunc)(Object *p_obj);
  591. static Object *get_instance(uint32_t p_instance_ID);
  592. static void debug_objects(DebugFunc p_func);
  593. static int get_object_count();
  594. #ifdef DEBUG_ENABLED
  595. _FORCE_INLINE_ static bool instance_validate(Object *p_ptr) {
  596. return instance_checks.has(p_ptr);
  597. }
  598. #else
  599. _FORCE_INLINE_ static bool instance_validate(Object *p_ptr) { return true; }
  600. #endif
  601. };
  602. //needed by macros
  603. #include "object_type_db.h"
  604. #endif