OLD | NEW |
---|---|
1 {#--- | 1 {#--- |
2 Macro for enum definition, and the declaration of associated functions. | 2 Macro for enum definition, and the declaration of associated functions. |
3 `is_static` is relevant if this enum declaration is defined within a class, in | 3 `is_static` is relevant if this enum declaration is defined within a class, in |
4 which case associated functions need to be static. | 4 which case associated functions need to be static. |
5 ---#} | 5 ---#} |
6 {%- macro enum_decl(enum, is_static=false) %} | 6 {%- macro enum_decl(enum, is_static=false) %} |
7 enum {{enum.name}} : int32_t { | 7 enum class {{enum.name}} : int32_t { |
8 {%- for field in enum.fields %} | 8 {%- for field in enum.fields %} |
9 {%- if field.value %} | 9 {%- if field.value %} |
10 {{enum.name|to_all_caps}}_{{field.name}} = {{field.value|expression_to_text}}, | 10 {{field.name}} = {{field.value|expression_to_text}}, |
11 {%- else %} | 11 {%- else %} |
12 {{enum.name|to_all_caps}}_{{field.name}}, | 12 {{field.name}}, |
13 {%- endif %} | 13 {%- endif %} |
14 {%- endfor %} | 14 {%- endfor %} |
15 }; | 15 }; |
16 {{is_valid_enum_decl(enum, is_static)}} | 16 {{is_valid_enum_decl(enum, is_static)}} |
17 {%- endmacro %} | 17 {%- endmacro %} |
18 | 18 |
19 {#--- macros for the declaration & definitions of enum-associated functions. | 19 {#--- macros for the declaration & definitions of enum-associated functions. |
20 Namely: | 20 Namely: |
21 * {enum_name}_IsValidValue: returns true if the given enum has a valid value | 21 * {enum_name}_IsValidValue: returns true if the given enum has a valid value |
22 for this generated version of enum. | 22 for this generated version of enum. |
23 ---#} | 23 ---#} |
24 | 24 |
25 {%- macro is_valid_enum_decl(enum, is_static=false) %} | 25 {%- macro is_valid_enum_decl(enum, is_static=false) %} |
26 {% if is_static %}static {% endif -%} | 26 {% if is_static %}static {% endif -%} |
27 bool {{enum.name}}_IsValidValue({{enum.name}} value); | 27 bool {{enum.name}}_IsValidValue({{enum.name}} value); |
28 {%- endmacro %} | 28 {%- endmacro %} |
29 | 29 |
30 {%- macro enum_stream_operator(enum) %} | |
31 inline std::ostream& operator<<(std::ostream& os, {{enum|get_name_for_kind}} val ue) { | |
32 switch(value) { | |
33 {%- for _, values in enum.fields|groupby('numeric_value') %} | |
34 case {{enum|get_name_for_kind}}::{{values[0].name}}: | |
35 return os << "{{enum|get_name_for_kind}}:: | |
36 {%- if values|length > 1 -%} | |
37 {{'{'}} | |
38 {%- endif -%} | |
39 {{values|map(attribute='name')|join(', ')}} | |
yzshen1
2016/01/07 19:24:52
nit: does it make sense to also add indent for thi
Sam McNally
2016/01/08 05:12:51
Done.
| |
40 {%- if values|length > 1 -%} | |
41 {{'}'}} | |
42 {%- endif -%} | |
43 "; | |
44 {%- endfor %} | |
45 default: | |
46 return os << "Unknown {{enum|get_name_for_kind}} value: " << static_cast<i nt32_t>(value); | |
47 } | |
48 } | |
49 {%- endmacro %} | |
50 | |
30 {%- macro is_valid_enum_def(enum, class_name = '') %} | 51 {%- macro is_valid_enum_def(enum, class_name = '') %} |
31 {% if class_name != '' -%} | 52 {% if class_name != '' -%} |
32 // static | 53 // static |
33 bool {{class_name}}:: | 54 bool {{class_name}}:: |
34 {%- else -%} | 55 {%- else -%} |
35 {{"bool "}} | 56 {{"bool "}} |
36 {%- endif -%} | 57 {%- endif -%} |
37 {{enum.name}}_IsValidValue({{enum.name}} value) { | 58 {{enum.name}}_IsValidValue({{enum.name}} value) { |
38 switch (static_cast<int32_t>(value)) { | 59 switch (static_cast<int32_t>(value)) { |
39 {%- for enum_field in enum.fields|groupby('numeric_value') %} | 60 {%- for enum_field in enum.fields|groupby('numeric_value') %} |
40 case {{enum_field[0]}}: | 61 case {{enum_field[0]}}: |
41 {%- endfor %} | 62 {%- endfor %} |
42 return true; | 63 return true; |
43 } | 64 } |
44 return false; | 65 return false; |
45 } | 66 } |
46 {%- endmacro %} | 67 {%- endmacro %} |
OLD | NEW |