Chromium Code Reviews| Index: mojo/public/tools/bindings/generators/cpp_templates/enum_macros.tmpl |
| diff --git a/mojo/public/tools/bindings/generators/cpp_templates/enum_macros.tmpl b/mojo/public/tools/bindings/generators/cpp_templates/enum_macros.tmpl |
| index 0a446a7cb9abb307e62c14e6e4139a38c0bff2d0..087f225517db74e4772c5338adf30c56bfad60e0 100644 |
| --- a/mojo/public/tools/bindings/generators/cpp_templates/enum_macros.tmpl |
| +++ b/mojo/public/tools/bindings/generators/cpp_templates/enum_macros.tmpl |
| @@ -1,20 +1,49 @@ |
| {#--- |
| 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 %} |
| +{%- set enum_name = enum|get_name_for_kind(flatten_nested_kind=True) %} |
|
Ken Rockot(use gerrit already)
2016/08/17 15:10:55
clever solution :)
|
| +enum class {{enum_name}} : int32_t { |
| +{%- for field in enum.fields %} |
| +{%- if field.value %} |
| {{field.name}} = {{field.value|expression_to_text}}, |
| -{%- else %} |
| +{%- else %} |
| {{field.name}}, |
| -{%- endif %} |
| -{%- endfor %} |
| +{%- endif %} |
| +{%- endfor %} |
| }; |
| + |
| +inline std::ostream& operator<<(std::ostream& os, {{enum_name}} value) { |
| + switch(value) { |
| +{%- for _, values in enum.fields|groupby('numeric_value') %} |
| + case {{enum_name}}::{{values[0].name}}: |
| + return os << "{{enum_name}}:: |
| +{%- if values|length > 1 -%} |
| + {{'{'}} |
| +{%- endif -%} |
| + {{values|map(attribute='name')|join(', ')}} |
| +{%- if values|length > 1 -%} |
| + {{'}'}} |
| +{%- endif -%} |
| + "; |
| +{%- endfor %} |
| + default: |
| + return os << "Unknown {{enum_name}} value: " << static_cast<int32_t>(value); |
| + } |
| +} |
| + |
| +{#- Returns true if the given enum value exists in this version of enum. #} |
| +inline bool IsKnownEnumValue({{enum_name}} value) { |
| + return {{enum|get_name_for_kind(internal=True, |
| + flatten_nested_kind=True)}}::IsKnownValue( |
| + static_cast<int32_t>(value)); |
| +} |
| {%- endmacro %} |
| {%- macro enum_data_decl(enum) %} |
| -struct {{enum.name}}_Data { |
| +{%- set enum_name = enum|get_name_for_kind(flatten_nested_kind=True) %} |
| +struct {{enum_name}}_Data { |
| public: |
| static bool const kIsExtensible = {% if enum.extensible %}true{% else %}false{% endif %}; |
| @@ -42,42 +71,10 @@ struct {{enum.name}}_Data { |
| }; |
| {%- 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 %} |
| - |
| {%- macro enum_hash(enum) %} |
| +{%- set enum_name = enum|get_qualified_name_for_kind( |
| + flatten_nested_kind=True) %} |
| template <> |
| -struct hash<{{enum|get_qualified_name_for_kind}}> |
| - : public mojo::internal::EnumHashImpl<{{enum|get_qualified_name_for_kind}}> {}; |
| +struct hash<{{enum_name}}> |
| + : public mojo::internal::EnumHashImpl<{{enum_name}}> {}; |
| {%- endmacro %} |