object.h 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  1. /**************************************************************************/
  2. /* object.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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 "core/hash_map.h"
  33. #include "core/list.h"
  34. #include "core/map.h"
  35. #include "core/object_id.h"
  36. #include "core/os/rw_lock.h"
  37. #include "core/safe_refcount.h"
  38. #include "core/set.h"
  39. #include "core/variant.h"
  40. #include "core/vmap.h"
  41. #include <atomic>
  42. #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(), const Variant &p_arg6 = Variant(), const Variant &p_arg7 = Variant(), const Variant &p_arg8 = Variant()
  43. #define VARIANT_ARG_PASS p_arg1, p_arg2, p_arg3, p_arg4, p_arg5, p_arg6, p_arg7, p_arg8
  44. #define VARIANT_ARG_DECLARE const Variant &p_arg1, const Variant &p_arg2, const Variant &p_arg3, const Variant &p_arg4, const Variant &p_arg5, const Variant &p_arg6, const Variant &p_arg7, const Variant &p_arg8
  45. #define VARIANT_ARG_MAX 8
  46. #define VARIANT_ARGPTRS const Variant *argptr[8] = { &p_arg1, &p_arg2, &p_arg3, &p_arg4, &p_arg5, &p_arg6, &p_arg7, &p_arg8 };
  47. #define VARIANT_ARGPTRS_PASS *argptr[0], *argptr[1], *argptr[2], *argptr[3], *argptr[4], *argptr[5], *argptr[6], *argptr[7]
  48. #define VARIANT_ARGS_FROM_ARRAY(m_arr) m_arr[0], m_arr[1], m_arr[2], m_arr[3], m_arr[4], m_arr[5], m_arr[6], m_arr[7]
  49. /**
  50. @author Juan Linietsky <reduzio@gmail.com>
  51. */
  52. enum PropertyHint {
  53. PROPERTY_HINT_NONE, ///< no hint provided.
  54. PROPERTY_HINT_RANGE, ///< hint_text = "min,max,step,slider; //slider is optional"
  55. PROPERTY_HINT_EXP_RANGE, ///< hint_text = "min,max,step", exponential edit
  56. PROPERTY_HINT_ENUM, ///< hint_text= "val1,val2,val3,etc"
  57. PROPERTY_HINT_EXP_EASING, /// exponential easing function (Math::ease) use "attenuation" hint string to revert (flip h), "full" to also include in/out. (ie: "attenuation,inout")
  58. PROPERTY_HINT_LENGTH, ///< hint_text= "length" (as integer)
  59. PROPERTY_HINT_LINK,
  60. PROPERTY_HINT_SPRITE_FRAME, // FIXME: Obsolete: drop whenever we can break compat. Keeping now for GDNative compat.
  61. PROPERTY_HINT_KEY_ACCEL, ///< hint_text= "length" (as integer)
  62. PROPERTY_HINT_FLAGS, ///< hint_text= "flag1,flag2,etc" (as bit flags)
  63. PROPERTY_HINT_LAYERS_2D_RENDER,
  64. PROPERTY_HINT_LAYERS_2D_PHYSICS,
  65. PROPERTY_HINT_LAYERS_2D_NAVIGATION,
  66. PROPERTY_HINT_LAYERS_3D_RENDER,
  67. PROPERTY_HINT_LAYERS_3D_PHYSICS,
  68. PROPERTY_HINT_LAYERS_3D_NAVIGATION,
  69. PROPERTY_HINT_FILE, ///< a file path must be passed, hint_text (optionally) is a filter "*.png,*.wav,*.doc,"
  70. PROPERTY_HINT_DIR, ///< a directory path must be passed
  71. PROPERTY_HINT_GLOBAL_FILE, ///< a file path must be passed, hint_text (optionally) is a filter "*.png,*.wav,*.doc,"
  72. PROPERTY_HINT_GLOBAL_DIR, ///< a directory path must be passed
  73. PROPERTY_HINT_RESOURCE_TYPE, ///< a resource object type
  74. PROPERTY_HINT_MULTILINE_TEXT, ///< used for string properties that can contain multiple lines
  75. PROPERTY_HINT_PLACEHOLDER_TEXT, ///< used to set a placeholder text for string properties
  76. PROPERTY_HINT_COLOR_NO_ALPHA, ///< used for ignoring alpha component when editing a color
  77. PROPERTY_HINT_IMAGE_COMPRESS_LOSSY,
  78. PROPERTY_HINT_IMAGE_COMPRESS_LOSSLESS,
  79. PROPERTY_HINT_OBJECT_ID,
  80. PROPERTY_HINT_TYPE_STRING, ///< a type string, the hint is the base type to choose
  81. PROPERTY_HINT_NODE_PATH_TO_EDITED_NODE, ///< so something else can provide this (used in scripts)
  82. PROPERTY_HINT_METHOD_OF_VARIANT_TYPE, ///< a method of a type
  83. PROPERTY_HINT_METHOD_OF_BASE_TYPE, ///< a method of a base type
  84. PROPERTY_HINT_METHOD_OF_INSTANCE, ///< a method of an instance
  85. PROPERTY_HINT_METHOD_OF_SCRIPT, ///< a method of a script & base
  86. PROPERTY_HINT_PROPERTY_OF_VARIANT_TYPE, ///< a property of a type
  87. PROPERTY_HINT_PROPERTY_OF_BASE_TYPE, ///< a property of a base type
  88. PROPERTY_HINT_PROPERTY_OF_INSTANCE, ///< a property of an instance
  89. PROPERTY_HINT_PROPERTY_OF_SCRIPT, ///< a property of a script & base
  90. PROPERTY_HINT_OBJECT_TOO_BIG, ///< object is too big to send
  91. PROPERTY_HINT_NODE_PATH_VALID_TYPES,
  92. PROPERTY_HINT_SAVE_FILE, ///< a file path must be passed, hint_text (optionally) is a filter "*.png,*.wav,*.doc,". This opens a save dialog
  93. PROPERTY_HINT_ENUM_SUGGESTION, ///< hint_text= "val1,val2,val3,etc"
  94. PROPERTY_HINT_LOCALE_ID,
  95. PROPERTY_HINT_MAX,
  96. // When updating PropertyHint, also sync the hardcoded list in VisualScriptEditorVariableEdit
  97. };
  98. enum PropertyUsageFlags {
  99. PROPERTY_USAGE_STORAGE = 1,
  100. PROPERTY_USAGE_EDITOR = 2,
  101. PROPERTY_USAGE_NETWORK = 4,
  102. PROPERTY_USAGE_EDITOR_HELPER = 8,
  103. PROPERTY_USAGE_CHECKABLE = 16, //used for editing global variables
  104. PROPERTY_USAGE_CHECKED = 32, //used for editing global variables
  105. PROPERTY_USAGE_INTERNATIONALIZED = 64, //hint for internationalized strings
  106. PROPERTY_USAGE_GROUP = 128, //used for grouping props in the editor
  107. PROPERTY_USAGE_CATEGORY = 256,
  108. // FIXME: Drop in 4.0, possibly reorder other flags?
  109. // Those below are deprecated thanks to ClassDB's now class value cache
  110. //PROPERTY_USAGE_STORE_IF_NONZERO = 512, //only store if nonzero
  111. //PROPERTY_USAGE_STORE_IF_NONONE = 1024, //only store if false
  112. PROPERTY_USAGE_NO_INSTANCE_STATE = 2048,
  113. PROPERTY_USAGE_RESTART_IF_CHANGED = 4096,
  114. PROPERTY_USAGE_SCRIPT_VARIABLE = 8192,
  115. PROPERTY_USAGE_STORE_IF_NULL = 16384,
  116. PROPERTY_USAGE_ANIMATE_AS_TRIGGER = 32768,
  117. PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED = 65536,
  118. PROPERTY_USAGE_SCRIPT_DEFAULT_VALUE = 1 << 17,
  119. PROPERTY_USAGE_CLASS_IS_ENUM = 1 << 18,
  120. PROPERTY_USAGE_NIL_IS_VARIANT = 1 << 19,
  121. PROPERTY_USAGE_INTERNAL = 1 << 20,
  122. PROPERTY_USAGE_DO_NOT_SHARE_ON_DUPLICATE = 1 << 21, // If the object is duplicated also this property will be duplicated
  123. PROPERTY_USAGE_HIGH_END_GFX = 1 << 22,
  124. PROPERTY_USAGE_NODE_PATH_FROM_SCENE_ROOT = 1 << 23,
  125. PROPERTY_USAGE_RESOURCE_NOT_PERSISTENT = 1 << 24,
  126. PROPERTY_USAGE_KEYING_INCREMENTS = 1 << 25, // Used in inspector to increment property when keyed in animation player
  127. PROPERTY_USAGE_DEFAULT = PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_NETWORK,
  128. PROPERTY_USAGE_DEFAULT_INTL = PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_NETWORK | PROPERTY_USAGE_INTERNATIONALIZED,
  129. PROPERTY_USAGE_NOEDITOR = PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_NETWORK,
  130. };
  131. #define ADD_SIGNAL(m_signal) ClassDB::add_signal(get_class_static(), m_signal)
  132. #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))
  133. #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)
  134. #define ADD_PROPERTY_DEFAULT(m_property, m_default) ClassDB::set_property_default_value(get_class_static(), m_property, m_default)
  135. #define ADD_GROUP(m_name, m_prefix) ClassDB::add_property_group(get_class_static(), m_name, m_prefix)
  136. struct PropertyInfo {
  137. Variant::Type type;
  138. String name;
  139. StringName class_name; //for classes
  140. PropertyHint hint;
  141. String hint_string;
  142. uint32_t usage;
  143. _FORCE_INLINE_ PropertyInfo added_usage(int p_fl) const {
  144. PropertyInfo pi = *this;
  145. pi.usage |= p_fl;
  146. return pi;
  147. }
  148. operator Dictionary() const;
  149. static PropertyInfo from_dict(const Dictionary &p_dict);
  150. PropertyInfo() :
  151. type(Variant::NIL),
  152. hint(PROPERTY_HINT_NONE),
  153. usage(PROPERTY_USAGE_DEFAULT) {
  154. }
  155. 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()) :
  156. type(p_type),
  157. name(p_name),
  158. hint(p_hint),
  159. hint_string(p_hint_string),
  160. usage(p_usage) {
  161. if (hint == PROPERTY_HINT_RESOURCE_TYPE) {
  162. class_name = hint_string;
  163. } else {
  164. class_name = p_class_name;
  165. }
  166. }
  167. PropertyInfo(const StringName &p_class_name) :
  168. type(Variant::OBJECT),
  169. class_name(p_class_name),
  170. hint(PROPERTY_HINT_NONE),
  171. usage(PROPERTY_USAGE_DEFAULT) {
  172. }
  173. bool operator==(const PropertyInfo &p_info) const {
  174. return ((type == p_info.type) &&
  175. (name == p_info.name) &&
  176. (class_name == p_info.class_name) &&
  177. (hint == p_info.hint) &&
  178. (hint_string == p_info.hint_string) &&
  179. (usage == p_info.usage));
  180. }
  181. bool operator<(const PropertyInfo &p_info) const {
  182. return name < p_info.name;
  183. }
  184. };
  185. Array convert_property_list(const List<PropertyInfo> *p_list);
  186. struct MethodInfo {
  187. String name;
  188. PropertyInfo return_val;
  189. uint32_t flags;
  190. int id;
  191. List<PropertyInfo> arguments;
  192. Vector<Variant> default_arguments;
  193. inline bool operator==(const MethodInfo &p_method) const { return id == p_method.id; }
  194. inline bool operator<(const MethodInfo &p_method) const { return id == p_method.id ? (name < p_method.name) : (id < p_method.id); }
  195. operator Dictionary() const;
  196. static MethodInfo from_dict(const Dictionary &p_dict);
  197. MethodInfo();
  198. MethodInfo(const String &p_name);
  199. MethodInfo(const String &p_name, const PropertyInfo &p_param1);
  200. MethodInfo(const String &p_name, const PropertyInfo &p_param1, const PropertyInfo &p_param2);
  201. MethodInfo(const String &p_name, const PropertyInfo &p_param1, const PropertyInfo &p_param2, const PropertyInfo &p_param3);
  202. MethodInfo(const String &p_name, const PropertyInfo &p_param1, const PropertyInfo &p_param2, const PropertyInfo &p_param3, const PropertyInfo &p_param4);
  203. 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);
  204. MethodInfo(Variant::Type ret);
  205. MethodInfo(Variant::Type ret, const String &p_name);
  206. MethodInfo(Variant::Type ret, const String &p_name, const PropertyInfo &p_param1);
  207. MethodInfo(Variant::Type ret, const String &p_name, const PropertyInfo &p_param1, const PropertyInfo &p_param2);
  208. MethodInfo(Variant::Type ret, const String &p_name, const PropertyInfo &p_param1, const PropertyInfo &p_param2, const PropertyInfo &p_param3);
  209. MethodInfo(Variant::Type ret, const String &p_name, const PropertyInfo &p_param1, const PropertyInfo &p_param2, const PropertyInfo &p_param3, const PropertyInfo &p_param4);
  210. 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);
  211. MethodInfo(const PropertyInfo &p_ret, const String &p_name);
  212. MethodInfo(const PropertyInfo &p_ret, const String &p_name, const PropertyInfo &p_param1);
  213. MethodInfo(const PropertyInfo &p_ret, const String &p_name, const PropertyInfo &p_param1, const PropertyInfo &p_param2);
  214. MethodInfo(const PropertyInfo &p_ret, const String &p_name, const PropertyInfo &p_param1, const PropertyInfo &p_param2, const PropertyInfo &p_param3);
  215. 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);
  216. 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);
  217. };
  218. // old cast_to
  219. //if ( is_type(T::get_class_static()) )
  220. //return static_cast<T*>(this);
  221. ////else
  222. //return NULL;
  223. /*
  224. 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.
  225. */
  226. #define REVERSE_GET_PROPERTY_LIST \
  227. public: \
  228. _FORCE_INLINE_ bool _is_gpl_reversed() const { return true; }; \
  229. \
  230. private:
  231. #define UNREVERSE_GET_PROPERTY_LIST \
  232. public: \
  233. _FORCE_INLINE_ bool _is_gpl_reversed() const { return false; }; \
  234. \
  235. private:
  236. #define GDCLASS(m_class, m_inherits) \
  237. private: \
  238. void operator=(const m_class &p_rval) {} \
  239. mutable StringName _class_name; \
  240. friend class ClassDB; \
  241. \
  242. public: \
  243. typedef m_class self_type; \
  244. virtual String get_class() const { \
  245. return String(#m_class); \
  246. } \
  247. virtual const StringName *_get_class_namev() const { \
  248. if (!_class_name) \
  249. _class_name = get_class_static(); \
  250. return &_class_name; \
  251. } \
  252. static _FORCE_INLINE_ void *get_class_ptr_static() { \
  253. static int ptr; \
  254. return &ptr; \
  255. } \
  256. static _FORCE_INLINE_ String get_class_static() { \
  257. return String(#m_class); \
  258. } \
  259. static _FORCE_INLINE_ String get_parent_class_static() { \
  260. return m_inherits::get_class_static(); \
  261. } \
  262. static void get_inheritance_list_static(List<String> *p_inheritance_list) { \
  263. m_inherits::get_inheritance_list_static(p_inheritance_list); \
  264. p_inheritance_list->push_back(String(#m_class)); \
  265. } \
  266. static String get_category_static() { \
  267. String category = m_inherits::get_category_static(); \
  268. if (_get_category != m_inherits::_get_category) { \
  269. if (category != "") \
  270. category += "/"; \
  271. category += _get_category(); \
  272. } \
  273. return category; \
  274. } \
  275. static String inherits_static() { \
  276. return String(#m_inherits); \
  277. } \
  278. virtual bool is_class(const String &p_class) const { return (p_class == (#m_class)) ? true : m_inherits::is_class(p_class); } \
  279. virtual bool is_class_ptr(void *p_ptr) const { return (p_ptr == get_class_ptr_static()) ? true : m_inherits::is_class_ptr(p_ptr); } \
  280. \
  281. static void get_valid_parents_static(List<String> *p_parents) { \
  282. if (m_class::_get_valid_parents_static != m_inherits::_get_valid_parents_static) { \
  283. m_class::_get_valid_parents_static(p_parents); \
  284. } \
  285. \
  286. m_inherits::get_valid_parents_static(p_parents); \
  287. } \
  288. \
  289. protected: \
  290. _FORCE_INLINE_ static void (*_get_bind_methods())() { \
  291. return &m_class::_bind_methods; \
  292. } \
  293. \
  294. public: \
  295. static void initialize_class() { \
  296. static bool initialized = false; \
  297. if (initialized) \
  298. return; \
  299. m_inherits::initialize_class(); \
  300. ClassDB::_add_class<m_class>(); \
  301. if (m_class::_get_bind_methods() != m_inherits::_get_bind_methods()) \
  302. _bind_methods(); \
  303. initialized = true; \
  304. } \
  305. \
  306. protected: \
  307. virtual void _initialize_classv() { \
  308. initialize_class(); \
  309. } \
  310. _FORCE_INLINE_ bool (Object::*_get_get() const)(const StringName &p_name, Variant &) const { \
  311. return (bool(Object::*)(const StringName &, Variant &) const) & m_class::_get; \
  312. } \
  313. virtual bool _getv(const StringName &p_name, Variant &r_ret) const { \
  314. if (m_class::_get_get() != m_inherits::_get_get()) { \
  315. if (_get(p_name, r_ret)) \
  316. return true; \
  317. } \
  318. return m_inherits::_getv(p_name, r_ret); \
  319. } \
  320. _FORCE_INLINE_ bool (Object::*_get_set() const)(const StringName &p_name, const Variant &p_property) { \
  321. return (bool(Object::*)(const StringName &, const Variant &)) & m_class::_set; \
  322. } \
  323. virtual bool _setv(const StringName &p_name, const Variant &p_property) { \
  324. if (m_inherits::_setv(p_name, p_property)) \
  325. return true; \
  326. if (m_class::_get_set() != m_inherits::_get_set()) { \
  327. return _set(p_name, p_property); \
  328. } \
  329. return false; \
  330. } \
  331. _FORCE_INLINE_ void (Object::*_get_get_property_list() const)(List<PropertyInfo> * p_list) const { \
  332. return (void(Object::*)(List<PropertyInfo> *) const) & m_class::_get_property_list; \
  333. } \
  334. virtual void _get_property_listv(List<PropertyInfo> *p_list, bool p_reversed) const { \
  335. if (!p_reversed) { \
  336. m_inherits::_get_property_listv(p_list, p_reversed); \
  337. } \
  338. p_list->push_back(PropertyInfo(Variant::NIL, get_class_static(), PROPERTY_HINT_NONE, String(), PROPERTY_USAGE_CATEGORY)); \
  339. if (!_is_gpl_reversed()) \
  340. ClassDB::get_property_list(#m_class, p_list, true, this); \
  341. if (m_class::_get_get_property_list() != m_inherits::_get_get_property_list()) { \
  342. _get_property_list(p_list); \
  343. } \
  344. if (_is_gpl_reversed()) \
  345. ClassDB::get_property_list(#m_class, p_list, true, this); \
  346. if (p_reversed) { \
  347. m_inherits::_get_property_listv(p_list, p_reversed); \
  348. } \
  349. } \
  350. _FORCE_INLINE_ void (Object::*_get_notification() const)(int) { \
  351. return (void(Object::*)(int)) & m_class::_notification; \
  352. } \
  353. virtual void _notificationv(int p_notification, bool p_reversed) { \
  354. if (!p_reversed) \
  355. m_inherits::_notificationv(p_notification, p_reversed); \
  356. if (m_class::_get_notification() != m_inherits::_get_notification()) { \
  357. _notification(p_notification); \
  358. } \
  359. if (p_reversed) \
  360. m_inherits::_notificationv(p_notification, p_reversed); \
  361. } \
  362. \
  363. private:
  364. #define OBJ_CATEGORY(m_category) \
  365. protected: \
  366. _FORCE_INLINE_ static String _get_category() { return m_category; } \
  367. \
  368. private:
  369. #define OBJ_SAVE_TYPE(m_class) \
  370. public: \
  371. virtual String get_save_class() const { return #m_class; } \
  372. \
  373. private:
  374. class ScriptInstance;
  375. class ObjectRC;
  376. class Object {
  377. public:
  378. typedef Object self_type;
  379. enum ConnectFlags {
  380. CONNECT_DEFERRED = 1,
  381. CONNECT_PERSIST = 2, // hint for scene to save this connection
  382. CONNECT_ONESHOT = 4,
  383. CONNECT_REFERENCE_COUNTED = 8,
  384. };
  385. struct Connection {
  386. Object *source;
  387. StringName signal;
  388. Object *target;
  389. StringName method;
  390. uint32_t flags;
  391. Vector<Variant> binds;
  392. bool operator<(const Connection &p_conn) const;
  393. operator Variant() const;
  394. Connection() {
  395. source = nullptr;
  396. target = nullptr;
  397. flags = 0;
  398. }
  399. Connection(const Variant &p_variant);
  400. };
  401. private:
  402. enum {
  403. MAX_SCRIPT_INSTANCE_BINDINGS = 8
  404. };
  405. #ifdef DEBUG_ENABLED
  406. friend struct _ObjectDebugLock;
  407. #endif
  408. friend bool predelete_handler(Object *);
  409. friend void postinitialize_handler(Object *);
  410. struct Signal {
  411. struct Target {
  412. ObjectID _id;
  413. StringName method;
  414. _FORCE_INLINE_ bool operator<(const Target &p_target) const { return (_id == p_target._id) ? (method < p_target.method) : (_id < p_target._id); }
  415. Target(const ObjectID &p_id, const StringName &p_method) :
  416. _id(p_id),
  417. method(p_method) {
  418. }
  419. Target() { _id = 0; }
  420. };
  421. struct Slot {
  422. int reference_count;
  423. Connection conn;
  424. List<Connection>::Element *cE;
  425. Slot() {
  426. reference_count = 0;
  427. cE = nullptr;
  428. }
  429. };
  430. MethodInfo user;
  431. VMap<Target, Slot> slot_map;
  432. Signal() {}
  433. };
  434. HashMap<StringName, Signal> signal_map;
  435. List<Connection> connections;
  436. #ifdef DEBUG_ENABLED
  437. SafeRefCount _lock_index;
  438. #endif
  439. bool _block_signals;
  440. int _predelete_ok;
  441. Set<Object *> change_receptors;
  442. ObjectID _instance_id;
  443. std::atomic<ObjectRC *> _rc;
  444. bool _predelete();
  445. void _postinitialize();
  446. bool _can_translate;
  447. bool _emitting;
  448. #ifdef TOOLS_ENABLED
  449. bool _edited;
  450. uint32_t _edited_version;
  451. Set<String> editor_section_folding;
  452. #endif
  453. ScriptInstance *script_instance;
  454. RefPtr script;
  455. Dictionary metadata;
  456. mutable StringName _class_name;
  457. mutable const StringName *_class_ptr;
  458. void _add_user_signal(const String &p_name, const Array &p_args = Array());
  459. bool _has_user_signal(const StringName &p_name) const;
  460. Variant _emit_signal(const Variant **p_args, int p_argcount, Variant::CallError &r_error);
  461. Array _get_signal_list() const;
  462. Array _get_signal_connection_list(const String &p_signal) const;
  463. Array _get_incoming_connections() const;
  464. void _set_bind(const String &p_set, const Variant &p_value);
  465. Variant _get_bind(const String &p_name) const;
  466. void _set_indexed_bind(const NodePath &p_name, const Variant &p_value);
  467. Variant _get_indexed_bind(const NodePath &p_name) const;
  468. friend class Reference;
  469. SafeNumeric<uint32_t> instance_binding_count;
  470. void *_script_instance_bindings[MAX_SCRIPT_INSTANCE_BINDINGS];
  471. protected:
  472. virtual void _initialize_classv() { initialize_class(); }
  473. virtual bool _setv(const StringName &p_name, const Variant &p_property) { return false; };
  474. virtual bool _getv(const StringName &p_name, Variant &r_property) const { return false; };
  475. virtual void _get_property_listv(List<PropertyInfo> *p_list, bool p_reversed) const {};
  476. virtual void _notificationv(int p_notification, bool p_reversed){};
  477. static String _get_category() { return ""; }
  478. static void _bind_methods();
  479. bool _set(const StringName &p_name, const Variant &p_property) { return false; };
  480. bool _get(const StringName &p_name, Variant &r_property) const { return false; };
  481. void _get_property_list(List<PropertyInfo> *p_list) const {};
  482. void _notification(int p_notification){};
  483. _FORCE_INLINE_ static void (*_get_bind_methods())() {
  484. return &Object::_bind_methods;
  485. }
  486. _FORCE_INLINE_ bool (Object::*_get_get() const)(const StringName &p_name, Variant &r_ret) const {
  487. return &Object::_get;
  488. }
  489. _FORCE_INLINE_ bool (Object::*_get_set() const)(const StringName &p_name, const Variant &p_property) {
  490. return &Object::_set;
  491. }
  492. _FORCE_INLINE_ void (Object::*_get_get_property_list() const)(List<PropertyInfo> *p_list) const {
  493. return &Object::_get_property_list;
  494. }
  495. _FORCE_INLINE_ void (Object::*_get_notification() const)(int) {
  496. return &Object::_notification;
  497. }
  498. static void get_valid_parents_static(List<String> *p_parents);
  499. static void _get_valid_parents_static(List<String> *p_parents);
  500. void cancel_delete();
  501. void property_list_changed_notify();
  502. virtual void _changed_callback(Object *p_changed, const char *p_prop);
  503. //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());
  504. //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());
  505. Variant _call_bind(const Variant **p_args, int p_argcount, Variant::CallError &r_error);
  506. Variant _call_deferred_bind(const Variant **p_args, int p_argcount, Variant::CallError &r_error);
  507. virtual const StringName *_get_class_namev() const {
  508. if (!_class_name) {
  509. _class_name = get_class_static();
  510. }
  511. return &_class_name;
  512. }
  513. PoolVector<String> _get_meta_list_bind() const;
  514. Array _get_property_list_bind() const;
  515. Array _get_method_list_bind() const;
  516. void _clear_internal_resource_paths(const Variant &p_var);
  517. friend class ClassDB;
  518. virtual void _validate_property(PropertyInfo &property) const;
  519. void _disconnect(const StringName &p_signal, Object *p_to_object, const StringName &p_to_method, bool p_force = false);
  520. public: //should be protected, but bug in clang++
  521. static void initialize_class();
  522. _FORCE_INLINE_ static void register_custom_data_to_otdb(){};
  523. public:
  524. #ifdef TOOLS_ENABLED
  525. _FORCE_INLINE_ void _change_notify(const char *p_property = "") {
  526. _edited = true;
  527. for (Set<Object *>::Element *E = change_receptors.front(); E; E = E->next()) {
  528. ((Object *)(E->get()))->_changed_callback(this, p_property);
  529. }
  530. }
  531. #else
  532. _FORCE_INLINE_ void _change_notify(const char *p_what = "") {}
  533. #endif
  534. static void *get_class_ptr_static() {
  535. static int ptr;
  536. return &ptr;
  537. }
  538. ObjectRC *_use_rc();
  539. bool _is_gpl_reversed() const { return false; }
  540. _FORCE_INLINE_ ObjectID get_instance_id() const { return _instance_id; }
  541. // this is used for editors
  542. void add_change_receptor(Object *p_receptor);
  543. void remove_change_receptor(Object *p_receptor);
  544. template <class T>
  545. static T *cast_to(Object *p_object) {
  546. #ifndef NO_SAFE_CAST
  547. return dynamic_cast<T *>(p_object);
  548. #else
  549. if (!p_object)
  550. return NULL;
  551. if (p_object->is_class_ptr(T::get_class_ptr_static()))
  552. return static_cast<T *>(p_object);
  553. else
  554. return NULL;
  555. #endif
  556. }
  557. template <class T>
  558. static const T *cast_to(const Object *p_object) {
  559. #ifndef NO_SAFE_CAST
  560. return dynamic_cast<const T *>(p_object);
  561. #else
  562. if (!p_object)
  563. return NULL;
  564. if (p_object->is_class_ptr(T::get_class_ptr_static()))
  565. return static_cast<const T *>(p_object);
  566. else
  567. return NULL;
  568. #endif
  569. }
  570. enum {
  571. NOTIFICATION_POSTINITIALIZE = 0,
  572. NOTIFICATION_PREDELETE = 1
  573. };
  574. /* TYPE API */
  575. static void get_inheritance_list_static(List<String> *p_inheritance_list) { p_inheritance_list->push_back("Object"); }
  576. static String get_class_static() { return "Object"; }
  577. static String get_parent_class_static() { return String(); }
  578. static String get_category_static() { return String(); }
  579. virtual String get_class() const { return "Object"; }
  580. virtual String get_save_class() const { return get_class(); } //class stored when saving
  581. virtual bool is_class(const String &p_class) const { return (p_class == "Object"); }
  582. virtual bool is_class_ptr(void *p_ptr) const { return get_class_ptr_static() == p_ptr; }
  583. _FORCE_INLINE_ const StringName &get_class_name() const {
  584. if (!_class_ptr) {
  585. return *_get_class_namev();
  586. } else {
  587. return *_class_ptr;
  588. }
  589. }
  590. /* IAPI */
  591. //void set(const String& p_name, const Variant& p_value);
  592. //Variant get(const String& p_name) const;
  593. void set(const StringName &p_name, const Variant &p_value, bool *r_valid = nullptr);
  594. Variant get(const StringName &p_name, bool *r_valid = nullptr) const;
  595. void set_indexed(const Vector<StringName> &p_names, const Variant &p_value, bool *r_valid = nullptr);
  596. Variant get_indexed(const Vector<StringName> &p_names, bool *r_valid = nullptr) const;
  597. void get_property_list(List<PropertyInfo> *p_list, bool p_reversed = false) const;
  598. bool has_method(const StringName &p_method) const;
  599. void get_method_list(List<MethodInfo> *p_list) const;
  600. Variant callv(const StringName &p_method, const Array &p_args);
  601. virtual Variant call(const StringName &p_method, const Variant **p_args, int p_argcount, Variant::CallError &r_error);
  602. virtual void call_multilevel(const StringName &p_method, const Variant **p_args, int p_argcount);
  603. virtual void call_multilevel_reversed(const StringName &p_method, const Variant **p_args, int p_argcount);
  604. Variant call(const StringName &p_name, VARIANT_ARG_LIST); // C++ helper
  605. void call_multilevel(const StringName &p_name, VARIANT_ARG_LIST); // C++ helper
  606. void notification(int p_notification, bool p_reversed = false);
  607. virtual void notification_callback(int p_message_type) {}
  608. virtual String to_string();
  609. //used mainly by script, get and set all INCLUDING string
  610. virtual Variant getvar(const Variant &p_key, bool *r_valid = nullptr) const;
  611. virtual void setvar(const Variant &p_key, const Variant &p_value, bool *r_valid = nullptr);
  612. /* SCRIPT */
  613. void set_script(const RefPtr &p_script);
  614. RefPtr get_script() const;
  615. bool has_meta(const String &p_name) const;
  616. void set_meta(const String &p_name, const Variant &p_value);
  617. void remove_meta(const String &p_name);
  618. Variant get_meta(const String &p_name, const Variant &p_default = Variant()) const;
  619. void get_meta_list(List<String> *p_list) const;
  620. #ifdef TOOLS_ENABLED
  621. void set_edited(bool p_edited);
  622. bool is_edited() const;
  623. 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
  624. #endif
  625. void set_script_instance(ScriptInstance *p_instance);
  626. _FORCE_INLINE_ ScriptInstance *get_script_instance() const { return script_instance; }
  627. 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
  628. void add_user_signal(const MethodInfo &p_signal);
  629. Error emit_signal(const StringName &p_name, VARIANT_ARG_LIST);
  630. Error emit_signal(const StringName &p_name, const Variant **p_args, int p_argcount);
  631. bool has_signal(const StringName &p_name) const;
  632. void get_signal_list(List<MethodInfo> *p_signals) const;
  633. void get_signal_connection_list(const StringName &p_signal, List<Connection> *p_connections) const;
  634. void get_all_signal_connections(List<Connection> *p_connections) const;
  635. int get_persistent_signal_connection_count() const;
  636. void get_signals_connected_to_this(List<Connection> *p_connections) const;
  637. 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);
  638. void disconnect(const StringName &p_signal, Object *p_to_object, const StringName &p_to_method);
  639. bool is_connected(const StringName &p_signal, Object *p_to_object, const StringName &p_to_method) const;
  640. void call_deferred(const StringName &p_method, VARIANT_ARG_LIST);
  641. void set_deferred(const StringName &p_property, const Variant &p_value);
  642. void set_block_signals(bool p_block);
  643. bool is_blocking_signals() const;
  644. Variant::Type get_static_property_type(const StringName &p_property, bool *r_valid = nullptr) const;
  645. Variant::Type get_static_property_type_indexed(const Vector<StringName> &p_path, bool *r_valid = nullptr) const;
  646. virtual void get_translatable_strings(List<String> *p_strings) const;
  647. virtual void get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const;
  648. StringName tr(const StringName &p_message) const; // translate message (internationalization)
  649. bool _is_queued_for_deletion; // set to true by SceneTree::queue_delete()
  650. bool is_queued_for_deletion() const;
  651. _FORCE_INLINE_ void set_message_translation(bool p_enable) { _can_translate = p_enable; }
  652. _FORCE_INLINE_ bool can_translate_messages() const { return _can_translate; }
  653. #ifdef TOOLS_ENABLED
  654. void editor_set_section_unfold(const String &p_section, bool p_unfolded);
  655. bool editor_is_section_unfolded(const String &p_section);
  656. const Set<String> &editor_get_section_folding() const { return editor_section_folding; }
  657. void editor_clear_section_folding() { editor_section_folding.clear(); }
  658. #endif
  659. //used by script languages to store binding data
  660. void *get_script_instance_binding(int p_script_language_index);
  661. bool has_script_instance_binding(int p_script_language_index);
  662. void set_script_instance_binding(int p_script_language_index, void *p_data);
  663. void clear_internal_resource_paths();
  664. Object();
  665. virtual ~Object();
  666. };
  667. bool predelete_handler(Object *p_object);
  668. void postinitialize_handler(Object *p_object);
  669. class ObjectDB {
  670. struct ObjectPtrHash {
  671. static _FORCE_INLINE_ uint32_t hash(const Object *p_obj) {
  672. union {
  673. const Object *p;
  674. unsigned long i;
  675. } u;
  676. u.p = p_obj;
  677. return HashMapHasherDefault::hash((uint64_t)u.i);
  678. }
  679. };
  680. static HashMap<ObjectID, Object *> instances;
  681. static HashMap<Object *, ObjectID, ObjectPtrHash> instance_checks;
  682. static ObjectID instance_counter;
  683. friend class Object;
  684. friend void unregister_core_types();
  685. static RWLock rw_lock;
  686. static void cleanup();
  687. static ObjectID add_instance(Object *p_object);
  688. static void remove_instance(Object *p_object);
  689. friend void register_core_types();
  690. public:
  691. typedef void (*DebugFunc)(Object *p_obj);
  692. static Object *get_instance(ObjectID p_instance_id);
  693. static void debug_objects(DebugFunc p_func);
  694. static int get_object_count();
  695. // This one may give false positives because a new object may be allocated at the same memory of a previously freed one
  696. _FORCE_INLINE_ static bool instance_validate(Object *p_ptr) {
  697. rw_lock.read_lock();
  698. bool exists = instance_checks.has(p_ptr);
  699. rw_lock.read_unlock();
  700. return exists;
  701. }
  702. };
  703. //needed by macros
  704. #include "core/class_db.h"
  705. #endif // OBJECT_H