object.h 25 KB

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