object.h 25 KB

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