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

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

Issue 2339413004: Allow Mojo structs as map keys (Closed)
Patch Set: Remove left-over import Created 4 years, 3 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 {%- set mojom_type = union|get_qualified_name_for_kind %} 1 {%- set mojom_type = union|get_qualified_name_for_kind %}
2 2
3 template <> 3 template <>
4 struct {{export_attribute}} UnionTraits<{{mojom_type}}::DataView, 4 struct {{export_attribute}} UnionTraits<{{mojom_type}}::DataView,
5 {{mojom_type}}Ptr> { 5 {{mojom_type}}Ptr> {
6 static bool IsNull(const {{mojom_type}}Ptr& input) { return !input; } 6 static bool IsNull(const {{mojom_type}}Ptr& input) { return !input; }
7 static void SetToNull({{mojom_type}}Ptr* output) { output->reset(); } 7 static void SetToNull({{mojom_type}}Ptr* output) { output->reset(); }
8 8
9 static {{mojom_type}}::Tag GetTag(const {{mojom_type}}Ptr& input) { 9 static {{mojom_type}}::Tag GetTag(const {{mojom_type}}Ptr& input) {
10 return input->which(); 10 return input->which();
11 } 11 }
12 12
13 {%- for field in union.fields %} 13 {%- for field in union.fields %}
14 {%- set return_ref = field.kind|is_object_kind or 14 {%- set return_ref = field.kind|is_object_kind or
15 field.kind|is_any_handle_or_interface_kind %} 15 field.kind|is_any_handle_or_interface_kind %}
16 {%- if return_ref %} 16 {%- if return_ref %}
17 {%- if field.kind|contains_handles %}
17 static decltype(std::declval<{{mojom_type}}>().get_{{field.name}}()) 18 static decltype(std::declval<{{mojom_type}}>().get_{{field.name}}())
18 {{field.name}}({{mojom_type}}Ptr& input) { 19 {{field.name}}({{mojom_type}}Ptr& input) {
19 return input->get_{{field.name}}(); 20 return input->get_{{field.name}}();
20 } 21 }
22 {%- else %}
23 {# We want the field accessor to be const whenever possible to allow
24 structs to be used as map keys. This complicated looking use of decltype
25 etc. gives us a const reference to the field type. #}
26 {# TODO(tibell): Output the actual type of the field instead of using decltype
27 to compute it. #}
28 static std::add_lvalue_reference<const std::remove_reference<decltype(std::dec lval<{{mojom_type}}>().get_{{field.name}}())>::type>::type
yzshen1 2016/09/20 23:44:40 This is quite hard to understand, maybe as you sai
tibell 2016/09/22 05:17:23 Done.
29 {{field.name}}(const {{mojom_type}}Ptr& input) {
30 return input->get_{{field.name}}();
31 }
32 {%- endif %}
21 {%- else %} 33 {%- else %}
22 static decltype(std::declval<{{mojom_type}}>().get_{{field.name}}()) 34 static decltype(std::declval<{{mojom_type}}>().get_{{field.name}}())
23 {{field.name}}(const {{mojom_type}}Ptr& input) { 35 {{field.name}}(const {{mojom_type}}Ptr& input) {
24 return input->get_{{field.name}}(); 36 return input->get_{{field.name}}();
25 } 37 }
26 {%- endif %} 38 {%- endif %}
27 {%- endfor %} 39 {%- endfor %}
28 40
29 static bool Read({{mojom_type}}::DataView input, {{mojom_type}}Ptr* output); 41 static bool Read({{mojom_type}}::DataView input, {{mojom_type}}Ptr* output);
30 }; 42 };
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698