OLD | NEW |
| (Empty) |
1 size_t GetSerializedSize_(const {{union.name}}Ptr& input) { | |
2 if (!input) | |
3 return 0; | |
4 | |
5 size_t size = sizeof(internal::{{union.name}}_Data); | |
6 switch (input->which()) { | |
7 {% for field in union.fields %} | |
8 {% if field.kind|is_string_kind %} | |
9 case {{union.name}}::Tag::{{field.name|upper}}: | |
10 size += GetSerializedSize_(input->get_{{field.name}}()); | |
11 break; | |
12 {%- endif %} | |
13 {%- endfor %} | |
14 default: | |
15 break; | |
16 } | |
17 return size; | |
18 } | |
19 | |
20 | |
21 void Serialize_({{union.name}}Ptr input, mojo::internal::Buffer* buf, | |
22 internal::{{union.name}}_Data** output) { | |
23 if (input) { | |
24 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. | |
28 result->tag = input->which(); | |
29 switch (input->which()) { | |
30 {% for field in union.fields %} | |
31 case {{union.name}}::Tag::{{field.name|upper}}: | |
32 {% if field.kind|is_string_kind %} | |
33 Serialize_(input_acc.data()->{{field.name}}, buf, &result->data.{{field.
name}}.ptr); | |
34 {% else %} | |
35 result->data.{{field.name}} = input_acc.data()->{{field.name}}; | |
36 {%- endif %} | |
37 break; | |
38 {%- endfor %} | |
39 } | |
40 *output = result; | |
41 } else { | |
42 *output = nullptr; | |
43 } | |
44 } | |
45 | |
46 void Deserialize_(internal::{{union.name}}_Data* input, | |
47 {{union.name}}Ptr* output) { | |
48 if (input) { | |
49 {{union.name}}Ptr result({{union.name}}::New()); | |
50 mojo::internal::UnionAccessor<{{union.name}}> result_acc(result.get()); | |
51 switch (input->tag) { | |
52 {% for field in union.fields %} | |
53 case {{union.name}}::Tag::{{field.name|upper}}: | |
54 {% if field.kind|is_string_kind %} | |
55 result_acc.SwitchActive({{union.name}}::Tag::{{field.name|upper}}); | |
56 Deserialize_(input->data.{{field.name}}.ptr, &result_acc.data()->{{field
.name}}); | |
57 {% else %} | |
58 result->set_{{field.name}}(input->data.{{field.name}}); | |
59 {%- endif %} | |
60 break; | |
61 {%- endfor %} | |
62 } | |
63 *output = result.Pass(); | |
64 } else { | |
65 output->reset(); | |
66 } | |
67 } | |
OLD | NEW |