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

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

Issue 1966933002: Mojo C++ bindings: switch the existing usage of StructTraits to use the new data view interface. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@26_reader
Patch Set: Created 4 years, 7 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 {%- import "struct_macros.tmpl" as struct_macros %} 1 {%- import "struct_macros.tmpl" as struct_macros %}
2 {%- set mojom_type = struct|get_qualified_name_for_kind %} 2 {%- set mojom_type = struct|get_qualified_name_for_kind %}
3 {%- set data_type = struct|get_qualified_name_for_kind(internal=True) %} 3 {%- set data_type = struct|get_qualified_name_for_kind(internal=True) %}
4 4
5 template <> 5 template <>
6 struct StructTraits<{{mojom_type}}, {{mojom_type}}Ptr> { 6 struct StructTraits<{{mojom_type}}, {{mojom_type}}Ptr> {
7 static bool IsNull(const {{mojom_type}}Ptr& input) { return !input; } 7 static bool IsNull(const {{mojom_type}}Ptr& input) { return !input; }
8 8
9 {%- for field in struct.fields %} 9 {%- for field in struct.fields %}
10 {%- set return_ref = field.kind|is_object_kind or 10 {%- set return_ref = field.kind|is_object_kind or
(...skipping 10 matching lines...) Expand all
21 return input->{{field.name}}; 21 return input->{{field.name}};
22 } 22 }
23 {%- else %} 23 {%- else %}
24 static decltype({{mojom_type}}::{{field.name}}) {{field.name}}( 24 static decltype({{mojom_type}}::{{field.name}}) {{field.name}}(
25 const {{mojom_type}}Ptr& input) { 25 const {{mojom_type}}Ptr& input) {
26 return input->{{field.name}}; 26 return input->{{field.name}};
27 } 27 }
28 {%- endif %} 28 {%- endif %}
29 {%- endfor %} 29 {%- endfor %}
30 30
31 static bool ReadFromDataView({{mojom_type}}DataView input, 31 static bool Read({{mojom_type}}DataView input, {{mojom_type}}Ptr* output);
32 {{mojom_type}}Ptr* output);
33 }; 32 };
34 33
35 namespace internal { 34 namespace internal {
36 35
37 template <typename MaybeConstUserType> 36 template <typename MaybeConstUserType>
38 struct Serializer<{{mojom_type}}Ptr, MaybeConstUserType> { 37 struct Serializer<{{mojom_type}}Ptr, MaybeConstUserType> {
39 using UserType = typename std::remove_const<MaybeConstUserType>::type; 38 using UserType = typename std::remove_const<MaybeConstUserType>::type;
40 using Traits = StructTraits<{{mojom_type}}, UserType>; 39 using Traits = StructTraits<{{mojom_type}}, UserType>;
41 40
42 static size_t PrepareToSerialize(MaybeConstUserType& input, 41 static size_t PrepareToSerialize(MaybeConstUserType& input,
(...skipping 15 matching lines...) Expand all
58 "context", True)|indent(4)}} 57 "context", True)|indent(4)}}
59 *output = result; 58 *output = result;
60 } else { 59 } else {
61 *output = nullptr; 60 *output = nullptr;
62 } 61 }
63 } 62 }
64 63
65 static bool Deserialize({{data_type}}* input, 64 static bool Deserialize({{data_type}}* input,
66 UserType* output, 65 UserType* output,
67 SerializationContext* context) { 66 SerializationContext* context) {
68 return ReadCaller<Traits, HasReadFromDataViewMethod<Traits>::value>::Run( 67 {{mojom_type}}DataView data_view(input, context);
69 input, output, context); 68 return Traits::Read(data_view, output);
70 } 69 }
71
72
73 public:
74 template <typename Traits, bool use_read_from_data_view>
75 struct ReadCaller;
76
77 template <typename Traits>
78 struct ReadCaller<Traits, false> {
79 static bool Run({{data_type}}* input,
80 UserType* output,
81 SerializationContext* context) {
82 {{mojom_type}}_Reader reader(input, context);
83 return Traits::Read(reader, output);
84 }
85 };
86
87 template <typename Traits>
88 struct ReadCaller<Traits, true> {
89 static bool Run({{data_type}}* input,
90 UserType* output,
91 SerializationContext* context) {
92 {{mojom_type}}DataView data_view(input, context);
93 return Traits::ReadFromDataView(data_view, output);
94 }
95 };
96 }; 70 };
97 71
98 } // namespace internal 72 } // namespace internal
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698