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.
This commit is contained in:
George Marques 2025-07-01 15:04:31 -03:00
parent e0603aeda3
commit 28d3214acd
No known key found for this signature in database
GPG key ID: 046BD46A3201E43D
3 changed files with 121 additions and 69 deletions

View file

@ -45,6 +45,116 @@ struct ContainerTypeValidate {
Ref<Script> script; Ref<Script> script;
const char *where = "container"; const char *where = "container";
private:
// Coerces String and StringName into each other and int into float when needed.
_FORCE_INLINE_ bool _internal_validate(Variant &inout_variant, const char *p_operation, bool p_output_errors) const {
if (type == Variant::NIL) {
return true;
}
if (type != inout_variant.get_type()) {
if (inout_variant.get_type() == Variant::NIL && type == Variant::OBJECT) {
return true;
}
if (type == Variant::STRING && inout_variant.get_type() == Variant::STRING_NAME) {
inout_variant = String(inout_variant);
return true;
} else if (type == Variant::STRING_NAME && inout_variant.get_type() == Variant::STRING) {
inout_variant = StringName(inout_variant);
return true;
} else if (type == Variant::FLOAT && inout_variant.get_type() == Variant::INT) {
inout_variant = (float)inout_variant;
return true;
}
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 _internal_validate_object(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
}
#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 && !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()) {
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, 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 { _FORCE_INLINE_ bool can_reference(const ContainerTypeValidate &p_type) const {
if (type != p_type.type) { if (type != p_type.type) {
return false; return false;
@ -77,73 +187,4 @@ struct ContainerTypeValidate {
_FORCE_INLINE_ bool operator!=(const ContainerTypeValidate &p_type) const { _FORCE_INLINE_ bool operator!=(const ContainerTypeValidate &p_type) const {
return type != p_type.type || class_name != p_type.class_name || script != p_type.script; return type != p_type.type || class_name != p_type.class_name || script != p_type.script;
} }
// 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 {
if (type == Variant::NIL) {
return true;
}
if (type != inout_variant.get_type()) {
if (inout_variant.get_type() == Variant::NIL && type == Variant::OBJECT) {
return true;
}
if (type == Variant::STRING && inout_variant.get_type() == Variant::STRING_NAME) {
inout_variant = String(inout_variant);
return true;
} else if (type == Variant::STRING_NAME && inout_variant.get_type() == Variant::STRING) {
inout_variant = StringName(inout_variant);
return true;
} else if (type == Variant::FLOAT && inout_variant.get_type() == Variant::INT) {
inout_variant = (float)inout_variant;
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 (type != Variant::OBJECT) {
return true;
}
return validate_object(inout_variant, p_operation);
}
_FORCE_INLINE_ bool validate_object(const Variant &p_variant, const char *p_operation = "use") 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);
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)));
#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) {
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 (script.is_null()) {
return true; // All good, no script requested.
}
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())));
return true;
}
}; };

View file

@ -722,6 +722,14 @@ Variant Dictionary::get_typed_value_script() const {
return _p->typed_value.script; 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) { void Dictionary::operator=(const Dictionary &p_dictionary) {
if (this == &p_dictionary) { if (this == &p_dictionary) {
return; return;

View file

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