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

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, 8 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): 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 This macro is expanded to compute seriailized size for both: 8 This macro is expanded to compute seriailized size for both:
9 - user-defined structs: the input is an instance of the corresponding struct 9 - user-defined structs: the input is an instance of the corresponding struct
10 wrapper class. 10 wrapper class.
11 - method parameters/response parameters: the input is a list of 11 - method parameters/response parameters: the input is a list of
12 arguments. 12 arguments.
13 It declares |size| of type size_t to store the resulting size. #} 13 It declares |size| of type size_t to store the resulting size. #}
14 {%- macro get_serialized_size(struct, input_field_pattern) -%} 14 {%- macro get_serialized_size(struct, input_field_pattern) -%}
15 size_t size = sizeof(internal::{{struct.name}}_Data); 15 size_t size = sizeof(internal::{{struct.name}}_Data);
16 {%- for pf in struct.packed.packed_fields_in_ordinal_order if pf.field.kind|is _object_kind %} 16 {%- for pf in struct.packed.packed_fields_in_ordinal_order if pf.field.kind|is _object_kind %}
17 {%- if pf.field.kind|is_union_kind %}
18 size += GetSerializedSize_({{input_field_pattern|format(pf.field.name)}}, true );
19 {%- else %}
17 size += GetSerializedSize_({{input_field_pattern|format(pf.field.name)}}); 20 size += GetSerializedSize_({{input_field_pattern|format(pf.field.name)}});
21 {%- endif %}
18 {%- endfor %} 22 {%- endfor %}
19 {%- endmacro -%} 23 {%- endmacro -%}
20 24
21 {# Serializes the specified struct. 25 {# Serializes the specified struct.
22 |struct| is the struct definition. 26 |struct| is the struct definition.
23 |struct_display_name| is the display name for the struct that can be showed 27 |struct_display_name| is the display name for the struct that can be showed
24 in error/log messages, for example, "FooStruct", "FooMethod request". 28 in error/log messages, for example, "FooStruct", "FooMethod request".
25 |input_field_pattern| should be a pattern that contains one string 29 |input_field_pattern| should be a pattern that contains one string
26 placeholder, for example, "input->%s", "p_%s". The placeholder will be 30 placeholder, for example, "input->%s", "p_%s". The placeholder will be
27 substituted with struct field names to refer to the input fields. 31 substituted with struct field names to refer to the input fields.
(...skipping 11 matching lines...) Expand all
39 {%- set input_field = input_field_pattern|format(pf.field.name) %} 43 {%- set input_field = input_field_pattern|format(pf.field.name) %}
40 {%- set name = pf.field.name %} 44 {%- set name = pf.field.name %}
41 {%- set kind = pf.field.kind %} 45 {%- set kind = pf.field.kind %}
42 {%- if kind|is_object_kind %} 46 {%- if kind|is_object_kind %}
43 {%- if kind|is_array_kind %} 47 {%- if kind|is_array_kind %}
44 mojo::SerializeArray_<{{kind|get_array_validate_params|indent(24)}}>( 48 mojo::SerializeArray_<{{kind|get_array_validate_params|indent(24)}}>(
45 mojo::internal::Forward({{input_field}}), {{buffer}}, &{{output}}->{{name} }.ptr); 49 mojo::internal::Forward({{input_field}}), {{buffer}}, &{{output}}->{{name} }.ptr);
46 {%- elif kind|is_map_kind %} 50 {%- elif kind|is_map_kind %}
47 mojo::SerializeMap_<{{kind.value_kind|get_map_validate_params|indent(24)}}>( 51 mojo::SerializeMap_<{{kind.value_kind|get_map_validate_params|indent(24)}}>(
48 mojo::internal::Forward({{input_field}}), {{buffer}}, &{{output}}->{{name} }.ptr); 52 mojo::internal::Forward({{input_field}}), {{buffer}}, &{{output}}->{{name} }.ptr);
53 {%- elif kind|is_union_kind %}
54 internal::{{kind.name}}_Data* {{name}}_ptr = &{{output}}->{{name}};
55 SerializeUnion_(mojo::internal::Forward({{input_field}}), {{buffer}}, &{{name} }_ptr, true);
49 {%- else %} 56 {%- else %}
50 Serialize_(mojo::internal::Forward({{input_field}}), {{buffer}}, &{{output}}-> {{name}}.ptr); 57 Serialize_(mojo::internal::Forward({{input_field}}), {{buffer}}, &{{output}}-> {{name}}.ptr);
51 {%- endif %} 58 {%- endif %}
52 {%- if not kind|is_nullable_kind %} 59 {%- if not kind|is_nullable_kind %}
53 MOJO_INTERNAL_DLOG_SERIALIZATION_WARNING( 60 MOJO_INTERNAL_DLOG_SERIALIZATION_WARNING(
61 {%- if kind|is_union_kind %}
62 {{output}}->{{name}}.is_null(),
63 {%- else %}
54 !{{output}}->{{name}}.ptr, 64 !{{output}}->{{name}}.ptr,
65 {%- endif %}
55 mojo::internal::VALIDATION_ERROR_UNEXPECTED_NULL_POINTER, 66 mojo::internal::VALIDATION_ERROR_UNEXPECTED_NULL_POINTER,
56 "null {{name}} in {{struct_display_name}}"); 67 "null {{name}} in {{struct_display_name}}");
57 {%- endif %} 68 {%- endif %}
58 {%- elif kind|is_any_handle_kind %} 69 {%- elif kind|is_any_handle_kind %}
59 {%- if kind|is_interface_kind or kind|is_interface_request_kind %} 70 {%- if kind|is_interface_kind or kind|is_interface_request_kind %}
60 {{output}}->{{name}} = {{input_field}}.PassMessagePipe().release(); 71 {{output}}->{{name}} = {{input_field}}.PassMessagePipe().release();
61 {%- else %} 72 {%- else %}
62 {{output}}->{{name}} = {{input_field}}.release(); 73 {{output}}->{{name}} = {{input_field}}.release();
63 {%- endif %} 74 {%- endif %}
64 {%- if not kind|is_nullable_kind %} 75 {%- if not kind|is_nullable_kind %}
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
96 {%- for pf in struct.packed.packed_fields_in_ordinal_order %} 107 {%- for pf in struct.packed.packed_fields_in_ordinal_order %}
97 {%- set output_field = output_field_pattern|format(pf.field.name) %} 108 {%- set output_field = output_field_pattern|format(pf.field.name) %}
98 {%- set name = pf.field.name %} 109 {%- set name = pf.field.name %}
99 {%- set kind = pf.field.kind %} 110 {%- set kind = pf.field.kind %}
100 {%- if pf.min_version > last_checked_version %} 111 {%- if pf.min_version > last_checked_version %}
101 {%- set last_checked_version = pf.min_version %} 112 {%- set last_checked_version = pf.min_version %}
102 if ({{input}}->header_.version < {{pf.min_version}}) 113 if ({{input}}->header_.version < {{pf.min_version}})
103 break; 114 break;
104 {%- endif %} 115 {%- endif %}
105 {%- if kind|is_object_kind %} 116 {%- if kind|is_object_kind %}
117 {%- if kind|is_union_kind %}
118 Deserialize_(&{{input}}->{{name}}, &{{output_field}});
119 {%- else %}
106 Deserialize_({{input}}->{{name}}.ptr, &{{output_field}}); 120 Deserialize_({{input}}->{{name}}.ptr, &{{output_field}});
121 {%- endif %}
107 {%- elif kind|is_interface_kind or kind|is_interface_request_kind %} 122 {%- elif kind|is_interface_kind or kind|is_interface_request_kind %}
108 {{output_field}}.Bind(mojo::MakeScopedHandle(mojo::internal::FetchAndReset(& {{input}}->{{name}}))); 123 {{output_field}}.Bind(mojo::MakeScopedHandle(mojo::internal::FetchAndReset(& {{input}}->{{name}})));
109 {%- elif kind|is_any_handle_kind %} 124 {%- elif kind|is_any_handle_kind %}
110 {{output_field}}.reset(mojo::internal::FetchAndReset(&{{input}}->{{name}})); 125 {{output_field}}.reset(mojo::internal::FetchAndReset(&{{input}}->{{name}}));
111 {%- elif kind|is_enum_kind %} 126 {%- elif kind|is_enum_kind %}
112 {{output_field}} = static_cast<{{kind|cpp_wrapper_type}}>({{input}}->{{name} }); 127 {{output_field}} = static_cast<{{kind|cpp_wrapper_type}}>({{input}}->{{name} });
113 {%- else %} 128 {%- else %}
114 {{output_field}} = {{input}}->{{name}}; 129 {{output_field}} = {{input}}->{{name}};
115 {%- endif %} 130 {%- endif %}
116 {%- endfor %} 131 {%- endfor %}
117 } while (false); 132 } while (false);
118 {%- endmacro %} 133 {%- endmacro %}
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698