Commit a0136831 authored by Wouter's avatar Wouter

Improve generated wrappers

parent de51d3d6
......@@ -43,6 +43,18 @@ public:
*/
virtual bool isValid();
{% if class.constraints.enums %}
/**
* Get a list of permitted values.
*/
virtual QList<{{ class.wrappedType.type.className }}> allowedValues() const;
{% endif %}
/**
* Destructor
*/
virtual ~{{ class.className }}();
}; // Class {{ class.className }}
{% endautoescape %}
{% load grantleecodegen %}
{% autoescape off %}
{% comment %}
{% if not class.defaultValue %}
// Default Constructor
{{ class.fullName }}::{{ class.className }}()
: {% for base in class.inheritance %}{% if not forloop.first %}, {% endif %}{{ base.type.fullName }}()
{% empty %}AbstractWrapper<{{ class.wrappedType.type.className }}>()
{% endfor %}
{
}
{% endif %}
{{ class.fullName }}::{{ class.className }}({{ class.wrappedType.signature }} value)
: {% for base in class.inheritance %}{% if not forloop.first %}, {% endif %}{{ base.type.fullName }}()
{% empty %}AbstractWrapper<{{ class.wrappedType.type.className }}>(value)
{% endfor %}
{
}
{% switch field.type.type.sourceType %}
{% case "integer" %}
{{ field.name }} = data.value("{{field.originalName}}").toInt();
{% case "number" %}
{{ field.name }} = data.value("{{field.originalName}}").toFloat();
{% case "string" %}
{{ field.name }} = data.value("{{field.originalName}}").toString();
{% default %}
// Unsupported wrapped type initializer {{ field.type.type.sourceType }}!
{% endswitch %}
{{ class.fullName }} {{ class.fullName }}::fromVariant(const QVariant &value)
{
{% switch class.sourceType %}
{% case "integer" %}
return {{ class.fullName }}(value.toInt());
{% case "number" %}
return {{ class.fullName }}(value.toFloat());
{% case "string" %}
return {{ class.fullName }}(value.toString());
{% default %}
// Unsupported wrapped type initializer {{ field.type.type.sourceType }}!
{% endswitch %}
}
bool {{ class.fullName }}::isValid()
{
bool valid = true;
{% endcomment %}
// Check constraints
{% if class.constraints.minLength %}
// Minimum length: {{ class.constraints.minLength }}
valid = (valid && value().size() >= {{ class.constraints.minLength }});
{% endif %}
{% if class.constraints.maxLength %}
// Maximum length: {{ class.constraints.maxLength }}
valid = (valid && value().size() <= {{ class.constraints.maxLength }});
{% endif %}
{% if class.constraints.enums %}
// Allowed values
valid = (valid && allowedValues().contains(value()));
{% endif %}
return valid;
}
{% if class.constraints.enums %}
virtual QSet<{{ class.wrappedType.type.className }}> {{ class.fullName }}::allowedValues() const {
QSet<{{ class.wrappedType.type.className }}> result;
{% for base in class.inheritance %}result.unite({{ class.wrappedType.type.fullName }}::allowedValues());{% endfor %}
{% for item in class.constraints.enums %}
result.append({{ item }});
{% endfor %}
return result;
}
{% endif %}
{{ class.fullName }}::~{{ class.className }}()
{
}
{% endautoescape %}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment