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. |
(...skipping 14 matching lines...) Expand all Loading... |
37 {{enum.name}}_IsValidValue({{enum.name}} value) { | 37 {{enum.name}}_IsValidValue({{enum.name}} value) { |
38 switch (static_cast<int32_t>(value)) { | 38 switch (static_cast<int32_t>(value)) { |
39 {%- for enum_field in enum.fields|groupby('numeric_value') %} | 39 {%- for enum_field in enum.fields|groupby('numeric_value') %} |
40 case {{enum_field[0]}}: | 40 case {{enum_field[0]}}: |
41 {%- endfor %} | 41 {%- endfor %} |
42 return true; | 42 return true; |
43 } | 43 } |
44 return false; | 44 return false; |
45 } | 45 } |
46 {%- endmacro %} | 46 {%- endmacro %} |
| 47 |
| 48 {%- macro global_enum_operators_decl(enum, class_name = '') %} |
| 49 {% if class_name != '' -%} |
| 50 std::ostream& operator<<(std::ostream& stream, |
| 51 const {{class_name}}::{{enum.name}}& val); |
| 52 {%- else -%} |
| 53 std::ostream& operator<<(std::ostream& stream, const {{enum.name}}& val); |
| 54 {%- endif -%} |
| 55 {%- endmacro %} |
| 56 |
| 57 {%- macro global_enum_operators_def(enum, class_name = '') %} |
| 58 {% if class_name != '' -%} |
| 59 std::ostream& operator<<(std::ostream& stream, |
| 60 const {{class_name}}::{{enum.name}}& val) { |
| 61 return (stream << static_cast<int32_t>(val)); |
| 62 } |
| 63 {%- else -%} |
| 64 std::ostream& operator<<(std::ostream& stream, const {{enum.name}}& val) { |
| 65 return (stream << static_cast<int32_t>(val)); |
| 66 } |
| 67 {%- endif -%} |
| 68 {%- endmacro %} |
OLD | NEW |