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

Side by Side Diff: mojo/public/tools/bindings/generators/cpp_templates/struct_definition.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 {%- set class_name = struct.name ~ "_Data" %} 1 {%- set class_name = struct.name ~ "_Data" %}
2 2
3 {#- TODO(yzshen): Consider eliminating _validate_object() and 3 {#- TODO(yzshen): Consider eliminating _validate_object() and
4 _validate_handle(). #} 4 _validate_handle(). #}
5 5
6 {#- Validates the specified struct field, which is supposed to be an object 6 {#- Validates the specified struct field, which is supposed to be an object
7 (struct/array/string/map/union). 7 (struct/array/string/map/union).
8 This macro is expanded by the Validate() method. #} 8 This macro is expanded by the Validate() method. #}
9 {%- macro _validate_object(struct, packed_field) %} 9 {%- macro _validate_object(struct, packed_field) %}
10 {%- set name = packed_field.field.name %} 10 {%- set name = packed_field.field.name %}
11 {%- set kind = packed_field.field.kind %} 11 {%- set kind = packed_field.field.kind %}
12 {%- set wrapper_type = kind|cpp_wrapper_type %} 12 {%- set wrapper_type = kind|cpp_wrapper_type %}
13 {%- if not kind|is_nullable_kind %} 13 {%- if not kind|is_nullable_kind %}
14 {%- if kind|is_union_kind %}
15 if (object->{{name}}.is_null()) {
16 {%- else %}
14 if (!object->{{name}}.offset) { 17 if (!object->{{name}}.offset) {
18 {%- endif %}
15 ReportValidationError( 19 ReportValidationError(
16 mojo::internal::VALIDATION_ERROR_UNEXPECTED_NULL_POINTER, 20 mojo::internal::VALIDATION_ERROR_UNEXPECTED_NULL_POINTER,
17 "null {{name}} field in {{struct.name}} struct"); 21 "null {{name}} field in {{struct.name}} struct");
18 return false; 22 return false;
19 } 23 }
20 {%- endif %} 24 {%- endif %}
25 {%- if not kind|is_union_kind %}
21 if (!mojo::internal::ValidateEncodedPointer(&object->{{name}}.offset)) { 26 if (!mojo::internal::ValidateEncodedPointer(&object->{{name}}.offset)) {
22 ReportValidationError(mojo::internal::VALIDATION_ERROR_ILLEGAL_POINTER); 27 ReportValidationError(mojo::internal::VALIDATION_ERROR_ILLEGAL_POINTER);
23 return false; 28 return false;
24 } 29 }
30 {%- endif %}
25 {%- if kind|is_array_kind or kind|is_string_kind %} 31 {%- if kind|is_array_kind or kind|is_string_kind %}
26 if (!{{wrapper_type}}::Data_::Validate< 32 if (!{{wrapper_type}}::Data_::Validate<
27 {{kind|get_array_validate_params|indent(10)}}>( 33 {{kind|get_array_validate_params|indent(10)}}>(
28 mojo::internal::DecodePointerRaw(&object->{{name}}.offset), 34 mojo::internal::DecodePointerRaw(&object->{{name}}.offset),
29 bounds_checker)) { 35 bounds_checker)) {
30 {%- elif kind|is_map_kind %} 36 {%- elif kind|is_map_kind %}
31 if (!{{wrapper_type}}::Data_::Validate< 37 if (!{{wrapper_type}}::Data_::Validate<
32 {{kind.value_kind|get_map_validate_params|indent(10)}}>( 38 {{kind.value_kind|get_map_validate_params|indent(10)}}>(
33 mojo::internal::DecodePointerRaw(&object->{{name}}.offset), 39 mojo::internal::DecodePointerRaw(&object->{{name}}.offset),
34 bounds_checker)) { 40 bounds_checker)) {
35 {%- elif kind|is_struct_kind %} 41 {%- elif kind|is_struct_kind %}
36 if (!{{kind|get_name_for_kind}}::Data_::Validate( 42 if (!{{kind|get_name_for_kind}}::Data_::Validate(
37 mojo::internal::DecodePointerRaw(&object->{{name}}.offset), 43 mojo::internal::DecodePointerRaw(&object->{{name}}.offset),
38 bounds_checker)) { 44 bounds_checker)) {
45 {%- elif kind|is_union_kind %}
46 if (!{{kind|get_name_for_kind}}::Data_::Validate(
47 &object->{{name}}, bounds_checker, true)) {
yzshen1 2015/03/26 07:30:14 wrong indent?
azani 2015/03/26 22:27:39 Done.
39 {%- else %} 48 {%- else %}
40 if (!{{wrapper_type}}::Data_::Validate( 49 if (!{{wrapper_type}}::Data_::Validate(
41 mojo::internal::DecodePointerRaw(&object->{{name}}.offset), 50 mojo::internal::DecodePointerRaw(&object->{{name}}.offset),
42 bounds_checker)) { 51 bounds_checker)) {
43 {%- endif %} 52 {%- endif %}
44 return false; 53 return false;
45 } 54 }
46 {%- endmacro %} 55 {%- endmacro %}
47 56
48 {#- 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 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
132 {%- endif %} 141 {%- endif %}
133 {%- endfor %} 142 {%- endfor %}
134 143
135 return true; 144 return true;
136 } 145 }
137 146
138 void {{class_name}}::EncodePointersAndHandles( 147 void {{class_name}}::EncodePointersAndHandles(
139 std::vector<mojo::Handle>* handles) { 148 std::vector<mojo::Handle>* handles) {
140 MOJO_CHECK(header_.version == {{struct.versions[-1].version}}); 149 MOJO_CHECK(header_.version == {{struct.versions[-1].version}});
141 {%- for pf in struct.packed.packed_fields_in_ordinal_order %} 150 {%- for pf in struct.packed.packed_fields_in_ordinal_order %}
142 {%- if pf.field.kind|is_object_kind %} 151 {%- if pf.field.kind|is_object_kind and not pf.field.kind|is_union_kind %}
143 mojo::internal::Encode(&{{pf.field.name}}, handles); 152 mojo::internal::Encode(&{{pf.field.name}}, handles);
144 {%- elif pf.field.kind|is_any_handle_kind %} 153 {%- elif pf.field.kind|is_any_handle_kind %}
145 mojo::internal::EncodeHandle(&{{pf.field.name}}, handles); 154 mojo::internal::EncodeHandle(&{{pf.field.name}}, handles);
146 {%- endif %} 155 {%- endif %}
147 {%- endfor %} 156 {%- endfor %}
148 } 157 }
149 158
150 void {{class_name}}::DecodePointersAndHandles( 159 void {{class_name}}::DecodePointersAndHandles(
151 std::vector<mojo::Handle>* handles) { 160 std::vector<mojo::Handle>* handles) {
152 // NOTE: The memory backing |this| may has be smaller than |sizeof(*this)|, if 161 // NOTE: The memory backing |this| may has be smaller than |sizeof(*this)|, if
153 // the message comes from an older version. 162 // the message comes from an older version.
154 {#- Before decoding fields introduced at a certain version, we need to add 163 {#- Before decoding fields introduced at a certain version, we need to add
155 a version check, which makes sure we skip further decoding if |this| 164 a version check, which makes sure we skip further decoding if |this|
156 is from an earlier version. |last_checked_version| records the last 165 is from an earlier version. |last_checked_version| records the last
157 version that we have added such version check. #} 166 version that we have added such version check. #}
158 {%- set last_checked_version = 0 %} 167 {%- set last_checked_version = 0 %}
159 {%- for pf in struct.packed.packed_fields_in_ordinal_order %} 168 {%- for pf in struct.packed.packed_fields_in_ordinal_order %}
160 {%- set name = pf.field.name %} 169 {%- set name = pf.field.name %}
161 {%- set kind = pf.field.kind %} 170 {%- set kind = pf.field.kind %}
162 {%- if kind|is_object_kind or kind|is_any_handle_kind %} 171 {%- if kind|is_object_kind or kind|is_any_handle_kind %}
163 {%- if pf.min_version > last_checked_version %} 172 {%- if pf.min_version > last_checked_version %}
164 {%- set last_checked_version = pf.min_version %} 173 {%- set last_checked_version = pf.min_version %}
165 if (header_.version < {{pf.min_version}}) 174 if (header_.version < {{pf.min_version}})
166 return; 175 return;
167 {%- endif %} 176 {%- endif %}
168 {%- if kind|is_object_kind %} 177 {%- if kind|is_union_kind %}
yzshen1 2015/03/26 07:30:14 this if-elif seems weird. you could do something s
azani 2015/03/26 22:27:39 Won't work because then it falls to the "else" cla
178 {%- elif kind|is_object_kind %}
169 mojo::internal::Decode(&{{name}}, handles); 179 mojo::internal::Decode(&{{name}}, handles);
170 {%- else %} 180 {%- else %}
171 mojo::internal::DecodeHandle(&{{name}}, handles); 181 mojo::internal::DecodeHandle(&{{name}}, handles);
172 {%- endif %} 182 {%- endif %}
173 {%- endif %} 183 {%- endif %}
174 {%- endfor %} 184 {%- endfor %}
175 } 185 }
176 186
177 {{class_name}}::{{class_name}}() { 187 {{class_name}}::{{class_name}}() {
178 header_.num_bytes = sizeof(*this); 188 header_.num_bytes = sizeof(*this);
179 header_.version = {{struct.versions[-1].version}}; 189 header_.version = {{struct.versions[-1].version}};
180 } 190 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698