1{# TODO(yzshen): Make these templates more readable. #} 2 3{# Computes the serialized size for the specified struct. 4 |struct| is the struct definition. 5 |input_field_pattern| should be a pattern that contains one string 6 placeholder, for example, "input->%s", "p_%s". The placeholder will be 7 substituted with struct field names to refer to the input fields. 8 |context| is the name of the serialization context. 9 |input_may_be_temp| indicates whether any input may be temporary obejcts. 10 We need to assign temporary objects to local variables before passing it to 11 Serializer, because it is illegal to pass temporary objects as non-const 12 references. 13 This macro is expanded to compute seriailized size for both: 14 - user-defined structs: the input is an instance of the corresponding struct 15 wrapper class. 16 - method parameters/response parameters: the input is a list of 17 arguments. 18 It declares |size| of type size_t to store the resulting size. #} 19{%- macro get_serialized_size(struct, input_field_pattern, context, 20 input_may_be_temp=False) -%} 21 size_t size = sizeof({{struct|get_qualified_name_for_kind(internal=True)}}); 22{%- for pf in struct.packed.packed_fields_in_ordinal_order if pf.field.kind|is_object_kind %} 23{%- set name = pf.field.name -%} 24{%- set kind = pf.field.kind -%} 25{%- set original_input_field = input_field_pattern|format(name) %} 26{%- set input_field = "in_%s"|format(name) if input_may_be_temp 27 else original_input_field %} 28{%- if input_may_be_temp %} 29 decltype({{original_input_field}}) in_{{name}} = {{original_input_field}}; 30{%- endif %} 31 32{%- set serializer_type = kind|unmapped_type_for_serializer %} 33{%- if kind|is_union_kind %} 34 size += mojo::internal::PrepareToSerialize<{{serializer_type}}>( 35 {{input_field}}, true, {{context}}); 36{%- else %} 37 size += mojo::internal::PrepareToSerialize<{{serializer_type}}>( 38 {{input_field}}, {{context}}); 39{%- endif %} 40{%- endfor %} 41{%- endmacro -%} 42 43{# Serializes the specified struct. 44 |struct| is the struct definition. 45 |struct_display_name| is the display name for the struct that can be showed 46 in error/log messages, for example, "FooStruct", "FooMethod request". 47 |input_field_pattern| should be a pattern that contains one string 48 placeholder, for example, "input->%s", "p_%s". The placeholder will be 49 substituted with struct field names to refer to the input fields. 50 |output| is the name of the output struct instance. 51 |buffer| is the name of the Buffer instance used. 52 |context| is the name of the serialization context. 53 |input_may_be_temp|: please see the comments of get_serialized_size. 54 This macro is expanded to do serialization for both: 55 - user-defined structs: the input is an instance of the corresponding struct 56 wrapper class. 57 - method parameters/response parameters: the input is a list of 58 arguments. #} 59{%- macro serialize(struct, struct_display_name, input_field_pattern, output, 60 buffer, context, input_may_be_temp=False) -%} 61 auto {{output}} = 62 {{struct|get_qualified_name_for_kind(internal=True)}}::New({{buffer}}); 63 ALLOW_UNUSED_LOCAL({{output}}); 64{%- for pf in struct.packed.packed_fields_in_ordinal_order %} 65{%- set input_field = input_field_pattern|format(pf.field.name) %} 66{%- set name = pf.field.name %} 67{%- set kind = pf.field.kind %} 68{%- set serializer_type = kind|unmapped_type_for_serializer %} 69 70{%- if kind|is_object_kind or kind|is_any_handle_or_interface_kind %} 71{%- set original_input_field = input_field_pattern|format(name) %} 72{%- set input_field = "in_%s"|format(name) if input_may_be_temp 73 else original_input_field %} 74{%- if input_may_be_temp %} 75 decltype({{original_input_field}}) in_{{name}} = {{original_input_field}}; 76{%- endif %} 77{%- endif %} 78 79{%- if kind|is_object_kind %} 80{%- if kind|is_array_kind or kind|is_map_kind %} 81 typename decltype({{output}}->{{name}})::BaseType* {{name}}_ptr; 82 const mojo::internal::ContainerValidateParams {{name}}_validate_params( 83 {{kind|get_container_validate_params_ctor_args|indent(10)}}); 84 mojo::internal::Serialize<{{serializer_type}}>( 85 {{input_field}}, {{buffer}}, &{{name}}_ptr, &{{name}}_validate_params, 86 {{context}}); 87 {{output}}->{{name}}.Set({{name}}_ptr); 88{%- elif kind|is_union_kind %} 89 auto {{name}}_ptr = &{{output}}->{{name}}; 90 mojo::internal::Serialize<{{serializer_type}}>( 91 {{input_field}}, {{buffer}}, &{{name}}_ptr, true, {{context}}); 92{%- else %} 93 typename decltype({{output}}->{{name}})::BaseType* {{name}}_ptr; 94 mojo::internal::Serialize<{{serializer_type}}>( 95 {{input_field}}, {{buffer}}, &{{name}}_ptr, {{context}}); 96 {{output}}->{{name}}.Set({{name}}_ptr); 97{%- endif %} 98{%- if not kind|is_nullable_kind %} 99 MOJO_INTERNAL_DLOG_SERIALIZATION_WARNING( 100 {{output}}->{{name}}.is_null(), 101 mojo::internal::VALIDATION_ERROR_UNEXPECTED_NULL_POINTER, 102 "null {{name}} in {{struct_display_name}}"); 103{%- endif %} 104 105{%- elif kind|is_any_handle_or_interface_kind %} 106 mojo::internal::Serialize<{{serializer_type}}>( 107 {{input_field}}, &{{output}}->{{name}}, {{context}}); 108{%- if not kind|is_nullable_kind %} 109 MOJO_INTERNAL_DLOG_SERIALIZATION_WARNING( 110 !mojo::internal::IsHandleOrInterfaceValid({{output}}->{{name}}), 111{%- if kind|is_associated_kind %} 112 mojo::internal::VALIDATION_ERROR_UNEXPECTED_INVALID_INTERFACE_ID, 113{%- else %} 114 mojo::internal::VALIDATION_ERROR_UNEXPECTED_INVALID_HANDLE, 115{%- endif %} 116 "invalid {{name}} in {{struct_display_name}}"); 117{%- endif %} 118 119{%- elif kind|is_enum_kind %} 120 mojo::internal::Serialize<{{serializer_type}}>( 121 {{input_field}}, &{{output}}->{{name}}); 122 123{%- else %} 124 {{output}}->{{name}} = {{input_field}}; 125{%- endif %} 126{%- endfor %} 127{%- endmacro -%} 128 129{# Deserializes the specified struct. 130 |struct| is the struct definition. 131 |input| is the name of the input struct data view. It is expected to be 132 non-null. 133 |output_field_pattern| should be a pattern that contains one string 134 placeholder, for example, "result->%s", "p_%s". The placeholder will be 135 substituted with struct field names to refer to the output fields. 136 |context| is the name of the serialization context. 137 |success| is the name of a bool variable to track success of the operation. 138 This macro is expanded to do deserialization for both: 139 - user-defined structs: the output is an instance of the corresponding 140 struct wrapper class. 141 - method parameters/response parameters: the output is a list of 142 arguments. #} 143{%- macro deserialize(struct, input, output_field_pattern, success) -%} 144{%- for pf in struct.packed.packed_fields_in_ordinal_order %} 145{%- set output_field = output_field_pattern|format(pf.field.name) %} 146{%- set name = pf.field.name %} 147{%- set kind = pf.field.kind %} 148{%- if kind|is_object_kind or kind|is_enum_kind %} 149 if (!{{input}}.Read{{name|under_to_camel}}(&{{output_field}})) 150 {{success}} = false; 151{%- elif kind|is_any_handle_or_interface_kind %} 152 {{output_field}} = {{input}}.Take{{name|under_to_camel}}(); 153{%- else %} 154 {{output_field}} = {{input}}.{{name}}(); 155{%- endif %} 156{%- endfor %} 157{%- endmacro %} 158