Skip to content

armgit / godot

Public Imported from GitHub
0

Godot Engine – Multi-platform 2D and 3D game engine

Refactor `ContainerTypeValidate` to avoid unnecessary copies.

Lukas Tenbrink1755b8f30bf3
diff --git a/core/variant/array.cpp b/core/variant/array.cpp
index 100110c6912..2347becb37f 100644
--- a/core/variant/array.cpp
+++ b/core/variant/array.cpp
@@ -279,9 +279,10 @@ void Array::assign(const Array &p_array) {
void Array::push_back(const Variant &p_value) {
ERR_FAIL_COND_MSG(_p->read_only, "Array is in read-only state.");
- Variant value = p_value;
- ERR_FAIL_COND(!_p->typed.validate(value, "push_back"));
- _p->array.push_back(std::move(value));
+ Variant tmp;
+ const Variant *value = _p->typed.validate(p_value, tmp, "push_back");
+ ERR_FAIL_NULL(value);
+ _p->array.push_back(*value);
}
void Array::append_array(const Array &p_array) {
@@ -292,10 +293,18 @@ void Array::append_array(const Array &p_array) {
return;
}
- Vector<Variant> validated_array = p_array._p->array;
+ Vector<Variant> validated_array;
+ validated_array.resize(p_array.size());
+
+ const Variant *read = p_array._p->array.ptr();
Variant *write = validated_array.ptrw();
+
for (int i = 0; i < validated_array.size(); ++i) {
- ERR_FAIL_COND(!_p->typed.validate(write[i], "append_array"));
+ const Variant *value = _p->typed.validate(read[i], write[i], "append_array");
+ ERR_FAIL_NULL(value);
+ if (value != &write[i]) {
+ write[i] = *value;
+ }
}
_p->array.append_array(validated_array);
@@ -322,8 +331,9 @@ Error Array::reserve(int p_new_size) {
Error Array::insert(int p_pos, const Variant &p_value) {
ERR_FAIL_COND_V_MSG(_p->read_only, ERR_LOCKED, "Array is in read-only state.");
- Variant value = p_value;
- ERR_FAIL_COND_V(!_p->typed.validate(value, "insert"), ERR_INVALID_PARAMETER);
+ Variant tmp;
+ const Variant *value = _p->typed.validate(p_value, tmp, "insert");
+ ERR_FAIL_NULL_V(value, ERR_INVALID_PARAMETER);
if (p_pos < 0) {
// Relative offset from the end.
@@ -332,21 +342,23 @@ Error Array::insert(int p_pos, const Variant &p_value) {
ERR_FAIL_INDEX_V_MSG(p_pos, _p->array.size() + 1, ERR_INVALID_PARAMETER, vformat("The calculated index %d is out of bounds (the array has %d elements). Leaving the array untouched.", p_pos, _p->array.size()));
- return _p->array.insert(p_pos, std::move(value));
+ return _p->array.insert(p_pos, *value);
}
void Array::fill(const Variant &p_value) {
ERR_FAIL_COND_MSG(_p->read_only, "Array is in read-only state.");
- Variant value = p_value;
- ERR_FAIL_COND(!_p->typed.validate(value, "fill"));
- _p->array.fill(std::move(value));
+ Variant tmp;
+ const Variant *value = _p->typed.validate(p_value, tmp, "fill");
+ ERR_FAIL_NULL(value);
+ _p->array.fill(*value);
}
void Array::erase(const Variant &p_value) {
ERR_FAIL_COND_MSG(_p->read_only, "Array is in read-only state.");
- Variant value = p_value;
- ERR_FAIL_COND(!_p->typed.validate(value, "erase"));
- _p->array.erase(value);
+ Variant tmp;
+ const Variant *value = _p->typed.validate(p_value, tmp, "erase");
+ ERR_FAIL_NULL(value);
+ _p->array.erase(*value);
}
Variant Array::front() const {
@@ -368,8 +380,9 @@ int Array::find(const Variant &p_value, int p_from) const {
if (_p->array.is_empty()) {
return -1;
}
- Variant value = p_value;
- ERR_FAIL_COND_V(!_p->typed.validate(value, "find"), -1);
+ Variant tmp;
+ const Variant *value = _p->typed.validate(p_value, tmp, "find");
+ ERR_FAIL_NULL_V(value, -1);
int ret = -1;
@@ -378,7 +391,7 @@ int Array::find(const Variant &p_value, int p_from) const {
}
for (int i = p_from; i < size(); i++) {
- if (StringLikeVariantComparator::compare(_p->array[i], value)) {
+ if (StringLikeVariantComparator::compare(_p->array[i], *value)) {
ret = i;
break;
}
@@ -419,8 +432,9 @@ int Array::rfind(const Variant &p_value, int p_from) const {
if (_p->array.is_empty()) {
return -1;
}
- Variant value = p_value;
- ERR_FAIL_COND_V(!_p->typed.validate(value, "rfind"), -1);
+ Variant tmp;
+ const Variant *value = _p->typed.validate(p_value, tmp, "rfind");
+ ERR_FAIL_NULL_V(value, -1);
if (p_from < 0) {
// Relative offset from the end
@@ -432,7 +446,7 @@ int Array::rfind(const Variant &p_value, int p_from) const {
}
for (int i = p_from; i >= 0; i--) {
- if (StringLikeVariantComparator::compare(_p->array[i], value)) {
+ if (StringLikeVariantComparator::compare(_p->array[i], *value)) {
return i;
}
}
@@ -476,15 +490,16 @@ int Array::rfind_custom(const Callable &p_callable, int p_from) const {
}
int Array::count(const Variant &p_value) const {
- Variant value = p_value;
- ERR_FAIL_COND_V(!_p->typed.validate(value, "count"), 0);
if (_p->array.is_empty()) {
return 0;
}
+ Variant tmp;
+ const Variant *value = _p->typed.validate(p_value, tmp, "count");
+ ERR_FAIL_NULL_V(value, 0);
int amount = 0;
for (int i = 0; i < _p->array.size(); i++) {
- if (StringLikeVariantComparator::compare(_p->array[i], value)) {
+ if (StringLikeVariantComparator::compare(_p->array[i], *value)) {
amount++;
}
}
@@ -493,10 +508,11 @@ int Array::count(const Variant &p_value) const {
}
bool Array::has(const Variant &p_value) const {
- Variant value = p_value;
- ERR_FAIL_COND_V(!_p->typed.validate(value, "use 'has' with"), false);
+ Variant tmp;
+ const Variant *value = _p->typed.validate(p_value, tmp, "use 'has' with");
+ ERR_FAIL_NULL_V(value, false);
- return find(value) != -1;
+ return find(*value) != -1;
}
void Array::remove_at(int p_pos) {
@@ -514,10 +530,11 @@ void Array::remove_at(int p_pos) {
void Array::set(int p_idx, const Variant &p_value) {
ERR_FAIL_COND_MSG(_p->read_only, "Array is in read-only state.");
- Variant value = p_value;
- ERR_FAIL_COND(!_p->typed.validate(value, "set"));
+ Variant tmp;
+ const Variant *value = _p->typed.validate(p_value, tmp, "set");
+ ERR_FAIL_NULL(value);
- _p->array.write[p_idx] = std::move(value);
+ _p->array.write[p_idx] = *value;
}
const Variant &Array::get(int p_idx) const {
@@ -752,16 +769,17 @@ void Array::shuffle() {
}
int Array::bsearch(const Variant &p_value, bool p_before) const {
- Variant value = p_value;
- ERR_FAIL_COND_V(!_p->typed.validate(value, "binary search using"), -1);
- return _p->array.span().bisect<_ArrayVariantSort>(value, p_before);
+ Variant tmp;
+ const Variant *value = _p->typed.validate(p_value, tmp, "binary search using");
+ ERR_FAIL_NULL_V(value, -1);
+ return _p->array.span().bisect<_ArrayVariantSort>(*value, p_before);
}
int Array::bsearch_custom(const Variant &p_value, const Callable &p_callable, bool p_before) const {
- Variant value = p_value;
- ERR_FAIL_COND_V(!_p->typed.validate(value, "binary search with a custom comparator using"), -1);
-
- return _p->array.bsearch_custom<CallableComparator>(value, p_before, p_callable);
+ Variant tmp;
+ const Variant *value = _p->typed.validate(p_value, tmp, "binary search with a custom comparator using");
+ ERR_FAIL_NULL_V(value, -1);
+ return _p->array.bsearch_custom<CallableComparator>(*value, p_before, p_callable);
}
void Array::reverse() {
@@ -771,9 +789,10 @@ void Array::reverse() {
void Array::push_front(const Variant &p_value) {
ERR_FAIL_COND_MSG(_p->read_only, "Array is in read-only state.");
- Variant value = p_value;
- ERR_FAIL_COND(!_p->typed.validate(value, "push_front"));
- _p->array.insert(0, std::move(value));
+ Variant tmp;
+ const Variant *value = _p->typed.validate(p_value, tmp, "push_front");
+ ERR_FAIL_NULL(value);
+ _p->array.insert(0, *value);
}
Variant Array::pop_back() {
diff --git a/core/variant/container_type_validate.cpp b/core/variant/container_type_validate.cpp
new file mode 100644
index 00000000000..53e76328e2f
--- /dev/null
+++ b/core/variant/container_type_validate.cpp
@@ -0,0 +1,115 @@
+/**************************************************************************/
+/* container_type_validate.cpp */
+/**************************************************************************/
+/* This file is part of: */
+/* GODOT ENGINE */
+/* https://godotengine.org */
+/**************************************************************************/
+/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
+/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
+/* */
+/* Permission is hereby granted, free of charge, to any person obtaining */
+/* a copy of this software and associated documentation files (the */
+/* "Software"), to deal in the Software without restriction, including */
+/* without limitation the rights to use, copy, modify, merge, publish, */
+/* distribute, sublicense, and/or sell copies of the Software, and to */
+/* permit persons to whom the Software is furnished to do so, subject to */
+/* the following conditions: */
+/* */
+/* The above copyright notice and this permission notice shall be */
+/* included in all copies or substantial portions of the Software. */
+/* */
+/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
+/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
+/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
+/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
+/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
+/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
+/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
+/**************************************************************************/
+
+#include "core/variant/container_type_validate.h"
+
+const Variant *ContainerTypeValidate::_internal_convert_variant(const Variant &p_variant, Variant &r_tmp_variant, const char *p_operation, bool p_output_errors) const {
+ if (Variant::can_convert_strict(p_variant.get_type(), variant_type)) {
+ const Variant *converted_from = &p_variant;
+ Callable::CallError call_error;
+ Variant::construct(variant_type, r_tmp_variant, &converted_from, 1, call_error);
+
+ if (call_error.error == Callable::CallError::CALL_OK) {
+ return &r_tmp_variant;
+ }
+ }
+
+ if (p_output_errors) {
+ ERR_FAIL_V_MSG(nullptr, vformat("Attempted to %s a variable of type '%s' into a %s of incompatible type '%s'.", String(p_operation), Variant::get_type_name(p_variant.get_type()), where, Variant::get_type_name(variant_type)));
+ } else {
+ return nullptr;
+ }
+}
+
+bool ContainerTypeValidate::_internal_validate_object(const Variant &p_variant, const char *p_operation, bool p_output_errors) const {
+ ERR_FAIL_COND_V(p_variant.get_type() != Variant::OBJECT, false);
+
+#ifdef DEBUG_ENABLED
+ ObjectID object_id = p_variant;
+ if (object_id == ObjectID()) {
+ return true; // This is fine, it's null.
+ }
+ Object *object = ObjectDB::get_instance(object_id);
+ if (object == nullptr) {
+ if (p_output_errors) {
+ ERR_FAIL_V_MSG(false, vformat("Attempted to %s an invalid (previously freed?) object instance into a '%s'.", String(p_operation), String(where)));
+ } else {
+ return false;
+ }
+ }
+#else
+ Object *object = p_variant;
+ if (object == nullptr) {
+ return true; //fine
+ }
+#endif
+ if (class_name == StringName()) {
+ return true; // All good, no class type requested.
+ }
+
+ const StringName &obj_class = object->get_class_name();
+ if (obj_class != class_name && !object->is_class(class_name)) {
+ if (p_output_errors) {
+ String object_class_name = object->get_class();
+ if (const Ref<Script> other_script = object->get_script(); other_script.is_valid()) {
+ if (const StringName &script_global_name = other_script->get_global_name(); !script_global_name.is_empty()) {
+ object_class_name = script_global_name;
+ }
+ }
+ ERR_FAIL_V_MSG(false, vformat("Attempted to %s an object of type '%s' into a %s of incompatible type '%s'.", String(p_operation), object_class_name, where, String(class_name)));
+ } else {
+ return false;
+ }
+ }
+
+ if (script.is_null()) {
+ return true; // All good, no script requested.
+ }
+
+ Ref<Script> other_script = object->get_script();
+
+ // Check base script..
+ if (other_script.is_null()) {
+ if (p_output_errors) {
+ ERR_FAIL_V_MSG(false, vformat("Attempted to %s an object into a %s of incompatible type '%s'.", String(p_operation), String(where), String(script->get_class_name())));
+ } else {
+ return false;
+ }
+ }
+ if (!other_script->inherits_script(script)) {
+ if (p_output_errors) {
+ ERR_FAIL_V_MSG(false, vformat("Attempted to %s an object into a %s of incompatible type '%s'.", String(p_operation), String(where), String(script->get_class_name())));
+ } else {
+ return false;
+ }
+ }
+
+ return true;
+}
diff --git a/core/variant/container_type_validate.h b/core/variant/container_type_validate.h
index c2e7d2a9758..83d24697995 100644
--- a/core/variant/container_type_validate.h
+++ b/core/variant/container_type_validate.h
@@ -47,111 +47,29 @@ struct ContainerTypeValidate {
const char *where = "container";
private:
- _FORCE_INLINE_ bool _internal_validate(Variant &r_inout_variant, const char *p_operation, bool p_output_errors) const {
+ const Variant *_internal_convert_variant(const Variant &p_variant, Variant &r_tmp_variant, const char *p_operation, bool p_output_errors) const;
+ _FORCE_INLINE_ const Variant *_internal_validate(const Variant &p_variant, Variant &r_tmp_variant, const char *p_operation, bool p_output_errors) const {
if (variant_type == Variant::NIL) {
- return true;
+ return &p_variant;
}
-
- if (variant_type != r_inout_variant.get_type()) {
- if (r_inout_variant.get_type() == Variant::NIL && variant_type == Variant::OBJECT) {
- return true;
- }
-
- if (Variant::can_convert_strict(r_inout_variant.get_type(), variant_type)) {
- Variant converted_to;
- const Variant *converted_from = &r_inout_variant;
- Callable::CallError call_error;
- Variant::construct(variant_type, converted_to, &converted_from, 1, call_error);
-
- if (call_error.error == Callable::CallError::CALL_OK) {
- r_inout_variant = converted_to;
- return true;
- }
- }
-
- if (p_output_errors) {
- ERR_FAIL_V_MSG(false, vformat("Attempted to %s a variable of type '%s' into a %s of incompatible type '%s'.", String(p_operation), Variant::get_type_name(r_inout_variant.get_type()), where, Variant::get_type_name(variant_type)));
- } else {
- return false;
+ if (p_variant.get_type() != variant_type) {
+ if (p_variant.get_type() == Variant::NIL && variant_type == Variant::OBJECT) {
+ return &p_variant;
}
+ return _internal_convert_variant(p_variant, r_tmp_variant, p_operation, p_output_errors);
}
-
if (variant_type != Variant::OBJECT) {
- return true;
- }
-
- return _internal_validate_object(r_inout_variant, p_operation, p_output_errors);
- }
-
- _FORCE_INLINE_ bool _internal_validate_object(const Variant &p_variant, const char *p_operation, bool p_output_errors) const {
- ERR_FAIL_COND_V(p_variant.get_type() != Variant::OBJECT, false);
-
-#ifdef DEBUG_ENABLED
- ObjectID object_id = p_variant;
- if (object_id == ObjectID()) {
- return true; // This is fine, it's null.
- }
- Object *object = ObjectDB::get_instance(object_id);
- if (object == nullptr) {
- if (p_output_errors) {
- ERR_FAIL_V_MSG(false, vformat("Attempted to %s an invalid (previously freed?) object instance into a '%s'.", String(p_operation), String(where)));
- } else {
- return false;
- }
- }
-#else
- Object *object = p_variant;
- if (object == nullptr) {
- return true; //fine
+ return &p_variant;
}
-#endif
- if (class_name == StringName()) {
- return true; // All good, no class type requested.
- }
-
- const StringName &obj_class = object->get_class_name();
- if (obj_class != class_name && !object->is_class(class_name)) {
- if (p_output_errors) {
- String object_class_name = object->get_class();
- if (const Ref<Script> other_script = object->get_script(); other_script.is_valid()) {
- if (const StringName &script_global_name = other_script->get_global_name(); !script_global_name.is_empty()) {
- object_class_name = script_global_name;
- }
- }
- ERR_FAIL_V_MSG(false, vformat("Attempted to %s an object of type '%s' into a %s of incompatible type '%s'.", String(p_operation), object_class_name, where, String(class_name)));
- } else {
- return false;
- }
- }
-
- if (script.is_null()) {
- return true; // All good, no script requested.
- }
-
- Ref<Script> other_script = object->get_script();
-
- // Check base script..
- if (other_script.is_null()) {
- if (p_output_errors) {
- ERR_FAIL_V_MSG(false, vformat("Attempted to %s an object into a %s of incompatible type '%s'.", String(p_operation), String(where), String(script->get_class_name())));
- } else {
- return false;
- }
- }
- if (!other_script->inherits_script(script)) {
- if (p_output_errors) {
- ERR_FAIL_V_MSG(false, vformat("Attempted to %s an object into a %s of incompatible type '%s'.", String(p_operation), String(where), String(script->get_class_name())));
- } else {
- return false;
- }
- }
-
- return true;
+ return _internal_validate_object(p_variant, p_operation, p_output_errors) ? &p_variant : nullptr;
}
+ bool _internal_validate_object(const Variant &p_variant, const char *p_operation, bool p_output_errors) const;
public:
- _FORCE_INLINE_ bool validate(Variant &r_inout_variant, const char *p_operation = "use") const {
- return _internal_validate(r_inout_variant, p_operation, true);
+ // Returns a pointer to a Variant holding a compatible value.
+ // Modifies and uses r_tmp_variant if conversions are needed.
+ _FORCE_INLINE_ const Variant *validate(const Variant &p_variant, Variant &r_tmp_variant, const char *p_operation = "use") const {
+ return _internal_validate(p_variant, r_tmp_variant, p_operation, true);
}
_FORCE_INLINE_ bool validate_object(const Variant &p_variant, const char *p_operation = "use") const {
@@ -159,8 +77,8 @@ public:
}
_FORCE_INLINE_ bool test_validate(const Variant &p_variant) const {
- Variant tmp = p_variant;
- return _internal_validate(tmp, "", false);
+ Variant tmp;
+ return _internal_validate(p_variant, tmp, "", false) != nullptr;
}
_FORCE_INLINE_ bool can_reference(const ContainerTypeValidate &p_type) const {
diff --git a/core/variant/dictionary.cpp b/core/variant/dictionary.cpp
index 60b23be80f3..e7d050f0c0f 100644
--- a/core/variant/dictionary.cpp
+++ b/core/variant/dictionary.cpp
@@ -94,23 +94,25 @@ Variant Dictionary::get_value_at_index(int p_index) const {
// WARNING: This operator does not validate the value type. For scripting/extensions this is
// done in `variant_setget.cpp`. Consider using `set()` if the data might be invalid.
Variant &Dictionary::operator[](const Variant &p_key) {
- Variant key = p_key;
- if (unlikely(!_p->typed_key.validate(key, "use `operator[]`"))) {
+ Variant tmpk;
+ const Variant *key = _p->typed_key.validate(p_key, tmpk, "use `operator[]`");
+
+ if (unlikely(!key)) {
if (unlikely(!_p->typed_fallback)) {
_p->typed_fallback = memnew(Variant);
}
VariantInternal::initialize(_p->typed_fallback, _p->typed_value.variant_type);
return *_p->typed_fallback;
} else if (unlikely(_p->read_only)) {
- if (likely(_p->variant_map.has(key))) {
- *_p->read_only = _p->variant_map[key];
+ if (likely(_p->variant_map.has(*key))) {
+ *_p->read_only = _p->variant_map[*key];
} else {
VariantInternal::initialize(_p->read_only, _p->typed_value.variant_type);
}
return *_p->read_only;
} else {
const uint32_t old_size = _p->variant_map.size();
- Variant &value = _p->variant_map[key];
+ Variant &value = _p->variant_map[*key];
if (_p->variant_map.size() > old_size) {
VariantInternal::initialize(&value, _p->typed_value.variant_type);
}
@@ -119,8 +121,10 @@ Variant &Dictionary::operator[](const Variant &p_key) {
}
const Variant &Dictionary::operator[](const Variant &p_key) const {
- Variant key = p_key;
- if (unlikely(!_p->typed_key.validate(key, "use `operator[]`"))) {
+ Variant tmpk;
+ const Variant *key = _p->typed_key.validate(p_key, tmpk, "use `operator[]`");
+
+ if (unlikely(!key)) {
if (unlikely(!_p->typed_fallback)) {
_p->typed_fallback = memnew(Variant);
}
@@ -128,18 +132,18 @@ const Variant &Dictionary::operator[](const Variant &p_key) const {
return *_p->typed_fallback;
} else {
static Variant empty;
- const Variant *value = _p->variant_map.getptr(key);
- ERR_FAIL_COND_V_MSG(!value, empty, vformat(R"(Bug: Dictionary::operator[] used when there was no value for the given key "%s". Please report.)", key));
+ const Variant *value = _p->variant_map.getptr(*key);
+ ERR_FAIL_NULL_V_MSG(value, empty, vformat(R"(Bug: Dictionary::operator[] used when there was no value for the given key "%s". Please report.)", *key));
return *value;
}
}
const Variant *Dictionary::getptr(const Variant &p_key) const {
- Variant key = p_key;
- if (unlikely(!_p->typed_key.validate(key, "getptr"))) {
- return nullptr;
- }
- HashMap<Variant, Variant, HashMapHasherDefault, StringLikeVariantComparator>::ConstIterator E(_p->variant_map.find(key));
+ Variant tmpk;
+ const Variant *key = _p->typed_key.validate(p_key, tmpk, "getptr");
+ ERR_FAIL_NULL_V(key, nullptr);
+
+ HashMap<Variant, Variant, HashMapHasherDefault, StringLikeVariantComparator>::ConstIterator E(_p->variant_map.find(*key));
if (!E) {
return nullptr;
}
@@ -148,11 +152,11 @@ const Variant *Dictionary::getptr(const Variant &p_key) const {
// WARNING: This method does not validate the value type.
Variant *Dictionary::getptr(const Variant &p_key) {
- Variant key = p_key;
- if (unlikely(!_p->typed_key.validate(key, "getptr"))) {
- return nullptr;
- }
- HashMap<Variant, Variant, HashMapHasherDefault, StringLikeVariantComparator>::Iterator E(_p->variant_map.find(key));
+ Variant tmpk;
+ const Variant *key = _p->typed_key.validate(p_key, tmpk, "getptr");
+ ERR_FAIL_NULL_V(key, nullptr);
+
+ HashMap<Variant, Variant, HashMapHasherDefault, StringLikeVariantComparator>::Iterator E(_p->variant_map.find(*key));
if (!E) {
return nullptr;
}
@@ -165,9 +169,11 @@ Variant *Dictionary::getptr(const Variant &p_key) {
}
Variant Dictionary::get_valid(const Variant &p_key) const {
- Variant key = p_key;
- ERR_FAIL_COND_V(!_p->typed_key.validate(key, "get_valid"), Variant());
- HashMap<Variant, Variant, HashMapHasherDefault, StringLikeVariantComparator>::ConstIterator E(_p->variant_map.find(key));
+ Variant tmpk;
+ const Variant *key = _p->typed_key.validate(p_key, tmpk, "get_valid");
+ ERR_FAIL_NULL_V(key, Variant());
+
+ HashMap<Variant, Variant, HashMapHasherDefault, StringLikeVariantComparator>::ConstIterator E(_p->variant_map.find(*key));
if (!E) {
return Variant();
@@ -176,9 +182,11 @@ Variant Dictionary::get_valid(const Variant &p_key) const {
}
Variant Dictionary::get(const Variant &p_key, const Variant &p_default) const {
- Variant key = p_key;
- ERR_FAIL_COND_V(!_p->typed_key.validate(key, "get"), p_default);
- const Variant *result = getptr(key);
+ Variant tmpk;
+ const Variant *key = _p->typed_key.validate(p_key, tmpk, "get");
+ ERR_FAIL_NULL_V(key, p_default);
+
+ const Variant *result = getptr(*key);
if (!result) {
return p_default;
}
@@ -187,25 +195,32 @@ Variant Dictionary::get(const Variant &p_key, const Variant &p_default) const {
}
Variant Dictionary::get_or_add(const Variant &p_key, const Variant &p_default) {
- Variant key = p_key;
- ERR_FAIL_COND_V(!_p->typed_key.validate(key, "get"), p_default);
- const Variant *result = getptr(key);
+ Variant tmpk;
+ const Variant *key = _p->typed_key.validate(p_key, tmpk, "get_or_add");
+ ERR_FAIL_NULL_V(key, p_default);
+
+ const Variant *result = getptr(*key);
if (!result) {
- Variant value = p_default;
- ERR_FAIL_COND_V(!_p->typed_value.validate(value, "add"), value);
- operator[](key) = value;
- return value;
+ Variant tmpv;
+ const Variant *value = _p->typed_value.validate(p_default, tmpv, "add");
+ ERR_FAIL_NULL_V(value, p_default);
+
+ operator[](*key) = *value;
+ return *value;
}
return *result;
}
bool Dictionary::set(const Variant &p_key, const Variant &p_value) {
ERR_FAIL_COND_V_MSG(_p->read_only, false, "Dictionary is in read-only state.");
- Variant key = p_key;
- ERR_FAIL_COND_V(!_p->typed_key.validate(key, "set"), false);
- Variant value = p_value;
- ERR_FAIL_COND_V(!_p->typed_value.validate(value, "set"), false);
- _p->variant_map[key] = value;
+ Variant tmpk;
+ const Variant *key = _p->typed_key.validate(p_key, tmpk, "set");
+ ERR_FAIL_NULL_V(key, false);
+ Variant tmpv;
+ const Variant *value = _p->typed_value.validate(p_value, tmpv, "set");
+ ERR_FAIL_NULL_V(value, false);
+
+ _p->variant_map[*key] = *value;
return true;
}
@@ -218,16 +233,19 @@ bool Dictionary::is_empty() const {
}
bool Dictionary::has(const Variant &p_key) const {
- Variant key = p_key;
- ERR_FAIL_COND_V(!_p->typed_key.validate(key, "use 'has'"), false);
- return _p->variant_map.has(key);
+ Variant tmpk;
+ const Variant *key = _p->typed_key.validate(p_key, tmpk, "use 'has'");
+ ERR_FAIL_NULL_V(key, false);
+ return _p->variant_map.has(*key);
}
bool Dictionary::has_all(const Array &p_keys) const {
for (int i = 0; i < p_keys.size(); i++) {
- Variant key = p_keys[i];
- ERR_FAIL_COND_V(!_p->typed_key.validate(key, "use 'has_all'"), false);
- if (!_p->variant_map.has(key)) {
+ Variant tmpk;
+ const Variant *key = _p->typed_key.validate(p_keys[i], tmpk, "use 'has_all'");
+ ERR_FAIL_NULL_V(key, false);
+
+ if (!_p->variant_map.has(*key)) {
return false;
}
}
@@ -235,10 +253,12 @@ bool Dictionary::has_all(const Array &p_keys) const {
}
Variant Dictionary::find_key(const Variant &p_value) const {
- Variant value = p_value;
- ERR_FAIL_COND_V(!_p->typed_value.validate(value, "find_key"), Variant());
+ Variant tmpv;
+ const Variant *value = _p->typed_value.validate(p_value, tmpv, "find_key");
+ ERR_FAIL_NULL_V(value, Variant());
+
for (const KeyValue<Variant, Variant> &E : _p->variant_map) {
- if (E.value == value) {
+ if (E.value == *value) {
return E.key;
}
}
@@ -246,10 +266,12 @@ Variant Dictionary::find_key(const Variant &p_value) const {
}
bool Dictionary::erase(const Variant &p_key) {
- Variant key = p_key;
- ERR_FAIL_COND_V(!_p->typed_key.validate(key, "erase"), false);
+ Variant tmpk;
+ const Variant *key = _p->typed_key.validate(p_key, tmpk, "erase");
+ ERR_FAIL_NULL_V(key, false);
+
ERR_FAIL_COND_V_MSG(_p->read_only, false, "Dictionary is in read-only state.");
- return _p->variant_map.erase(key);
+ return _p->variant_map.erase(*key);
}
bool Dictionary::operator==(const Dictionary &p_dictionary) const {
@@ -332,12 +354,16 @@ void Dictionary::sort() {
void Dictionary::merge(const Dictionary &p_dictionary, bool p_overwrite) {
ERR_FAIL_COND_MSG(_p->read_only, "Dictionary is in read-only state.");
for (const KeyValue<Variant, Variant> &E : p_dictionary._p->variant_map) {
- Variant key = E.key;
- Variant value = E.value;
- ERR_FAIL_COND(!_p->typed_key.validate(key, "merge"));
- ERR_FAIL_COND(!_p->typed_value.validate(value, "merge"));
- if (p_overwrite || !has(key)) {
- operator[](key) = value;
+ Variant tmpk;
+ const Variant *key = _p->typed_key.validate(E.key, tmpk, "merge");
+ ERR_FAIL_NULL(key);
+
+ Variant tmpv;
+ const Variant *value = _p->typed_value.validate(E.value, tmpv, "merge");
+ ERR_FAIL_NULL(value);
+
+ if (p_overwrite || !has(*key)) {
+ operator[](*key) = *value;
}
}
}
@@ -567,9 +593,11 @@ const Variant *Dictionary::next(const Variant *p_key) const {
}
return nullptr;
}
- Variant key = *p_key;
- ERR_FAIL_COND_V(!_p->typed_key.validate(key, "next"), nullptr);
- HashMap<Variant, Variant, HashMapHasherDefault, StringLikeVariantComparator>::Iterator E = _p->variant_map.find(key);
+ Variant tmpk;
+ const Variant *key = _p->typed_key.validate(*p_key, tmpk, "next");
+ ERR_FAIL_NULL_V(key, nullptr);
+
+ HashMap<Variant, Variant, HashMapHasherDefault, StringLikeVariantComparator>::Iterator E = _p->variant_map.find(*key);
if (!E) {
return nullptr;
diff --git a/modules/gdscript/tests/scripts/runtime/errors/typed_array.out b/modules/gdscript/tests/scripts/runtime/errors/typed_array.out
index ab46089122e..bbbcec078d6 100644
--- a/modules/gdscript/tests/scripts/runtime/errors/typed_array.out
+++ b/modules/gdscript/tests/scripts/runtime/errors/typed_array.out
@@ -4,7 +4,7 @@ GDTEST_RUNTIME_ERROR
>> SCRIPT ERROR at runtime/errors/typed_array.gd:15 on subtest_assign_basic_to_differently_typed(): Trying to assign an array of type "Array[float]" to a variable of type "Array[int]".
>> ERROR: Method/function failed. Returning: false
>> Attempted to set an object into a TypedArray of incompatible type 'GDScript'.
->> ERROR: Condition "!_p->typed.validate(value, "set")" is true.
+>> ERROR: Parameter "value" is null.
end subtest_assign_wrong_to_typed
>> SCRIPT ERROR at runtime/errors/typed_array.gd:24 on subtest_pass_basic_to_typed(): Invalid type in function 'expect_typed' in base 'RefCounted (typed_array.gd)'. The array of argument 1 (Array) does not have the same element type as the expected typed array argument.
>> SCRIPT ERROR at runtime/errors/typed_array.gd:29 on subtest_pass_basic_to_differently_typed(): Invalid type in function 'expect_typed' in base 'RefCounted (typed_array.gd)'. The array of argument 1 (Array[float]) does not have the same element type as the expected typed array argument.
diff --git a/modules/gdscript/tests/scripts/runtime/errors/typed_dictionary.out b/modules/gdscript/tests/scripts/runtime/errors/typed_dictionary.out
index b390c32df78..ac3c7d5124a 100644
--- a/modules/gdscript/tests/scripts/runtime/errors/typed_dictionary.out
+++ b/modules/gdscript/tests/scripts/runtime/errors/typed_dictionary.out
@@ -1,18 +1,18 @@
GDTEST_RUNTIME_ERROR
~~ WARNING at line 45: (UNSAFE_CALL_ARGUMENT) The argument 1 of the function "expect_typed()" requires the subtype "Dictionary[int, int]" but the supertype "Variant" was provided.
>> SCRIPT ERROR at runtime/errors/typed_dictionary.gd:16 on subtest_assign_basic_to_typed(): Trying to assign a dictionary of type "Dictionary" to a variable of type "Dictionary[int, int]".
->> ERROR: Method/function failed. Returning: false
+>> ERROR: Method/function failed. Returning: nullptr
>> Attempted to set a variable of type 'String' into a TypedDictionary.Key of incompatible type 'int'.
->> ERROR: Condition "!_p->typed_key.validate(key, "set")" is true. Returning: false
+>> ERROR: Parameter "key" is null.
>> SCRIPT ERROR at runtime/errors/typed_dictionary.gd:21 on subtest_assign_basic_to_differently_typed_key(): Invalid assignment of property or key 'key' with value of type 'int' on a base object of type 'Dictionary[int, int]'.
->> ERROR: Method/function failed. Returning: false
+>> ERROR: Method/function failed. Returning: nullptr
>> Attempted to set a variable of type 'String' into a TypedDictionary.Value of incompatible type 'int'.
->> ERROR: Condition "!_p->typed_value.validate(value, "set")" is true. Returning: false
+>> ERROR: Parameter "value" is null.
>> SCRIPT ERROR at runtime/errors/typed_dictionary.gd:26 on subtest_assign_basic_to_differently_typed_value(): Invalid assignment of property or key '0' with value of type 'String' on a base object of type 'Dictionary[int, int]'.
>> SCRIPT ERROR at runtime/errors/typed_dictionary.gd:31 on subtest_assign_differently_typed(): Trying to assign a dictionary of type "Dictionary[float, float]" to a variable of type "Dictionary[int, int]".
>> ERROR: Method/function failed. Returning: false
>> Attempted to set an object into a TypedDictionary.Key of incompatible type 'GDScript'.
->> ERROR: Condition "!_p->typed_key.validate(key, "set")" is true. Returning: false
+>> ERROR: Parameter "key" is null.
end subtest_assign_wrong_to_typed
>> SCRIPT ERROR at runtime/errors/typed_dictionary.gd:40 on subtest_pass_basic_to_typed(): Invalid type in function 'expect_typed' in base 'RefCounted (typed_dictionary.gd)'. The dictionary of argument 1 (Dictionary) does not have the same element type as the expected typed dictionary argument.
>> SCRIPT ERROR at runtime/errors/typed_dictionary.gd:45 on subtest_pass_basic_to_differently_typed(): Invalid type in function 'expect_typed' in base 'RefCounted (typed_dictionary.gd)'. The dictionary of argument 1 (Dictionary[float, float]) does not have the same element type as the expected typed dictionary argument.