Chromium Code Reviews| 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 "mojo/public/cpp/bindings/lib/array_internal.h" | 8 #include "mojo/public/cpp/bindings/lib/array_internal.h" |
| 9 #include "mojo/public/cpp/bindings/lib/array_serialization.h" | 9 #include "mojo/public/cpp/bindings/lib/array_serialization.h" |
| 10 #include "mojo/public/cpp/bindings/lib/bindings_internal.h" | 10 #include "mojo/public/cpp/bindings/lib/bindings_internal.h" |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 121 value_data_size; | 121 value_data_size; |
| 122 } | 122 } |
| 123 | 123 |
| 124 // We don't need an ArrayValidateParams instance for key validation since | 124 // We don't need an ArrayValidateParams instance for key validation since |
| 125 // we can deduce it from the Key type. (which can only be primitive types or | 125 // we can deduce it from the Key type. (which can only be primitive types or |
| 126 // non-nullable strings.) | 126 // non-nullable strings.) |
| 127 template <typename MapKey, | 127 template <typename MapKey, |
| 128 typename MapValue, | 128 typename MapValue, |
| 129 typename DataKey, | 129 typename DataKey, |
| 130 typename DataValue> | 130 typename DataValue> |
| 131 inline void SerializeMap_( | 131 inline internal::ValidationError SerializeMap_( |
| 132 Map<MapKey, MapValue>* input, | 132 Map<MapKey, MapValue>* input, |
| 133 internal::Buffer* buf, | 133 internal::Buffer* buf, |
| 134 internal::Map_Data<DataKey, DataValue>** output, | 134 internal::Map_Data<DataKey, DataValue>** output, |
| 135 const internal::ArrayValidateParams* value_validate_params) { | 135 const internal::ArrayValidateParams* value_validate_params) { |
| 136 if (input && *input) { | 136 if (!input->is_null()) { |
|
viettrungluu
2015/10/08 15:51:25
I assume that |input| itself shouldn't be a null p
vardhan
2015/10/08 20:53:51
That's right, thought i'd fix it here.
| |
| 137 internal::Map_Data<DataKey, DataValue>* result = | 137 internal::Map_Data<DataKey, DataValue>* result = |
| 138 internal::Map_Data<DataKey, DataValue>::New(buf); | 138 internal::Map_Data<DataKey, DataValue>::New(buf); |
| 139 | |
| 140 // We *must* serialize the keys before we allocate an Array_Data for the | |
| 141 // values. | |
| 139 internal::Array_Data<DataKey>* keys_data = | 142 internal::Array_Data<DataKey>* keys_data = |
| 140 internal::Array_Data<DataKey>::New(input->size(), buf); | 143 internal::Array_Data<DataKey>::New(input->size(), buf); |
| 144 result->keys.ptr = keys_data; | |
| 141 | 145 |
| 142 if (result && keys_data) { | 146 internal::MapKeyIterator<MapKey, MapValue> key_iter(input); |
| 143 result->keys.ptr = keys_data; | 147 const internal::ArrayValidateParams* key_validate_params = |
| 148 internal::MapKeyValidateParamsFactory<DataKey>::Get(); | |
| 144 | 149 |
| 145 // We *must* serialize the keys before we allocate an Array_Data for the | 150 auto keys_retval = |
| 146 // values. | 151 internal::ArraySerializer<MapKey, DataKey>::SerializeElements( |
| 147 internal::MapKeyIterator<MapKey, MapValue> key_iter(input); | 152 key_iter.begin(), input->size(), buf, result->keys.ptr, |
| 148 const internal::ArrayValidateParams* key_validate_params = | 153 key_validate_params); |
| 149 internal::MapKeyValidateParamsFactory<DataKey>::Get(); | 154 if (keys_retval != internal::ValidationError::VALIDATION_ERROR_NONE) |
| 155 return keys_retval; | |
| 150 | 156 |
| 151 internal::ArraySerializer<MapKey, DataKey>::SerializeElements( | 157 // Now we try allocate an Array_Data for the values |
| 152 key_iter.begin(), input->size(), buf, result->keys.ptr, | 158 internal::Array_Data<DataValue>* values_data = |
| 153 key_validate_params); | 159 internal::Array_Data<DataValue>::New(input->size(), buf); |
| 160 result->values.ptr = values_data; | |
| 154 | 161 |
| 155 // Now we try allocate an Array_Data for the values | 162 internal::MapValueIterator<MapKey, MapValue> value_iter(input); |
| 156 internal::Array_Data<DataValue>* values_data = | 163 |
| 157 internal::Array_Data<DataValue>::New(input->size(), buf); | 164 auto values_retval = |
| 158 if (values_data) { | |
| 159 result->values.ptr = values_data; | |
| 160 internal::MapValueIterator<MapKey, MapValue> value_iter(input); | |
| 161 internal::ArraySerializer<MapValue, DataValue>::SerializeElements( | 165 internal::ArraySerializer<MapValue, DataValue>::SerializeElements( |
| 162 value_iter.begin(), input->size(), buf, result->values.ptr, | 166 value_iter.begin(), input->size(), buf, result->values.ptr, |
| 163 value_validate_params); | 167 value_validate_params); |
| 164 } | 168 |
| 165 } | 169 if (values_retval != internal::ValidationError::VALIDATION_ERROR_NONE) |
| 170 return values_retval; | |
| 171 | |
| 166 *output = result; | 172 *output = result; |
| 167 } else { | 173 } else { |
| 174 // |input| could be a nullable map, in which case |output| is serialized as | |
| 175 // null, which is valid. | |
| 168 *output = nullptr; | 176 *output = nullptr; |
| 169 } | 177 } |
| 178 return internal::ValidationError::VALIDATION_ERROR_NONE; | |
| 170 } | 179 } |
| 171 | 180 |
| 172 template <typename MapKey, | 181 template <typename MapKey, |
| 173 typename MapValue, | 182 typename MapValue, |
| 174 typename DataKey, | 183 typename DataKey, |
| 175 typename DataValue> | 184 typename DataValue> |
| 176 inline void Deserialize_(internal::Map_Data<DataKey, DataValue>* input, | 185 inline void Deserialize_(internal::Map_Data<DataKey, DataValue>* input, |
| 177 Map<MapKey, MapValue>* output) { | 186 Map<MapKey, MapValue>* output) { |
| 178 if (input) { | 187 if (input) { |
| 179 Array<MapKey> keys; | 188 Array<MapKey> keys; |
| 180 Array<MapValue> values; | 189 Array<MapValue> values; |
| 181 | 190 |
| 182 Deserialize_(input->keys.ptr, &keys); | 191 Deserialize_(input->keys.ptr, &keys); |
| 183 Deserialize_(input->values.ptr, &values); | 192 Deserialize_(input->values.ptr, &values); |
| 184 | 193 |
| 185 *output = Map<MapKey, MapValue>(keys.Pass(), values.Pass()); | 194 *output = Map<MapKey, MapValue>(keys.Pass(), values.Pass()); |
| 186 } else { | 195 } else { |
| 187 output->reset(); | 196 output->reset(); |
| 188 } | 197 } |
| 189 } | 198 } |
| 190 | 199 |
| 191 } // namespace mojo | 200 } // namespace mojo |
| 192 | 201 |
| 193 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_SERIALIZATION_H_ | 202 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_SERIALIZATION_H_ |
| OLD | NEW |