OLD | NEW |
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 %} | 17 {%- if pf.field.kind|is_union_kind %} |
18 size += GetSerializedSize_({{input_field_pattern|format(pf.field.name)}}, true
); | 18 size += GetSerializedSize_({{input_field_pattern|format(pf.field.name)}}, true
); |
| 19 {%- elif pf.field.kind|is_struct_kind %} |
| 20 size += {{input_field_pattern|format(pf.field.name)}}.is_null() |
| 21 ? 0 |
| 22 : GetSerializedSize_(*{{input_field_pattern|format(pf.field.name)}
}); |
19 {%- else %} | 23 {%- else %} |
20 size += GetSerializedSize_({{input_field_pattern|format(pf.field.name)}}); | 24 size += GetSerializedSize_({{input_field_pattern|format(pf.field.name)}}); |
21 {%- endif %} | 25 {%- endif %} |
22 {%- endfor %} | 26 {%- endfor %} |
23 {%- endmacro -%} | 27 {%- endmacro -%} |
24 | 28 |
25 {# Serializes the specified struct. | 29 {# Serializes the specified struct. |
26 |struct| is the struct definition. | 30 |struct| is the struct definition. |
27 |struct_display_name| is the display name for the struct that can be showed | 31 |struct_display_name| is the display name for the struct that can be showed |
28 in error/log messages, for example, "FooStruct", "FooMethod request". | 32 in error/log messages, for example, "FooStruct", "FooMethod request". |
(...skipping 11 matching lines...) Expand all Loading... |
40 internal::{{struct.name}}_Data* {{output}} = | 44 internal::{{struct.name}}_Data* {{output}} = |
41 internal::{{struct.name}}_Data::New({{buffer}}); | 45 internal::{{struct.name}}_Data::New({{buffer}}); |
42 {%- for pf in struct.packed.packed_fields_in_ordinal_order %} | 46 {%- for pf in struct.packed.packed_fields_in_ordinal_order %} |
43 {%- set input_field = input_field_pattern|format(pf.field.name) %} | 47 {%- set input_field = input_field_pattern|format(pf.field.name) %} |
44 {%- set name = pf.field.name %} | 48 {%- set name = pf.field.name %} |
45 {%- set kind = pf.field.kind %} | 49 {%- set kind = pf.field.kind %} |
46 {%- if kind|is_object_kind %} | 50 {%- if kind|is_object_kind %} |
47 {%- if kind|is_array_kind %} | 51 {%- if kind|is_array_kind %} |
48 const mojo::internal::ArrayValidateParams {{name}}_validate_params( | 52 const mojo::internal::ArrayValidateParams {{name}}_validate_params( |
49 {{kind|get_array_validate_params_ctor_args|indent(10)}}); | 53 {{kind|get_array_validate_params_ctor_args|indent(10)}}); |
50 mojo::SerializeArray_(mojo::internal::Forward({{input_field}}), {{buffer}}, | 54 mojo::SerializeArray_(&{{input_field}}, {{buffer}}, |
51 &{{output}}->{{name}}.ptr, &{{name}}_validate_params); | 55 &{{output}}->{{name}}.ptr, &{{name}}_validate_params); |
52 {%- elif kind|is_map_kind %} | 56 {%- elif kind|is_map_kind %} |
53 const mojo::internal::ArrayValidateParams {{name}}_validate_params( | 57 const mojo::internal::ArrayValidateParams {{name}}_validate_params( |
54 {{kind.value_kind|get_map_validate_params_ctor_args|indent(10)}}); | 58 {{kind.value_kind|get_map_validate_params_ctor_args|indent(10)}}); |
55 mojo::SerializeMap_( | 59 mojo::SerializeMap_( |
56 mojo::internal::Forward({{input_field}}), {{buffer}}, &{{output}}->{{name}
}.ptr, | 60 &{{input_field}}, {{buffer}}, &{{output}}->{{name}}.ptr, |
57 &{{name}}_validate_params); | 61 &{{name}}_validate_params); |
58 {%- elif kind|is_union_kind %} | 62 {%- elif kind|is_union_kind %} |
59 internal::{{kind.name}}_Data* {{name}}_ptr = &{{output}}->{{name}}; | 63 internal::{{kind.name}}_Data* {{name}}_ptr = &{{output}}->{{name}}; |
60 SerializeUnion_(mojo::internal::Forward({{input_field}}), {{buffer}}, &{{name}
}_ptr, true); | 64 SerializeUnion_({{input_field}}.get(), {{buffer}}, &{{name}}_ptr, true); |
| 65 {%- elif kind|is_string_kind %} |
| 66 SerializeString_({{input_field}}, {{buffer}}, &{{output}}->{{name}}.ptr); |
61 {%- else %} | 67 {%- else %} |
62 Serialize_(mojo::internal::Forward({{input_field}}), {{buffer}}, &{{output}}->
{{name}}.ptr); | 68 Serialize_({{input_field}}.get(), {{buffer}}, &{{output}}->{{name}}.ptr); |
63 {%- endif %} | 69 {%- endif %} |
64 {%- if not kind|is_nullable_kind %} | 70 {%- if not kind|is_nullable_kind %} |
65 MOJO_INTERNAL_DLOG_SERIALIZATION_WARNING( | 71 MOJO_INTERNAL_DLOG_SERIALIZATION_WARNING( |
66 {%- if kind|is_union_kind %} | 72 {%- if kind|is_union_kind %} |
67 {{output}}->{{name}}.is_null(), | 73 {{output}}->{{name}}.is_null(), |
68 {%- else %} | 74 {%- else %} |
69 !{{output}}->{{name}}.ptr, | 75 !{{output}}->{{name}}.ptr, |
70 {%- endif %} | 76 {%- endif %} |
71 mojo::internal::VALIDATION_ERROR_UNEXPECTED_NULL_POINTER, | 77 mojo::internal::VALIDATION_ERROR_UNEXPECTED_NULL_POINTER, |
72 "null {{name}} in {{struct_display_name}}"); | 78 "null {{name}} in {{struct_display_name}}"); |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 {%- set output_field = output_field_pattern|format(pf.field.name) %} | 125 {%- set output_field = output_field_pattern|format(pf.field.name) %} |
120 {%- set name = pf.field.name %} | 126 {%- set name = pf.field.name %} |
121 {%- set kind = pf.field.kind %} | 127 {%- set kind = pf.field.kind %} |
122 {%- if pf.min_version > last_checked_version %} | 128 {%- if pf.min_version > last_checked_version %} |
123 {%- set last_checked_version = pf.min_version %} | 129 {%- set last_checked_version = pf.min_version %} |
124 if ({{input}}->header_.version < {{pf.min_version}}) | 130 if ({{input}}->header_.version < {{pf.min_version}}) |
125 break; | 131 break; |
126 {%- endif %} | 132 {%- endif %} |
127 {%- if kind|is_object_kind %} | 133 {%- if kind|is_object_kind %} |
128 {%- if kind|is_union_kind %} | 134 {%- if kind|is_union_kind %} |
129 Deserialize_(&{{input}}->{{name}}, &{{output_field}}); | 135 {{output_field}} = {{kind|get_name_for_kind}}::New(); |
| 136 Deserialize_(&{{input}}->{{name}}, {{output_field}}.get()); |
| 137 {%- elif kind|is_struct_kind %} |
| 138 if ({{input}}->{{name}}.ptr) { |
| 139 {{output_field}} = {{kind|get_name_for_kind}}::New(); |
| 140 Deserialize_({{input}}->{{name}}.ptr, {{output_field}}.get()); |
| 141 } |
130 {%- else %} | 142 {%- else %} |
| 143 {#- Arrays and Maps #} |
131 Deserialize_({{input}}->{{name}}.ptr, &{{output_field}}); | 144 Deserialize_({{input}}->{{name}}.ptr, &{{output_field}}); |
132 {%- endif %} | 145 {%- endif %} |
133 {%- elif kind|is_interface_kind %} | 146 {%- elif kind|is_interface_kind %} |
134 mojo::internal::InterfaceDataToPointer(&{{input}}->{{name}}, &{{output_field
}}); | 147 mojo::internal::InterfaceDataToPointer(&{{input}}->{{name}}, &{{output_field
}}); |
135 {%- elif kind|is_interface_request_kind %} | 148 {%- elif kind|is_interface_request_kind %} |
136 {{output_field}}.Bind(mojo::MakeScopedHandle(mojo::internal::FetchAndReset(&
{{input}}->{{name}}))); | 149 {{output_field}}.Bind(mojo::MakeScopedHandle(mojo::internal::FetchAndReset(&
{{input}}->{{name}}))); |
137 {%- elif kind|is_any_handle_kind %} | 150 {%- elif kind|is_any_handle_kind %} |
138 {{output_field}}.reset(mojo::internal::FetchAndReset(&{{input}}->{{name}})); | 151 {{output_field}}.reset(mojo::internal::FetchAndReset(&{{input}}->{{name}})); |
139 {%- elif kind|is_enum_kind %} | 152 {%- elif kind|is_enum_kind %} |
140 {{output_field}} = static_cast<{{kind|cpp_wrapper_type}}>({{input}}->{{name}
}); | 153 {{output_field}} = static_cast<{{kind|cpp_wrapper_type}}>({{input}}->{{name}
}); |
141 {%- else %} | 154 {%- else %} |
142 {{output_field}} = {{input}}->{{name}}; | 155 {{output_field}} = {{input}}->{{name}}; |
143 {%- endif %} | 156 {%- endif %} |
144 {%- endfor %} | 157 {%- endfor %} |
145 } while (false); | 158 } while (false); |
146 {%- endmacro %} | 159 {%- endmacro %} |
OLD | NEW |