blob: a200a6297c93abc5305f3664cfebb398af07b86d (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
{#---
Macro for enum definition, and the declaration of associated functions.
---#}
{%- macro enum_decl(enum) %}
enum class {{enum.name}} : int32_t {
{%- for field in enum.fields %}
{%- if field.value %}
{{field.name}} = {{field.value|expression_to_text}},
{%- else %}
{{field.name}},
{%- endif %}
{%- endfor %}
};
{%- endmacro %}
{%- macro enum_data_decl(enum) %}
struct {{enum.name}}_Data {
public:
static bool const kIsExtensible = {% if enum.extensible %}true{% else %}false{% endif %};
static bool IsKnownValue(int32_t value) {
switch (value) {
{%- for enum_field in enum.fields|groupby('numeric_value') %}
case {{enum_field[0]}}:
{%- endfor %}
return true;
}
return false;
}
static bool Validate(int32_t value) {
if (kIsExtensible || IsKnownValue(value))
return true;
ReportValidationError(mojo::internal::VALIDATION_ERROR_UNKNOWN_ENUM_VALUE);
return false;
}
};
{%- endmacro %}
{#--- macros for enum-associated functions. Namely:
* operator<<(): outputs the given enum value.
* IsKnownEnumValue(): returns true if the given enum value exists in this
generated version of enum.
---#}
{%- macro enum_stream_operator(enum) %}
inline std::ostream& operator<<(std::ostream& os, {{enum|get_name_for_kind}} value) {
switch(value) {
{%- for _, values in enum.fields|groupby('numeric_value') %}
case {{enum|get_name_for_kind}}::{{values[0].name}}:
return os << "{{enum|get_name_for_kind}}::
{%- if values|length > 1 -%}
{{'{'}}
{%- endif -%}
{{values|map(attribute='name')|join(', ')}}
{%- if values|length > 1 -%}
{{'}'}}
{%- endif -%}
";
{%- endfor %}
default:
return os << "Unknown {{enum|get_name_for_kind}} value: " << static_cast<int32_t>(value);
}
}
{%- endmacro %}
{%- macro is_known_enum_value(enum) %}
inline bool IsKnownEnumValue({{enum|get_name_for_kind}} value) {
return {{enum|get_qualified_name_for_kind(internal=True)}}::IsKnownValue(
static_cast<int32_t>(value));
}
{%- endmacro %}
|