object.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. /*************************************************************************/
  2. /* object.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 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 <[email protected]>
  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_DEFAULT=PROPERTY_USAGE_STORAGE|PROPERTY_USAGE_EDITOR|PROPERTY_USAGE_NETWORK,
  79. PROPERTY_USAGE_DEFAULT_INTL=PROPERTY_USAGE_STORAGE|PROPERTY_USAGE_EDITOR|PROPERTY_USAGE_NETWORK|PROPERTY_USAGE_INTERNATIONALIZED,
  80. PROPERTY_USAGE_NOEDITOR=PROPERTY_USAGE_STORAGE|PROPERTY_USAGE_NETWORK,
  81. };
  82. #define ADD_SIGNAL( m_signal ) ObjectTypeDB::add_signal( get_type_static(), m_signal )
  83. #define ADD_PROPERTY( m_property, m_setter, m_getter ) ObjectTypeDB::add_property( get_type_static(), m_property, m_setter, m_getter )
  84. #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 )
  85. #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 )
  86. #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 )
  87. struct PropertyInfo {
  88. Variant::Type type;
  89. String name;
  90. PropertyHint hint;
  91. String hint_string;
  92. uint32_t usage;
  93. _FORCE_INLINE_ PropertyInfo added_usage(int p_fl) const { PropertyInfo pi=*this; pi.usage|=p_fl; return pi; }
  94. PropertyInfo() { type=Variant::NIL; hint=PROPERTY_HINT_NONE; usage = PROPERTY_USAGE_DEFAULT; }
  95. 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) {
  96. type=p_type; name=p_name; hint=p_hint; hint_string=p_hint_string; usage=p_usage;
  97. }
  98. };
  99. Array convert_property_list(const List<PropertyInfo> * p_list);
  100. struct MethodInfo {
  101. String name;
  102. List<PropertyInfo> arguments;
  103. Vector<Variant> default_arguments;
  104. PropertyInfo return_val;
  105. uint32_t flags;
  106. int id;
  107. inline bool operator<(const MethodInfo& p_method) const { return id==p_method.id?(name < p_method.name):(id<p_method.id); }
  108. MethodInfo();
  109. MethodInfo(const String& p_name);
  110. MethodInfo(const String& p_name, const PropertyInfo& p_param1);
  111. MethodInfo(const String& p_name, const PropertyInfo& p_param1,const PropertyInfo& p_param2);
  112. MethodInfo(const String& p_name, const PropertyInfo& p_param1,const PropertyInfo& p_param2,const PropertyInfo& p_param3);
  113. MethodInfo(const String& p_name, const PropertyInfo& p_param1,const PropertyInfo& p_param2,const PropertyInfo& p_param3,const PropertyInfo& p_param4);
  114. 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);
  115. MethodInfo(Variant::Type ret);
  116. MethodInfo(Variant::Type ret,const String& p_name);
  117. MethodInfo(Variant::Type ret,const String& p_name, const PropertyInfo& p_param1);
  118. MethodInfo(Variant::Type ret,const String& p_name, const PropertyInfo& p_param1,const PropertyInfo& p_param2);
  119. MethodInfo(Variant::Type ret,const String& p_name, const PropertyInfo& p_param1,const PropertyInfo& p_param2,const PropertyInfo& p_param3);
  120. MethodInfo(Variant::Type ret,const String& p_name, const PropertyInfo& p_param1,const PropertyInfo& p_param2,const PropertyInfo& p_param3,const PropertyInfo& p_param4);
  121. 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);
  122. };
  123. // old cast_to
  124. //if ( is_type(T::get_type_static()) )
  125. //return static_cast<T*>(this);
  126. ////else
  127. //return NULL;
  128. /*
  129. 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.
  130. */
  131. #define REVERSE_GET_PROPERTY_LIST \
  132. public:\
  133. _FORCE_INLINE_ bool _is_gpl_reversed() const { return true; };\
  134. private:
  135. #define UNREVERSE_GET_PROPERTY_LIST \
  136. public:\
  137. _FORCE_INLINE_ bool _is_gpl_reversed() const { return false; };\
  138. private:
  139. #define OBJ_TYPE( m_type, m_inherits )\
  140. private:\
  141. void operator=(const m_type& p_rval) {}\
  142. mutable StringName _type_name;\
  143. friend class ObjectTypeDB;\
  144. public:\
  145. virtual String get_type() const { \
  146. return String(#m_type);\
  147. }\
  148. virtual StringName get_type_name() const { \
  149. if (!_type_name)\
  150. _type_name=get_type_static();\
  151. return _type_name;\
  152. }\
  153. static _FORCE_INLINE_ void* get_type_ptr_static() { \
  154. static int ptr;\
  155. return &ptr;\
  156. }\
  157. static _FORCE_INLINE_ String get_type_static() { \
  158. return String(#m_type);\
  159. }\
  160. static _FORCE_INLINE_ String get_parent_type_static() { \
  161. return m_inherits::get_type_static();\
  162. }\
  163. static void get_inheritance_list_static(List<String>* p_inheritance_list) { \
  164. m_inherits::get_inheritance_list_static(p_inheritance_list);\
  165. p_inheritance_list->push_back(String(#m_type));\
  166. }\
  167. static String get_category_static() { \
  168. String category = m_inherits::get_category_static();\
  169. if (_get_category!=m_inherits::_get_category) {\
  170. if (category!="")\
  171. category+="/";\
  172. category+=_get_category();\
  173. }\
  174. return category;\
  175. }\
  176. static String inherits_static() {\
  177. return String(#m_inherits);\
  178. }\
  179. virtual bool is_type(const String& p_type) const { return (p_type==(#m_type))?true:m_inherits::is_type(p_type); }\
  180. virtual bool is_type_ptr(void *p_ptr) const { return (p_ptr==get_type_ptr_static())?true:m_inherits::is_type_ptr(p_ptr); }\
  181. \
  182. \
  183. static void get_valid_parents_static(List<String> *p_parents) {\
  184. \
  185. if (m_type::_get_valid_parents_static!=m_inherits::_get_valid_parents_static) { \
  186. m_type::_get_valid_parents_static(p_parents);\
  187. }\
  188. \
  189. m_inherits::get_valid_parents_static(p_parents);\
  190. }\
  191. protected:\
  192. _FORCE_INLINE_ static void (*_get_bind_methods())() {\
  193. return &m_type::_bind_methods;\
  194. }\
  195. public:\
  196. static void initialize_type() {\
  197. static bool initialized=false;\
  198. if (initialized)\
  199. return;\
  200. m_inherits::initialize_type();\
  201. ObjectTypeDB::_add_type<m_type>();\
  202. if (m_type::_get_bind_methods() != m_inherits::_get_bind_methods())\
  203. _bind_methods();\
  204. initialized=true;\
  205. }\
  206. protected:\
  207. virtual void _initialize_typev() {\
  208. initialize_type();\
  209. }\
  210. _FORCE_INLINE_ bool (Object::* (_get_get() const))(const StringName& p_name,Variant&) const {\
  211. return (bool (Object::*)(const StringName&,Variant&)const) &m_type::_get;\
  212. }\
  213. virtual bool _getv(const StringName& p_name, Variant& r_ret) const { \
  214. if (m_type::_get_get() != m_inherits::_get_get()) {\
  215. if (_get(p_name,r_ret))\
  216. return true;\
  217. }\
  218. return m_inherits::_getv(p_name,r_ret);\
  219. }\
  220. _FORCE_INLINE_ bool (Object::* (_get_set() const))(const StringName& p_name,const Variant &p_property) {\
  221. return (bool (Object::*)(const StringName&, const Variant&))&m_type::_set;\
  222. }\
  223. virtual bool _setv(const StringName& p_name,const Variant &p_property) { \
  224. if (m_inherits::_setv(p_name,p_property)) return true;\
  225. if (m_type::_get_set() != m_inherits::_get_set()) {\
  226. return _set(p_name,p_property);\
  227. \
  228. }\
  229. return false;\
  230. }\
  231. _FORCE_INLINE_ void (Object::* (_get_get_property_list() const))(List<PropertyInfo> *p_list) const{\
  232. return (void (Object::*)(List<PropertyInfo>*)const)&m_type::_get_property_list;\
  233. }\
  234. virtual void _get_property_listv(List<PropertyInfo> *p_list,bool p_reversed) const { \
  235. if (!p_reversed) {\
  236. m_inherits::_get_property_listv(p_list,p_reversed);\
  237. }\
  238. p_list->push_back( PropertyInfo(Variant::NIL,get_type_static(),PROPERTY_HINT_NONE,String(),PROPERTY_USAGE_CATEGORY));\
  239. if (!_is_gpl_reversed())\
  240. ObjectTypeDB::get_property_list(#m_type,p_list,true);\
  241. if (m_type::_get_get_property_list() != m_inherits::_get_get_property_list()) {\
  242. _get_property_list(p_list);\
  243. }\
  244. if (_is_gpl_reversed())\
  245. ObjectTypeDB::get_property_list(#m_type,p_list,true);\
  246. if (p_reversed) {\
  247. m_inherits::_get_property_listv(p_list,p_reversed);\
  248. }\
  249. \
  250. }\
  251. _FORCE_INLINE_ void (Object::* (_get_notification() const))(int){\
  252. return (void (Object::*)(int)) &m_type::_notification;\
  253. }\
  254. virtual void _notificationv(int p_notification,bool p_reversed) { \
  255. if (!p_reversed) \
  256. m_inherits::_notificationv(p_notification,p_reversed);\
  257. if (m_type::_get_notification() != m_inherits::_get_notification()) {\
  258. _notification(p_notification);\
  259. }\
  260. if (p_reversed)\
  261. m_inherits::_notificationv(p_notification,p_reversed);\
  262. }\
  263. \
  264. private:
  265. #define OBJ_CATEGORY(m_category)\
  266. protected:\
  267. _FORCE_INLINE_ static String _get_category() { return m_category; }\
  268. private:
  269. #define OBJ_SAVE_TYPE(m_type) \
  270. public: \
  271. virtual String get_save_type() const { return #m_type; }\
  272. private:
  273. class ScriptInstance;
  274. typedef uint32_t ObjectID;
  275. class Object {
  276. public:
  277. enum ConnectFlags {
  278. CONNECT_DEFERRED=1,
  279. CONNECT_PERSIST=2, // hint for scene to save this connection
  280. CONNECT_ONESHOT=4
  281. };
  282. struct Connection {
  283. Object *source;
  284. StringName signal;
  285. Object *target;
  286. StringName method;
  287. uint32_t flags;
  288. Vector<Variant> binds;
  289. bool operator<(const Connection& p_conn) const;
  290. operator Variant() const;
  291. Connection() { source=NULL; target=NULL; flags=0; }
  292. Connection(const Variant& p_variant);
  293. };
  294. private:
  295. friend bool predelete_handler(Object*);
  296. friend void postinitialize_handler(Object*);
  297. struct Signal {
  298. struct Target {
  299. ObjectID _id;
  300. StringName method;
  301. _FORCE_INLINE_ bool operator<(const Target& p_target) const { return (_id==p_target._id)?(method<p_target.method):(_id<p_target._id); }
  302. Target(const ObjectID& p_id, const StringName& p_method) { _id=p_id; method=p_method; }
  303. Target() { _id=0; }
  304. };
  305. struct Slot {
  306. Connection conn;
  307. List<Connection>::Element *cE;
  308. };
  309. MethodInfo user;
  310. VMap<Target,Slot> slot_map;
  311. int lock;
  312. Signal() { lock=0; }
  313. };
  314. HashMap< StringName, Signal, StringNameHasher> signal_map;
  315. List<Connection> connections;
  316. bool _block_signals;
  317. int _predelete_ok;
  318. Set<Object*> change_receptors;
  319. uint32_t _instance_ID;
  320. bool _predelete();
  321. void _postinitialize();
  322. bool _can_translate;
  323. #ifdef TOOLS_ENABLED
  324. bool _edited;
  325. #endif
  326. ScriptInstance *script_instance;
  327. RefPtr script;
  328. Dictionary metadata;
  329. void _add_user_signal(const String& p_name, const Array& p_pargs=Array());
  330. Variant _emit_signal(const Variant** p_args, int p_argcount, Variant::CallError& r_error);
  331. Array _get_signal_list() const;
  332. Array _get_signal_connection_list(const String& p_signal) const;
  333. void _set_bind(const String& p_set,const Variant& p_value);
  334. Variant _get_bind(const String& p_name) const;
  335. void property_list_changed_notify();
  336. protected:
  337. virtual bool _use_builtin_script() const { return false; }
  338. virtual void _initialize_typev() { initialize_type(); }
  339. virtual bool _setv(const StringName& p_name,const Variant &p_property) { return false; };
  340. virtual bool _getv(const StringName& p_name,Variant &r_property) const { return false; };
  341. virtual void _get_property_listv(List<PropertyInfo> *p_list,bool p_reversed) const {};
  342. virtual void _notificationv(int p_notification,bool p_reversed) {};
  343. static String _get_category() { return ""; }
  344. static void _bind_methods();
  345. bool _set(const StringName& p_name,const Variant &p_property) { return false; };
  346. bool _get(const StringName& p_name,Variant &r_property) const { return false; };
  347. void _get_property_list(List<PropertyInfo> *p_list) const {};
  348. void _notification(int p_notification) {};
  349. _FORCE_INLINE_ static void (*_get_bind_methods())() {
  350. return &Object::_bind_methods;
  351. }
  352. _FORCE_INLINE_ bool (Object::* (_get_get() const))(const StringName& p_name,Variant &r_ret) const {
  353. return &Object::_get;
  354. }
  355. _FORCE_INLINE_ bool (Object::* (_get_set() const))(const StringName& p_name,const Variant &p_property) {
  356. return &Object::_set;
  357. }
  358. _FORCE_INLINE_ void (Object::* (_get_get_property_list() const))(List<PropertyInfo> *p_list) const{
  359. return &Object::_get_property_list;
  360. }
  361. _FORCE_INLINE_ void (Object::* (_get_notification() const))(int){
  362. return &Object::_notification;
  363. }
  364. static void get_valid_parents_static(List<String> *p_parents);
  365. static void _get_valid_parents_static(List<String> *p_parents);
  366. void cancel_delete();
  367. virtual void _changed_callback(Object *p_changed,const char *p_prop);
  368. //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());
  369. //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());
  370. Variant _call_bind(const Variant** p_args, int p_argcount, Variant::CallError& r_error);
  371. Variant _call_deferred_bind(const Variant** p_args, int p_argcount, Variant::CallError& r_error);
  372. DVector<String> _get_meta_list_bind() const;
  373. Array _get_property_list_bind() const;
  374. public: //should be protected, but bug in clang++
  375. static void initialize_type();
  376. _FORCE_INLINE_ static void register_custom_data_to_otdb() {};
  377. public:
  378. #ifdef TOOLS_ENABLED
  379. _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); }
  380. #else
  381. _FORCE_INLINE_ void _change_notify(const char *p_what="") { }
  382. #endif
  383. static void* get_type_ptr_static() {
  384. static int ptr;
  385. return &ptr;
  386. }
  387. bool _is_gpl_reversed() const { return false; }
  388. _FORCE_INLINE_ ObjectID get_instance_ID() const { return _instance_ID; }
  389. // this is used for editors
  390. void add_change_receptor( Object *p_receptor );
  391. void remove_change_receptor( Object *p_receptor );
  392. template<class T>
  393. T *cast_to() {
  394. #ifndef NO_SAFE_CAST
  395. return SAFE_CAST<T*>(this);
  396. #else
  397. if (!this)
  398. return NULL;
  399. if (is_type_ptr(T::get_type_ptr_static()))
  400. return static_cast<T*>(this);
  401. else
  402. return NULL;
  403. #endif
  404. }
  405. template<class T>
  406. const T *cast_to() const {
  407. #ifndef NO_SAFE_CAST
  408. return SAFE_CAST<const T*>(this);
  409. #else
  410. if (!this)
  411. return NULL;
  412. if (is_type_ptr(T::get_type_ptr_static()))
  413. return static_cast<const T*>(this);
  414. else
  415. return NULL;
  416. #endif
  417. }
  418. enum {
  419. NOTIFICATION_POSTINITIALIZE=0,
  420. NOTIFICATION_PREDELETE=1
  421. };
  422. /* TYPE API */
  423. static void get_inheritance_list_static(List<String>* p_inheritance_list) { p_inheritance_list->push_back("Object"); }
  424. static String get_type_static() { return "Object"; }
  425. static String get_parent_type_static() { return String(); }
  426. static String get_category_static() { return String(); }
  427. virtual String get_type() const { return "Object"; }
  428. virtual String get_save_type() const { return get_type(); } //type stored when saving
  429. virtual StringName get_type_name() const { return StringName("Object"); }
  430. virtual bool is_type(const String& p_type) const { return (p_type=="Object"); }
  431. virtual bool is_type_ptr(void *p_ptr) const { return get_type_ptr_static()==p_ptr; }
  432. /* IAPI */
  433. // void set(const String& p_name, const Variant& p_value);
  434. // Variant get(const String& p_name) const;
  435. void set(const StringName& p_name, const Variant& p_value, bool *r_valid=NULL);
  436. Variant get(const StringName& p_name, bool *r_valid=NULL) const;
  437. void get_property_list(List<PropertyInfo> *p_list,bool p_reversed=false) const;
  438. bool has_method(const StringName& p_method) const;
  439. void get_method_list(List<MethodInfo> *p_list) const;
  440. Variant callv(const StringName& p_method,const Array& p_args);
  441. virtual Variant call(const StringName& p_method,const Variant** p_args,int p_argcount,Variant::CallError &r_error);
  442. virtual void call_multilevel(const StringName& p_method,const Variant** p_args,int p_argcount);
  443. virtual void call_multilevel_reversed(const StringName& p_method,const Variant** p_args,int p_argcount);
  444. Variant call(const StringName& p_name, VARIANT_ARG_LIST); // C++ helper
  445. void call_multilevel(const StringName& p_name, VARIANT_ARG_LIST); // C++ helper
  446. void notification(int p_notification,bool p_reversed=false);
  447. //used mainly by script, get and set all INCLUDING string
  448. virtual Variant getvar(const Variant& p_key, bool *r_valid=NULL) const;
  449. virtual void setvar(const Variant& p_key, const Variant& p_value,bool *r_valid=NULL);
  450. /* SCRIPT */
  451. void set_script(const RefPtr& p_script);
  452. RefPtr get_script() const;
  453. /* SCRIPT */
  454. bool has_meta(const String& p_name) const;
  455. void set_meta(const String& p_name, const Variant& p_value );
  456. Variant get_meta(const String& p_name) const;
  457. void get_meta_list(List<String> *p_list) const;
  458. #ifdef TOOLS_ENABLED
  459. void set_edited(bool p_edited);
  460. bool is_edited() const;
  461. #endif
  462. void set_script_instance(ScriptInstance *p_instance);
  463. _FORCE_INLINE_ ScriptInstance* get_script_instance() const { return script_instance; }
  464. void add_user_signal(const MethodInfo& p_signal);
  465. void emit_signal(const StringName& p_name,VARIANT_ARG_LIST);
  466. void get_signal_list(List<MethodInfo> *p_signals ) const;
  467. void get_signal_connection_list(const StringName& p_signal,List<Connection> *p_connections) const;
  468. void 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);
  469. void disconnect(const StringName& p_signal, Object *p_to_object, const StringName& p_to_method);
  470. bool is_connected(const StringName& p_signal, Object *p_to_object, const StringName& p_to_method) const;
  471. void call_deferred(const StringName& p_method,VARIANT_ARG_LIST);
  472. void set_block_signals(bool p_block);
  473. bool is_blocking_signals() const;
  474. virtual void get_translatable_strings(List<String> *p_strings) const;
  475. StringName XL_MESSAGE(const StringName& p_message) const; //translate message (internationalization)
  476. StringName tr(const StringName& p_message) const; //translate message (alternative)
  477. _FORCE_INLINE_ void set_message_translation(bool p_enable) { _can_translate=p_enable; }
  478. _FORCE_INLINE_ bool can_translate_messages() const { return _can_translate; }
  479. Object();
  480. virtual ~Object();
  481. };
  482. bool predelete_handler(Object *p_object);
  483. void postinitialize_handler(Object *p_object);
  484. class ObjectDB {
  485. struct ObjectPtrHash {
  486. static _FORCE_INLINE_ uint32_t hash(const Object *p_obj) {
  487. union {
  488. const Object*p;
  489. unsigned long i;
  490. } u;
  491. u.p=p_obj;
  492. return HashMapHahserDefault::hash((uint64_t)u.i);
  493. }
  494. };
  495. static HashMap<uint32_t,Object*> instances;
  496. static HashMap<Object*,ObjectID,ObjectPtrHash> instance_checks;
  497. static uint32_t instance_counter;
  498. friend class Object;
  499. friend void unregister_core_types();
  500. static void cleanup();
  501. static uint32_t add_instance(Object *p_object);
  502. static void remove_instance(Object *p_object);
  503. public:
  504. typedef void (*DebugFunc)(Object *p_obj);
  505. static Object *get_instance(uint32_t p_instance_ID);
  506. static void debug_objects(DebugFunc p_func);
  507. static int get_object_count();
  508. #ifdef DEBUG_ENABLED
  509. _FORCE_INLINE_ static bool instance_validate(Object* p_ptr) {
  510. return instance_checks.has(p_ptr);
  511. }
  512. #else
  513. _FORCE_INLINE_ static bool instance_validate(Object* p_ptr) { return true; }
  514. #endif
  515. };
  516. //needed by macros
  517. #include "object_type_db.h"
  518. #endif