OLD | NEW |
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 static void SetToNull({{mojom_type}}Ptr* output) { output->reset(); } |
8 | 9 |
9 {%- for field in struct.fields %} | 10 {%- for field in struct.fields %} |
10 {%- set return_ref = field.kind|is_object_kind or | 11 {%- set return_ref = field.kind|is_object_kind or |
11 field.kind|is_any_handle_kind or | 12 field.kind|is_any_handle_kind or |
12 field.kind|is_interface_kind or | 13 field.kind|is_interface_kind or |
13 field.kind|is_associated_kind %} | 14 field.kind|is_associated_kind %} |
14 {%- if return_ref %} | 15 {%- if return_ref %} |
15 static decltype({{mojom_type}}::{{field.name}})& {{field.name}}( | 16 static decltype({{mojom_type}}::{{field.name}})& {{field.name}}( |
16 {{mojom_type}}Ptr& input) { | 17 {{mojom_type}}Ptr& input) { |
17 return input->{{field.name}}; | 18 return input->{{field.name}}; |
18 } | 19 } |
19 static const decltype({{mojom_type}}::{{field.name}})& {{field.name}}( | 20 static const decltype({{mojom_type}}::{{field.name}})& {{field.name}}( |
20 const {{mojom_type}}Ptr& input) { | 21 const {{mojom_type}}Ptr& input) { |
21 return input->{{field.name}}; | 22 return input->{{field.name}}; |
22 } | 23 } |
23 {%- else %} | 24 {%- else %} |
24 static decltype({{mojom_type}}::{{field.name}}) {{field.name}}( | 25 static decltype({{mojom_type}}::{{field.name}}) {{field.name}}( |
25 const {{mojom_type}}Ptr& input) { | 26 const {{mojom_type}}Ptr& input) { |
26 return input->{{field.name}}; | 27 return input->{{field.name}}; |
27 } | 28 } |
28 {%- endif %} | 29 {%- endif %} |
29 {%- endfor %} | 30 {%- endfor %} |
30 | 31 |
31 static bool ReadFromDataView({{mojom_type}}DataView input, | 32 static bool Read({{mojom_type}}DataView input, {{mojom_type}}Ptr* output); |
32 {{mojom_type}}Ptr* output); | |
33 }; | 33 }; |
34 | 34 |
35 namespace internal { | 35 namespace internal { |
36 | 36 |
37 template <typename MaybeConstUserType> | 37 template <typename MaybeConstUserType> |
38 struct Serializer<{{mojom_type}}Ptr, MaybeConstUserType> { | 38 struct Serializer<{{mojom_type}}Ptr, MaybeConstUserType> { |
39 using UserType = typename std::remove_const<MaybeConstUserType>::type; | 39 using UserType = typename std::remove_const<MaybeConstUserType>::type; |
40 using Traits = StructTraits<{{mojom_type}}, UserType>; | 40 using Traits = StructTraits<{{mojom_type}}, UserType>; |
41 | 41 |
42 static size_t PrepareToSerialize(MaybeConstUserType& input, | 42 static size_t PrepareToSerialize(MaybeConstUserType& input, |
(...skipping 15 matching lines...) Expand all Loading... |
58 "context", True)|indent(4)}} | 58 "context", True)|indent(4)}} |
59 *output = result; | 59 *output = result; |
60 } else { | 60 } else { |
61 *output = nullptr; | 61 *output = nullptr; |
62 } | 62 } |
63 } | 63 } |
64 | 64 |
65 static bool Deserialize({{data_type}}* input, | 65 static bool Deserialize({{data_type}}* input, |
66 UserType* output, | 66 UserType* output, |
67 SerializationContext* context) { | 67 SerializationContext* context) { |
68 return ReadCaller<Traits, HasReadFromDataViewMethod<Traits>::value>::Run( | 68 if (!input) |
69 input, output, context); | 69 return CallSetToNullIfExists<Traits>(output); |
| 70 |
| 71 {{mojom_type}}DataView data_view(input, context); |
| 72 return Traits::Read(data_view, output); |
70 } | 73 } |
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 }; | 74 }; |
97 | 75 |
98 } // namespace internal | 76 } // namespace internal |
OLD | NEW |