OLD | NEW |
1 {# TODO(yzshen): Make these templates more readable. #} | 1 {# TODO(yzshen): Make these templates more readable. #} |
2 | 2 |
3 {# Computes the serialized size for the specified struct. | 3 {# Computes the serialized size for the specified struct. |
4 |struct| is the struct definition. | 4 |struct| is the struct definition. |
5 |input_field_pattern| should be a pattern that contains one string | 5 |input_field_pattern| should be a pattern that contains one string |
6 placeholder, for example, "input->%s", "p_%s". The placeholder will be | 6 placeholder, for example, "input->%s", "p_%s". The placeholder will be |
7 substituted with struct field names to refer to the input fields. | 7 substituted with struct field names to refer to the input fields. |
8 |context| is the name of the serialization context. | 8 |context| is the name of the serialization context. |
9 |input_may_be_temp| indicates whether any input may be temporary obejcts. | 9 |input_may_be_temp| indicates whether any input may be temporary obejcts. |
10 We need to assign temporary objects to local variables before passing it to | 10 We need to assign temporary objects to local variables before passing it to |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 {%- elif kind|is_enum_kind %} | 128 {%- elif kind|is_enum_kind %} |
129 {{output}}->{{name}} = static_cast<int32_t>({{input_field}}); | 129 {{output}}->{{name}} = static_cast<int32_t>({{input_field}}); |
130 {%- else %} | 130 {%- else %} |
131 {{output}}->{{name}} = {{input_field}}; | 131 {{output}}->{{name}} = {{input_field}}; |
132 {%- endif %} | 132 {%- endif %} |
133 {%- endfor %} | 133 {%- endfor %} |
134 {%- endmacro -%} | 134 {%- endmacro -%} |
135 | 135 |
136 {# Deserializes the specified struct. | 136 {# Deserializes the specified struct. |
137 |struct| is the struct definition. | 137 |struct| is the struct definition. |
138 |input| is the name of the input struct instance. | 138 |input| is the name of the input struct data view. It is expected to be |
| 139 non-null. |
139 |output_field_pattern| should be a pattern that contains one string | 140 |output_field_pattern| should be a pattern that contains one string |
140 placeholder, for example, "result->%s", "p_%s". The placeholder will be | 141 placeholder, for example, "result->%s", "p_%s". The placeholder will be |
141 substituted with struct field names to refer to the output fields. | 142 substituted with struct field names to refer to the output fields. |
142 |context| is the name of the serialization context. | 143 |context| is the name of the serialization context. |
143 |success| is the name of a bool variable to track success of the operation. | 144 |success| is the name of a bool variable to track success of the operation. |
144 This macro is expanded to do deserialization for both: | 145 This macro is expanded to do deserialization for both: |
145 - user-defined structs: the output is an instance of the corresponding | 146 - user-defined structs: the output is an instance of the corresponding |
146 struct wrapper class. | 147 struct wrapper class. |
147 - method parameters/response parameters: the output is a list of | 148 - method parameters/response parameters: the output is a list of |
148 arguments. #} | 149 arguments. #} |
149 {%- macro deserialize(struct, input, output_field_pattern, context, success) -%} | 150 {%- macro deserialize(struct, input, output_field_pattern, success) -%} |
150 do { | 151 DCHECK(!{{input}}.is_null()); |
151 // NOTE: The memory backing |{{input}}| may be smaller than | 152 |
152 // |sizeof(*{{input}})| if the message comes from an older version. | |
153 {#- Before deserialize fields introduced at a certain version, we need to add | |
154 a version check, which makes sure we skip further deserialization if | |
155 |input| is from an earlier version. |last_checked_version| records the | |
156 last version that we have added such version check. #} | |
157 {%- set last_checked_version = 0 %} | |
158 {%- for pf in struct.packed.packed_fields_in_ordinal_order %} | 153 {%- for pf in struct.packed.packed_fields_in_ordinal_order %} |
159 {%- set output_field = output_field_pattern|format(pf.field.name) %} | 154 {%- set output_field = output_field_pattern|format(pf.field.name) %} |
160 {%- set name = pf.field.name %} | 155 {%- set name = pf.field.name %} |
161 {%- set kind = pf.field.kind %} | 156 {%- set kind = pf.field.kind %} |
162 {%- if pf.min_version > last_checked_version %} | 157 {%- if kind|is_object_kind %} |
163 {%- set last_checked_version = pf.min_version %} | 158 if (!{{input}}.Read{{name|under_to_camel}}(&{{output_field}})) |
164 if ({{input}}->header_.version < {{pf.min_version}}) | 159 {{success}} = false; |
165 break; | 160 {%- elif kind|is_interface_kind or kind|is_any_handle_kind or |
166 {%- endif %} | 161 kind|is_associated_kind %} |
167 {%- set serializer_type = kind|unmapped_type_for_serializer %} | 162 {{output_field}} = {{input}}.Take{{name|under_to_camel}}(); |
168 {%- if kind|is_union_kind %} | |
169 if (!mojo::internal::Deserialize<{{serializer_type}}>( | |
170 &{{input}}->{{name}}, &{{output_field}}, {{context}})) { | |
171 {{success}} = false; | |
172 } | |
173 {%- elif kind|is_object_kind %} | |
174 if (!mojo::internal::Deserialize<{{serializer_type}}>( | |
175 {{input}}->{{name}}.ptr, &{{output_field}}, {{context}})) { | |
176 {{success}} = false; | |
177 } | |
178 {%- elif kind|is_interface_kind %} | |
179 mojo::internal::InterfaceDataToPointer( | |
180 &{{input}}->{{name}}, &{{output_field}}, {{context}}); | |
181 {%- elif kind|is_interface_request_kind %} | |
182 {{output_field}}.Bind( | |
183 ({{context}})->handles.TakeHandleAs<mojo::MessagePipeHandle>( | |
184 {{input}}->{{name}})); | |
185 {%- elif kind|is_any_handle_kind %} | |
186 {{output_field}} = ({{context}})->handles.TakeHandleAs< | |
187 typename decltype({{output_field}})::RawHandleType>( | |
188 {{input}}->{{name}}); | |
189 {%- elif kind|is_associated_interface_kind %} | |
190 mojo::internal::AssociatedInterfaceDataToPtrInfo(&{{input}}->{{name}}, &{{ou
tput_field}}, ({{context}})->router.get()); | |
191 {%- elif kind|is_associated_interface_request_kind %} | |
192 mojo::internal::AssociatedInterfaceRequestHelper::SetHandle( | |
193 &{{output_field}}, | |
194 ({{context}})->router->CreateLocalEndpointHandle(mojo::internal::FetchAn
dReset(&{{input}}->{{name}}))); | |
195 {%- elif kind|is_enum_kind %} | |
196 {{output_field}} = static_cast<{{kind|get_qualified_name_for_kind}}>({{input
}}->{{name}}); | |
197 {%- else %} | 163 {%- else %} |
198 {{output_field}} = {{input}}->{{name}}; | 164 {{output_field}} = {{input}}.{{name}}(); |
199 {%- endif %} | 165 {%- endif %} |
200 {%- endfor %} | 166 {%- endfor %} |
201 } while (false); | |
202 {%- endmacro %} | 167 {%- endmacro %} |
OLD | NEW |