فهرست منبع

Expose type validator from Dictionary and allow testing without error

- Now you can get the ContainerTypeValidate from a Dictionary (both for
  keys and for values).
- ContainerTypeValidate exposes a validator function that does not show
  any error in case of failure. This allows testing values before trying
  to use them in Dictionary.
George Marques 3 هفته پیش
والد
کامیت
28d3214acd
3فایلهای تغییر یافته به همراه94 افزوده شده و 42 حذف شده
  1. 83 42
      core/variant/container_type_validate.h
  2. 8 0
      core/variant/dictionary.cpp
  3. 3 0
      core/variant/dictionary.h

+ 83 - 42
core/variant/container_type_validate.h

@@ -45,41 +45,9 @@ struct ContainerTypeValidate {
 	Ref<Script> script;
 	const char *where = "container";
 
-	_FORCE_INLINE_ bool can_reference(const ContainerTypeValidate &p_type) const {
-		if (type != p_type.type) {
-			return false;
-		} else if (type != Variant::OBJECT) {
-			return true;
-		}
-
-		if (class_name == StringName()) {
-			return true;
-		} else if (p_type.class_name == StringName()) {
-			return false;
-		} else if (class_name != p_type.class_name && !ClassDB::is_parent_class(p_type.class_name, class_name)) {
-			return false;
-		}
-
-		if (script.is_null()) {
-			return true;
-		} else if (p_type.script.is_null()) {
-			return false;
-		} else if (script != p_type.script && !p_type.script->inherits_script(script)) {
-			return false;
-		}
-
-		return true;
-	}
-
-	_FORCE_INLINE_ bool operator==(const ContainerTypeValidate &p_type) const {
-		return type == p_type.type && class_name == p_type.class_name && script == p_type.script;
-	}
-	_FORCE_INLINE_ bool operator!=(const ContainerTypeValidate &p_type) const {
-		return type != p_type.type || class_name != p_type.class_name || script != p_type.script;
-	}
-
+private:
 	// Coerces String and StringName into each other and int into float when needed.
-	_FORCE_INLINE_ bool validate(Variant &inout_variant, const char *p_operation = "use") const {
+	_FORCE_INLINE_ bool _internal_validate(Variant &inout_variant, const char *p_operation, bool p_output_errors) const {
 		if (type == Variant::NIL) {
 			return true;
 		}
@@ -99,17 +67,21 @@ struct ContainerTypeValidate {
 				return true;
 			}
 
-			ERR_FAIL_V_MSG(false, vformat("Attempted to %s a variable of type '%s' into a %s of type '%s'.", String(p_operation), Variant::get_type_name(inout_variant.get_type()), where, Variant::get_type_name(type)));
+			if (p_output_errors) {
+				ERR_FAIL_V_MSG(false, vformat("Attempted to %s a variable of type '%s' into a %s of type '%s'.", String(p_operation), Variant::get_type_name(inout_variant.get_type()), where, Variant::get_type_name(type)));
+			} else {
+				return false;
+			}
 		}
 
 		if (type != Variant::OBJECT) {
 			return true;
 		}
 
-		return validate_object(inout_variant, p_operation);
+		return _internal_validate_object(inout_variant, p_operation, p_output_errors);
 	}
 
-	_FORCE_INLINE_ bool validate_object(const Variant &p_variant, const char *p_operation = "use") const {
+	_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
@@ -118,7 +90,13 @@ struct ContainerTypeValidate {
 			return true; // This is fine, it's null.
 		}
 		Object *object = ObjectDB::get_instance(object_id);
-		ERR_FAIL_NULL_V_MSG(object, false, vformat("Attempted to %s an invalid (previously freed?) object instance into a '%s'.", String(p_operation), String(where)));
+		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) {
@@ -130,8 +108,12 @@ struct ContainerTypeValidate {
 		}
 
 		const StringName &obj_class = object->get_class_name();
-		if (obj_class != class_name) {
-			ERR_FAIL_COND_V_MSG(!ClassDB::is_parent_class(obj_class, class_name), false, vformat("Attempted to %s an object of type '%s' into a %s, which does not inherit from '%s'.", String(p_operation), object->get_class(), where, String(class_name)));
+		if (obj_class != class_name && !ClassDB::is_parent_class(obj_class, class_name)) {
+			if (p_output_errors) {
+				ERR_FAIL_V_MSG(false, vformat("Attempted to %s an object of type '%s' into a %s, which does not inherit from '%s'.", String(p_operation), object->get_class(), where, String(class_name)));
+			} else {
+				return false;
+			}
 		}
 
 		if (script.is_null()) {
@@ -141,9 +123,68 @@ struct ContainerTypeValidate {
 		Ref<Script> other_script = object->get_script();
 
 		// Check base script..
-		ERR_FAIL_COND_V_MSG(other_script.is_null(), false, vformat("Attempted to %s an object into a %s, that does not inherit from '%s'.", String(p_operation), String(where), String(script->get_class_name())));
-		ERR_FAIL_COND_V_MSG(!other_script->inherits_script(script), false, vformat("Attempted to %s an object into a %s, that does not inherit from '%s'.", String(p_operation), String(where), String(script->get_class_name())));
+		if (other_script.is_null()) {
+			if (p_output_errors) {
+				ERR_FAIL_V_MSG(false, vformat("Attempted to %s an object into a %s, that does not inherit from '%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, that does not inherit from '%s'.", String(p_operation), String(where), String(script->get_class_name())));
+			} else {
+				return false;
+			}
+		}
+
+		return true;
+	}
+
+public:
+	_FORCE_INLINE_ bool validate(Variant &inout_variant, const char *p_operation = "use") const {
+		return _internal_validate(inout_variant, p_operation, true);
+	}
+
+	_FORCE_INLINE_ bool validate_object(const Variant &p_variant, const char *p_operation = "use") const {
+		return _internal_validate_object(p_variant, p_operation, true);
+	}
+
+	_FORCE_INLINE_ bool test_validate(const Variant &p_variant) const {
+		Variant tmp = p_variant;
+		return _internal_validate(tmp, "", false);
+	}
+
+	_FORCE_INLINE_ bool can_reference(const ContainerTypeValidate &p_type) const {
+		if (type != p_type.type) {
+			return false;
+		} else if (type != Variant::OBJECT) {
+			return true;
+		}
+
+		if (class_name == StringName()) {
+			return true;
+		} else if (p_type.class_name == StringName()) {
+			return false;
+		} else if (class_name != p_type.class_name && !ClassDB::is_parent_class(p_type.class_name, class_name)) {
+			return false;
+		}
+
+		if (script.is_null()) {
+			return true;
+		} else if (p_type.script.is_null()) {
+			return false;
+		} else if (script != p_type.script && !p_type.script->inherits_script(script)) {
+			return false;
+		}
 
 		return true;
 	}
+
+	_FORCE_INLINE_ bool operator==(const ContainerTypeValidate &p_type) const {
+		return type == p_type.type && class_name == p_type.class_name && script == p_type.script;
+	}
+	_FORCE_INLINE_ bool operator!=(const ContainerTypeValidate &p_type) const {
+		return type != p_type.type || class_name != p_type.class_name || script != p_type.script;
+	}
 };

+ 8 - 0
core/variant/dictionary.cpp

@@ -722,6 +722,14 @@ Variant Dictionary::get_typed_value_script() const {
 	return _p->typed_value.script;
 }
 
+const ContainerTypeValidate &Dictionary::get_key_validator() const {
+	return _p->typed_key;
+}
+
+const ContainerTypeValidate &Dictionary::get_value_validator() const {
+	return _p->typed_value;
+}
+
 void Dictionary::operator=(const Dictionary &p_dictionary) {
 	if (this == &p_dictionary) {
 		return;

+ 3 - 0
core/variant/dictionary.h

@@ -40,6 +40,7 @@
 class Variant;
 
 struct ContainerType;
+struct ContainerTypeValidate;
 struct DictionaryPrivate;
 struct StringLikeVariantComparator;
 struct VariantHasher;
@@ -121,6 +122,8 @@ public:
 	StringName get_typed_value_class_name() const;
 	Variant get_typed_key_script() const;
 	Variant get_typed_value_script() const;
+	const ContainerTypeValidate &get_key_validator() const;
+	const ContainerTypeValidate &get_value_validator() const;
 
 	void make_read_only();
 	bool is_read_only() const;