| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_SERIALIZATION_H_ | 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_SERIALIZATION_H_ |
| 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_SERIALIZATION_H_ | 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_SERIALIZATION_H_ |
| 7 | 7 |
| 8 #include <type_traits> | 8 #include <type_traits> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 SerializationContext* context) { | 115 SerializationContext* context) { |
| 116 DCHECK(validate_params->key_validate_params); | 116 DCHECK(validate_params->key_validate_params); |
| 117 DCHECK(validate_params->element_validate_params); | 117 DCHECK(validate_params->element_validate_params); |
| 118 if (CallIsNullIfExists<Traits>(input)) { | 118 if (CallIsNullIfExists<Traits>(input)) { |
| 119 *output = nullptr; | 119 *output = nullptr; |
| 120 return; | 120 return; |
| 121 } | 121 } |
| 122 | 122 |
| 123 auto result = Data::New(buf); | 123 auto result = Data::New(buf); |
| 124 if (result) { | 124 if (result) { |
| 125 result->keys.ptr = | 125 auto keys_ptr = |
| 126 MojomTypeTraits<Array<Key>>::Data::New(Traits::GetSize(input), buf); | 126 MojomTypeTraits<Array<Key>>::Data::New(Traits::GetSize(input), buf); |
| 127 if (result->keys.ptr) { | 127 if (keys_ptr) { |
| 128 MapKeyReader<MaybeConstUserType> key_reader(input); | 128 MapKeyReader<MaybeConstUserType> key_reader(input); |
| 129 KeyArraySerializer::SerializeElements( | 129 KeyArraySerializer::SerializeElements( |
| 130 &key_reader, buf, result->keys.ptr, | 130 &key_reader, buf, keys_ptr, validate_params->key_validate_params, |
| 131 validate_params->key_validate_params, context); | 131 context); |
| 132 result->keys.Set(keys_ptr); |
| 132 } | 133 } |
| 133 | 134 |
| 134 result->values.ptr = | 135 auto values_ptr = |
| 135 MojomTypeTraits<Array<Value>>::Data::New(Traits::GetSize(input), buf); | 136 MojomTypeTraits<Array<Value>>::Data::New(Traits::GetSize(input), buf); |
| 136 if (result->values.ptr) { | 137 if (values_ptr) { |
| 137 MapValueReader<MaybeConstUserType> value_reader(input); | 138 MapValueReader<MaybeConstUserType> value_reader(input); |
| 138 ValueArraySerializer::SerializeElements( | 139 ValueArraySerializer::SerializeElements( |
| 139 &value_reader, buf, result->values.ptr, | 140 &value_reader, buf, values_ptr, |
| 140 validate_params->element_validate_params, context); | 141 validate_params->element_validate_params, context); |
| 142 result->values.Set(values_ptr); |
| 141 } | 143 } |
| 142 } | 144 } |
| 143 *output = result; | 145 *output = result; |
| 144 } | 146 } |
| 145 | 147 |
| 146 static bool Deserialize(Data* input, | 148 static bool Deserialize(Data* input, |
| 147 UserType* output, | 149 UserType* output, |
| 148 SerializationContext* context) { | 150 SerializationContext* context) { |
| 149 if (!input) | 151 if (!input) |
| 150 return CallSetToNullIfExists<Traits>(output); | 152 return CallSetToNullIfExists<Traits>(output); |
| 151 | 153 |
| 152 std::vector<UserKey> keys; | 154 std::vector<UserKey> keys; |
| 153 std::vector<UserValue> values; | 155 std::vector<UserValue> values; |
| 154 | 156 |
| 155 if (!KeyArraySerializer::DeserializeElements(input->keys.ptr, &keys, | 157 if (!KeyArraySerializer::DeserializeElements(input->keys.Get(), &keys, |
| 156 context) || | 158 context) || |
| 157 !ValueArraySerializer::DeserializeElements(input->values.ptr, &values, | 159 !ValueArraySerializer::DeserializeElements(input->values.Get(), &values, |
| 158 context)) { | 160 context)) { |
| 159 return false; | 161 return false; |
| 160 } | 162 } |
| 161 | 163 |
| 162 DCHECK_EQ(keys.size(), values.size()); | 164 DCHECK_EQ(keys.size(), values.size()); |
| 163 size_t size = keys.size(); | 165 size_t size = keys.size(); |
| 164 Traits::SetToEmpty(output); | 166 Traits::SetToEmpty(output); |
| 165 | 167 |
| 166 for (size_t i = 0; i < size; ++i) { | 168 for (size_t i = 0; i < size; ++i) { |
| 167 if (!Traits::Insert(*output, std::move(keys[i]), std::move(values[i]))) | 169 if (!Traits::Insert(*output, std::move(keys[i]), std::move(values[i]))) |
| 168 return false; | 170 return false; |
| 169 } | 171 } |
| 170 return true; | 172 return true; |
| 171 } | 173 } |
| 172 }; | 174 }; |
| 173 | 175 |
| 174 } // namespace internal | 176 } // namespace internal |
| 175 } // namespace mojo | 177 } // namespace mojo |
| 176 | 178 |
| 177 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_SERIALIZATION_H_ | 179 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_SERIALIZATION_H_ |
| OLD | NEW |