Commit 081b88ca authored by Wouter's avatar Wouter

Add enum code to wrapped string types

parent c922cef9
......@@ -10,6 +10,23 @@ class {{ class.className }}
{
public:
{# Enum declaration for easier construction #}
{% if class.constraints.enums %}{% ifequal class.sourceType "string" %}
enum {{ class.className }}Option {
{% if not class.defaultValue %}
{{ class.className }}Null,
{% endif %}
{% for item in class.constraints.enums %}
{% ifequal item|unquote|identifier|title class.className %}
{{ class.className }}{{ class.className }}
{% else %}
{{ item|unquote|identifier|title }}
{% endifequal %}
{% if not forloop.last %},{% endif %}
{% endfor %}
};
{% endifequal %}{% endif %}
{% if not class.defaultValue %}
{# Provide default constructor for more lazy initialization if no default is provided. It's likely this is invalid! #}
/**
......@@ -40,6 +57,13 @@ public:
{{ class.className }}(const char* value);
{% endifequal %}
{% if class.constraints.enums %}{% ifequal class.sourceType "string" %}
/**
* Create a new {{ class.className }} instance from an option.
*/
{{ class.className }}({{ class.className }}Option value);
{% endifequal %}{% endif %}
/**
* Create a new {{ class.className }} instance from a variant.
*
......@@ -62,6 +86,16 @@ public:
* Get a list of permitted values.
*/
virtual QSet<{{ class.wrappedType.type.className }}> allowedValues() const;
{% ifequal class.sourceType "string" %}
/**
* Convert a valid value to an {{ class.className }}Option enum.
*
* If the value is not valid, this will return {% if not class.defaultValue %}{{ class.className }}Null{% else %}
{% ifequal class.defaultValue|unquote|identifier|title class.className %}{{ class.className }}{{ class.className }}{% else %}{{ class.defaultValue|unquote|identifier|title }}{% endifequal %}{% endif %}.
*/
operator {{ class.className }}Option();
{% endifequal %}
{% endif %}
/**
......
......@@ -31,6 +31,35 @@
}
{% endifequal %}
{% if class.constraints.enums %}
{% ifequal class.sourceType "string" %}
{{ class.fullName }}::{{ class.className }}({{ class.fullName }}::{{ class.className }}Option value)
: {% for base in class.inheritance %}{% if not forloop.first %}, {% endif %}{{ base.type.fullName }}()
{% empty %}AbstractWrapper<{{ class.wrappedType.type.className }}>()
{% endfor %}
{
switch(value)
{
{% for item in class.constraints.enums %}
{% ifequal item|unquote|identifier|title class.className %}
case {{ class.className }}{{ class.className }}:
{% else %}
case {{ item|unquote|identifier|title }}:
{% endifequal %}
setValue({{ item }});
break;
{% endfor %}
{% if not class.defaultValue %}
case {{ class.className }}Null:
{% endif %}
default:
break;
}
}
{% endifequal %}
{% endif %}
{{ class.fullName }} {{ class.fullName }}::fromVariant(const QVariant& value, bool* ok)
{
{{ class.fullName }} result = {% switch class.sourceType %}
......@@ -86,6 +115,25 @@ QSet<{{ class.wrappedType.type.className }}> {{ class.fullName }}::allowedValues
return result;
}
{% ifequal class.sourceType "string" %}
{{ class.fullName }}::operator {{ class.className }}Option()
{
{% for item in class.constraints.enums %}
{% if not forloop.first %}else {% endif %}if (value() == {{ item }}) {
{% ifequal item|unquote|identifier|title class.className %}
return {{ class.className }}{{ class.className }};
{% else %}
return {{ item|unquote|identifier|title }};
{% endifequal %}
}
{% endfor %}
// Default
return {% if not class.defaultValue %}{{ class.className }}Null{% else %}
{% ifequal class.defaultValue|unquote|identifier|title class.className %}{{ class.className }}{{ class.className }}{% else %}{{ class.defaultValue|unquote|identifier|title }}{% endifequal %}{% endif %};
}
{% endifequal %}
{% endif %}
{{ class.fullName }}::~{{ class.className }}()
......
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