diff options
Diffstat (limited to 'core/object.h')
-rw-r--r-- | core/object.h | 370 |
1 files changed, 184 insertions, 186 deletions
diff --git a/core/object.h b/core/object.h index b40aef2a42..d9847d10aa 100644 --- a/core/object.h +++ b/core/object.h @@ -124,6 +124,7 @@ enum PropertyUsageFlags { PROPERTY_USAGE_RESOURCE_NOT_PERSISTENT = 1 << 24, PROPERTY_USAGE_KEYING_INCREMENTS = 1 << 25, // Used in inspector to increment property when keyed in animation player PROPERTY_USAGE_DEFERRED_SET_RESOURCE = 1 << 26, // when loading, the resource for this property can be set at the end of loading + PROPERTY_USAGE_EDITOR_INSTANTIATE_OBJECT = 1 << 27, // For Object properties, instantiate them when creating in editor. PROPERTY_USAGE_DEFAULT = PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_NETWORK, PROPERTY_USAGE_DEFAULT_INTL = PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_NETWORK | PROPERTY_USAGE_INTERNATIONALIZED, @@ -138,13 +139,12 @@ enum PropertyUsageFlags { #define ADD_SUBGROUP(m_name, m_prefix) ClassDB::add_property_subgroup(get_class_static(), m_name, m_prefix) struct PropertyInfo { - - Variant::Type type; + Variant::Type type = Variant::NIL; String name; StringName class_name; //for classes - PropertyHint hint; + PropertyHint hint = PROPERTY_HINT_NONE; String hint_string; - uint32_t usage; + uint32_t usage = PROPERTY_USAGE_DEFAULT; _FORCE_INLINE_ PropertyInfo added_usage(int p_fl) const { PropertyInfo pi = *this; @@ -156,11 +156,7 @@ struct PropertyInfo { static PropertyInfo from_dict(const Dictionary &p_dict); - PropertyInfo() : - type(Variant::NIL), - hint(PROPERTY_HINT_NONE), - usage(PROPERTY_USAGE_DEFAULT) { - } + PropertyInfo() {} 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()) : type(p_type), @@ -168,7 +164,6 @@ struct PropertyInfo { hint(p_hint), hint_string(p_hint_string), usage(p_usage) { - if (hint == PROPERTY_HINT_RESOURCE_TYPE) { class_name = hint_string; } else { @@ -178,10 +173,7 @@ struct PropertyInfo { PropertyInfo(const StringName &p_class_name) : type(Variant::OBJECT), - class_name(p_class_name), - hint(PROPERTY_HINT_NONE), - usage(PROPERTY_USAGE_DEFAULT) { - } + class_name(p_class_name) {} bool operator==(const PropertyInfo &p_info) const { return ((type == p_info.type) && @@ -200,11 +192,10 @@ struct PropertyInfo { Array convert_property_list(const List<PropertyInfo> *p_list); struct MethodInfo { - String name; PropertyInfo return_val; - uint32_t flags; - int id; + uint32_t flags; // NOLINT - prevent clang-tidy to assign method_bind.h constant here, it should stay in .cpp. + int id = 0; List<PropertyInfo> arguments; Vector<Variant> default_arguments; @@ -214,6 +205,7 @@ struct MethodInfo { operator Dictionary() const; static MethodInfo from_dict(const Dictionary &p_dict); + MethodInfo(); MethodInfo(const String &p_name); MethodInfo(const String &p_name, const PropertyInfo &p_param1); @@ -258,132 +250,142 @@ public: \ \ private: -#define GDCLASS(m_class, m_inherits) \ -private: \ - void operator=(const m_class &p_rval) {} \ - mutable StringName _class_name; \ - friend class ClassDB; \ - \ -public: \ - virtual String get_class() const { \ - return String(#m_class); \ - } \ - virtual const StringName *_get_class_namev() const { \ - if (!_class_name) \ - _class_name = get_class_static(); \ - return &_class_name; \ - } \ - static _FORCE_INLINE_ void *get_class_ptr_static() { \ - static int ptr; \ - return &ptr; \ - } \ - static _FORCE_INLINE_ String get_class_static() { \ - return String(#m_class); \ - } \ - static _FORCE_INLINE_ String get_parent_class_static() { \ - return m_inherits::get_class_static(); \ - } \ - static void get_inheritance_list_static(List<String> *p_inheritance_list) { \ - m_inherits::get_inheritance_list_static(p_inheritance_list); \ - p_inheritance_list->push_back(String(#m_class)); \ - } \ - static String get_category_static() { \ - String category = m_inherits::get_category_static(); \ - if (_get_category != m_inherits::_get_category) { \ - if (category != "") \ - category += "/"; \ - category += _get_category(); \ - } \ - return category; \ - } \ - static String inherits_static() { \ - return String(#m_inherits); \ - } \ - virtual bool is_class(const String &p_class) const { return (p_class == (#m_class)) ? true : m_inherits::is_class(p_class); } \ - virtual bool is_class_ptr(void *p_ptr) const { return (p_ptr == get_class_ptr_static()) ? true : m_inherits::is_class_ptr(p_ptr); } \ - \ - static void get_valid_parents_static(List<String> *p_parents) { \ - \ - if (m_class::_get_valid_parents_static != m_inherits::_get_valid_parents_static) { \ - m_class::_get_valid_parents_static(p_parents); \ - } \ - \ - m_inherits::get_valid_parents_static(p_parents); \ - } \ - \ -protected: \ - _FORCE_INLINE_ static void (*_get_bind_methods())() { \ - return &m_class::_bind_methods; \ - } \ - \ -public: \ - static void initialize_class() { \ - static bool initialized = false; \ - if (initialized) \ - return; \ - m_inherits::initialize_class(); \ - ClassDB::_add_class<m_class>(); \ - if (m_class::_get_bind_methods() != m_inherits::_get_bind_methods()) \ - _bind_methods(); \ - initialized = true; \ - } \ - \ -protected: \ - virtual void _initialize_classv() { \ - initialize_class(); \ - } \ - _FORCE_INLINE_ bool (Object::*_get_get() const)(const StringName &p_name, Variant &) const { \ - return (bool (Object::*)(const StringName &, Variant &) const) & m_class::_get; \ - } \ - virtual bool _getv(const StringName &p_name, Variant &r_ret) const { \ - if (m_class::_get_get() != m_inherits::_get_get()) { \ - if (_get(p_name, r_ret)) \ - return true; \ - } \ - return m_inherits::_getv(p_name, r_ret); \ - } \ - _FORCE_INLINE_ bool (Object::*_get_set() const)(const StringName &p_name, const Variant &p_property) { \ - return (bool (Object::*)(const StringName &, const Variant &)) & m_class::_set; \ - } \ - virtual bool _setv(const StringName &p_name, const Variant &p_property) { \ - if (m_inherits::_setv(p_name, p_property)) return true; \ - if (m_class::_get_set() != m_inherits::_get_set()) { \ - return _set(p_name, p_property); \ - } \ - return false; \ - } \ - _FORCE_INLINE_ void (Object::*_get_get_property_list() const)(List<PropertyInfo> * p_list) const { \ - return (void (Object::*)(List<PropertyInfo> *) const) & m_class::_get_property_list; \ - } \ - virtual void _get_property_listv(List<PropertyInfo> *p_list, bool p_reversed) const { \ - if (!p_reversed) { \ - m_inherits::_get_property_listv(p_list, p_reversed); \ - } \ - p_list->push_back(PropertyInfo(Variant::NIL, get_class_static(), PROPERTY_HINT_NONE, String(), PROPERTY_USAGE_CATEGORY)); \ - if (!_is_gpl_reversed()) \ - ClassDB::get_property_list(#m_class, p_list, true, this); \ - if (m_class::_get_get_property_list() != m_inherits::_get_get_property_list()) { \ - _get_property_list(p_list); \ - } \ - if (_is_gpl_reversed()) \ - ClassDB::get_property_list(#m_class, p_list, true, this); \ - if (p_reversed) { \ - m_inherits::_get_property_listv(p_list, p_reversed); \ - } \ - } \ - _FORCE_INLINE_ void (Object::*_get_notification() const)(int) { \ - return (void (Object::*)(int)) & m_class::_notification; \ - } \ - virtual void _notificationv(int p_notification, bool p_reversed) { \ - if (!p_reversed) \ - m_inherits::_notificationv(p_notification, p_reversed); \ - if (m_class::_get_notification() != m_inherits::_get_notification()) { \ - _notification(p_notification); \ - } \ - if (p_reversed) \ - m_inherits::_notificationv(p_notification, p_reversed); \ - } \ - \ +#define GDCLASS(m_class, m_inherits) \ +private: \ + void operator=(const m_class &p_rval) {} \ + mutable StringName _class_name; \ + friend class ClassDB; \ + \ +public: \ + virtual String get_class() const override { \ + return String(#m_class); \ + } \ + virtual const StringName *_get_class_namev() const override { \ + if (!_class_name) { \ + _class_name = get_class_static(); \ + } \ + return &_class_name; \ + } \ + static _FORCE_INLINE_ void *get_class_ptr_static() { \ + static int ptr; \ + return &ptr; \ + } \ + static _FORCE_INLINE_ String get_class_static() { \ + return String(#m_class); \ + } \ + static _FORCE_INLINE_ String get_parent_class_static() { \ + return m_inherits::get_class_static(); \ + } \ + static void get_inheritance_list_static(List<String> *p_inheritance_list) { \ + m_inherits::get_inheritance_list_static(p_inheritance_list); \ + p_inheritance_list->push_back(String(#m_class)); \ + } \ + static String get_category_static() { \ + String category = m_inherits::get_category_static(); \ + if (_get_category != m_inherits::_get_category) { \ + if (category != "") { \ + category += "/"; \ + } \ + category += _get_category(); \ + } \ + return category; \ + } \ + static String inherits_static() { \ + return String(#m_inherits); \ + } \ + virtual bool is_class(const String &p_class) const override { return (p_class == (#m_class)) ? true : m_inherits::is_class(p_class); } \ + virtual bool is_class_ptr(void *p_ptr) const override { return (p_ptr == get_class_ptr_static()) ? true : m_inherits::is_class_ptr(p_ptr); } \ + \ + static void get_valid_parents_static(List<String> *p_parents) { \ + if (m_class::_get_valid_parents_static != m_inherits::_get_valid_parents_static) { \ + m_class::_get_valid_parents_static(p_parents); \ + } \ + \ + m_inherits::get_valid_parents_static(p_parents); \ + } \ + \ +protected: \ + _FORCE_INLINE_ static void (*_get_bind_methods())() { \ + return &m_class::_bind_methods; \ + } \ + \ +public: \ + static void initialize_class() { \ + static bool initialized = false; \ + if (initialized) { \ + return; \ + } \ + m_inherits::initialize_class(); \ + ClassDB::_add_class<m_class>(); \ + if (m_class::_get_bind_methods() != m_inherits::_get_bind_methods()) { \ + _bind_methods(); \ + } \ + initialized = true; \ + } \ + \ +protected: \ + virtual void _initialize_classv() override { \ + initialize_class(); \ + } \ + _FORCE_INLINE_ bool (Object::*_get_get() const)(const StringName &p_name, Variant &) const { \ + return (bool (Object::*)(const StringName &, Variant &) const) & m_class::_get; \ + } \ + virtual bool _getv(const StringName &p_name, Variant &r_ret) const override { \ + if (m_class::_get_get() != m_inherits::_get_get()) { \ + if (_get(p_name, r_ret)) { \ + return true; \ + } \ + } \ + return m_inherits::_getv(p_name, r_ret); \ + } \ + _FORCE_INLINE_ bool (Object::*_get_set() const)(const StringName &p_name, const Variant &p_property) { \ + return (bool (Object::*)(const StringName &, const Variant &)) & m_class::_set; \ + } \ + virtual bool _setv(const StringName &p_name, const Variant &p_property) override { \ + if (m_inherits::_setv(p_name, p_property)) { \ + return true; \ + } \ + if (m_class::_get_set() != m_inherits::_get_set()) { \ + return _set(p_name, p_property); \ + } \ + return false; \ + } \ + _FORCE_INLINE_ void (Object::*_get_get_property_list() const)(List<PropertyInfo> * p_list) const { \ + return (void (Object::*)(List<PropertyInfo> *) const) & m_class::_get_property_list; \ + } \ + virtual void _get_property_listv(List<PropertyInfo> *p_list, bool p_reversed) const override { \ + if (!p_reversed) { \ + m_inherits::_get_property_listv(p_list, p_reversed); \ + } \ + p_list->push_back(PropertyInfo(Variant::NIL, get_class_static(), PROPERTY_HINT_NONE, String(), PROPERTY_USAGE_CATEGORY)); \ + if (!_is_gpl_reversed()) { \ + ClassDB::get_property_list(#m_class, p_list, true, this); \ + } \ + if (m_class::_get_get_property_list() != m_inherits::_get_get_property_list()) { \ + _get_property_list(p_list); \ + } \ + if (_is_gpl_reversed()) { \ + ClassDB::get_property_list(#m_class, p_list, true, this); \ + } \ + if (p_reversed) { \ + m_inherits::_get_property_listv(p_list, p_reversed); \ + } \ + } \ + _FORCE_INLINE_ void (Object::*_get_notification() const)(int) { \ + return (void (Object::*)(int)) & m_class::_notification; \ + } \ + virtual void _notificationv(int p_notification, bool p_reversed) override { \ + if (!p_reversed) { \ + m_inherits::_notificationv(p_notification, p_reversed); \ + } \ + if (m_class::_get_notification() != m_inherits::_get_notification()) { \ + _notification(p_notification); \ + } \ + if (p_reversed) { \ + m_inherits::_notificationv(p_notification, p_reversed); \ + } \ + } \ + \ private: #define OBJ_CATEGORY(m_category) \ @@ -392,10 +394,10 @@ protected: \ \ private: -#define OBJ_SAVE_TYPE(m_class) \ -public: \ - virtual String get_save_class() const { return #m_class; } \ - \ +#define OBJ_SAVE_TYPE(m_class) \ +public: \ + virtual String get_save_class() const override { return #m_class; } \ + \ private: class ScriptInstance; @@ -411,18 +413,16 @@ public: }; struct Connection { - ::Signal signal; Callable callable; - uint32_t flags; + uint32_t flags = 0; Vector<Variant> binds; bool operator<(const Connection &p_conn) const; operator Variant() const; - Connection() { - flags = 0; - } + + Connection() {} Connection(const Variant &p_variant); }; @@ -438,18 +438,14 @@ private: friend void postinitialize_handler(Object *); struct SignalData { - struct Slot { - - int reference_count; + int reference_count = 0; Connection conn; - List<Connection>::Element *cE; - Slot() { reference_count = 0; } + List<Connection>::Element *cE = nullptr; }; MethodInfo user; VMap<Callable, Slot> slot_map; - SignalData() {} }; HashMap<StringName, SignalData> signal_map; @@ -457,24 +453,24 @@ private: #ifdef DEBUG_ENABLED SafeRefCount _lock_index; #endif - bool _block_signals; - int _predelete_ok; + bool _block_signals = false; + int _predelete_ok = 0; Set<Object *> change_receptors; ObjectID _instance_id; bool _predelete(); void _postinitialize(); - bool _can_translate; - bool _emitting; + bool _can_translate = true; + bool _emitting = false; #ifdef TOOLS_ENABLED - bool _edited; - uint32_t _edited_version; + bool _edited = false; + uint32_t _edited_version = 0; Set<String> editor_section_folding; #endif - ScriptInstance *script_instance; + ScriptInstance *script_instance = nullptr; Variant script; //reference does not yet exist, store it in a Dictionary metadata; mutable StringName _class_name; - mutable const StringName *_class_ptr; + mutable const StringName *_class_ptr = nullptr; void _add_user_signal(const String &p_name, const Array &p_args = Array()); bool _has_user_signal(const StringName &p_name) const; @@ -493,8 +489,9 @@ private: friend class Reference; bool type_is_reference = false; - uint32_t instance_binding_count; + uint32_t instance_binding_count = 0; void *_script_instance_bindings[MAX_SCRIPT_INSTANCE_BINDINGS]; + Object(bool p_reference); protected: @@ -502,14 +499,14 @@ protected: virtual bool _setv(const StringName &p_name, const Variant &p_property) { return false; }; virtual bool _getv(const StringName &p_name, Variant &r_property) const { return false; }; virtual void _get_property_listv(List<PropertyInfo> *p_list, bool p_reversed) const {}; - virtual void _notificationv(int p_notification, bool p_reversed){}; + virtual void _notificationv(int p_notification, bool p_reversed) {} static String _get_category() { return ""; } static void _bind_methods(); bool _set(const StringName &p_name, const Variant &p_property) { return false; }; bool _get(const StringName &p_name, Variant &r_property) const { return false; }; void _get_property_list(List<PropertyInfo> *p_list) const {}; - void _notification(int p_notification){}; + void _notification(int p_notification) {} _FORCE_INLINE_ static void (*_get_bind_methods())() { return &Object::_bind_methods; @@ -540,8 +537,9 @@ protected: Variant _call_deferred_bind(const Variant **p_args, int p_argcount, Callable::CallError &r_error); virtual const StringName *_get_class_namev() const { - if (!_class_name) + if (!_class_name) { _class_name = get_class_static(); + } return &_class_name; } @@ -558,14 +556,15 @@ protected: public: //should be protected, but bug in clang++ static void initialize_class(); - _FORCE_INLINE_ static void register_custom_data_to_otdb(){}; + _FORCE_INLINE_ static void register_custom_data_to_otdb() {} public: #ifdef TOOLS_ENABLED _FORCE_INLINE_ void _change_notify(const char *p_property = "") { _edited = true; - for (Set<Object *>::Element *E = change_receptors.front(); E; E = E->next()) + for (Set<Object *>::Element *E = change_receptors.front(); E; E = E->next()) { ((Object *)(E->get()))->_changed_callback(this, p_property); + } } #else _FORCE_INLINE_ void _change_notify(const char *p_what = "") {} @@ -578,8 +577,8 @@ public: bool _is_gpl_reversed() const { return false; } _FORCE_INLINE_ ObjectID get_instance_id() const { return _instance_id; } - // this is used for editors + // this is used for editors void add_change_receptor(Object *p_receptor); void remove_change_receptor(Object *p_receptor); @@ -588,12 +587,14 @@ public: #ifndef NO_SAFE_CAST return dynamic_cast<T *>(p_object); #else - if (!p_object) + if (!p_object) { return nullptr; - if (p_object->is_class_ptr(T::get_class_ptr_static())) + } + if (p_object->is_class_ptr(T::get_class_ptr_static())) { return static_cast<T *>(p_object); - else + } else { return nullptr; + } #endif } @@ -602,17 +603,18 @@ public: #ifndef NO_SAFE_CAST return dynamic_cast<const T *>(p_object); #else - if (!p_object) + if (!p_object) { return nullptr; - if (p_object->is_class_ptr(T::get_class_ptr_static())) + } + if (p_object->is_class_ptr(T::get_class_ptr_static())) { return static_cast<const T *>(p_object); - else + } else { return nullptr; + } #endif } enum { - NOTIFICATION_POSTINITIALIZE = 0, NOTIFICATION_PREDELETE = 1 }; @@ -653,10 +655,7 @@ public: void get_method_list(List<MethodInfo> *p_list) const; Variant callv(const StringName &p_method, const Array &p_args); virtual Variant call(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error); - virtual void call_multilevel(const StringName &p_method, const Variant **p_args, int p_argcount); - virtual void call_multilevel_reversed(const StringName &p_method, const Variant **p_args, int p_argcount); Variant call(const StringName &p_name, VARIANT_ARG_LIST); // C++ helper - void call_multilevel(const StringName &p_name, VARIANT_ARG_LIST); // C++ helper void notification(int p_notification, bool p_reversed = false); String to_string(); @@ -722,7 +721,7 @@ public: StringName tr(const StringName &p_message) const; // translate message (internationalization) - bool _is_queued_for_deletion; // set to true by SceneTree::queue_delete() + bool _is_queued_for_deletion = false; // set to true by SceneTree::queue_delete() bool is_queued_for_deletion() const; _FORCE_INLINE_ void set_message_translation(bool p_enable) { _can_translate = p_enable; } @@ -744,6 +743,7 @@ public: void clear_internal_resource_paths(); _ALWAYS_INLINE_ bool is_reference() const { return type_is_reference; } + Object(); virtual ~Object(); }; @@ -752,7 +752,6 @@ bool predelete_handler(Object *p_object); void postinitialize_handler(Object *p_object); class ObjectDB { - //this needs to add up to 63, 1 bit is for reference #define OBJECTDB_VALIDATOR_BITS 39 #define OBJECTDB_VALIDATOR_MASK ((uint64_t(1) << OBJECTDB_VALIDATOR_BITS) - 1) @@ -787,7 +786,6 @@ public: typedef void (*DebugFunc)(Object *p_obj); _ALWAYS_INLINE_ static Object *get_instance(ObjectID p_instance_id) { - uint64_t id = p_instance_id; uint32_t slot = id & OBJECTDB_SLOT_MAX_COUNT_MASK; |