object.h 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  1. /*************************************************************************/
  2. /* object.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 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. // When updating PropertyHint, also sync the hardcoded list in VisualScriptEditorVariableEdit
  85. };
  86. enum PropertyUsageFlags {
  87. PROPERTY_USAGE_STORAGE = 1,
  88. PROPERTY_USAGE_EDITOR = 2,
  89. PROPERTY_USAGE_NETWORK = 4,
  90. PROPERTY_USAGE_EDITOR_HELPER = 8,
  91. PROPERTY_USAGE_CHECKABLE = 16, //used for editing global variables
  92. PROPERTY_USAGE_CHECKED = 32, //used for editing global variables
  93. PROPERTY_USAGE_INTERNATIONALIZED = 64, //hint for internationalized strings
  94. PROPERTY_USAGE_GROUP = 128, //used for grouping props in the editor
  95. PROPERTY_USAGE_CATEGORY = 256,
  96. PROPERTY_USAGE_STORE_IF_NONZERO = 512, //only store if nonzero
  97. PROPERTY_USAGE_STORE_IF_NONONE = 1024, //only store if false
  98. PROPERTY_USAGE_NO_INSTANCE_STATE = 2048,
  99. PROPERTY_USAGE_RESTART_IF_CHANGED = 4096,
  100. PROPERTY_USAGE_SCRIPT_VARIABLE = 8192,
  101. PROPERTY_USAGE_STORE_IF_NULL = 16384,
  102. PROPERTY_USAGE_ANIMATE_AS_TRIGGER = 32768,
  103. PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED = 65536,
  104. PROPERTY_USAGE_SCRIPT_DEFAULT_VALUE = 1 << 17,
  105. PROPERTY_USAGE_CLASS_IS_ENUM = 1 << 18,
  106. PROPERTY_USAGE_NIL_IS_VARIANT = 1 << 19,
  107. PROPERTY_USAGE_INTERNAL = 1 << 20,
  108. PROPERTY_USAGE_DO_NOT_SHARE_ON_DUPLICATE = 1 << 21, // If the object is duplicated also this property will be duplicated
  109. PROPERTY_USAGE_DEFAULT = PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_NETWORK,
  110. PROPERTY_USAGE_DEFAULT_INTL = PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_NETWORK | PROPERTY_USAGE_INTERNATIONALIZED,
  111. PROPERTY_USAGE_NOEDITOR = PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_NETWORK,
  112. };
  113. #define ADD_SIGNAL(m_signal) ClassDB::add_signal(get_class_static(), m_signal)
  114. #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))
  115. #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)
  116. #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))
  117. #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)
  118. #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))
  119. #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)
  120. #define ADD_GROUP(m_name, m_prefix) ClassDB::add_property_group(get_class_static(), m_name, m_prefix)
  121. struct PropertyInfo {
  122. Variant::Type type;
  123. String name;
  124. StringName class_name; //for classes
  125. PropertyHint hint;
  126. String hint_string;
  127. uint32_t usage;
  128. _FORCE_INLINE_ PropertyInfo added_usage(int p_fl) const {
  129. PropertyInfo pi = *this;
  130. pi.usage |= p_fl;
  131. return pi;
  132. }
  133. operator Dictionary() const;
  134. static PropertyInfo from_dict(const Dictionary &p_dict);
  135. PropertyInfo() :
  136. type(Variant::NIL),
  137. hint(PROPERTY_HINT_NONE),
  138. usage(PROPERTY_USAGE_DEFAULT) {
  139. }
  140. 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()) :
  141. type(p_type),
  142. name(p_name),
  143. hint(p_hint),
  144. hint_string(p_hint_string),
  145. usage(p_usage) {
  146. if (hint == PROPERTY_HINT_RESOURCE_TYPE) {
  147. class_name = hint_string;
  148. } else {
  149. class_name = p_class_name;
  150. }
  151. }
  152. PropertyInfo(const StringName &p_class_name) :
  153. type(Variant::OBJECT),
  154. class_name(p_class_name),
  155. hint(PROPERTY_HINT_NONE),
  156. usage(PROPERTY_USAGE_DEFAULT) {
  157. }
  158. bool operator<(const PropertyInfo &p_info) const {
  159. return name < p_info.name;
  160. }
  161. };
  162. Array convert_property_list(const List<PropertyInfo> *p_list);
  163. struct MethodInfo {
  164. String name;
  165. List<PropertyInfo> arguments;
  166. Vector<Variant> default_arguments;
  167. PropertyInfo return_val;
  168. uint32_t flags;
  169. int id;
  170. inline bool operator==(const MethodInfo &p_method) const { return id == p_method.id; }
  171. inline bool operator<(const MethodInfo &p_method) const { return id == p_method.id ? (name < p_method.name) : (id < p_method.id); }
  172. operator Dictionary() const;
  173. static MethodInfo from_dict(const Dictionary &p_dict);
  174. MethodInfo();
  175. MethodInfo(const String &p_name);
  176. MethodInfo(const String &p_name, const PropertyInfo &p_param1);
  177. MethodInfo(const String &p_name, const PropertyInfo &p_param1, const PropertyInfo &p_param2);
  178. MethodInfo(const String &p_name, const PropertyInfo &p_param1, const PropertyInfo &p_param2, const PropertyInfo &p_param3);
  179. MethodInfo(const String &p_name, const PropertyInfo &p_param1, const PropertyInfo &p_param2, const PropertyInfo &p_param3, const PropertyInfo &p_param4);
  180. 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);
  181. MethodInfo(Variant::Type ret);
  182. MethodInfo(Variant::Type ret, const String &p_name);
  183. MethodInfo(Variant::Type ret, const String &p_name, const PropertyInfo &p_param1);
  184. MethodInfo(Variant::Type ret, const String &p_name, const PropertyInfo &p_param1, const PropertyInfo &p_param2);
  185. MethodInfo(Variant::Type ret, const String &p_name, const PropertyInfo &p_param1, const PropertyInfo &p_param2, const PropertyInfo &p_param3);
  186. MethodInfo(Variant::Type ret, const String &p_name, const PropertyInfo &p_param1, const PropertyInfo &p_param2, const PropertyInfo &p_param3, const PropertyInfo &p_param4);
  187. 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);
  188. MethodInfo(const PropertyInfo &p_ret, const String &p_name);
  189. MethodInfo(const PropertyInfo &p_ret, const String &p_name, const PropertyInfo &p_param1);
  190. MethodInfo(const PropertyInfo &p_ret, const String &p_name, const PropertyInfo &p_param1, const PropertyInfo &p_param2);
  191. MethodInfo(const PropertyInfo &p_ret, const String &p_name, const PropertyInfo &p_param1, const PropertyInfo &p_param2, const PropertyInfo &p_param3);
  192. 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);
  193. 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);
  194. };
  195. // old cast_to
  196. //if ( is_type(T::get_class_static()) )
  197. //return static_cast<T*>(this);
  198. ////else
  199. //return NULL;
  200. /*
  201. 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.
  202. */
  203. #define REVERSE_GET_PROPERTY_LIST \
  204. public: \
  205. _FORCE_INLINE_ bool _is_gpl_reversed() const { return true; }; \
  206. \
  207. private:
  208. #define UNREVERSE_GET_PROPERTY_LIST \
  209. public: \
  210. _FORCE_INLINE_ bool _is_gpl_reversed() const { return false; }; \
  211. \
  212. private:
  213. #define GDCLASS(m_class, m_inherits) \
  214. private: \
  215. void operator=(const m_class &p_rval) {} \
  216. mutable StringName _class_name; \
  217. friend class ClassDB; \
  218. \
  219. public: \
  220. virtual String get_class() const { \
  221. return String(#m_class); \
  222. } \
  223. virtual const StringName *_get_class_namev() const { \
  224. if (!_class_name) \
  225. _class_name = get_class_static(); \
  226. return &_class_name; \
  227. } \
  228. static _FORCE_INLINE_ void *get_class_ptr_static() { \
  229. static int ptr; \
  230. return &ptr; \
  231. } \
  232. static _FORCE_INLINE_ String get_class_static() { \
  233. return String(#m_class); \
  234. } \
  235. static _FORCE_INLINE_ String get_parent_class_static() { \
  236. return m_inherits::get_class_static(); \
  237. } \
  238. static void get_inheritance_list_static(List<String> *p_inheritance_list) { \
  239. m_inherits::get_inheritance_list_static(p_inheritance_list); \
  240. p_inheritance_list->push_back(String(#m_class)); \
  241. } \
  242. static String get_category_static() { \
  243. String category = m_inherits::get_category_static(); \
  244. if (_get_category != m_inherits::_get_category) { \
  245. if (category != "") \
  246. category += "/"; \
  247. category += _get_category(); \
  248. } \
  249. return category; \
  250. } \
  251. static String inherits_static() { \
  252. return String(#m_inherits); \
  253. } \
  254. virtual bool is_class(const String &p_class) const { return (p_class == (#m_class)) ? true : m_inherits::is_class(p_class); } \
  255. virtual bool is_class_ptr(void *p_ptr) const { return (p_ptr == get_class_ptr_static()) ? true : m_inherits::is_class_ptr(p_ptr); } \
  256. \
  257. static void get_valid_parents_static(List<String> *p_parents) { \
  258. \
  259. if (m_class::_get_valid_parents_static != m_inherits::_get_valid_parents_static) { \
  260. m_class::_get_valid_parents_static(p_parents); \
  261. } \
  262. \
  263. m_inherits::get_valid_parents_static(p_parents); \
  264. } \
  265. \
  266. protected: \
  267. _FORCE_INLINE_ static void (*_get_bind_methods())() { \
  268. return &m_class::_bind_methods; \
  269. } \
  270. \
  271. public: \
  272. static void initialize_class() { \
  273. static bool initialized = false; \
  274. if (initialized) \
  275. return; \
  276. m_inherits::initialize_class(); \
  277. ClassDB::_add_class<m_class>(); \
  278. if (m_class::_get_bind_methods() != m_inherits::_get_bind_methods()) \
  279. _bind_methods(); \
  280. initialized = true; \
  281. } \
  282. \
  283. protected: \
  284. virtual void _initialize_classv() { \
  285. initialize_class(); \
  286. } \
  287. _FORCE_INLINE_ bool (Object::*(_get_get() const))(const StringName &p_name, Variant &) const { \
  288. return (bool (Object::*)(const StringName &, Variant &) const) & m_class::_get; \
  289. } \
  290. virtual bool _getv(const StringName &p_name, Variant &r_ret) const { \
  291. if (m_class::_get_get() != m_inherits::_get_get()) { \
  292. if (_get(p_name, r_ret)) \
  293. return true; \
  294. } \
  295. return m_inherits::_getv(p_name, r_ret); \
  296. } \
  297. _FORCE_INLINE_ bool (Object::*(_get_set() const))(const StringName &p_name, const Variant &p_property) { \
  298. return (bool (Object::*)(const StringName &, const Variant &)) & m_class::_set; \
  299. } \
  300. virtual bool _setv(const StringName &p_name, const Variant &p_property) { \
  301. if (m_inherits::_setv(p_name, p_property)) return true; \
  302. if (m_class::_get_set() != m_inherits::_get_set()) { \
  303. return _set(p_name, p_property); \
  304. } \
  305. return false; \
  306. } \
  307. _FORCE_INLINE_ void (Object::*(_get_get_property_list() const))(List<PropertyInfo> * p_list) const { \
  308. return (void (Object::*)(List<PropertyInfo> *) const) & m_class::_get_property_list; \
  309. } \
  310. virtual void _get_property_listv(List<PropertyInfo> *p_list, bool p_reversed) const { \
  311. if (!p_reversed) { \
  312. m_inherits::_get_property_listv(p_list, p_reversed); \
  313. } \
  314. p_list->push_back(PropertyInfo(Variant::NIL, get_class_static(), PROPERTY_HINT_NONE, String(), PROPERTY_USAGE_CATEGORY)); \
  315. if (!_is_gpl_reversed()) \
  316. ClassDB::get_property_list(#m_class, p_list, true, this); \
  317. if (m_class::_get_get_property_list() != m_inherits::_get_get_property_list()) { \
  318. _get_property_list(p_list); \
  319. } \
  320. if (_is_gpl_reversed()) \
  321. ClassDB::get_property_list(#m_class, p_list, true, this); \
  322. if (p_reversed) { \
  323. m_inherits::_get_property_listv(p_list, p_reversed); \
  324. } \
  325. } \
  326. _FORCE_INLINE_ void (Object::*(_get_notification() const))(int) { \
  327. return (void (Object::*)(int)) & m_class::_notification; \
  328. } \
  329. virtual void _notificationv(int p_notification, bool p_reversed) { \
  330. if (!p_reversed) \
  331. m_inherits::_notificationv(p_notification, p_reversed); \
  332. if (m_class::_get_notification() != m_inherits::_get_notification()) { \
  333. _notification(p_notification); \
  334. } \
  335. if (p_reversed) \
  336. m_inherits::_notificationv(p_notification, p_reversed); \
  337. } \
  338. \
  339. private:
  340. #define OBJ_CATEGORY(m_category) \
  341. protected: \
  342. _FORCE_INLINE_ static String _get_category() { return m_category; } \
  343. \
  344. private:
  345. #define OBJ_SAVE_TYPE(m_class) \
  346. public: \
  347. virtual String get_save_class() const { return #m_class; } \
  348. \
  349. private:
  350. class ScriptInstance;
  351. typedef uint64_t ObjectID;
  352. class Object {
  353. public:
  354. enum ConnectFlags {
  355. CONNECT_DEFERRED = 1,
  356. CONNECT_PERSIST = 2, // hint for scene to save this connection
  357. CONNECT_ONESHOT = 4
  358. };
  359. struct Connection {
  360. Object *source;
  361. StringName signal;
  362. Object *target;
  363. StringName method;
  364. uint32_t flags;
  365. Vector<Variant> binds;
  366. bool operator<(const Connection &p_conn) const;
  367. operator Variant() const;
  368. Connection() {
  369. source = NULL;
  370. target = NULL;
  371. flags = 0;
  372. }
  373. Connection(const Variant &p_variant);
  374. };
  375. private:
  376. enum {
  377. MAX_SCRIPT_INSTANCE_BINDINGS = 8
  378. };
  379. #ifdef DEBUG_ENABLED
  380. friend class _ObjectDebugLock;
  381. #endif
  382. friend bool predelete_handler(Object *);
  383. friend void postinitialize_handler(Object *);
  384. struct Signal {
  385. struct Target {
  386. ObjectID _id;
  387. StringName method;
  388. _FORCE_INLINE_ bool operator<(const Target &p_target) const { return (_id == p_target._id) ? (method < p_target.method) : (_id < p_target._id); }
  389. Target(const ObjectID &p_id, const StringName &p_method) :
  390. _id(p_id),
  391. method(p_method) {
  392. }
  393. Target() { _id = 0; }
  394. };
  395. struct Slot {
  396. Connection conn;
  397. List<Connection>::Element *cE;
  398. };
  399. MethodInfo user;
  400. VMap<Target, Slot> slot_map;
  401. int lock;
  402. Signal() { lock = 0; }
  403. };
  404. HashMap<StringName, Signal, StringNameHasher> signal_map;
  405. List<Connection> connections;
  406. #ifdef DEBUG_ENABLED
  407. SafeRefCount _lock_index;
  408. #endif
  409. bool _block_signals;
  410. int _predelete_ok;
  411. Set<Object *> change_receptors;
  412. ObjectID _instance_ID;
  413. bool _predelete();
  414. void _postinitialize();
  415. bool _can_translate;
  416. #ifdef TOOLS_ENABLED
  417. bool _edited;
  418. uint32_t _edited_version;
  419. Set<String> editor_section_folding;
  420. #endif
  421. ScriptInstance *script_instance;
  422. RefPtr script;
  423. Dictionary metadata;
  424. mutable StringName _class_name;
  425. mutable const StringName *_class_ptr;
  426. void _add_user_signal(const String &p_name, const Array &p_args = Array());
  427. bool _has_user_signal(const StringName &p_name) const;
  428. Variant _emit_signal(const Variant **p_args, int p_argcount, Variant::CallError &r_error);
  429. Array _get_signal_list() const;
  430. Array _get_signal_connection_list(const String &p_signal) const;
  431. Array _get_incoming_connections() const;
  432. void _set_bind(const String &p_set, const Variant &p_value);
  433. Variant _get_bind(const String &p_name) const;
  434. void _set_indexed_bind(const NodePath &p_name, const Variant &p_value);
  435. Variant _get_indexed_bind(const NodePath &p_name) const;
  436. void *_script_instance_bindings[MAX_SCRIPT_INSTANCE_BINDINGS];
  437. void property_list_changed_notify();
  438. protected:
  439. virtual void _initialize_classv() { initialize_class(); }
  440. virtual bool _setv(const StringName &p_name, const Variant &p_property) { return false; };
  441. virtual bool _getv(const StringName &p_name, Variant &r_property) const { return false; };
  442. virtual void _get_property_listv(List<PropertyInfo> *p_list, bool p_reversed) const {};
  443. virtual void _notificationv(int p_notification, bool p_reversed){};
  444. static String _get_category() { return ""; }
  445. static void _bind_methods();
  446. bool _set(const StringName &p_name, const Variant &p_property) { return false; };
  447. bool _get(const StringName &p_name, Variant &r_property) const { return false; };
  448. void _get_property_list(List<PropertyInfo> *p_list) const {};
  449. void _notification(int p_notification){};
  450. _FORCE_INLINE_ static void (*_get_bind_methods())() {
  451. return &Object::_bind_methods;
  452. }
  453. _FORCE_INLINE_ bool (Object::*(_get_get() const))(const StringName &p_name, Variant &r_ret) const {
  454. return &Object::_get;
  455. }
  456. _FORCE_INLINE_ bool (Object::*(_get_set() const))(const StringName &p_name, const Variant &p_property) {
  457. return &Object::_set;
  458. }
  459. _FORCE_INLINE_ void (Object::*(_get_get_property_list() const))(List<PropertyInfo> *p_list) const {
  460. return &Object::_get_property_list;
  461. }
  462. _FORCE_INLINE_ void (Object::*(_get_notification() const))(int) {
  463. return &Object::_notification;
  464. }
  465. static void get_valid_parents_static(List<String> *p_parents);
  466. static void _get_valid_parents_static(List<String> *p_parents);
  467. void cancel_delete();
  468. virtual void _changed_callback(Object *p_changed, const char *p_prop);
  469. //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());
  470. //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());
  471. Variant _call_bind(const Variant **p_args, int p_argcount, Variant::CallError &r_error);
  472. Variant _call_deferred_bind(const Variant **p_args, int p_argcount, Variant::CallError &r_error);
  473. virtual const StringName *_get_class_namev() const {
  474. if (!_class_name)
  475. _class_name = get_class_static();
  476. return &_class_name;
  477. }
  478. PoolVector<String> _get_meta_list_bind() const;
  479. Array _get_property_list_bind() const;
  480. Array _get_method_list_bind() const;
  481. void _clear_internal_resource_paths(const Variant &p_var);
  482. friend class ClassDB;
  483. virtual void _validate_property(PropertyInfo &property) const;
  484. public: //should be protected, but bug in clang++
  485. static void initialize_class();
  486. _FORCE_INLINE_ static void register_custom_data_to_otdb(){};
  487. public:
  488. #ifdef TOOLS_ENABLED
  489. _FORCE_INLINE_ void _change_notify(const char *p_property = "") {
  490. _edited = true;
  491. for (Set<Object *>::Element *E = change_receptors.front(); E; E = E->next())
  492. ((Object *)(E->get()))->_changed_callback(this, p_property);
  493. }
  494. #else
  495. _FORCE_INLINE_ void _change_notify(const char *p_what = "") {}
  496. #endif
  497. static void *get_class_ptr_static() {
  498. static int ptr;
  499. return &ptr;
  500. }
  501. bool _is_gpl_reversed() const { return false; }
  502. _FORCE_INLINE_ ObjectID get_instance_id() const { return _instance_ID; }
  503. // this is used for editors
  504. void add_change_receptor(Object *p_receptor);
  505. void remove_change_receptor(Object *p_receptor);
  506. template <class T>
  507. static T *cast_to(Object *p_object) {
  508. #ifndef NO_SAFE_CAST
  509. return dynamic_cast<T *>(p_object);
  510. #else
  511. if (!p_object)
  512. return NULL;
  513. if (p_object->is_class_ptr(T::get_class_ptr_static()))
  514. return static_cast<T *>(p_object);
  515. else
  516. return NULL;
  517. #endif
  518. }
  519. template <class T>
  520. static const T *cast_to(const Object *p_object) {
  521. #ifndef NO_SAFE_CAST
  522. return dynamic_cast<const T *>(p_object);
  523. #else
  524. if (!p_object)
  525. return NULL;
  526. if (p_object->is_class_ptr(T::get_class_ptr_static()))
  527. return static_cast<const T *>(p_object);
  528. else
  529. return NULL;
  530. #endif
  531. }
  532. enum {
  533. NOTIFICATION_POSTINITIALIZE = 0,
  534. NOTIFICATION_PREDELETE = 1
  535. };
  536. /* TYPE API */
  537. static void get_inheritance_list_static(List<String> *p_inheritance_list) { p_inheritance_list->push_back("Object"); }
  538. static String get_class_static() { return "Object"; }
  539. static String get_parent_class_static() { return String(); }
  540. static String get_category_static() { return String(); }
  541. virtual String get_class() const { return "Object"; }
  542. virtual String get_save_class() const { return get_class(); } //class stored when saving
  543. virtual bool is_class(const String &p_class) const { return (p_class == "Object"); }
  544. virtual bool is_class_ptr(void *p_ptr) const { return get_class_ptr_static() == p_ptr; }
  545. _FORCE_INLINE_ const StringName &get_class_name() const {
  546. if (!_class_ptr) {
  547. return *_get_class_namev();
  548. } else {
  549. return *_class_ptr;
  550. }
  551. }
  552. /* IAPI */
  553. //void set(const String& p_name, const Variant& p_value);
  554. //Variant get(const String& p_name) const;
  555. void set(const StringName &p_name, const Variant &p_value, bool *r_valid = NULL);
  556. Variant get(const StringName &p_name, bool *r_valid = NULL) const;
  557. void set_indexed(const Vector<StringName> &p_names, const Variant &p_value, bool *r_valid = NULL);
  558. Variant get_indexed(const Vector<StringName> &p_names, bool *r_valid = NULL) const;
  559. void get_property_list(List<PropertyInfo> *p_list, bool p_reversed = false) const;
  560. bool has_method(const StringName &p_method) const;
  561. void get_method_list(List<MethodInfo> *p_list) const;
  562. Variant callv(const StringName &p_method, const Array &p_args);
  563. virtual Variant call(const StringName &p_method, const Variant **p_args, int p_argcount, Variant::CallError &r_error);
  564. virtual void call_multilevel(const StringName &p_method, const Variant **p_args, int p_argcount);
  565. virtual void call_multilevel_reversed(const StringName &p_method, const Variant **p_args, int p_argcount);
  566. Variant call(const StringName &p_name, VARIANT_ARG_LIST); // C++ helper
  567. void call_multilevel(const StringName &p_name, VARIANT_ARG_LIST); // C++ helper
  568. void notification(int p_notification, bool p_reversed = false);
  569. //used mainly by script, get and set all INCLUDING string
  570. virtual Variant getvar(const Variant &p_key, bool *r_valid = NULL) const;
  571. virtual void setvar(const Variant &p_key, const Variant &p_value, bool *r_valid = NULL);
  572. /* SCRIPT */
  573. void set_script(const RefPtr &p_script);
  574. RefPtr get_script() const;
  575. /* SCRIPT */
  576. bool has_meta(const String &p_name) const;
  577. void set_meta(const String &p_name, const Variant &p_value);
  578. Variant get_meta(const String &p_name) const;
  579. void get_meta_list(List<String> *p_list) const;
  580. #ifdef TOOLS_ENABLED
  581. void set_edited(bool p_edited);
  582. bool is_edited() const;
  583. 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
  584. #endif
  585. void set_script_instance(ScriptInstance *p_instance);
  586. _FORCE_INLINE_ ScriptInstance *get_script_instance() const { return script_instance; }
  587. 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
  588. void add_user_signal(const MethodInfo &p_signal);
  589. Error emit_signal(const StringName &p_name, VARIANT_ARG_LIST);
  590. Error emit_signal(const StringName &p_name, const Variant **p_args, int p_argcount);
  591. void get_signal_list(List<MethodInfo> *p_signals) const;
  592. void get_signal_connection_list(const StringName &p_signal, List<Connection> *p_connections) const;
  593. void get_all_signal_connections(List<Connection> *p_connections) const;
  594. bool has_persistent_signal_connections() const;
  595. void get_signals_connected_to_this(List<Connection> *p_connections) const;
  596. 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);
  597. void disconnect(const StringName &p_signal, Object *p_to_object, const StringName &p_to_method);
  598. bool is_connected(const StringName &p_signal, Object *p_to_object, const StringName &p_to_method) const;
  599. void call_deferred(const StringName &p_method, VARIANT_ARG_LIST);
  600. void set_block_signals(bool p_block);
  601. bool is_blocking_signals() const;
  602. Variant::Type get_static_property_type(const StringName &p_property, bool *r_valid = NULL) const;
  603. Variant::Type get_static_property_type_indexed(const Vector<StringName> &p_path, bool *r_valid = NULL) const;
  604. virtual void get_translatable_strings(List<String> *p_strings) const;
  605. virtual void get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const;
  606. StringName tr(const StringName &p_message) const; // translate message (internationalization)
  607. bool _is_queued_for_deletion; // set to true by SceneTree::queue_delete()
  608. bool is_queued_for_deletion() const;
  609. _FORCE_INLINE_ void set_message_translation(bool p_enable) { _can_translate = p_enable; }
  610. _FORCE_INLINE_ bool can_translate_messages() const { return _can_translate; }
  611. #ifdef TOOLS_ENABLED
  612. void editor_set_section_unfold(const String &p_section, bool p_unfolded);
  613. bool editor_is_section_unfolded(const String &p_section);
  614. #endif
  615. //used by script languages to store binding data
  616. void *get_script_instance_binding(int p_script_language_index);
  617. void clear_internal_resource_paths();
  618. Object();
  619. virtual ~Object();
  620. };
  621. bool predelete_handler(Object *p_object);
  622. void postinitialize_handler(Object *p_object);
  623. class ObjectDB {
  624. struct ObjectPtrHash {
  625. static _FORCE_INLINE_ uint32_t hash(const Object *p_obj) {
  626. union {
  627. const Object *p;
  628. unsigned long i;
  629. } u;
  630. u.p = p_obj;
  631. return HashMapHasherDefault::hash((uint64_t)u.i);
  632. }
  633. };
  634. static HashMap<ObjectID, Object *> instances;
  635. static HashMap<Object *, ObjectID, ObjectPtrHash> instance_checks;
  636. static ObjectID instance_counter;
  637. friend class Object;
  638. friend void unregister_core_types();
  639. static RWLock *rw_lock;
  640. static void cleanup();
  641. static ObjectID add_instance(Object *p_object);
  642. static void remove_instance(Object *p_object);
  643. friend void register_core_types();
  644. static void setup();
  645. public:
  646. typedef void (*DebugFunc)(Object *p_obj);
  647. static Object *get_instance(ObjectID p_instance_ID);
  648. static void debug_objects(DebugFunc p_func);
  649. static int get_object_count();
  650. #ifdef DEBUG_ENABLED
  651. _FORCE_INLINE_ static bool instance_validate(Object *p_ptr) {
  652. return instance_checks.has(p_ptr);
  653. }
  654. #else
  655. _FORCE_INLINE_ static bool instance_validate(Object *p_ptr) { return true; }
  656. #endif
  657. };
  658. //needed by macros
  659. #include "class_db.h"
  660. #endif