|
@@ -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;
|
|
|
+ }
|
|
|
};
|