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

Side by Side Diff: mojo/public/tools/bindings/generators/cpp_templates/union_serialization_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 size_t GetSerializedSize_(const {{union.name}}Ptr& input) { 1 size_t GetSerializedSize_(const {{union.name}}Ptr& input) {
2 size_t size = sizeof(internal::{{union.name}}_Data);
2 if (!input) 3 if (!input)
3 return 0; 4 return size;
yzshen1 2015/03/26 07:30:15 I think this is incorrect. When a struct calculate
azani 2015/03/26 22:27:40 Removed that function because it was unused and th
yzshen1 2015/03/27 16:26:33 It seems the problem is not addressed. You could t
yzshen1 2015/03/27 21:52:42 This comment is not addressed.
azani 2015/03/30 22:21:03 Sorry, I finally understood what you meant. I've a
yzshen1 2015/03/30 22:55:18 It seems better to follow the same style as Serial
4 5
5 size_t size = sizeof(internal::{{union.name}}_Data);
6 switch (input->which()) { 6 switch (input->which()) {
7 {% for field in union.fields %} 7 {% for field in union.fields %}
8 {% if field.kind|is_string_kind %} 8 {% if field.kind|is_string_kind %}
9 case {{union.name}}::Tag::{{field.name|upper}}: 9 case {{union.name}}::Tag::{{field.name|upper}}:
10 size += GetSerializedSize_(input->get_{{field.name}}()); 10 size += GetSerializedSize_(input->get_{{field.name}}());
11 break; 11 break;
12 {%- endif %} 12 {%- endif %}
13 {%- endfor %} 13 {%- endfor %}
14 default: 14 default:
15 break; 15 break;
16 } 16 }
17 return size; 17 return size;
18 } 18 }
19 19
20 size_t GetUnionInUnionSerializedSize_(const {{union.name}}Ptr& input) {
yzshen1 2015/03/26 07:30:15 The names seem confusing. Maybe we should just fol
azani 2015/03/26 22:27:40 It was unused, so this was premature anyways.
21 if (!input)
22 return 0;
20 23
21 void Serialize_({{union.name}}Ptr input, mojo::internal::Buffer* buf, 24 return GetSerializedSize_(input);
22 internal::{{union.name}}_Data** output) { 25 }
26
27 void SerializeUnion_({{union.name}}Ptr input, mojo::internal::Buffer* buf,
28 internal::{{union.name}}_Data** output, bool inlined) {
29 internal::{{union.name}}_Data* result = nullptr;
30 if (inlined) {
31 result = *output;
32 } else {
33 result = internal::{{union.name}}_Data::New(buf);
34 }
23 if (input) { 35 if (input) {
24 mojo::internal::UnionAccessor<{{union.name}}> input_acc(input.get()); 36 mojo::internal::UnionAccessor<{{union.name}}> input_acc(input.get());
25 internal::{{union.name}}_Data* result =
26 internal::{{union.name}}_Data::New(buf);
27 // TODO(azani): Handle unknown and objects. 37 // TODO(azani): Handle unknown and objects.
38 // Set the not-null flag.
39 result->size = 16;
28 result->tag = input->which(); 40 result->tag = input->which();
29 switch (input->which()) { 41 switch (input->which()) {
30 {% for field in union.fields %} 42 {% for field in union.fields %}
31 case {{union.name}}::Tag::{{field.name|upper}}: 43 case {{union.name}}::Tag::{{field.name|upper}}:
32 {% if field.kind|is_string_kind %} 44 {% if field.kind|is_string_kind %}
33 {{field.kind|cpp_field_type}}* {{field.name}}_ptr = reinterpret_cast<{{f ield.kind|cpp_field_type}}*>(&result->data.f_{{field.name}}); 45 {{field.kind|cpp_field_type}}* {{field.name}}_ptr = reinterpret_cast<{{f ield.kind|cpp_field_type}}*>(&result->data.f_{{field.name}});
34 Serialize_(*(input_acc.data()->{{field.name}}), buf, &{{field.name}}_ptr ->ptr); 46 Serialize_(*(input_acc.data()->{{field.name}}), buf, &{{field.name}}_ptr ->ptr);
35 {% else %} 47 {% else %}
36 result->data.f_{{field.name}} = input_acc.data()->{{field.name}}; 48 result->data.f_{{field.name}} = input_acc.data()->{{field.name}};
37 {%- endif %} 49 {%- endif %}
38 break; 50 break;
39 {%- endfor %} 51 {%- endfor %}
40 } 52 }
41 *output = result;
42 } else {
43 *output = nullptr;
44 } 53 }
54 *output = result;
45 } 55 }
46 56
47 void Deserialize_(internal::{{union.name}}_Data* input, 57 void Deserialize_(internal::{{union.name}}_Data* input,
48 {{union.name}}Ptr* output) { 58 {{union.name}}Ptr* output) {
49 if (input) { 59 if (input && !input->is_null()) {
50 {{union.name}}Ptr result({{union.name}}::New()); 60 {{union.name}}Ptr result({{union.name}}::New());
51 mojo::internal::UnionAccessor<{{union.name}}> result_acc(result.get()); 61 mojo::internal::UnionAccessor<{{union.name}}> result_acc(result.get());
52 switch (input->tag) { 62 switch (input->tag) {
53 {% for field in union.fields %} 63 {% for field in union.fields %}
54 case {{union.name}}::Tag::{{field.name|upper}}: 64 case {{union.name}}::Tag::{{field.name|upper}}:
55 {% if field.kind|is_string_kind %} 65 {% if field.kind|is_string_kind %}
56 result_acc.SwitchActive({{union.name}}::Tag::{{field.name|upper}}); 66 result_acc.SwitchActive({{union.name}}::Tag::{{field.name|upper}});
57 {{field.kind|cpp_field_type}}* {{field.name}}_ptr = reinterpret_cast<{{f ield.kind|cpp_field_type}}*>(&input->data.f_{{field.name}}); 67 {{field.kind|cpp_field_type}}* {{field.name}}_ptr = reinterpret_cast<{{f ield.kind|cpp_field_type}}*>(&input->data.f_{{field.name}});
58 Deserialize_({{field.name}}_ptr->ptr, result_acc.data()->{{field.name}}) ; 68 Deserialize_({{field.name}}_ptr->ptr, result_acc.data()->{{field.name}}) ;
59 {% else %} 69 {% else %}
60 result->set_{{field.name}}(input->data.f_{{field.name}}); 70 result->set_{{field.name}}(input->data.f_{{field.name}});
61 {%- endif %} 71 {%- endif %}
62 break; 72 break;
63 {%- endfor %} 73 {%- endfor %}
64 } 74 }
65 *output = result.Pass(); 75 *output = result.Pass();
66 } else { 76 } else {
67 output->reset(); 77 output->reset();
68 } 78 }
69 } 79 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698