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

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

Issue 1527183003: Change mojo enums to be scoped enums in the generated C++ bindings. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@mojo-binding-equals
Patch Set: rebase Created 4 years, 11 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 0e44acd57e65ce0bc4b131b73b8d5924eb3dd8a3..d11b198bc658d7623e77a8611659a98d8a118e92 100644
--- a/mojo/public/tools/bindings/generators/cpp_templates/enum_macros.tmpl
+++ b/mojo/public/tools/bindings/generators/cpp_templates/enum_macros.tmpl
@@ -4,12 +4,12 @@
which case associated functions need to be static.
---#}
{%- macro enum_decl(enum, is_static=false) %}
-enum {{enum.name}} : int32_t {
+enum class {{enum.name}} : int32_t {
{%- for field in enum.fields %}
{%- if field.value %}
- {{enum.name|to_all_caps}}_{{field.name}} = {{field.value|expression_to_text}},
+ {{field.name}} = {{field.value|expression_to_text}},
{%- else %}
- {{enum.name|to_all_caps}}_{{field.name}},
+ {{field.name}},
{%- endif %}
{%- endfor %}
};
@@ -27,6 +27,27 @@ enum {{enum.name}} : int32_t {
bool {{enum.name}}_IsValidValue({{enum.name}} value);
{%- endmacro %}
+{%- 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_valid_enum_def(enum, class_name = '') %}
{% if class_name != '' -%}
// static

Powered by Google App Engine
This is Rietveld 408576698