summaryrefslogtreecommitdiff
path: root/core/variant
diff options
context:
space:
mode:
Diffstat (limited to 'core/variant')
-rw-r--r--core/variant/array.cpp10
-rw-r--r--core/variant/array.h2
-rw-r--r--core/variant/dictionary.cpp10
-rw-r--r--core/variant/dictionary.h2
-rw-r--r--core/variant/variant.cpp40
-rw-r--r--core/variant/variant.h1
-rw-r--r--core/variant/variant_call.cpp5
-rw-r--r--core/variant/variant_utility.cpp7
8 files changed, 58 insertions, 19 deletions
diff --git a/core/variant/array.cpp b/core/variant/array.cpp
index f8af78f3c1..94d1596514 100644
--- a/core/variant/array.cpp
+++ b/core/variant/array.cpp
@@ -775,15 +775,9 @@ Variant Array::get_typed_script() const {
return _p->typed.script;
}
-void Array::set_read_only(bool p_enable) {
- if (p_enable == bool(_p->read_only != nullptr)) {
- return;
- }
- if (p_enable) {
+void Array::make_read_only() {
+ if (_p->read_only == nullptr) {
_p->read_only = memnew(Variant);
- } else {
- memdelete(_p->read_only);
- _p->read_only = nullptr;
}
}
diff --git a/core/variant/array.h b/core/variant/array.h
index 3728c22fe0..d9ca3278fb 100644
--- a/core/variant/array.h
+++ b/core/variant/array.h
@@ -127,7 +127,7 @@ public:
StringName get_typed_class_name() const;
Variant get_typed_script() const;
- void set_read_only(bool p_enable);
+ void make_read_only();
bool is_read_only() const;
Array(const Array &p_base, uint32_t p_type, const StringName &p_class_name, const Variant &p_script);
diff --git a/core/variant/dictionary.cpp b/core/variant/dictionary.cpp
index f87064a0d1..0429508cc5 100644
--- a/core/variant/dictionary.cpp
+++ b/core/variant/dictionary.cpp
@@ -333,15 +333,9 @@ Dictionary Dictionary::duplicate(bool p_deep) const {
return recursive_duplicate(p_deep, 0);
}
-void Dictionary::set_read_only(bool p_enable) {
- if (p_enable == bool(_p->read_only != nullptr)) {
- return;
- }
- if (p_enable) {
+void Dictionary::make_read_only() {
+ if (_p->read_only == nullptr) {
_p->read_only = memnew(Variant);
- } else {
- memdelete(_p->read_only);
- _p->read_only = nullptr;
}
}
bool Dictionary::is_read_only() const {
diff --git a/core/variant/dictionary.h b/core/variant/dictionary.h
index e6d8ebe25b..8935d35ed9 100644
--- a/core/variant/dictionary.h
+++ b/core/variant/dictionary.h
@@ -86,7 +86,7 @@ public:
Dictionary duplicate(bool p_deep = false) const;
Dictionary recursive_duplicate(bool p_deep, int recursion_count) const;
- void set_read_only(bool p_enable);
+ void make_read_only();
bool is_read_only() const;
const void *id() const;
diff --git a/core/variant/variant.cpp b/core/variant/variant.cpp
index ca42738b05..672b030806 100644
--- a/core/variant/variant.cpp
+++ b/core/variant/variant.cpp
@@ -3492,6 +3492,46 @@ bool Variant::hash_compare(const Variant &p_variant, int recursion_count) const
}
}
+bool Variant::identity_compare(const Variant &p_variant) const {
+ if (type != p_variant.type) {
+ return false;
+ }
+
+ switch (type) {
+ case OBJECT: {
+ return _get_obj().obj == p_variant._get_obj().obj;
+ } break;
+
+ case DICTIONARY: {
+ const Dictionary &l = *(reinterpret_cast<const Dictionary *>(_data._mem));
+ const Dictionary &r = *(reinterpret_cast<const Dictionary *>(p_variant._data._mem));
+ return l.id() == r.id();
+ } break;
+
+ case ARRAY: {
+ const Array &l = *(reinterpret_cast<const Array *>(_data._mem));
+ const Array &r = *(reinterpret_cast<const Array *>(p_variant._data._mem));
+ return l.id() == r.id();
+ } break;
+
+ case PACKED_BYTE_ARRAY:
+ case PACKED_INT32_ARRAY:
+ case PACKED_INT64_ARRAY:
+ case PACKED_FLOAT32_ARRAY:
+ case PACKED_FLOAT64_ARRAY:
+ case PACKED_STRING_ARRAY:
+ case PACKED_VECTOR2_ARRAY:
+ case PACKED_VECTOR3_ARRAY:
+ case PACKED_COLOR_ARRAY: {
+ return _data.packed_array == p_variant._data.packed_array;
+ } break;
+
+ default: {
+ return hash_compare(p_variant);
+ }
+ }
+}
+
bool StringLikeVariantComparator::compare(const Variant &p_lhs, const Variant &p_rhs) {
if (p_lhs.hash_compare(p_rhs)) {
return true;
diff --git a/core/variant/variant.h b/core/variant/variant.h
index b9294de77d..b2f31a6d57 100644
--- a/core/variant/variant.h
+++ b/core/variant/variant.h
@@ -748,6 +748,7 @@ public:
uint32_t recursive_hash(int recursion_count) const;
bool hash_compare(const Variant &p_variant, int recursion_count = 0) const;
+ bool identity_compare(const Variant &p_variant) const;
bool booleanize() const;
String stringify(int recursion_count = 0) const;
String to_json_string() const;
diff --git a/core/variant/variant_call.cpp b/core/variant/variant_call.cpp
index 9e8c6fccb3..3b2d29734d 100644
--- a/core/variant/variant_call.cpp
+++ b/core/variant/variant_call.cpp
@@ -1695,6 +1695,7 @@ static void _register_variant_builtin_methods() {
bind_string_method(json_escape, sarray(), varray());
bind_string_method(validate_node_name, sarray(), varray());
+ bind_string_method(validate_filename, sarray(), varray());
bind_string_method(is_valid_identifier, sarray(), varray());
bind_string_method(is_valid_int, sarray(), varray());
@@ -2179,6 +2180,8 @@ static void _register_variant_builtin_methods() {
bind_method(Dictionary, values, sarray(), varray());
bind_method(Dictionary, duplicate, sarray("deep"), varray(false));
bind_method(Dictionary, get, sarray("key", "default"), varray(Variant()));
+ bind_method(Dictionary, make_read_only, sarray(), varray());
+ bind_method(Dictionary, is_read_only, sarray(), varray());
/* Array */
@@ -2226,7 +2229,7 @@ static void _register_variant_builtin_methods() {
bind_method(Array, get_typed_builtin, sarray(), varray());
bind_method(Array, get_typed_class_name, sarray(), varray());
bind_method(Array, get_typed_script, sarray(), varray());
- bind_method(Array, set_read_only, sarray("enable"), varray());
+ bind_method(Array, make_read_only, sarray(), varray());
bind_method(Array, is_read_only, sarray(), varray());
/* Byte Array */
diff --git a/core/variant/variant_utility.cpp b/core/variant/variant_utility.cpp
index fe7150bca9..042ebe368a 100644
--- a/core/variant/variant_utility.cpp
+++ b/core/variant/variant_utility.cpp
@@ -1007,9 +1007,14 @@ struct VariantUtilityFunctions {
static inline uint64_t rid_allocate_id() {
return RID_AllocBase::_gen_id();
}
+
static inline RID rid_from_int64(uint64_t p_base) {
return RID::from_uint64(p_base);
}
+
+ static inline bool is_same(const Variant &p_a, const Variant &p_b) {
+ return p_a.identity_compare(p_b);
+ }
};
#ifdef DEBUG_METHODS_ENABLED
@@ -1601,6 +1606,8 @@ void Variant::_register_variant_utility_functions() {
FUNCBINDR(rid_allocate_id, Vector<String>(), Variant::UTILITY_FUNC_TYPE_GENERAL);
FUNCBINDR(rid_from_int64, sarray("base"), Variant::UTILITY_FUNC_TYPE_GENERAL);
+
+ FUNCBINDR(is_same, sarray("a", "b"), Variant::UTILITY_FUNC_TYPE_GENERAL);
}
void Variant::_unregister_variant_utility_functions() {