| OLD | NEW |
| 1 {%- set mojom_type = union|get_qualified_name_for_kind %} | 1 {%- set mojom_type = union|get_qualified_name_for_kind %} |
| 2 {%- set data_type = union|get_qualified_name_for_kind(internal=True) %} | |
| 3 | |
| 4 namespace internal { | |
| 5 | 2 |
| 6 // static | 3 // static |
| 7 size_t UnionSerializerImpl<{{mojom_type}}Ptr>::PrepareToSerialize( | 4 bool UnionTraits<{{mojom_type}}, {{mojom_type}}Ptr>::Read( |
| 8 {{mojom_type}}Ptr& input, | 5 {{mojom_type}}DataView input, |
| 9 bool inlined, | 6 {{mojom_type}}Ptr* output) { |
| 10 SerializationContext* context) { | 7 *output = {{mojom_type}}::New(); |
| 11 size_t size = inlined ? 0 : sizeof({{data_type}}); | 8 {{mojom_type}}Ptr& result = *output; |
| 12 | 9 |
| 13 if (!input) | 10 internal::UnionAccessor<{{mojom_type}}> result_acc(result.get()); |
| 14 return size; | 11 switch (input.tag()) { |
| 12 {%- for field in union.fields %} |
| 13 case {{mojom_type}}::Tag::{{field.name|upper}}: { |
| 14 {%- set name = field.name %} |
| 15 {%- set kind = field.kind %} |
| 16 {%- set serializer_type = kind|unmapped_type_for_serializer %} |
| 17 {%- if kind|is_object_kind %} |
| 18 result_acc.SwitchActive({{mojom_type}}::Tag::{{name|upper}}); |
| 19 if (!input.Read{{name|under_to_camel}}(result_acc.data()->{{name}})) |
| 20 return false; |
| 15 | 21 |
| 16 UnionAccessor<{{mojom_type}}> input_acc(input.get()); | 22 {%- elif kind|is_any_handle_or_interface_kind %} |
| 17 switch (input->which()) { | 23 auto result_{{name}} = input.Take{{name|under_to_camel}}(); |
| 18 {% for field in union.fields %} | 24 result->set_{{name}}(std::move(result_{{name}})); |
| 19 {% if field.kind|is_object_kind %} | 25 |
| 20 {%- set serializer_type = field.kind|unmapped_type_for_serializer %} | 26 {%- elif kind|is_enum_kind %} |
| 21 case {{mojom_type}}::Tag::{{field.name|upper}}: | 27 decltype(result->get_{{name}}()) result_{{name}}; |
| 22 {% if field.kind|is_union_kind %} | 28 if (!input.Read{{name|under_to_camel}}(&result_{{name}})) |
| 23 size += mojo::internal::PrepareToSerialize<{{serializer_type}}>( | 29 return false; |
| 24 *(input_acc.data()->{{field.name}}), false, context); | 30 result->set_{{name}}(result_{{name}}); |
| 25 {% else %} | 31 |
| 26 size += mojo::internal::PrepareToSerialize<{{serializer_type}}>( | 32 {%- else %} |
| 27 *(input_acc.data()->{{field.name}}), context); | 33 result->set_{{name}}(input.{{name}}()); |
| 28 {% endif %} | 34 {%- endif %} |
| 29 break; | 35 break; |
| 30 {%- endif %} | 36 } |
| 31 {%- endfor %} | 37 {%- endfor %} |
| 32 default: | 38 default: |
| 33 break; | 39 return false; |
| 34 } | 40 } |
| 35 return size; | 41 return true; |
| 36 } | 42 } |
| 37 | |
| 38 // static | |
| 39 void UnionSerializerImpl<{{mojom_type}}Ptr>::Serialize( | |
| 40 {{mojom_type}}Ptr& input, | |
| 41 Buffer* buf, | |
| 42 {{data_type}}** output, | |
| 43 bool inlined, | |
| 44 SerializationContext* context) { | |
| 45 {{data_type}}* result = *output; | |
| 46 if (input) { | |
| 47 if (!inlined) | |
| 48 result = {{data_type}}::New(buf); | |
| 49 UnionAccessor<{{mojom_type}}> input_acc(input.get()); | |
| 50 // TODO(azani): Handle unknown and objects. | |
| 51 // Set the not-null flag. | |
| 52 result->size = 16; | |
| 53 result->tag = input->which(); | |
| 54 switch (input->which()) { | |
| 55 {%- for field in union.fields %} | |
| 56 case {{mojom_type}}::Tag::{{field.name|upper}}: { | |
| 57 {%- set serializer_type = field.kind|unmapped_type_for_serializer %} | |
| 58 {%- if field.kind|is_object_kind %} | |
| 59 typename decltype(result->data.f_{{field.name}})::BaseType* ptr; | |
| 60 {%- if field.kind|is_union_kind %} | |
| 61 mojo::internal::Serialize<{{serializer_type}}>( | |
| 62 *(input_acc.data()->{{field.name}}), buf, &ptr, false, context); | |
| 63 {%- elif field.kind|is_array_kind or field.kind|is_map_kind %} | |
| 64 const ContainerValidateParams {{field.name}}_validate_params( | |
| 65 {{field.kind|get_container_validate_params_ctor_args|indent(16)}}); | |
| 66 mojo::internal::Serialize<{{serializer_type}}>( | |
| 67 *(input_acc.data()->{{field.name}}), buf, &ptr, | |
| 68 &{{field.name}}_validate_params, context); | |
| 69 {%- else %} | |
| 70 mojo::internal::Serialize<{{serializer_type}}>( | |
| 71 *(input_acc.data()->{{field.name}}), buf, &ptr, context); | |
| 72 {%- endif %} | |
| 73 result->data.f_{{field.name}}.Set(ptr); | |
| 74 {%- if not field.kind|is_nullable_kind %} | |
| 75 MOJO_INTERNAL_DLOG_SERIALIZATION_WARNING( | |
| 76 !ptr, mojo::internal::VALIDATION_ERROR_UNEXPECTED_NULL_POINTER, | |
| 77 "null {{field.name}} in {{union.name}} union"); | |
| 78 {%- endif %} | |
| 79 | |
| 80 {%- elif field.kind|is_any_handle_or_interface_kind %} | |
| 81 mojo::internal::Serialize<{{serializer_type}}>( | |
| 82 *input_acc.data()->{{field.name}}, &result->data.f_{{field.name}}, | |
| 83 context); | |
| 84 {%- if not field.kind|is_nullable_kind %} | |
| 85 MOJO_INTERNAL_DLOG_SERIALIZATION_WARNING( | |
| 86 !mojo::internal::IsHandleOrInterfaceValid(result->data.f_{{field.nam
e}}), | |
| 87 {%- if field.kind|is_associated_kind %} | |
| 88 mojo::internal::VALIDATION_ERROR_UNEXPECTED_INVALID_INTERFACE_ID, | |
| 89 {%- else %} | |
| 90 mojo::internal::VALIDATION_ERROR_UNEXPECTED_INVALID_HANDLE, | |
| 91 {%- endif %} | |
| 92 "invalid {{field.name}} in {{union.name}} union"); | |
| 93 {%- endif %} | |
| 94 | |
| 95 {%- elif field.kind|is_enum_kind %} | |
| 96 mojo::internal::Serialize<{{serializer_type}}>( | |
| 97 input_acc.data()->{{field.name}}, &result->data.f_{{field.name}}); | |
| 98 | |
| 99 {%- else %} | |
| 100 result->data.f_{{field.name}} = input_acc.data()->{{field.name}}; | |
| 101 {%- endif %} | |
| 102 break; | |
| 103 } | |
| 104 {%- endfor %} | |
| 105 } | |
| 106 } else if (inlined) { | |
| 107 result->set_null(); | |
| 108 } else { | |
| 109 result = nullptr; | |
| 110 } | |
| 111 *output = result; | |
| 112 } | |
| 113 | |
| 114 // static | |
| 115 bool UnionSerializerImpl<{{mojom_type}}Ptr>::Deserialize( | |
| 116 {{data_type}}* input, | |
| 117 {{mojom_type}}Ptr* output, | |
| 118 SerializationContext* context) { | |
| 119 bool success = true; | |
| 120 if (input && !input->is_null()) { | |
| 121 {{mojom_type}}Ptr result({{mojom_type}}::New()); | |
| 122 UnionAccessor<{{mojom_type}}> result_acc(result.get()); | |
| 123 switch (input->tag) { | |
| 124 {%- for field in union.fields %} | |
| 125 case {{mojom_type}}::Tag::{{field.name|upper}}: { | |
| 126 {%- set serializer_type = field.kind|unmapped_type_for_serializer %} | |
| 127 {%- if field.kind|is_object_kind %} | |
| 128 result_acc.SwitchActive({{mojom_type}}::Tag::{{field.name|upper}}); | |
| 129 if (!mojo::internal::Deserialize<{{serializer_type}}>( | |
| 130 input->data.f_{{field.name}}.Get(), | |
| 131 result_acc.data()->{{field.name}}, context)) | |
| 132 success = false; | |
| 133 | |
| 134 {%- elif field.kind|is_any_handle_or_interface_kind %} | |
| 135 typename std::remove_reference< | |
| 136 decltype(result->get_{{field.name}}())>::type result_{{field.name}}; | |
| 137 bool ret = mojo::internal::Deserialize<{{serializer_type}}>( | |
| 138 &input->data.f_{{field.name}}, &result_{{field.name}}, context); | |
| 139 DCHECK(ret); | |
| 140 result->set_{{field.name}}(std::move(result_{{field.name}})); | |
| 141 | |
| 142 {%- elif field.kind|is_enum_kind %} | |
| 143 decltype(result->get_{{field.name}}()) result_{{field.name}}; | |
| 144 if (!mojo::internal::Deserialize<{{serializer_type}}>( | |
| 145 input->data.f_{{field.name}}, &result_{{field.name}})) | |
| 146 success = false; | |
| 147 else | |
| 148 result->set_{{field.name}}(result_{{field.name}}); | |
| 149 | |
| 150 {%- else %} | |
| 151 result->set_{{field.name}}(input->data.f_{{field.name}}); | |
| 152 {%- endif %} | |
| 153 break; | |
| 154 } | |
| 155 {%- endfor %} | |
| 156 } | |
| 157 *output = std::move(result); | |
| 158 } else { | |
| 159 output->reset(); | |
| 160 } | |
| 161 return success; | |
| 162 } | |
| 163 | |
| 164 } // namespace internal | |
| OLD | NEW |