| 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 | |
| 4 which case associated functions need to be static. | |
| 5 ---#} | 3 ---#} |
| 6 {%- macro enum_decl(enum, is_static=false) %} | 4 {%- macro enum_decl(enum) %} |
| 7 enum class {{enum.name}} : int32_t { | 5 enum class {{enum.name}} : int32_t { |
| 8 {%- for field in enum.fields %} | 6 {%- for field in enum.fields %} |
| 9 {%- if field.value %} | 7 {%- if field.value %} |
| 10 {{field.name}} = {{field.value|expression_to_text}}, | 8 {{field.name}} = {{field.value|expression_to_text}}, |
| 11 {%- else %} | 9 {%- else %} |
| 12 {{field.name}}, | 10 {{field.name}}, |
| 13 {%- endif %} | 11 {%- endif %} |
| 14 {%- endfor %} | 12 {%- endfor %} |
| 15 }; | 13 }; |
| 16 {{is_valid_enum_decl(enum, is_static)}} | |
| 17 {%- endmacro %} | 14 {%- endmacro %} |
| 18 | 15 |
| 19 {#--- macros for the declaration & definitions of enum-associated functions. | 16 {%- macro enum_data_decl(enum) %} |
| 20 Namely: | 17 struct {{enum.name}}_Data { |
| 21 * {enum_name}_IsValidValue: returns true if the given enum has a valid value | 18 public: |
| 22 for this generated version of enum. | 19 // Used to identify Mojom Enum Data Classes. |
| 20 typedef void MojomEnumDataType; |
| 21 |
| 22 static bool const kIsExtensible = {% if enum.extensible %}true{% else %}false{
% endif %}; |
| 23 |
| 24 static bool IsKnownValue(int32_t value) { |
| 25 switch (value) { |
| 26 {%- for enum_field in enum.fields|groupby('numeric_value') %} |
| 27 case {{enum_field[0]}}: |
| 28 {%- endfor %} |
| 29 return true; |
| 30 } |
| 31 return false; |
| 32 } |
| 33 |
| 34 int32_t value; |
| 35 }; |
| 36 {%- endmacro %} |
| 37 |
| 38 {#--- macros for enum-associated functions. Namely: |
| 39 * operator<<(): outputs the given enum value. |
| 40 * IsKnownEnumValue(): returns true if the given enum value exists in this |
| 41 generated version of enum. |
| 23 ---#} | 42 ---#} |
| 24 | 43 |
| 25 {%- macro is_valid_enum_decl(enum, is_static=false) %} | |
| 26 {% if is_static %}static {% endif -%} | |
| 27 bool {{enum.name}}_IsValidValue({{enum.name}} value); | |
| 28 {%- endmacro %} | |
| 29 | |
| 30 {%- macro enum_stream_operator(enum) %} | 44 {%- macro enum_stream_operator(enum) %} |
| 31 inline std::ostream& operator<<(std::ostream& os, {{enum|get_name_for_kind}} val
ue) { | 45 inline std::ostream& operator<<(std::ostream& os, {{enum|get_name_for_kind}} val
ue) { |
| 32 switch(value) { | 46 switch(value) { |
| 33 {%- for _, values in enum.fields|groupby('numeric_value') %} | 47 {%- for _, values in enum.fields|groupby('numeric_value') %} |
| 34 case {{enum|get_name_for_kind}}::{{values[0].name}}: | 48 case {{enum|get_name_for_kind}}::{{values[0].name}}: |
| 35 return os << "{{enum|get_name_for_kind}}:: | 49 return os << "{{enum|get_name_for_kind}}:: |
| 36 {%- if values|length > 1 -%} | 50 {%- if values|length > 1 -%} |
| 37 {{'{'}} | 51 {{'{'}} |
| 38 {%- endif -%} | 52 {%- endif -%} |
| 39 {{values|map(attribute='name')|join(', ')}} | 53 {{values|map(attribute='name')|join(', ')}} |
| 40 {%- if values|length > 1 -%} | 54 {%- if values|length > 1 -%} |
| 41 {{'}'}} | 55 {{'}'}} |
| 42 {%- endif -%} | 56 {%- endif -%} |
| 43 "; | 57 "; |
| 44 {%- endfor %} | 58 {%- endfor %} |
| 45 default: | 59 default: |
| 46 return os << "Unknown {{enum|get_name_for_kind}} value: " << static_cast<i
nt32_t>(value); | 60 return os << "Unknown {{enum|get_name_for_kind}} value: " << static_cast<i
nt32_t>(value); |
| 47 } | 61 } |
| 48 } | 62 } |
| 49 {%- endmacro %} | 63 {%- endmacro %} |
| 50 | 64 |
| 51 {%- macro is_valid_enum_def(enum, class_name = '') %} | 65 {%- macro is_known_enum_value(enum) %} |
| 52 {% if class_name != '' -%} | 66 inline bool IsKnownEnumValue({{enum|get_name_for_kind}} value) { |
| 53 // static | 67 return {{enum|get_qualified_name_for_kind(internal=True)}}::IsKnownValue( |
| 54 bool {{class_name}}:: | 68 static_cast<int32_t>(value)); |
| 55 {%- else -%} | |
| 56 {{"bool "}} | |
| 57 {%- endif -%} | |
| 58 {{enum.name}}_IsValidValue({{enum.name}} value) { | |
| 59 switch (static_cast<int32_t>(value)) { | |
| 60 {%- for enum_field in enum.fields|groupby('numeric_value') %} | |
| 61 case {{enum_field[0]}}: | |
| 62 {%- endfor %} | |
| 63 return true; | |
| 64 } | |
| 65 return false; | |
| 66 } | 69 } |
| 67 {%- endmacro %} | 70 {%- endmacro %} |
| OLD | NEW |