object.h 24 KB

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