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

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

Issue 923033003: Implement unions as members of structs. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 5 years, 9 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 {# TODO(yzshen): Consider merging the template code for user-defined and 1 {# TODO(yzshen): Consider merging the template code for user-defined and
2 parameter data structs. That way we can eliminate validate(), fields(), 2 parameter data structs. That way we can eliminate validate(), fields(),
3 encodes() and decodes(). #} 3 encodes() and decodes(). #}
4 4
5 {# Validates the specified struct field, which is supposed to be an object 5 {# Validates the specified struct field, which is supposed to be an object
6 (struct/array/string/map/union). 6 (struct/array/string/map/union).
7 This macro is expanded by the validate() macro. #} 7 This macro is expanded by the validate() macro. #}
8 {%- macro _validate_object(struct, packed_field) %} 8 {%- macro _validate_object(struct, packed_field) %}
9 {%- set name = packed_field.field.name %} 9 {%- set name = packed_field.field.name %}
10 {%- set kind = packed_field.field.kind %} 10 {%- set kind = packed_field.field.kind %}
11 {%- set wrapper_type = kind|cpp_wrapper_type %} 11 {%- set wrapper_type = kind|cpp_wrapper_type %}
12 {%- if not kind|is_nullable_kind %} 12 {%- if not kind|is_nullable_kind %}
13 {%- if kind|is_union_kind %}
14 if (object->{{name}}.is_null()) {
15 {%- else %}
13 if (!object->{{name}}.offset) { 16 if (!object->{{name}}.offset) {
17 {%- endif %}
14 ReportValidationError( 18 ReportValidationError(
15 mojo::internal::VALIDATION_ERROR_UNEXPECTED_NULL_POINTER, 19 mojo::internal::VALIDATION_ERROR_UNEXPECTED_NULL_POINTER,
16 "null {{name}} field in {{struct.name}} struct"); 20 "null {{name}} field in {{struct.name}} struct");
17 return false; 21 return false;
18 } 22 }
19 {%- endif %} 23 {%- endif %}
24 {%- if not kind|is_union_kind %}
20 if (!mojo::internal::ValidateEncodedPointer(&object->{{name}}.offset)) { 25 if (!mojo::internal::ValidateEncodedPointer(&object->{{name}}.offset)) {
21 ReportValidationError(mojo::internal::VALIDATION_ERROR_ILLEGAL_POINTER); 26 ReportValidationError(mojo::internal::VALIDATION_ERROR_ILLEGAL_POINTER);
22 return false; 27 return false;
23 } 28 }
29 {%- endif %}
24 {%- if kind|is_array_kind or kind|is_string_kind %} 30 {%- if kind|is_array_kind or kind|is_string_kind %}
25 if (!{{wrapper_type}}::Data_::Validate< 31 if (!{{wrapper_type}}::Data_::Validate<
26 {{kind|get_array_validate_params|indent(10)}}>( 32 {{kind|get_array_validate_params|indent(10)}}>(
27 mojo::internal::DecodePointerRaw(&object->{{name}}.offset), 33 mojo::internal::DecodePointerRaw(&object->{{name}}.offset),
28 bounds_checker)) { 34 bounds_checker)) {
29 {%- elif kind|is_map_kind %} 35 {%- elif kind|is_map_kind %}
30 if (!{{wrapper_type}}::Data_::Validate< 36 if (!{{wrapper_type}}::Data_::Validate<
31 {{kind.value_kind|get_map_validate_params|indent(10)}}>( 37 {{kind.value_kind|get_map_validate_params|indent(10)}}>(
32 mojo::internal::DecodePointerRaw(&object->{{name}}.offset), 38 mojo::internal::DecodePointerRaw(&object->{{name}}.offset),
33 bounds_checker)) { 39 bounds_checker)) {
34 {%- elif kind|is_struct_kind %} 40 {%- elif kind|is_struct_kind %}
35 if (!{{kind|get_name_for_kind}}::Data_::Validate( 41 if (!{{kind|get_name_for_kind}}::Data_::Validate(
36 mojo::internal::DecodePointerRaw(&object->{{name}}.offset), 42 mojo::internal::DecodePointerRaw(&object->{{name}}.offset),
37 bounds_checker)) { 43 bounds_checker)) {
44 {%- elif kind|is_union_kind %}
45 // We already claimed the union's memory as part of the struct, so we don't
46 // try to claim the union's memory when validating the union.
47 if (!{{kind.name}}::Data_::Validate(&object->{{name}}, bounds_checker, true)) {
38 {%- else %} 48 {%- else %}
39 if (!{{wrapper_type}}::Data_::Validate( 49 if (!{{wrapper_type}}::Data_::Validate(
40 mojo::internal::DecodePointerRaw(&object->{{name}}.offset), 50 mojo::internal::DecodePointerRaw(&object->{{name}}.offset),
41 bounds_checker)) { 51 bounds_checker)) {
42 {%- endif %} 52 {%- endif %}
43 return false; 53 return false;
44 } 54 }
45 {%- endmacro %} 55 {%- endmacro %}
46 56
47 {# Validates the specified struct field, which is supposed to be a handle. 57 {# Validates the specified struct field, which is supposed to be a handle.
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
160 uint8_t padfinal_[{{pad}}]; 170 uint8_t padfinal_[{{pad}}];
161 {%- endif %} 171 {%- endif %}
162 {%- endif %} 172 {%- endif %}
163 {%- endmacro %} 173 {%- endmacro %}
164 174
165 {# Encodes the specified struct. 175 {# Encodes the specified struct.
166 Expands as the body of {{struct.name}}_Data::EncodePointersAndHandles(). #} 176 Expands as the body of {{struct.name}}_Data::EncodePointersAndHandles(). #}
167 {%- macro encodes(struct) -%} 177 {%- macro encodes(struct) -%}
168 MOJO_CHECK(header_.version == {{struct.versions[-1].version}}); 178 MOJO_CHECK(header_.version == {{struct.versions[-1].version}});
169 {%- for pf in struct.packed.packed_fields_in_ordinal_order %} 179 {%- for pf in struct.packed.packed_fields_in_ordinal_order %}
170 {%- if pf.field.kind|is_object_kind %} 180 {%- if pf.field.kind|is_union_kind %}
yzshen1 2015/02/25 21:07:00 Could you please move the "is_union_kind" part int
azani 2015/03/03 00:44:16 Done.
181 // TODO(azani): Encode pointers and handles.
182 {%- elif pf.field.kind|is_object_kind %}
171 mojo::internal::Encode(&{{pf.field.name}}, handles); 183 mojo::internal::Encode(&{{pf.field.name}}, handles);
172 {%- elif pf.field.kind|is_any_handle_kind %} 184 {%- elif pf.field.kind|is_any_handle_kind %}
173 mojo::internal::EncodeHandle(&{{pf.field.name}}, handles); 185 mojo::internal::EncodeHandle(&{{pf.field.name}}, handles);
174 {%- endif %} 186 {%- endif %}
175 {%- endfor %} 187 {%- endfor %}
176 {%- endmacro -%} 188 {%- endmacro -%}
177 189
178 {# Decodes the specified struct. 190 {# Decodes the specified struct.
179 This macro is expanded as the body of 191 This macro is expanded as the body of
180 {{struct.name}}_Data:DecodePointersAndHandles(). #} 192 {{struct.name}}_Data:DecodePointersAndHandles(). #}
181 {%- macro decodes(struct) -%} 193 {%- macro decodes(struct) -%}
182 // NOTE: The memory backing |this| may has be smaller than |sizeof(*this)|, if 194 // NOTE: The memory backing |this| may has be smaller than |sizeof(*this)|, if
183 // the message comes from an older version. 195 // the message comes from an older version.
184 {#- Before decoding fields introduced at a certain version, we need to add 196 {#- Before decoding fields introduced at a certain version, we need to add
185 a version check, which makes sure we skip further decoding if |this| 197 a version check, which makes sure we skip further decoding if |this|
186 is from an earlier version. |last_checked_version| records the last 198 is from an earlier version. |last_checked_version| records the last
187 version that we have added such version check. #} 199 version that we have added such version check. #}
188 {%- set last_checked_version = 0 %} 200 {%- set last_checked_version = 0 %}
189 {%- for pf in struct.packed.packed_fields_in_ordinal_order %} 201 {%- for pf in struct.packed.packed_fields_in_ordinal_order %}
190 {%- set name = pf.field.name %} 202 {%- set name = pf.field.name %}
191 {%- set kind = pf.field.kind %} 203 {%- set kind = pf.field.kind %}
192 {%- if kind|is_object_kind or kind|is_any_handle_kind %} 204 {%- if kind|is_object_kind or kind|is_any_handle_kind %}
193 {%- if pf.min_version > last_checked_version %} 205 {%- if pf.min_version > last_checked_version %}
194 {%- set last_checked_version = pf.min_version %} 206 {%- set last_checked_version = pf.min_version %}
195 if (header_.version < {{pf.min_version}}) 207 if (header_.version < {{pf.min_version}})
196 return; 208 return;
197 {%- endif %} 209 {%- endif %}
198 {%- if kind|is_object_kind %} 210 {%- if kind|is_union_kind %}
yzshen1 2015/02/25 21:07:00 ditto
azani 2015/03/03 00:44:16 Done.
211 // TODO(azani): Decode pointers and handles.
212 {%- elif kind|is_object_kind %}
199 mojo::internal::Decode(&{{name}}, handles); 213 mojo::internal::Decode(&{{name}}, handles);
200 {%- else %} 214 {%- else %}
201 mojo::internal::DecodeHandle(&{{name}}, handles); 215 mojo::internal::DecodeHandle(&{{name}}, handles);
202 {%- endif %} 216 {%- endif %}
203 {%- endif %} 217 {%- endif %}
204 {%- endfor %} 218 {%- endfor %}
205 {%- endmacro -%} 219 {%- endmacro -%}
206 220
207 {# Computes the serialized size for the specified struct. 221 {# Computes the serialized size for the specified struct.
208 |struct| is the struct definition. 222 |struct| is the struct definition.
(...skipping 27 matching lines...) Expand all
236 wrapper class. 250 wrapper class.
237 - method parameters/response parameters: the input is a list of 251 - method parameters/response parameters: the input is a list of
238 arguments. #} 252 arguments. #}
239 {%- macro serialize(struct, struct_display_name, input_field_pattern, output, bu ffer) -%} 253 {%- macro serialize(struct, struct_display_name, input_field_pattern, output, bu ffer) -%}
240 internal::{{struct.name}}_Data* {{output}} = 254 internal::{{struct.name}}_Data* {{output}} =
241 internal::{{struct.name}}_Data::New({{buffer}}); 255 internal::{{struct.name}}_Data::New({{buffer}});
242 {%- for pf in struct.packed.packed_fields_in_ordinal_order %} 256 {%- for pf in struct.packed.packed_fields_in_ordinal_order %}
243 {%- set input_field = input_field_pattern|format(pf.field.name) %} 257 {%- set input_field = input_field_pattern|format(pf.field.name) %}
244 {%- set name = pf.field.name %} 258 {%- set name = pf.field.name %}
245 {%- set kind = pf.field.kind %} 259 {%- set kind = pf.field.kind %}
246 {%- if kind|is_object_kind %} 260 {%- if kind|is_union_kind %}
yzshen1 2015/02/25 21:07:00 ditto.
azani 2015/03/03 00:44:15 Done.
261 internal::{{pf.field.kind.name}}_Data* {{pf.field.name}}_ptr = &result->{{pf.f ield.name}};
yzshen1 2015/02/25 21:07:00 Please use {{input_field}}, {{buffer}} and {{outpu
azani 2015/03/03 00:44:15 Done.
262 Serialize_(mojo::internal::Forward(input->{{pf.field.name}}), buf, &{{pf.field .name}}_ptr);
263 {%- elif kind|is_object_kind %}
247 {%- if kind|is_array_kind %} 264 {%- if kind|is_array_kind %}
248 mojo::SerializeArray_<{{kind|get_array_validate_params|indent(24)}}>( 265 mojo::SerializeArray_<{{kind|get_array_validate_params|indent(24)}}>(
249 mojo::internal::Forward({{input_field}}), {{buffer}}, &{{output}}->{{name} }.ptr); 266 mojo::internal::Forward({{input_field}}), {{buffer}}, &{{output}}->{{name} }.ptr);
250 {%- elif kind|is_map_kind %} 267 {%- elif kind|is_map_kind %}
251 mojo::SerializeMap_<{{kind.value_kind|get_map_validate_params|indent(24)}}>( 268 mojo::SerializeMap_<{{kind.value_kind|get_map_validate_params|indent(24)}}>(
252 mojo::internal::Forward({{input_field}}), {{buffer}}, &{{output}}->{{name} }.ptr); 269 mojo::internal::Forward({{input_field}}), {{buffer}}, &{{output}}->{{name} }.ptr);
253 {%- else %} 270 {%- else %}
254 Serialize_(mojo::internal::Forward({{input_field}}), {{buffer}}, &{{output}}-> {{name}}.ptr); 271 Serialize_(mojo::internal::Forward({{input_field}}), {{buffer}}, &{{output}}-> {{name}}.ptr);
255 {%- endif %} 272 {%- endif %}
256 {%- if not kind|is_nullable_kind %} 273 {%- if not kind|is_nullable_kind %}
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
299 {%- set last_checked_version = 0 %} 316 {%- set last_checked_version = 0 %}
300 {%- for pf in struct.packed.packed_fields_in_ordinal_order %} 317 {%- for pf in struct.packed.packed_fields_in_ordinal_order %}
301 {%- set output_field = output_field_pattern|format(pf.field.name) %} 318 {%- set output_field = output_field_pattern|format(pf.field.name) %}
302 {%- set name = pf.field.name %} 319 {%- set name = pf.field.name %}
303 {%- set kind = pf.field.kind %} 320 {%- set kind = pf.field.kind %}
304 {%- if pf.min_version > last_checked_version %} 321 {%- if pf.min_version > last_checked_version %}
305 {%- set last_checked_version = pf.min_version %} 322 {%- set last_checked_version = pf.min_version %}
306 if ({{input}}->header_.version < {{pf.min_version}}) 323 if ({{input}}->header_.version < {{pf.min_version}})
307 break; 324 break;
308 {%- endif %} 325 {%- endif %}
309 {%- if kind|is_object_kind %} 326 {%- if kind|is_union_kind %}
yzshen1 2015/02/25 21:07:00 Please move this if into the if for "is_object_kin
azani 2015/03/03 00:44:16 Done.
327 Deserialize_(&input->{{pf.field.name}}, &result->{{pf.field.name}});
yzshen1 2015/02/25 21:07:00 Please use {{output_field}}, {{input}}. Besides,
azani 2015/03/03 00:44:16 Done.
328 {%- elif kind|is_object_kind %}
310 Deserialize_({{input}}->{{name}}.ptr, &{{output_field}}); 329 Deserialize_({{input}}->{{name}}.ptr, &{{output_field}});
311 {%- elif kind|is_interface_kind or kind|is_interface_request_kind %} 330 {%- elif kind|is_interface_kind or kind|is_interface_request_kind %}
312 {{output_field}}.Bind(mojo::MakeScopedHandle(mojo::internal::FetchAndReset(& {{input}}->{{name}}))); 331 {{output_field}}.Bind(mojo::MakeScopedHandle(mojo::internal::FetchAndReset(& {{input}}->{{name}})));
313 {%- elif kind|is_any_handle_kind %} 332 {%- elif kind|is_any_handle_kind %}
314 {{output_field}}.reset(mojo::internal::FetchAndReset(&{{input}}->{{name}})); 333 {{output_field}}.reset(mojo::internal::FetchAndReset(&{{input}}->{{name}}));
315 {%- elif kind|is_enum_kind %} 334 {%- elif kind|is_enum_kind %}
316 {{output_field}} = static_cast<{{kind|cpp_wrapper_type}}>({{input}}->{{name} }); 335 {{output_field}} = static_cast<{{kind|cpp_wrapper_type}}>({{input}}->{{name} });
317 {%- else %} 336 {%- else %}
318 {{output_field}} = {{input}}->{{name}}; 337 {{output_field}} = {{input}}->{{name}};
319 {%- endif %} 338 {%- endif %}
320 {%- endfor %} 339 {%- endfor %}
321 } while (false); 340 } while (false);
322 {%- endmacro %} 341 {%- endmacro %}
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698