object.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. /*************************************************************************/
  2. /* object.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2015 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #ifndef OBJECT_H
  30. #define OBJECT_H
  31. #include "list.h"
  32. #include "variant.h"
  33. #include "set.h"
  34. #include "map.h"
  35. #include "vmap.h"
  36. #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()
  37. #define VARIANT_ARG_PASS p_arg1,p_arg2,p_arg3,p_arg4,p_arg5
  38. #define VARIANT_ARG_DECLARE const Variant& p_arg1,const Variant& p_arg2,const Variant& p_arg3,const Variant& p_arg4,const Variant& p_arg5
  39. #define VARIANT_ARG_MAX 5
  40. #define VARIANT_ARGPTRS const Variant *argptr[5]={&p_arg1,&p_arg2,&p_arg3,&p_arg4,&p_arg5};
  41. #define VARIANT_ARGPTRS_PASS *argptr[0],*argptr[1],*argptr[2],*argptr[3],*argptr[4]
  42. #define VARIANT_ARGS_FROM_ARRAY(m_arr) m_arr[0],m_arr[1],m_arr[2],m_arr[3],m_arr[4]
  43. /**
  44. @author Juan Linietsky <reduzio@gmail.com>
  45. */
  46. enum PropertyHint {
  47. PROPERTY_HINT_NONE, ///< no hint provided.
  48. PROPERTY_HINT_RANGE, ///< hint_text = "min,max,step"
  49. PROPERTY_HINT_EXP_RANGE, ///< hint_text = "min,max,step", exponential edit
  50. PROPERTY_HINT_ENUM, ///< hint_text= "val1,val2,val3,etc"
  51. PROPERTY_HINT_EXP_EASING, /// exponential easing funciton (Math::ease)
  52. PROPERTY_HINT_LENGTH, ///< hint_text= "length" (as integer)
  53. PROPERTY_HINT_KEY_ACCEL, ///< hint_text= "length" (as integer)
  54. PROPERTY_HINT_FLAGS, ///< hint_text= "flag1,flag2,etc" (as bit flags)
  55. PROPERTY_HINT_ALL_FLAGS,
  56. PROPERTY_HINT_FILE, ///< a file path must be passed, hint_text (optionally) is a filter "*.png,*.wav,*.doc,"
  57. PROPERTY_HINT_DIR, ///< a directort path must be passed
  58. PROPERTY_HINT_GLOBAL_FILE, ///< a file path must be passed, hint_text (optionally) is a filter "*.png,*.wav,*.doc,"
  59. PROPERTY_HINT_GLOBAL_DIR, ///< a directort path must be passed
  60. PROPERTY_HINT_RESOURCE_TYPE, ///< a resource object type
  61. PROPERTY_HINT_MULTILINE_TEXT, ///< used for string properties that can contain multiple lines
  62. PROPERTY_HINT_COLOR_NO_ALPHA, ///< used for ignoring alpha component when editing a color
  63. PROPERTY_HINT_IMAGE_COMPRESS_LOSSY,
  64. PROPERTY_HINT_IMAGE_COMPRESS_LOSSLESS,
  65. PROPERTY_HINT_MAX,
  66. };
  67. enum PropertyUsageFlags {
  68. PROPERTY_USAGE_STORAGE=1,
  69. PROPERTY_USAGE_EDITOR=2,
  70. PROPERTY_USAGE_NETWORK=4,
  71. PROPERTY_USAGE_EDITOR_HELPER=8,
  72. PROPERTY_USAGE_CHECKABLE=16, //used for editing global variables
  73. PROPERTY_USAGE_CHECKED=32, //used for editing global variables
  74. PROPERTY_USAGE_INTERNATIONALIZED=64, //hint for internationalized strings
  75. PROPERTY_USAGE_BUNDLE=128, //used for optimized bundles
  76. PROPERTY_USAGE_CATEGORY=256,
  77. PROPERTY_USAGE_STORE_IF_NONZERO=512, //only store if nonzero
  78. PROPERTY_USAGE_NO_INSTANCE_STATE=1024,
  79. PROPERTY_USAGE_DEFAULT=PROPERTY_USAGE_STORAGE|PROPERTY_USAGE_EDITOR|PROPERTY_USAGE_NETWORK,
  80. PROPERTY_USAGE_DEFAULT_INTL=PROPERTY_USAGE_STORAGE|PROPERTY_USAGE_EDITOR|PROPERTY_USAGE_NETWORK|PROPERTY_USAGE_INTERNATIONALIZED,
  81. PROPERTY_USAGE_NOEDITOR=PROPERTY_USAGE_STORAGE|PROPERTY_USAGE_NETWORK,
  82. };
  83. #define ADD_SIGNAL( m_signal ) ObjectTypeDB::add_signal( get_type_static(), m_signal )
  84. #define ADD_PROPERTY( m_property, m_setter, m_getter ) ObjectTypeDB::add_property( get_type_static(), m_property, m_setter, m_getter )
  85. #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 )
  86. #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 )
  87. #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 )
  88. struct PropertyInfo {
  89. Variant::Type type;
  90. String name;
  91. PropertyHint hint;
  92. String hint_string;
  93. uint32_t usage;
  94. _FORCE_INLINE_ PropertyInfo added_usage(int p_fl) const { PropertyInfo pi=*this; pi.usage|=p_fl; return pi; }
  95. PropertyInfo() { type=Variant::NIL; hint=PROPERTY_HINT_NONE; usage = PROPERTY_USAGE_DEFAULT; }
  96. 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) {
  97. type=p_type; name=p_name; hint=p_hint; hint_string=p_hint_string; usage=p_usage;
  98. }
  99. bool operator<(const PropertyInfo& p_info) const {
  100. return name<p_info.name;
  101. }
  102. };
  103. Array convert_property_list(const List<PropertyInfo> * p_list);
  104. struct MethodInfo {
  105. String name;
  106. List<PropertyInfo> arguments;
  107. Vector<Variant> default_arguments;
  108. PropertyInfo return_val;
  109. uint32_t flags;
  110. int id;
  111. inline bool operator<(const MethodInfo& p_method) const { return id==p_method.id?(name < p_method.name):(id<p_method.id); }
  112. MethodInfo();
  113. MethodInfo(const String& p_name);
  114. MethodInfo(const String& p_name, const PropertyInfo& p_param1);
  115. MethodInfo(const String& p_name, const PropertyInfo& p_param1,const PropertyInfo& p_param2);
  116. MethodInfo(const String& p_name, const PropertyInfo& p_param1,const PropertyInfo& p_param2,const PropertyInfo& p_param3);
  117. MethodInfo(const String& p_name, const PropertyInfo& p_param1,const PropertyInfo& p_param2,const PropertyInfo& p_param3,const PropertyInfo& p_param4);
  118. 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);
  119. MethodInfo(Variant::Type ret);
  120. MethodInfo(Variant::Type ret,const String& p_name);
  121. MethodInfo(Variant::Type ret,const String& p_name, const PropertyInfo& p_param1);
  122. MethodInfo(Variant::Type ret,const String& p_name, const PropertyInfo& p_param1,const PropertyInfo& p_param2);
  123. MethodInfo(Variant::Type ret,const String& p_name, const PropertyInfo& p_param1,const PropertyInfo& p_param2,const PropertyInfo& p_param3);
  124. MethodInfo(Variant::Type ret,const String& p_name, const PropertyInfo& p_param1,const PropertyInfo& p_param2,const PropertyInfo& p_param3,const PropertyInfo& p_param4);
  125. 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);
  126. };
  127. // old cast_to
  128. //if ( is_type(T::get_type_static()) )
  129. //return static_cast<T*>(this);
  130. ////else
  131. //return NULL;
  132. /*
  133. 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.
  134. */
  135. #define REVERSE_GET_PROPERTY_LIST \
  136. public:\
  137. _FORCE_INLINE_ bool _is_gpl_reversed() const { return true; };\
  138. private:
  139. #define UNREVERSE_GET_PROPERTY_LIST \
  140. public:\
  141. _FORCE_INLINE_ bool _is_gpl_reversed() const { return false; };\
  142. private:
  143. #define OBJ_TYPE( m_type, m_inherits )\
  144. private:\
  145. void operator=(const m_type& p_rval) {}\
  146. mutable StringName _type_name;\
  147. friend class ObjectTypeDB;\
  148. public:\
  149. virtual String get_type() const { \
  150. return String(#m_type);\
  151. }\
  152. virtual StringName get_type_name() const { \
  153. if (!_type_name)\
  154. _type_name=get_type_static();\
  155. return _type_name;\
  156. }\
  157. static _FORCE_INLINE_ void* get_type_ptr_static() { \
  158. static int ptr;\
  159. return &ptr;\
  160. }\
  161. static _FORCE_INLINE_ String get_type_static() { \
  162. return String(#m_type);\
  163. }\
  164. static _FORCE_INLINE_ String get_parent_type_static() { \
  165. return m_inherits::get_type_static();\
  166. }\
  167. static void get_inheritance_list_static(List<String>* p_inheritance_list) { \
  168. m_inherits::get_inheritance_list_static(p_inheritance_list);\
  169. p_inheritance_list->push_back(String(#m_type));\
  170. }\
  171. static String get_category_static() { \
  172. String category = m_inherits::get_category_static();\
  173. if (_get_category!=m_inherits::_get_category) {\
  174. if (category!="")\
  175. category+="/";\
  176. category+=_get_category();\
  177. }\
  178. return category;\
  179. }\
  180. static String inherits_static() {\
  181. return String(#m_inherits);\
  182. }\
  183. virtual bool is_type(const String& p_type) const { return (p_type==(#m_type))?true:m_inherits::is_type(p_type); }\
  184. virtual bool is_type_ptr(void *p_ptr) const { return (p_ptr==get_type_ptr_static())?true:m_inherits::is_type_ptr(p_ptr); }\
  185. \
  186. \
  187. static void get_valid_parents_static(List<String> *p_parents) {\
  188. \
  189. if (m_type::_get_valid_parents_static!=m_inherits::_get_valid_parents_static) { \
  190. m_type::_get_valid_parents_static(p_parents);\
  191. }\
  192. \
  193. m_inherits::get_valid_parents_static(p_parents);\
  194. }\
  195. protected:\
  196. _FORCE_INLINE_ static void (*_get_bind_methods())() {\
  197. return &m_type::_bind_methods;\
  198. }\
  199. public:\
  200. static void initialize_type() {\
  201. static bool initialized=false;\
  202. if (initialized)\
  203. return;\
  204. m_inherits::initialize_type();\
  205. ObjectTypeDB::_add_type<m_type>();\
  206. if (m_type::_get_bind_methods() != m_inherits::_get_bind_methods())\
  207. _bind_methods();\
  208. initialized=true;\
  209. }\
  210. protected:\
  211. virtual void _initialize_typev() {\
  212. initialize_type();\
  213. }\
  214. _FORCE_INLINE_ bool (Object::* (_get_get() const))(const StringName& p_name,Variant&) const {\
  215. return (bool (Object::*)(const StringName&,Variant&)const) &m_type::_get;\
  216. }\
  217. virtual bool _getv(const StringName& p_name, Variant& r_ret) const { \
  218. if (m_type::_get_get() != m_inherits::_get_get()) {\
  219. if (_get(p_name,r_ret))\
  220. return true;\
  221. }\
  222. return m_inherits::_getv(p_name,r_ret);\
  223. }\
  224. _FORCE_INLINE_ bool (Object::* (_get_set() const))(const StringName& p_name,const Variant &p_property) {\
  225. return (bool (Object::*)(const StringName&, const Variant&))&m_type::_set;\
  226. }\
  227. virtual bool _setv(const StringName& p_name,const Variant &p_property) { \
  228. if (m_inherits::_setv(p_name,p_property)) return true;\
  229. if (m_type::_get_set() != m_inherits::_get_set()) {\
  230. return _set(p_name,p_property);\
  231. \
  232. }\
  233. return false;\
  234. }\
  235. _FORCE_INLINE_ void (Object::* (_get_get_property_list() const))(List<PropertyInfo> *p_list) const{\
  236. return (void (Object::*)(List<PropertyInfo>*)const)&m_type::_get_property_list;\
  237. }\
  238. virtual void _get_property_listv(List<PropertyInfo> *p_list,bool p_reversed) const { \
  239. if (!p_reversed) {\
  240. m_inherits::_get_property_listv(p_list,p_reversed);\
  241. }\
  242. p_list->push_back( PropertyInfo(Variant::NIL,get_type_static(),PROPERTY_HINT_NONE,String(),PROPERTY_USAGE_CATEGORY));\
  243. if (!_is_gpl_reversed())\
  244. ObjectTypeDB::get_property_list(#m_type,p_list,true);\
  245. if (m_type::_get_get_property_list() != m_inherits::_get_get_property_list()) {\
  246. _get_property_list(p_list);\
  247. }\
  248. if (_is_gpl_reversed())\
  249. ObjectTypeDB::get_property_list(#m_type,p_list,true);\
  250. if (p_reversed) {\
  251. m_inherits::_get_property_listv(p_list,p_reversed);\
  252. }\
  253. \
  254. }\
  255. _FORCE_INLINE_ void (Object::* (_get_notification() const))(int){\
  256. return (void (Object::*)(int)) &m_type::_notification;\
  257. }\
  258. virtual void _notificationv(int p_notification,bool p_reversed) { \
  259. if (!p_reversed) \
  260. m_inherits::_notificationv(p_notification,p_reversed);\
  261. if (m_type::_get_notification() != m_inherits::_get_notification()) {\
  262. _notification(p_notification);\
  263. }\
  264. if (p_reversed)\
  265. m_inherits::_notificationv(p_notification,p_reversed);\
  266. }\
  267. \
  268. private:
  269. #define OBJ_CATEGORY(m_category)\
  270. protected:\
  271. _FORCE_INLINE_ static String _get_category() { return m_category; }\
  272. private:
  273. #define OBJ_SAVE_TYPE(m_type) \
  274. public: \
  275. virtual String get_save_type() const { return #m_type; }\
  276. private:
  277. class ScriptInstance;
  278. typedef uint32_t ObjectID;
  279. class Object {
  280. public:
  281. enum ConnectFlags {
  282. CONNECT_DEFERRED=1,
  283. CONNECT_PERSIST=2, // hint for scene to save this connection
  284. CONNECT_ONESHOT=4
  285. };
  286. struct Connection {
  287. Object *source;
  288. StringName signal;
  289. Object *target;
  290. StringName method;
  291. uint32_t flags;
  292. Vector<Variant> binds;
  293. bool operator<(const Connection& p_conn) const;
  294. operator Variant() const;
  295. Connection() { source=NULL; target=NULL; flags=0; }
  296. Connection(const Variant& p_variant);
  297. };
  298. private:
  299. #ifdef DEBUG_ENABLED
  300. friend class _ObjectDebugLock;
  301. #endif
  302. friend bool predelete_handler(Object*);
  303. friend void postinitialize_handler(Object*);
  304. struct Signal {
  305. struct Target {
  306. ObjectID _id;
  307. StringName method;
  308. _FORCE_INLINE_ bool operator<(const Target& p_target) const { return (_id==p_target._id)?(method<p_target.method):(_id<p_target._id); }
  309. Target(const ObjectID& p_id, const StringName& p_method) { _id=p_id; method=p_method; }
  310. Target() { _id=0; }
  311. };
  312. struct Slot {
  313. Connection conn;
  314. List<Connection>::Element *cE;
  315. };
  316. MethodInfo user;
  317. VMap<Target,Slot> slot_map;
  318. int lock;
  319. Signal() { lock=0; }
  320. };
  321. HashMap< StringName, Signal, StringNameHasher> signal_map;
  322. List<Connection> connections;
  323. #ifdef DEBUG_ENABLED
  324. SafeRefCount _lock_index;
  325. #endif
  326. bool _block_signals;
  327. int _predelete_ok;
  328. Set<Object*> change_receptors;
  329. uint32_t _instance_ID;
  330. bool _predelete();
  331. void _postinitialize();
  332. bool _can_translate;
  333. #ifdef TOOLS_ENABLED
  334. bool _edited;
  335. #endif
  336. ScriptInstance *script_instance;
  337. RefPtr script;
  338. Dictionary metadata;
  339. void _add_user_signal(const String& p_name, const Array& p_pargs=Array());
  340. bool _has_user_signal(const StringName& p_name) const;
  341. Variant _emit_signal(const Variant** p_args, int p_argcount, Variant::CallError& r_error);
  342. Array _get_signal_list() const;
  343. Array _get_signal_connection_list(const String& p_signal) const;
  344. void _set_bind(const String& p_set,const Variant& p_value);
  345. Variant _get_bind(const String& p_name) const;
  346. void property_list_changed_notify();
  347. protected:
  348. virtual bool _use_builtin_script() const { return false; }
  349. virtual void _initialize_typev() { initialize_type(); }
  350. virtual bool _setv(const StringName& p_name,const Variant &p_property) { return false; };
  351. virtual bool _getv(const StringName& p_name,Variant &r_property) const { return false; };
  352. virtual void _get_property_listv(List<PropertyInfo> *p_list,bool p_reversed) const {};
  353. virtual void _notificationv(int p_notification,bool p_reversed) {};
  354. static String _get_category() { return ""; }
  355. static void _bind_methods();
  356. bool _set(const StringName& p_name,const Variant &p_property) { return false; };
  357. bool _get(const StringName& p_name,Variant &r_property) const { return false; };
  358. void _get_property_list(List<PropertyInfo> *p_list) const {};
  359. void _notification(int p_notification) {};
  360. _FORCE_INLINE_ static void (*_get_bind_methods())() {
  361. return &Object::_bind_methods;
  362. }
  363. _FORCE_INLINE_ bool (Object::* (_get_get() const))(const StringName& p_name,Variant &r_ret) const {
  364. return &Object::_get;
  365. }
  366. _FORCE_INLINE_ bool (Object::* (_get_set() const))(const StringName& p_name,const Variant &p_property) {
  367. return &Object::_set;
  368. }
  369. _FORCE_INLINE_ void (Object::* (_get_get_property_list() const))(List<PropertyInfo> *p_list) const{
  370. return &Object::_get_property_list;
  371. }
  372. _FORCE_INLINE_ void (Object::* (_get_notification() const))(int){
  373. return &Object::_notification;
  374. }
  375. static void get_valid_parents_static(List<String> *p_parents);
  376. static void _get_valid_parents_static(List<String> *p_parents);
  377. void cancel_delete();
  378. virtual void _changed_callback(Object *p_changed,const char *p_prop);
  379. //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());
  380. //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());
  381. Variant _call_bind(const Variant** p_args, int p_argcount, Variant::CallError& r_error);
  382. Variant _call_deferred_bind(const Variant** p_args, int p_argcount, Variant::CallError& r_error);
  383. DVector<String> _get_meta_list_bind() const;
  384. Array _get_property_list_bind() const;
  385. public: //should be protected, but bug in clang++
  386. static void initialize_type();
  387. _FORCE_INLINE_ static void register_custom_data_to_otdb() {};
  388. public:
  389. #ifdef TOOLS_ENABLED
  390. _FORCE_INLINE_ void _change_notify(const char *p_property="") { _edited=true; for(Set<Object*>::Element *E=change_receptors.front();E;E=E->next()) ((Object*)(E->get()))->_changed_callback(this,p_property); }
  391. #else
  392. _FORCE_INLINE_ void _change_notify(const char *p_what="") { }
  393. #endif
  394. static void* get_type_ptr_static() {
  395. static int ptr;
  396. return &ptr;
  397. }
  398. bool _is_gpl_reversed() const { return false; }
  399. _FORCE_INLINE_ ObjectID get_instance_ID() const { return _instance_ID; }
  400. // this is used for editors
  401. void add_change_receptor( Object *p_receptor );
  402. void remove_change_receptor( Object *p_receptor );
  403. template<class T>
  404. T *cast_to() {
  405. #ifndef NO_SAFE_CAST
  406. return SAFE_CAST<T*>(this);
  407. #else
  408. if (!this)
  409. return NULL;
  410. if (is_type_ptr(T::get_type_ptr_static()))
  411. return static_cast<T*>(this);
  412. else
  413. return NULL;
  414. #endif
  415. }
  416. template<class T>
  417. const T *cast_to() const {
  418. #ifndef NO_SAFE_CAST
  419. return SAFE_CAST<const T*>(this);
  420. #else
  421. if (!this)
  422. return NULL;
  423. if (is_type_ptr(T::get_type_ptr_static()))
  424. return static_cast<const T*>(this);
  425. else
  426. return NULL;
  427. #endif
  428. }
  429. enum {
  430. NOTIFICATION_POSTINITIALIZE=0,
  431. NOTIFICATION_PREDELETE=1
  432. };
  433. /* TYPE API */
  434. static void get_inheritance_list_static(List<String>* p_inheritance_list) { p_inheritance_list->push_back("Object"); }
  435. static String get_type_static() { return "Object"; }
  436. static String get_parent_type_static() { return String(); }
  437. static String get_category_static() { return String(); }
  438. virtual String get_type() const { return "Object"; }
  439. virtual String get_save_type() const { return get_type(); } //type stored when saving
  440. virtual StringName get_type_name() const { return StringName("Object"); }
  441. virtual bool is_type(const String& p_type) const { return (p_type=="Object"); }
  442. virtual bool is_type_ptr(void *p_ptr) const { return get_type_ptr_static()==p_ptr; }
  443. /* IAPI */
  444. // void set(const String& p_name, const Variant& p_value);
  445. // Variant get(const String& p_name) const;
  446. void set(const StringName& p_name, const Variant& p_value, bool *r_valid=NULL);
  447. Variant get(const StringName& p_name, bool *r_valid=NULL) const;
  448. void get_property_list(List<PropertyInfo> *p_list,bool p_reversed=false) const;
  449. bool has_method(const StringName& p_method) const;
  450. void get_method_list(List<MethodInfo> *p_list) const;
  451. Variant callv(const StringName& p_method,const Array& p_args);
  452. virtual Variant call(const StringName& p_method,const Variant** p_args,int p_argcount,Variant::CallError &r_error);
  453. virtual void call_multilevel(const StringName& p_method,const Variant** p_args,int p_argcount);
  454. virtual void call_multilevel_reversed(const StringName& p_method,const Variant** p_args,int p_argcount);
  455. Variant call(const StringName& p_name, VARIANT_ARG_LIST); // C++ helper
  456. void call_multilevel(const StringName& p_name, VARIANT_ARG_LIST); // C++ helper
  457. void notification(int p_notification,bool p_reversed=false);
  458. //used mainly by script, get and set all INCLUDING string
  459. virtual Variant getvar(const Variant& p_key, bool *r_valid=NULL) const;
  460. virtual void setvar(const Variant& p_key, const Variant& p_value,bool *r_valid=NULL);
  461. /* SCRIPT */
  462. void set_script(const RefPtr& p_script);
  463. RefPtr get_script() const;
  464. /* SCRIPT */
  465. bool has_meta(const String& p_name) const;
  466. void set_meta(const String& p_name, const Variant& p_value );
  467. Variant get_meta(const String& p_name) const;
  468. void get_meta_list(List<String> *p_list) const;
  469. #ifdef TOOLS_ENABLED
  470. void set_edited(bool p_edited);
  471. bool is_edited() const;
  472. #endif
  473. void set_script_instance(ScriptInstance *p_instance);
  474. _FORCE_INLINE_ ScriptInstance* get_script_instance() const { return script_instance; }
  475. void add_user_signal(const MethodInfo& p_signal);
  476. void emit_signal(const StringName& p_name,VARIANT_ARG_LIST);
  477. void get_signal_list(List<MethodInfo> *p_signals ) const;
  478. void get_signal_connection_list(const StringName& p_signal,List<Connection> *p_connections) const;
  479. void get_all_signal_connections(List<Connection> *p_connections) const;
  480. 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);
  481. void disconnect(const StringName& p_signal, Object *p_to_object, const StringName& p_to_method);
  482. bool is_connected(const StringName& p_signal, Object *p_to_object, const StringName& p_to_method) const;
  483. void call_deferred(const StringName& p_method,VARIANT_ARG_LIST);
  484. void set_block_signals(bool p_block);
  485. bool is_blocking_signals() const;
  486. virtual void get_translatable_strings(List<String> *p_strings) const;
  487. virtual void get_argument_options(const StringName& p_function,int p_idx,List<String>*r_options) const;
  488. StringName XL_MESSAGE(const StringName& p_message) const; //translate message (internationalization)
  489. StringName tr(const StringName& p_message) const; //translate message (alternative)
  490. bool _is_queued_for_deletion; // set to true by SceneTree::queue_delete()
  491. bool is_queued_for_deletion() const;
  492. _FORCE_INLINE_ void set_message_translation(bool p_enable) { _can_translate=p_enable; }
  493. _FORCE_INLINE_ bool can_translate_messages() const { return _can_translate; }
  494. Object();
  495. virtual ~Object();
  496. };
  497. bool predelete_handler(Object *p_object);
  498. void postinitialize_handler(Object *p_object);
  499. class ObjectDB {
  500. struct ObjectPtrHash {
  501. static _FORCE_INLINE_ uint32_t hash(const Object *p_obj) {
  502. union {
  503. const Object*p;
  504. unsigned long i;
  505. } u;
  506. u.p=p_obj;
  507. return HashMapHahserDefault::hash((uint64_t)u.i);
  508. }
  509. };
  510. static HashMap<uint32_t,Object*> instances;
  511. static HashMap<Object*,ObjectID,ObjectPtrHash> instance_checks;
  512. static uint32_t instance_counter;
  513. friend class Object;
  514. friend void unregister_core_types();
  515. static void cleanup();
  516. static uint32_t add_instance(Object *p_object);
  517. static void remove_instance(Object *p_object);
  518. public:
  519. typedef void (*DebugFunc)(Object *p_obj);
  520. static Object *get_instance(uint32_t p_instance_ID);
  521. static void debug_objects(DebugFunc p_func);
  522. static int get_object_count();
  523. #ifdef DEBUG_ENABLED
  524. _FORCE_INLINE_ static bool instance_validate(Object* p_ptr) {
  525. return instance_checks.has(p_ptr);
  526. }
  527. #else
  528. _FORCE_INLINE_ static bool instance_validate(Object* p_ptr) { return true; }
  529. #endif
  530. };
  531. //needed by macros
  532. #include "object_type_db.h"
  533. #endif