Chromium Code Reviews| 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 %} | 19 {%- elif pf.field.kind|is_struct_kind %} |
| 20 size += {{input_field_pattern|format(pf.field.name)}}.is_null() | 20 size += {{input_field_pattern|format(pf.field.name)}}.is_null() |
| 21 ? 0 | 21 ? 0 |
| 22 : GetSerializedSize_(*{{input_field_pattern|format(pf.field.name)} }); | 22 : GetSerializedSize_(*{{input_field_pattern|format(pf.field.name)} }); |
| 23 {%- else %} | 23 {%- else %} |
| 24 size += GetSerializedSize_({{input_field_pattern|format(pf.field.name)}}); | 24 size += GetSerializedSize_({{input_field_pattern|format(pf.field.name)}}); |
| 25 {%- endif %} | 25 {%- endif %} |
| 26 {%- endfor %} | 26 {%- endfor %} |
| 27 {%- endmacro -%} | 27 {%- endmacro -%} |
| 28 | 28 |
| 29 {# A private macro that prints the C++ log-and-report macro, or test-and-return | |
|
viettrungluu
2015/10/06 16:40:06
Why do we need to generate two versions?
vardhan
2015/10/06 22:57:14
Done. (no longer relevant after some changes based
| |
| 30 macro depending on |should_return_errors| #} | |
| 31 {%- macro _validation_check_macro(should_return_errors=false) -%} | |
| 32 {%- if should_return_errors -%} | |
| 33 MOJO_INTERNAL_SERIALIZATION_CHECK_AND_RETURN | |
| 34 {%- else -%} | |
| 35 MOJO_INTERNAL_DLOG_SERIALIZATION_WARNING | |
| 36 {%- endif -%} | |
| 37 {%- endmacro -%} | |
| 38 | |
| 39 {# The following 4 macros call |SerializeArray_|, |SerializeMap_|, | |
| 40 |SerializeUnion_| and |Serialize_| respectively. If |should_return_error| | |
| 41 is true, these macros do the extra work of propagating non-successful error | |
| 42 codes. #} | |
| 43 {%- macro call_serialize_array(name, kind, input, buffer, output, | |
| 44 should_return_errors, indent_size) -%} | |
| 45 { | |
| 46 const mojo::internal::ArrayValidateParams {{name}}_validate_params( | |
| 47 {{kind|get_array_validate_params_ctor_args|indent(indent_size)}}); | |
| 48 {%- if should_return_errors -%} | |
| 49 auto retval = | |
| 50 {%- endif -%} | |
| 51 mojo::SerializeArray_({{input}}, {{buffer}}, {{output}}, | |
| 52 &{{name}}_validate_params); | |
| 53 {%- if should_return_errors %} | |
| 54 if (retval != mojo::internal::ValidationError::VALIDATION_ERROR_NONE) | |
| 55 return retval; | |
| 56 {%- endif %} | |
| 57 } | |
| 58 {%- endmacro -%} | |
| 59 | |
| 60 {%- macro call_serialize_map(name, kind, input, buffer, output, | |
| 61 should_return_errors, indent_size) -%} | |
| 62 { | |
| 63 const mojo::internal::ArrayValidateParams {{name}}_validate_params( | |
| 64 {{kind.value_kind|get_map_validate_params_ctor_args|indent(indent_size)} }); | |
| 65 {%- if should_return_errors -%} | |
| 66 auto retval = | |
| 67 {%- endif -%} | |
| 68 mojo::SerializeMap_( | |
| 69 {{input}}, {{buffer}}, {{output}}, | |
| 70 &{{name}}_validate_params); | |
| 71 {%- if should_return_errors %} | |
| 72 if (retval != mojo::internal::ValidationError::VALIDATION_ERROR_NONE) | |
| 73 return retval; | |
| 74 {%- endif %} | |
| 75 } | |
| 76 {%- endmacro -%} | |
| 77 | |
| 78 {%- macro call_serialize_union(input, buffer, output, inlined, | |
| 79 should_return_errors) -%} | |
| 80 { | |
| 81 {%- if should_return_errors -%} | |
| 82 auto retval = | |
| 83 {%- endif -%} | |
| 84 SerializeUnion_({{input}}, | |
| 85 {{buffer}}, | |
| 86 {{output}}, | |
| 87 {{inlined}}); | |
| 88 {%- if should_return_errors %} | |
| 89 if (retval != mojo::internal::ValidationError::VALIDATION_ERROR_NONE) | |
| 90 return retval; | |
| 91 {%- endif %} | |
| 92 } | |
| 93 {%- endmacro -%} | |
| 94 | |
| 95 {%- macro call_serialize_struct(input, buffer, output, should_return_errors) -%} | |
| 96 { | |
| 97 {%- if should_return_errors -%} | |
| 98 auto retval = | |
| 99 {%- endif -%} | |
| 100 Serialize_({{input}}, | |
| 101 {{buffer}}, | |
| 102 {{output}}); | |
| 103 {%- if should_return_errors %} | |
| 104 if (retval != mojo::internal::ValidationError::VALIDATION_ERROR_NONE) | |
| 105 return retval; | |
| 106 {%- endif %} | |
| 107 } | |
| 108 {%- endmacro -%} | |
| 109 | |
| 29 {# Serializes the specified struct. | 110 {# Serializes the specified struct. |
| 30 |struct| is the struct definition. | 111 |struct| is the struct definition. |
| 31 |struct_display_name| is the display name for the struct that can be showed | 112 |struct_display_name| is the display name for the struct that can be showed |
| 32 in error/log messages, for example, "FooStruct", "FooMethod request". | 113 in error/log messages, for example, "FooStruct", "FooMethod request". |
| 33 |input_field_pattern| should be a pattern that contains one string | 114 |input_field_pattern| should be a pattern that contains one string |
| 34 placeholder, for example, "input->%s", "p_%s". The placeholder will be | 115 placeholder, for example, "input->%s", "p_%s". The placeholder will be |
| 35 substituted with struct field names to refer to the input fields. | 116 substituted with struct field names to refer to the input fields. |
| 36 |output| is the name of the output struct instance. | 117 |output| is the name of the output struct instance. |
| 37 |buffer| is the name of the Buffer instance used. | 118 |buffer| is the name of the Buffer instance used. |
| 119 |should_return_errors| is true if validation errors need to be return'd. | |
| 120 This is needed when serializing interface parameters, where you cannot | |
| 121 return. | |
| 122 | |
| 38 This macro is expanded to do serialization for both: | 123 This macro is expanded to do serialization for both: |
| 39 - user-defined structs: the input is an instance of the corresponding struct | 124 - user-defined structs: the input is an instance of the corresponding struct |
| 40 wrapper class. | 125 wrapper class. |
| 41 - method parameters/response parameters: the input is a list of | 126 - method parameters/response parameters: the input is a list of |
| 42 arguments. #} | 127 arguments. |
| 43 {%- macro serialize(struct, struct_display_name, input_field_pattern, output, bu ffer) -%} | 128 This macro is expanded within the C++ struct serialization methods #} |
| 129 {%- macro serialize(struct, struct_display_name, input_field_pattern, | |
| 130 output, buffer, should_return_errors=false) -%} | |
| 44 internal::{{struct.name}}_Data* {{output}} = | 131 internal::{{struct.name}}_Data* {{output}} = |
| 45 internal::{{struct.name}}_Data::New({{buffer}}); | 132 internal::{{struct.name}}_Data::New({{buffer}}); |
| 46 {%- for pf in struct.packed.packed_fields_in_ordinal_order %} | 133 {%- for pf in struct.packed.packed_fields_in_ordinal_order %} |
| 47 {%- set input_field = input_field_pattern|format(pf.field.name) %} | 134 {%- set input_field = input_field_pattern|format(pf.field.name) %} |
| 48 {%- set name = pf.field.name %} | 135 {%- set name = pf.field.name %} |
| 49 {%- set kind = pf.field.kind %} | 136 {%- set kind = pf.field.kind %} |
| 50 {%- if kind|is_object_kind %} | 137 {%- if kind|is_object_kind %} |
| 51 {%- if kind|is_array_kind %} | 138 {%- if kind|is_array_kind %} |
| 52 const mojo::internal::ArrayValidateParams {{name}}_validate_params( | 139 {{call_serialize_array(name = name, |
| 53 {{kind|get_array_validate_params_ctor_args|indent(10)}}); | 140 kind = kind, |
| 54 mojo::SerializeArray_(&{{input_field}}, {{buffer}}, | 141 input = '&' ~ input_field, |
| 55 &{{output}}->{{name}}.ptr, &{{name}}_validate_params); | 142 buffer = buffer, |
| 143 output = "&%s->%s.ptr"|format(output,name), | |
| 144 should_return_errors = should_return_errors, | |
| 145 indent_size = 10)}} | |
| 56 {%- elif kind|is_map_kind %} | 146 {%- elif kind|is_map_kind %} |
| 57 const mojo::internal::ArrayValidateParams {{name}}_validate_params( | 147 {{call_serialize_map(name = name, |
| 58 {{kind.value_kind|get_map_validate_params_ctor_args|indent(10)}}); | 148 kind = kind, |
| 59 mojo::SerializeMap_( | 149 input = '&' ~ input_field, |
| 60 &{{input_field}}, {{buffer}}, &{{output}}->{{name}}.ptr, | 150 buffer = buffer, |
| 61 &{{name}}_validate_params); | 151 output = '&%s->%s.ptr'|format(output,name), |
| 152 should_return_errors = should_return_errors, | |
| 153 indent_size = 10)}} | |
| 62 {%- elif kind|is_union_kind %} | 154 {%- elif kind|is_union_kind %} |
| 63 internal::{{kind.name}}_Data* {{name}}_ptr = &{{output}}->{{name}}; | 155 internal::{{kind.name}}_Data* {{name}}_ptr = &{{output}}->{{name}}; |
| 64 SerializeUnion_({{input_field}}.get(), {{buffer}}, &{{name}}_ptr, true); | 156 {{call_serialize_union(input = input_field ~ ".get()", |
| 157 buffer = buffer, | |
| 158 output = "&%s_ptr"|format(name), | |
| 159 inlined = "true", | |
| 160 should_return_errors = should_return_errors)}} | |
| 65 {%- elif kind|is_string_kind %} | 161 {%- elif kind|is_string_kind %} |
| 66 SerializeString_({{input_field}}, {{buffer}}, &{{output}}->{{name}}.ptr); | 162 SerializeString_({{input_field}}, {{buffer}}, &{{output}}->{{name}}.ptr); |
| 67 {%- else %} | 163 {%- else %} |
| 68 Serialize_({{input_field}}.get(), {{buffer}}, &{{output}}->{{name}}.ptr); | 164 {{call_serialize_struct(input = input_field ~ ".get()", |
| 165 buffer = buffer, | |
| 166 output = "&%s->%s.ptr"|format(output,name), | |
| 167 should_return_errors = should_return_errors)}} | |
| 69 {%- endif %} | 168 {%- endif %} |
| 70 {%- if not kind|is_nullable_kind %} | 169 {%- if not kind|is_nullable_kind %} |
| 71 MOJO_INTERNAL_DLOG_SERIALIZATION_WARNING( | 170 {{_validation_check_macro(should_return_errors)}}( |
| 72 {%- if kind|is_union_kind %} | 171 {%- if kind|is_union_kind %} |
| 73 {{output}}->{{name}}.is_null(), | 172 {{output}}->{{name}}.is_null(), |
| 74 {%- else %} | 173 {%- else %} |
| 75 !{{output}}->{{name}}.ptr, | 174 !{{output}}->{{name}}.ptr, |
| 76 {%- endif %} | 175 {%- endif %} |
| 77 mojo::internal::VALIDATION_ERROR_UNEXPECTED_NULL_POINTER, | 176 mojo::internal::ValidationError::VALIDATION_ERROR_UNEXPECTED_NULL_POINTER, |
| 78 "null {{name}} in {{struct_display_name}}"); | 177 "null {{name}} in {{struct_display_name}}"); |
| 79 {%- endif %} | 178 {%- endif %} |
| 80 {%- elif kind|is_any_handle_kind or kind|is_interface_kind %} | 179 {%- elif kind|is_any_handle_kind or kind|is_interface_kind %} |
| 81 {%- if kind|is_interface_kind %} | 180 {%- if kind|is_interface_kind %} |
| 82 mojo::internal::InterfacePointerToData({{input_field}}.Pass(), &{{output}}->{{ name}}); | 181 mojo::internal::InterfacePointerToData({{input_field}}.Pass(), &{{output}}->{{ name}}); |
| 83 {%- elif kind|is_interface_request_kind %} | 182 {%- elif kind|is_interface_request_kind %} |
| 84 {{output}}->{{name}} = {{input_field}}.PassMessagePipe().release(); | 183 {{output}}->{{name}} = {{input_field}}.PassMessagePipe().release(); |
| 85 {%- else %} | 184 {%- else %} |
| 86 {{output}}->{{name}} = {{input_field}}.release(); | 185 {{output}}->{{name}} = {{input_field}}.release(); |
| 87 {%- endif %} | 186 {%- endif %} |
| 88 {%- if not kind|is_nullable_kind %} | 187 {%- if not kind|is_nullable_kind %} |
| 89 MOJO_INTERNAL_DLOG_SERIALIZATION_WARNING( | 188 {{_validation_check_macro(should_return_errors)}}( |
| 90 {%- if kind|is_interface_kind %} | 189 {%- if kind|is_interface_kind %} |
| 91 !{{output}}->{{name}}.handle.is_valid(), | 190 !{{output}}->{{name}}.handle.is_valid(), |
| 92 {%- else %} | 191 {%- else %} |
| 93 !{{output}}->{{name}}.is_valid(), | 192 !{{output}}->{{name}}.is_valid(), |
| 94 {%- endif %} | 193 {%- endif %} |
| 95 mojo::internal::VALIDATION_ERROR_UNEXPECTED_INVALID_HANDLE, | 194 mojo::internal::ValidationError::VALIDATION_ERROR_UNEXPECTED_INVALID_HANDL E, |
| 96 "invalid {{name}} in {{struct_display_name}}"); | 195 "invalid {{name}} in {{struct_display_name}}"); |
| 97 {%- endif %} | 196 {%- endif %} |
| 98 {%- elif kind|is_enum_kind %} | 197 {%- elif kind|is_enum_kind %} |
| 99 {{output}}->{{name}} = | 198 {{output}}->{{name}} = |
| 100 static_cast<int32_t>({{input_field}}); | 199 static_cast<int32_t>({{input_field}}); |
| 101 {%- else %} | 200 {%- else %} |
| 102 {{output}}->{{name}} = {{input_field}}; | 201 {{output}}->{{name}} = {{input_field}}; |
| 103 {%- endif %} | 202 {%- endif %} |
| 104 {%- endfor %} | 203 {%- endfor %} |
| 105 {%- endmacro -%} | 204 {%- endmacro -%} |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 153 {%- elif kind|is_any_handle_kind %} | 252 {%- elif kind|is_any_handle_kind %} |
| 154 {{output_field}}.reset(mojo::internal::FetchAndReset(&{{input}}->{{name}})); | 253 {{output_field}}.reset(mojo::internal::FetchAndReset(&{{input}}->{{name}})); |
| 155 {%- elif kind|is_enum_kind %} | 254 {%- elif kind|is_enum_kind %} |
| 156 {{output_field}} = static_cast<{{kind|cpp_wrapper_type}}>({{input}}->{{name} }); | 255 {{output_field}} = static_cast<{{kind|cpp_wrapper_type}}>({{input}}->{{name} }); |
| 157 {%- else %} | 256 {%- else %} |
| 158 {{output_field}} = {{input}}->{{name}}; | 257 {{output_field}} = {{input}}->{{name}}; |
| 159 {%- endif %} | 258 {%- endif %} |
| 160 {%- endfor %} | 259 {%- endfor %} |
| 161 } while (false); | 260 } while (false); |
| 162 {%- endmacro %} | 261 {%- endmacro %} |
| OLD | NEW |