Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(580)

Unified Diff: mojo/public/tools/bindings/generators/cpp_templates/enum_macros.tmpl

Issue 2247083003: Mojo C++ bindings: extract code shared by different variants. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: . Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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 %}

Powered by Google App Engine
This is Rietveld 408576698