object.h 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  1. /*************************************************************************/
  2. /* object.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef OBJECT_H
  31. #define OBJECT_H
  32. #include "list.h"
  33. #include "map.h"
  34. #include "os/rw_lock.h"
  35. #include "set.h"
  36. #include "variant.h"
  37. #include "vmap.h"
  38. #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()
  39. #define VARIANT_ARG_PASS p_arg1, p_arg2, p_arg3, p_arg4, p_arg5
  40. #define VARIANT_ARG_DECLARE const Variant &p_arg1, const Variant &p_arg2, const Variant &p_arg3, const Variant &p_arg4, const Variant &p_arg5
  41. #define VARIANT_ARG_MAX 5
  42. #define VARIANT_ARGPTRS const Variant *argptr[5] = { &p_arg1, &p_arg2, &p_arg3, &p_arg4, &p_arg5 };
  43. #define VARIANT_ARGPTRS_PASS *argptr[0], *argptr[1], *argptr[2], *argptr[3], *argptr[4]
  44. #define VARIANT_ARGS_FROM_ARRAY(m_arr) m_arr[0], m_arr[1], m_arr[2], m_arr[3], m_arr[4]
  45. /**
  46. @author Juan Linietsky <reduzio@gmail.com>
  47. */
  48. enum PropertyHint {
  49. PROPERTY_HINT_NONE, ///< no hint provided.
  50. PROPERTY_HINT_RANGE, ///< hint_text = "min,max,step,slider; //slider is optional"
  51. PROPERTY_HINT_EXP_RANGE, ///< hint_text = "min,max,step", exponential edit
  52. PROPERTY_HINT_ENUM, ///< hint_text= "val1,val2,val3,etc"
  53. PROPERTY_HINT_EXP_EASING, /// exponential easing function (Math::ease)
  54. PROPERTY_HINT_LENGTH, ///< hint_text= "length" (as integer)
  55. PROPERTY_HINT_SPRITE_FRAME,
  56. PROPERTY_HINT_KEY_ACCEL, ///< hint_text= "length" (as integer)
  57. PROPERTY_HINT_FLAGS, ///< hint_text= "flag1,flag2,etc" (as bit flags)
  58. PROPERTY_HINT_LAYERS_2D_RENDER,
  59. PROPERTY_HINT_LAYERS_2D_PHYSICS,
  60. PROPERTY_HINT_LAYERS_3D_RENDER,
  61. PROPERTY_HINT_LAYERS_3D_PHYSICS,
  62. PROPERTY_HINT_FILE, ///< a file path must be passed, hint_text (optionally) is a filter "*.png,*.wav,*.doc,"
  63. PROPERTY_HINT_DIR, ///< a directory path must be passed
  64. PROPERTY_HINT_GLOBAL_FILE, ///< a file path must be passed, hint_text (optionally) is a filter "*.png,*.wav,*.doc,"
  65. PROPERTY_HINT_GLOBAL_DIR, ///< a directory path must be passed
  66. PROPERTY_HINT_RESOURCE_TYPE, ///< a resource object type
  67. PROPERTY_HINT_MULTILINE_TEXT, ///< used for string properties that can contain multiple lines
  68. PROPERTY_HINT_COLOR_NO_ALPHA, ///< used for ignoring alpha component when editing a color
  69. PROPERTY_HINT_IMAGE_COMPRESS_LOSSY,
  70. PROPERTY_HINT_IMAGE_COMPRESS_LOSSLESS,
  71. PROPERTY_HINT_OBJECT_ID,
  72. PROPERTY_HINT_TYPE_STRING, ///< a type string, the hint is the base type to choose
  73. PROPERTY_HINT_NODE_PATH_TO_EDITED_NODE, ///< so something else can provide this (used in scripts)
  74. PROPERTY_HINT_METHOD_OF_VARIANT_TYPE, ///< a method of a type
  75. PROPERTY_HINT_METHOD_OF_BASE_TYPE, ///< a method of a base type
  76. PROPERTY_HINT_METHOD_OF_INSTANCE, ///< a method of an instance
  77. PROPERTY_HINT_METHOD_OF_SCRIPT, ///< a method of a script & base
  78. PROPERTY_HINT_PROPERTY_OF_VARIANT_TYPE, ///< a property of a type
  79. PROPERTY_HINT_PROPERTY_OF_BASE_TYPE, ///< a property of a base type
  80. PROPERTY_HINT_PROPERTY_OF_INSTANCE, ///< a property of an instance
  81. PROPERTY_HINT_PROPERTY_OF_SCRIPT, ///< a property of a script & base
  82. PROPERTY_HINT_OBJECT_TOO_BIG, ///< object is too big to send
  83. PROPERTY_HINT_MAX,
  84. };
  85. enum PropertyUsageFlags {
  86. PROPERTY_USAGE_STORAGE = 1,
  87. PROPERTY_USAGE_EDITOR = 2,
  88. PROPERTY_USAGE_NETWORK = 4,
  89. PROPERTY_USAGE_EDITOR_HELPER = 8,
  90. PROPERTY_USAGE_CHECKABLE = 16, //used for editing global variables
  91. PROPERTY_USAGE_CHECKED = 32, //used for editing global variables
  92. PROPERTY_USAGE_INTERNATIONALIZED = 64, //hint for internationalized strings
  93. PROPERTY_USAGE_GROUP = 128, //used for grouping props in the editor
  94. PROPERTY_USAGE_CATEGORY = 256,
  95. PROPERTY_USAGE_STORE_IF_NONZERO = 512, //only store if nonzero
  96. PROPERTY_USAGE_STORE_IF_NONONE = 1024, //only store if false
  97. PROPERTY_USAGE_NO_INSTANCE_STATE = 2048,
  98. PROPERTY_USAGE_RESTART_IF_CHANGED = 4096,
  99. PROPERTY_USAGE_SCRIPT_VARIABLE = 8192,
  100. PROPERTY_USAGE_STORE_IF_NULL = 16384,
  101. PROPERTY_USAGE_ANIMATE_AS_TRIGGER = 32768,
  102. PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED = 65536,
  103. PROPERTY_USAGE_SCRIPT_DEFAULT_VALUE = 1 << 17,
  104. PROPERTY_USAGE_CLASS_IS_ENUM = 1 << 18,
  105. PROPERTY_USAGE_NIL_IS_VARIANT = 1 << 19,
  106. PROPERTY_USAGE_DEFAULT = PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_NETWORK,
  107. PROPERTY_USAGE_DEFAULT_INTL = PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_NETWORK | PROPERTY_USAGE_INTERNATIONALIZED,
  108. PROPERTY_USAGE_NOEDITOR = PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_NETWORK,
  109. };
  110. #define ADD_SIGNAL(m_signal) ClassDB::add_signal(get_class_static(), m_signal)
  111. #define ADD_PROPERTY(m_property, m_setter, m_getter) ClassDB::add_property(get_class_static(), m_property, _scs_create(m_setter), _scs_create(m_getter))
  112. #define ADD_PROPERTYI(m_property, m_setter, m_getter, m_index) ClassDB::add_property(get_class_static(), m_property, _scs_create(m_setter), _scs_create(m_getter), m_index)
  113. #define ADD_PROPERTYNZ(m_property, m_setter, m_getter) ClassDB::add_property(get_class_static(), (m_property).added_usage(PROPERTY_USAGE_STORE_IF_NONZERO), _scs_create(m_setter), _scs_create(m_getter))
  114. #define ADD_PROPERTYINZ(m_property, m_setter, m_getter, m_index) ClassDB::add_property(get_class_static(), (m_property).added_usage(PROPERTY_USAGE_STORE_IF_NONZERO), _scs_create(m_setter), _scs_create(m_getter), m_index)
  115. #define ADD_PROPERTYNO(m_property, m_setter, m_getter) ClassDB::add_property(get_class_static(), (m_property).added_usage(PROPERTY_USAGE_STORE_IF_NONONE), _scs_create(m_setter), _scs_create(m_getter))
  116. #define ADD_PROPERTYINO(m_property, m_setter, m_getter, m_index) ClassDB::add_property(get_class_static(), (m_property).added_usage(PROPERTY_USAGE_STORE_IF_NONONE), _scs_create(m_setter), _scs_create(m_getter), m_index)
  117. #define ADD_GROUP(m_name, m_prefix) ClassDB::add_property_group(get_class_static(), m_name, m_prefix)
  118. struct PropertyInfo {
  119. Variant::Type type;
  120. String name;
  121. StringName class_name; //for classes
  122. PropertyHint hint;
  123. String hint_string;
  124. uint32_t usage;
  125. _FORCE_INLINE_ PropertyInfo added_usage(int p_fl) const {
  126. PropertyInfo pi = *this;
  127. pi.usage |= p_fl;
  128. return pi;
  129. }
  130. operator Dictionary() const;
  131. static PropertyInfo from_dict(const Dictionary &p_dict);
  132. PropertyInfo()
  133. : type(Variant::NIL),
  134. hint(PROPERTY_HINT_NONE),
  135. usage(PROPERTY_USAGE_DEFAULT) {
  136. }
  137. 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, const StringName &p_class_name = StringName())
  138. : type(p_type),
  139. name(p_name),
  140. hint(p_hint),
  141. hint_string(p_hint_string),
  142. usage(p_usage) {
  143. if (hint == PROPERTY_HINT_RESOURCE_TYPE) {
  144. class_name = hint_string;
  145. } else {
  146. class_name = p_class_name;
  147. }
  148. }
  149. PropertyInfo(const StringName &p_class_name)
  150. : type(Variant::OBJECT),
  151. class_name(p_class_name),
  152. hint(PROPERTY_HINT_NONE),
  153. usage(PROPERTY_USAGE_DEFAULT) {
  154. }
  155. bool operator<(const PropertyInfo &p_info) const {
  156. return name < p_info.name;
  157. }
  158. };
  159. Array convert_property_list(const List<PropertyInfo> *p_list);
  160. struct MethodInfo {
  161. String name;
  162. List<PropertyInfo> arguments;
  163. Vector<Variant> default_arguments;
  164. PropertyInfo return_val;
  165. uint32_t flags;
  166. int id;
  167. inline bool operator==(const MethodInfo &p_method) const { return id == p_method.id; }
  168. inline bool operator<(const MethodInfo &p_method) const { return id == p_method.id ? (name < p_method.name) : (id < p_method.id); }
  169. operator Dictionary() const;
  170. static MethodInfo from_dict(const Dictionary &p_dict);
  171. MethodInfo();
  172. MethodInfo(const String &p_name);
  173. MethodInfo(const String &p_name, const PropertyInfo &p_param1);
  174. MethodInfo(const String &p_name, const PropertyInfo &p_param1, const PropertyInfo &p_param2);
  175. MethodInfo(const String &p_name, const PropertyInfo &p_param1, const PropertyInfo &p_param2, const PropertyInfo &p_param3);
  176. MethodInfo(const String &p_name, const PropertyInfo &p_param1, const PropertyInfo &p_param2, const PropertyInfo &p_param3, const PropertyInfo &p_param4);
  177. 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);
  178. MethodInfo(Variant::Type ret);
  179. MethodInfo(Variant::Type ret, const String &p_name);
  180. MethodInfo(Variant::Type ret, const String &p_name, const PropertyInfo &p_param1);
  181. MethodInfo(Variant::Type ret, const String &p_name, const PropertyInfo &p_param1, const PropertyInfo &p_param2);
  182. MethodInfo(Variant::Type ret, const String &p_name, const PropertyInfo &p_param1, const PropertyInfo &p_param2, const PropertyInfo &p_param3);
  183. MethodInfo(Variant::Type ret, const String &p_name, const PropertyInfo &p_param1, const PropertyInfo &p_param2, const PropertyInfo &p_param3, const PropertyInfo &p_param4);
  184. 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);
  185. MethodInfo(const PropertyInfo &p_ret, const String &p_name);
  186. MethodInfo(const PropertyInfo &p_ret, const String &p_name, const PropertyInfo &p_param1);
  187. MethodInfo(const PropertyInfo &p_ret, const String &p_name, const PropertyInfo &p_param1, const PropertyInfo &p_param2);
  188. MethodInfo(const PropertyInfo &p_ret, const String &p_name, const PropertyInfo &p_param1, const PropertyInfo &p_param2, const PropertyInfo &p_param3);
  189. MethodInfo(const PropertyInfo &p_ret, const String &p_name, const PropertyInfo &p_param1, const PropertyInfo &p_param2, const PropertyInfo &p_param3, const PropertyInfo &p_param4);
  190. MethodInfo(const PropertyInfo &p_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);
  191. };
  192. // old cast_to
  193. //if ( is_type(T::get_class_static()) )
  194. //return static_cast<T*>(this);
  195. ////else
  196. //return NULL;
  197. /*
  198. the following is an incomprehensible 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.
  199. */
  200. #define REVERSE_GET_PROPERTY_LIST \
  201. public: \
  202. _FORCE_INLINE_ bool _is_gpl_reversed() const { return true; }; \
  203. \
  204. private:
  205. #define UNREVERSE_GET_PROPERTY_LIST \
  206. public: \
  207. _FORCE_INLINE_ bool _is_gpl_reversed() const { return false; }; \
  208. \
  209. private:
  210. #define GDCLASS(m_class, m_inherits) \
  211. private: \
  212. void operator=(const m_class &p_rval) {} \
  213. mutable StringName _class_name; \
  214. friend class ClassDB; \
  215. \
  216. public: \
  217. virtual String get_class() const { \
  218. return String(#m_class); \
  219. } \
  220. virtual const StringName *_get_class_namev() const { \
  221. if (!_class_name) \
  222. _class_name = get_class_static(); \
  223. return &_class_name; \
  224. } \
  225. static _FORCE_INLINE_ void *get_class_ptr_static() { \
  226. static int ptr; \
  227. return &ptr; \
  228. } \
  229. static _FORCE_INLINE_ String get_class_static() { \
  230. return String(#m_class); \
  231. } \
  232. static _FORCE_INLINE_ String get_parent_class_static() { \
  233. return m_inherits::get_class_static(); \
  234. } \
  235. static void get_inheritance_list_static(List<String> *p_inheritance_list) { \
  236. m_inherits::get_inheritance_list_static(p_inheritance_list); \
  237. p_inheritance_list->push_back(String(#m_class)); \
  238. } \
  239. static String get_category_static() { \
  240. String category = m_inherits::get_category_static(); \
  241. if (_get_category != m_inherits::_get_category) { \
  242. if (category != "") \
  243. category += "/"; \
  244. category += _get_category(); \
  245. } \
  246. return category; \
  247. } \
  248. static String inherits_static() { \
  249. return String(#m_inherits); \
  250. } \
  251. virtual bool is_class(const String &p_class) const { return (p_class == (#m_class)) ? true : m_inherits::is_class(p_class); } \
  252. virtual bool is_class_ptr(void *p_ptr) const { return (p_ptr == get_class_ptr_static()) ? true : m_inherits::is_class_ptr(p_ptr); } \
  253. \
  254. static void get_valid_parents_static(List<String> *p_parents) { \
  255. \
  256. if (m_class::_get_valid_parents_static != m_inherits::_get_valid_parents_static) { \
  257. m_class::_get_valid_parents_static(p_parents); \
  258. } \
  259. \
  260. m_inherits::get_valid_parents_static(p_parents); \
  261. } \
  262. \
  263. protected: \
  264. _FORCE_INLINE_ static void (*_get_bind_methods())() { \
  265. return &m_class::_bind_methods; \
  266. } \
  267. \
  268. public: \
  269. static void initialize_class() { \
  270. static bool initialized = false; \
  271. if (initialized) \
  272. return; \
  273. m_inherits::initialize_class(); \
  274. ClassDB::_add_class<m_class>(); \
  275. if (m_class::_get_bind_methods() != m_inherits::_get_bind_methods()) \
  276. _bind_methods(); \
  277. initialized = true; \
  278. } \
  279. \
  280. protected: \
  281. virtual void _initialize_classv() { \
  282. initialize_class(); \
  283. } \
  284. _FORCE_INLINE_ bool (Object::*(_get_get() const))(const StringName &p_name, Variant &) const { \
  285. return (bool (Object::*)(const StringName &, Variant &) const) & m_class::_get; \
  286. } \
  287. virtual bool _getv(const StringName &p_name, Variant &r_ret) const { \
  288. if (m_class::_get_get() != m_inherits::_get_get()) { \
  289. if (_get(p_name, r_ret)) \
  290. return true; \
  291. } \
  292. return m_inherits::_getv(p_name, r_ret); \
  293. } \
  294. _FORCE_INLINE_ bool (Object::*(_get_set() const))(const StringName &p_name, const Variant &p_property) { \
  295. return (bool (Object::*)(const StringName &, const Variant &)) & m_class::_set; \
  296. } \
  297. virtual bool _setv(const StringName &p_name, const Variant &p_property) { \
  298. if (m_inherits::_setv(p_name, p_property)) return true; \
  299. if (m_class::_get_set() != m_inherits::_get_set()) { \
  300. return _set(p_name, p_property); \
  301. } \
  302. return false; \
  303. } \
  304. _FORCE_INLINE_ void (Object::*(_get_get_property_list() const))(List<PropertyInfo> * p_list) const { \
  305. return (void (Object::*)(List<PropertyInfo> *) const) & m_class::_get_property_list; \
  306. } \
  307. virtual void _get_property_listv(List<PropertyInfo> *p_list, bool p_reversed) const { \
  308. if (!p_reversed) { \
  309. m_inherits::_get_property_listv(p_list, p_reversed); \
  310. } \
  311. p_list->push_back(PropertyInfo(Variant::NIL, get_class_static(), PROPERTY_HINT_NONE, String(), PROPERTY_USAGE_CATEGORY)); \
  312. if (!_is_gpl_reversed()) \
  313. ClassDB::get_property_list(#m_class, p_list, true, this); \
  314. if (m_class::_get_get_property_list() != m_inherits::_get_get_property_list()) { \
  315. _get_property_list(p_list); \
  316. } \
  317. if (_is_gpl_reversed()) \
  318. ClassDB::get_property_list(#m_class, p_list, true, this); \
  319. if (p_reversed) { \
  320. m_inherits::_get_property_listv(p_list, p_reversed); \
  321. } \
  322. } \
  323. _FORCE_INLINE_ void (Object::*(_get_notification() const))(int) { \
  324. return (void (Object::*)(int)) & m_class::_notification; \
  325. } \
  326. virtual void _notificationv(int p_notification, bool p_reversed) { \
  327. if (!p_reversed) \
  328. m_inherits::_notificationv(p_notification, p_reversed); \
  329. if (m_class::_get_notification() != m_inherits::_get_notification()) { \
  330. _notification(p_notification); \
  331. } \
  332. if (p_reversed) \
  333. m_inherits::_notificationv(p_notification, p_reversed); \
  334. } \
  335. \
  336. private:
  337. #define OBJ_CATEGORY(m_category) \
  338. protected: \
  339. _FORCE_INLINE_ static String _get_category() { return m_category; } \
  340. \
  341. private:
  342. #define OBJ_SAVE_TYPE(m_class) \
  343. public: \
  344. virtual String get_save_class() const { return #m_class; } \
  345. \
  346. private:
  347. class ScriptInstance;
  348. typedef uint64_t ObjectID;
  349. class Object {
  350. public:
  351. enum ConnectFlags {
  352. CONNECT_DEFERRED = 1,
  353. CONNECT_PERSIST = 2, // hint for scene to save this connection
  354. CONNECT_ONESHOT = 4
  355. };
  356. struct Connection {
  357. Object *source;
  358. StringName signal;
  359. Object *target;
  360. StringName method;
  361. uint32_t flags;
  362. Vector<Variant> binds;
  363. bool operator<(const Connection &p_conn) const;
  364. operator Variant() const;
  365. Connection() {
  366. source = NULL;
  367. target = NULL;
  368. flags = 0;
  369. }
  370. Connection(const Variant &p_variant);
  371. };
  372. private:
  373. enum {
  374. MAX_SCRIPT_INSTANCE_BINDINGS = 8
  375. };
  376. #ifdef DEBUG_ENABLED
  377. friend class _ObjectDebugLock;
  378. #endif
  379. friend bool predelete_handler(Object *);
  380. friend void postinitialize_handler(Object *);
  381. struct Signal {
  382. struct Target {
  383. ObjectID _id;
  384. StringName method;
  385. _FORCE_INLINE_ bool operator<(const Target &p_target) const { return (_id == p_target._id) ? (method < p_target.method) : (_id < p_target._id); }
  386. Target(const ObjectID &p_id, const StringName &p_method)
  387. : _id(p_id),
  388. method(p_method) {
  389. }
  390. Target() { _id = 0; }
  391. };
  392. struct Slot {
  393. Connection conn;
  394. List<Connection>::Element *cE;
  395. };
  396. MethodInfo user;
  397. VMap<Target, Slot> slot_map;
  398. int lock;
  399. Signal() { lock = 0; }
  400. };
  401. HashMap<StringName, Signal, StringNameHasher> signal_map;
  402. List<Connection> connections;
  403. #ifdef DEBUG_ENABLED
  404. SafeRefCount _lock_index;
  405. #endif
  406. bool _block_signals;
  407. int _predelete_ok;
  408. Set<Object *> change_receptors;
  409. ObjectID _instance_ID;
  410. bool _predelete();
  411. void _postinitialize();
  412. bool _can_translate;
  413. #ifdef TOOLS_ENABLED
  414. bool _edited;
  415. uint32_t _edited_version;
  416. Set<String> editor_section_folding;
  417. #endif
  418. ScriptInstance *script_instance;
  419. RefPtr script;
  420. Dictionary metadata;
  421. mutable StringName _class_name;
  422. mutable const StringName *_class_ptr;
  423. void _add_user_signal(const String &p_name, const Array &p_args = Array());
  424. bool _has_user_signal(const StringName &p_name) const;
  425. Variant _emit_signal(const Variant **p_args, int p_argcount, Variant::CallError &r_error);
  426. Array _get_signal_list() const;
  427. Array _get_signal_connection_list(const String &p_signal) const;
  428. Array _get_incoming_connections() const;
  429. void _set_bind(const String &p_set, const Variant &p_value);
  430. Variant _get_bind(const String &p_name) const;
  431. void *_script_instance_bindings[MAX_SCRIPT_INSTANCE_BINDINGS];
  432. void property_list_changed_notify();
  433. protected:
  434. virtual void _initialize_classv() { initialize_class(); }
  435. virtual bool _setv(const StringName &p_name, const Variant &p_property) { return false; };
  436. virtual bool _getv(const StringName &p_name, Variant &r_property) const { return false; };
  437. virtual void _get_property_listv(List<PropertyInfo> *p_list, bool p_reversed) const {};
  438. virtual void _notificationv(int p_notification, bool p_reversed){};
  439. static String _get_category() { return ""; }
  440. static void _bind_methods();
  441. bool _set(const StringName &p_name, const Variant &p_property) { return false; };
  442. bool _get(const StringName &p_name, Variant &r_property) const { return false; };
  443. void _get_property_list(List<PropertyInfo> *p_list) const {};
  444. void _notification(int p_notification){};
  445. _FORCE_INLINE_ static void (*_get_bind_methods())() {
  446. return &Object::_bind_methods;
  447. }
  448. _FORCE_INLINE_ bool (Object::*(_get_get() const))(const StringName &p_name, Variant &r_ret) const {
  449. return &Object::_get;
  450. }
  451. _FORCE_INLINE_ bool (Object::*(_get_set() const))(const StringName &p_name, const Variant &p_property) {
  452. return &Object::_set;
  453. }
  454. _FORCE_INLINE_ void (Object::*(_get_get_property_list() const))(List<PropertyInfo> *p_list) const {
  455. return &Object::_get_property_list;
  456. }
  457. _FORCE_INLINE_ void (Object::*(_get_notification() const))(int) {
  458. return &Object::_notification;
  459. }
  460. static void get_valid_parents_static(List<String> *p_parents);
  461. static void _get_valid_parents_static(List<String> *p_parents);
  462. void cancel_delete();
  463. virtual void _changed_callback(Object *p_changed, const char *p_prop);
  464. //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());
  465. //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());
  466. Variant _call_bind(const Variant **p_args, int p_argcount, Variant::CallError &r_error);
  467. Variant _call_deferred_bind(const Variant **p_args, int p_argcount, Variant::CallError &r_error);
  468. virtual const StringName *_get_class_namev() const {
  469. if (!_class_name)
  470. _class_name = get_class_static();
  471. return &_class_name;
  472. }
  473. PoolVector<String> _get_meta_list_bind() const;
  474. Array _get_property_list_bind() const;
  475. Array _get_method_list_bind() const;
  476. void _clear_internal_resource_paths(const Variant &p_var);
  477. friend class ClassDB;
  478. virtual void _validate_property(PropertyInfo &property) const;
  479. public: //should be protected, but bug in clang++
  480. static void initialize_class();
  481. _FORCE_INLINE_ static void register_custom_data_to_otdb(){};
  482. public:
  483. #ifdef TOOLS_ENABLED
  484. _FORCE_INLINE_ void _change_notify(const char *p_property = "") {
  485. _edited = true;
  486. for (Set<Object *>::Element *E = change_receptors.front(); E; E = E->next())
  487. ((Object *)(E->get()))->_changed_callback(this, p_property);
  488. }
  489. #else
  490. _FORCE_INLINE_ void _change_notify(const char *p_what = "") {}
  491. #endif
  492. static void *get_class_ptr_static() {
  493. static int ptr;
  494. return &ptr;
  495. }
  496. bool _is_gpl_reversed() const { return false; }
  497. _FORCE_INLINE_ ObjectID get_instance_id() const { return _instance_ID; }
  498. // this is used for editors
  499. void add_change_receptor(Object *p_receptor);
  500. void remove_change_receptor(Object *p_receptor);
  501. template <class T>
  502. static T *cast_to(Object *p_object) {
  503. #ifndef NO_SAFE_CAST
  504. return dynamic_cast<T *>(p_object);
  505. #else
  506. if (!p_object)
  507. return NULL;
  508. if (p_object->is_class_ptr(T::get_class_ptr_static()))
  509. return static_cast<T *>(p_object);
  510. else
  511. return NULL;
  512. #endif
  513. }
  514. template <class T>
  515. static const T *cast_to(const Object *p_object) {
  516. #ifndef NO_SAFE_CAST
  517. return dynamic_cast<const T *>(p_object);
  518. #else
  519. if (!p_object)
  520. return NULL;
  521. if (p_object->is_class_ptr(T::get_class_ptr_static()))
  522. return static_cast<const T *>(p_object);
  523. else
  524. return NULL;
  525. #endif
  526. }
  527. enum {
  528. NOTIFICATION_POSTINITIALIZE = 0,
  529. NOTIFICATION_PREDELETE = 1
  530. };
  531. /* TYPE API */
  532. static void get_inheritance_list_static(List<String> *p_inheritance_list) { p_inheritance_list->push_back("Object"); }
  533. static String get_class_static() { return "Object"; }
  534. static String get_parent_class_static() { return String(); }
  535. static String get_category_static() { return String(); }
  536. virtual String get_class() const { return "Object"; }
  537. virtual String get_save_class() const { return get_class(); } //class stored when saving
  538. virtual bool is_class(const String &p_class) const { return (p_class == "Object"); }
  539. virtual bool is_class_ptr(void *p_ptr) const { return get_class_ptr_static() == p_ptr; }
  540. _FORCE_INLINE_ const StringName &get_class_name() const {
  541. if (!_class_ptr) {
  542. return *_get_class_namev();
  543. } else {
  544. return *_class_ptr;
  545. }
  546. }
  547. /* IAPI */
  548. //void set(const String& p_name, const Variant& p_value);
  549. //Variant get(const String& p_name) const;
  550. void set(const StringName &p_name, const Variant &p_value, bool *r_valid = NULL);
  551. Variant get(const StringName &p_name, bool *r_valid = NULL) const;
  552. void get_property_list(List<PropertyInfo> *p_list, bool p_reversed = false) const;
  553. bool has_method(const StringName &p_method) const;
  554. void get_method_list(List<MethodInfo> *p_list) const;
  555. Variant callv(const StringName &p_method, const Array &p_args);
  556. virtual Variant call(const StringName &p_method, const Variant **p_args, int p_argcount, Variant::CallError &r_error);
  557. virtual void call_multilevel(const StringName &p_method, const Variant **p_args, int p_argcount);
  558. virtual void call_multilevel_reversed(const StringName &p_method, const Variant **p_args, int p_argcount);
  559. Variant call(const StringName &p_name, VARIANT_ARG_LIST); // C++ helper
  560. void call_multilevel(const StringName &p_name, VARIANT_ARG_LIST); // C++ helper
  561. void notification(int p_notification, bool p_reversed = false);
  562. //used mainly by script, get and set all INCLUDING string
  563. virtual Variant getvar(const Variant &p_key, bool *r_valid = NULL) const;
  564. virtual void setvar(const Variant &p_key, const Variant &p_value, bool *r_valid = NULL);
  565. /* SCRIPT */
  566. void set_script(const RefPtr &p_script);
  567. RefPtr get_script() const;
  568. /* SCRIPT */
  569. bool has_meta(const String &p_name) const;
  570. void set_meta(const String &p_name, const Variant &p_value);
  571. Variant get_meta(const String &p_name) const;
  572. void get_meta_list(List<String> *p_list) const;
  573. #ifdef TOOLS_ENABLED
  574. void set_edited(bool p_edited);
  575. bool is_edited() const;
  576. 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
  577. #endif
  578. void set_script_instance(ScriptInstance *p_instance);
  579. _FORCE_INLINE_ ScriptInstance *get_script_instance() const { return script_instance; }
  580. void set_script_and_instance(const RefPtr &p_script, ScriptInstance *p_instance); //some script languages can't control instance creation, so this function eases the process
  581. void add_user_signal(const MethodInfo &p_signal);
  582. Error emit_signal(const StringName &p_name, VARIANT_ARG_LIST);
  583. Error emit_signal(const StringName &p_name, const Variant **p_args, int p_argcount);
  584. void get_signal_list(List<MethodInfo> *p_signals) const;
  585. void get_signal_connection_list(const StringName &p_signal, List<Connection> *p_connections) const;
  586. void get_all_signal_connections(List<Connection> *p_connections) const;
  587. bool has_persistent_signal_connections() const;
  588. void get_signals_connected_to_this(List<Connection> *p_connections) const;
  589. 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);
  590. void disconnect(const StringName &p_signal, Object *p_to_object, const StringName &p_to_method);
  591. bool is_connected(const StringName &p_signal, Object *p_to_object, const StringName &p_to_method) const;
  592. void call_deferred(const StringName &p_method, VARIANT_ARG_LIST);
  593. void set_block_signals(bool p_block);
  594. bool is_blocking_signals() const;
  595. Variant::Type get_static_property_type(const StringName &p_property, bool *r_valid = NULL) const;
  596. virtual void get_translatable_strings(List<String> *p_strings) const;
  597. virtual void get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const;
  598. StringName tr(const StringName &p_message) const; // translate message (internationalization)
  599. bool _is_queued_for_deletion; // set to true by SceneTree::queue_delete()
  600. bool is_queued_for_deletion() const;
  601. _FORCE_INLINE_ void set_message_translation(bool p_enable) { _can_translate = p_enable; }
  602. _FORCE_INLINE_ bool can_translate_messages() const { return _can_translate; }
  603. #ifdef TOOLS_ENABLED
  604. void editor_set_section_unfold(const String &p_section, bool p_unfolded);
  605. bool editor_is_section_unfolded(const String &p_section);
  606. #endif
  607. //used by script languages to store binding data
  608. void *get_script_instance_binding(int p_script_language_index);
  609. void clear_internal_resource_paths();
  610. Object();
  611. virtual ~Object();
  612. };
  613. bool predelete_handler(Object *p_object);
  614. void postinitialize_handler(Object *p_object);
  615. class ObjectDB {
  616. struct ObjectPtrHash {
  617. static _FORCE_INLINE_ uint32_t hash(const Object *p_obj) {
  618. union {
  619. const Object *p;
  620. unsigned long i;
  621. } u;
  622. u.p = p_obj;
  623. return HashMapHasherDefault::hash((uint64_t)u.i);
  624. }
  625. };
  626. static HashMap<ObjectID, Object *> instances;
  627. static HashMap<Object *, ObjectID, ObjectPtrHash> instance_checks;
  628. static ObjectID instance_counter;
  629. friend class Object;
  630. friend void unregister_core_types();
  631. static RWLock *rw_lock;
  632. static void cleanup();
  633. static ObjectID add_instance(Object *p_object);
  634. static void remove_instance(Object *p_object);
  635. friend void register_core_types();
  636. static void setup();
  637. public:
  638. typedef void (*DebugFunc)(Object *p_obj);
  639. static Object *get_instance(ObjectID p_instance_ID);
  640. static void debug_objects(DebugFunc p_func);
  641. static int get_object_count();
  642. #ifdef DEBUG_ENABLED
  643. _FORCE_INLINE_ static bool instance_validate(Object *p_ptr) {
  644. return instance_checks.has(p_ptr);
  645. }
  646. #else
  647. _FORCE_INLINE_ static bool instance_validate(Object *p_ptr) { return true; }
  648. #endif
  649. };
  650. //needed by macros
  651. #include "class_db.h"
  652. #endif