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

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

Issue 1635613002: [mojo-bindings] Support reuse of native enum classes (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix more missing public_deps Created 4 years, 10 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 unified diff | Download patch
OLDNEW
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 ---#} 3 ---#}
4 {%- macro enum_decl(enum) %} 4 {%- macro enum_decl(enum) %}
5 {%- if enum.native_only %}
6 using {{enum.name}} = {{enum|cpp_wrapper_type}};
7 {%- else %}
5 enum class {{enum.name}} : int32_t { 8 enum class {{enum.name}} : int32_t {
6 {%- for field in enum.fields %} 9 {%- for field in enum.fields %}
7 {%- if field.value %} 10 {%- if field.value %}
8 {{field.name}} = {{field.value|expression_to_text}}, 11 {{field.name}} = {{field.value|expression_to_text}},
9 {%- else %} 12 {%- else %}
10 {{field.name}}, 13 {{field.name}},
11 {%- endif %} 14 {%- endif %}
12 {%- endfor %} 15 {%- endfor %}
13 }; 16 };
17 {%- endif %}
14 {%- endmacro %} 18 {%- endmacro %}
15 19
16 {%- macro enum_data_decl(enum) %} 20 {%- macro enum_data_decl(enum) %}
17 struct {{enum.name}}_Data { 21 struct {{enum.name}}_Data {
18 public: 22 public:
19 // Used to identify Mojom Enum Data Classes. 23 // Used to identify Mojom Enum Data Classes.
20 typedef void MojomEnumDataType; 24 typedef void MojomEnumDataType;
21 25
22 static bool const kIsExtensible = {% if enum.extensible %}true{% else %}false{ % endif %}; 26 static bool const kIsExtensible = {% if enum.extensible %}true{% else %}false{ % endif %};
23 27
24 static bool IsKnownValue(int32_t value) { 28 static bool IsKnownValue(int32_t value) {
29 {%- if enum.native_only %}
30 return mojo::EnumTraits<{{enum|cpp_wrapper_type}}>::IsKnownValue(value);
31 {%- else %}
25 switch (value) { 32 switch (value) {
26 {%- for enum_field in enum.fields|groupby('numeric_value') %} 33 {%- for enum_field in enum.fields|groupby('numeric_value') %}
27 case {{enum_field[0]}}: 34 case {{enum_field[0]}}:
28 {%- endfor %} 35 {%- endfor %}
29 return true; 36 return true;
30 } 37 }
31 return false; 38 return false;
39 {%- endif %}
32 } 40 }
33 41
34 int32_t value; 42 int32_t value;
35 }; 43 };
36 {%- endmacro %} 44 {%- endmacro %}
37 45
38 {#--- macros for enum-associated functions. Namely: 46 {#--- macros for enum-associated functions. Namely:
39 * operator<<(): outputs the given enum value. 47 * operator<<(): outputs the given enum value.
40 * IsKnownEnumValue(): returns true if the given enum value exists in this 48 * IsKnownEnumValue(): returns true if the given enum value exists in this
41 generated version of enum. 49 generated version of enum.
42 ---#} 50 ---#}
43 51
44 {%- macro enum_stream_operator(enum) %} 52 {%- macro enum_stream_operator(enum) %}
53 {%- if not enum.native_only %}
45 inline std::ostream& operator<<(std::ostream& os, {{enum|get_name_for_kind}} val ue) { 54 inline std::ostream& operator<<(std::ostream& os, {{enum|get_name_for_kind}} val ue) {
46 switch(value) { 55 switch(value) {
47 {%- for _, values in enum.fields|groupby('numeric_value') %} 56 {%- for _, values in enum.fields|groupby('numeric_value') %}
48 case {{enum|get_name_for_kind}}::{{values[0].name}}: 57 case {{enum|get_name_for_kind}}::{{values[0].name}}:
49 return os << "{{enum|get_name_for_kind}}:: 58 return os << "{{enum|get_name_for_kind}}::
50 {%- if values|length > 1 -%} 59 {%- if values|length > 1 -%}
51 {{'{'}} 60 {{'{'}}
52 {%- endif -%} 61 {%- endif -%}
53 {{values|map(attribute='name')|join(', ')}} 62 {{values|map(attribute='name')|join(', ')}}
54 {%- if values|length > 1 -%} 63 {%- if values|length > 1 -%}
55 {{'}'}} 64 {{'}'}}
56 {%- endif -%} 65 {%- endif -%}
57 "; 66 ";
58 {%- endfor %} 67 {%- endfor %}
59 default: 68 default:
60 return os << "Unknown {{enum|get_name_for_kind}} value: " << static_cast<i nt32_t>(value); 69 return os << "Unknown {{enum|get_name_for_kind}} value: " << static_cast<i nt32_t>(value);
61 } 70 }
62 } 71 }
72 {%- endif %}
63 {%- endmacro %} 73 {%- endmacro %}
64 74
65 {%- macro is_known_enum_value(enum) %} 75 {%- macro is_known_enum_value(enum) %}
66 inline bool IsKnownEnumValue({{enum|get_name_for_kind}} value) { 76 inline bool IsKnownEnumValue({{enum|get_name_for_kind}} value) {
67 return {{enum|get_qualified_name_for_kind(internal=True)}}::IsKnownValue( 77 return {{enum|get_qualified_name_for_kind(internal=True)}}::IsKnownValue(
68 static_cast<int32_t>(value)); 78 static_cast<int32_t>(value));
69 } 79 }
70 {%- endmacro %} 80 {%- endmacro %}
OLDNEW
« no previous file with comments | « mojo/public/cpp/bindings/enum_traits.h ('k') | mojo/public/tools/bindings/generators/cpp_templates/module.h.tmpl » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698