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 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 114 internal::MapSerializer<MapKey, DataKey>::GetItemSize(it.GetKey()); | 114 internal::MapSerializer<MapKey, DataKey>::GetItemSize(it.GetKey()); |
| 115 value_data_size += | 115 value_data_size += |
| 116 internal::MapSerializer<MapValue, DataValue>::GetItemSize( | 116 internal::MapSerializer<MapValue, DataValue>::GetItemSize( |
| 117 it.GetValue()); | 117 it.GetValue()); |
| 118 } | 118 } |
| 119 | 119 |
| 120 return struct_overhead + key_base_size + key_data_size + value_base_size + | 120 return struct_overhead + key_base_size + key_data_size + value_base_size + |
| 121 value_data_size; | 121 value_data_size; |
| 122 } | 122 } |
| 123 | 123 |
| 124 // SerializeMap_ will return VALIDATION_ERROR_NONE on success and sets | |
|
viettrungluu
2015/10/09 01:35:19
"sets" -> "set"
vardhan
2015/10/09 22:31:28
Done.
| |
| 125 // |output| accordingly. On failure, |input| will be partially serialized into | |
| 126 // |output| up until an error occurs (which is propagated up and returned by | |
| 127 // SerializeMap_), in which case |buf| is also partially consumed. | |
| 128 // | |
| 124 // We don't need an ArrayValidateParams instance for key validation since | 129 // 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 | 130 // we can deduce it from the Key type. (which can only be primitive types or |
| 126 // non-nullable strings.) | 131 // non-nullable strings.) |
| 127 template <typename MapKey, | 132 template <typename MapKey, |
| 128 typename MapValue, | 133 typename MapValue, |
| 129 typename DataKey, | 134 typename DataKey, |
| 130 typename DataValue> | 135 typename DataValue> |
| 131 inline void SerializeMap_( | 136 inline internal::ValidationError SerializeMap_( |
| 132 Map<MapKey, MapValue>* input, | 137 Map<MapKey, MapValue>* input, |
| 133 internal::Buffer* buf, | 138 internal::Buffer* buf, |
| 134 internal::Map_Data<DataKey, DataValue>** output, | 139 internal::Map_Data<DataKey, DataValue>** output, |
| 135 const internal::ArrayValidateParams* value_validate_params) { | 140 const internal::ArrayValidateParams* value_validate_params) { |
| 136 if (input && *input) { | 141 if (input->is_null()) { |
| 137 internal::Map_Data<DataKey, DataValue>* result = | 142 // |input| could be a nullable map, in which case |output| is serialized as |
| 138 internal::Map_Data<DataKey, DataValue>::New(buf); | 143 // null, which is valid. |
| 139 internal::Array_Data<DataKey>* keys_data = | 144 *output = nullptr; |
| 140 internal::Array_Data<DataKey>::New(input->size(), buf); | 145 return internal::VALIDATION_ERROR_NONE; |
| 146 } | |
| 141 | 147 |
| 142 if (result && keys_data) { | 148 internal::Map_Data<DataKey, DataValue>* result = |
| 143 result->keys.ptr = keys_data; | 149 internal::Map_Data<DataKey, DataValue>::New(buf); |
| 144 | 150 |
| 145 // We *must* serialize the keys before we allocate an Array_Data for the | 151 // We *must* serialize the keys before we allocate an Array_Data for the |
| 146 // values. | 152 // values. |
| 147 internal::MapKeyIterator<MapKey, MapValue> key_iter(input); | 153 internal::Array_Data<DataKey>* keys_data = |
| 148 const internal::ArrayValidateParams* key_validate_params = | 154 internal::Array_Data<DataKey>::New(input->size(), buf); |
| 149 internal::MapKeyValidateParamsFactory<DataKey>::Get(); | 155 result->keys.ptr = keys_data; |
| 150 | 156 |
| 157 internal::MapKeyIterator<MapKey, MapValue> key_iter(input); | |
| 158 const internal::ArrayValidateParams* key_validate_params = | |
| 159 internal::MapKeyValidateParamsFactory<DataKey>::Get(); | |
| 160 | |
| 161 auto keys_retval = | |
| 151 internal::ArraySerializer<MapKey, DataKey>::SerializeElements( | 162 internal::ArraySerializer<MapKey, DataKey>::SerializeElements( |
| 152 key_iter.begin(), input->size(), buf, result->keys.ptr, | 163 key_iter.begin(), input->size(), buf, result->keys.ptr, |
| 153 key_validate_params); | 164 key_validate_params); |
| 165 if (keys_retval != internal::VALIDATION_ERROR_NONE) | |
| 166 return keys_retval; | |
| 154 | 167 |
| 155 // Now we try allocate an Array_Data for the values | 168 // Now we try allocate an Array_Data for the values |
| 156 internal::Array_Data<DataValue>* values_data = | 169 internal::Array_Data<DataValue>* values_data = |
| 157 internal::Array_Data<DataValue>::New(input->size(), buf); | 170 internal::Array_Data<DataValue>::New(input->size(), buf); |
| 158 if (values_data) { | 171 result->values.ptr = values_data; |
| 159 result->values.ptr = values_data; | 172 |
| 160 internal::MapValueIterator<MapKey, MapValue> value_iter(input); | 173 internal::MapValueIterator<MapKey, MapValue> value_iter(input); |
| 161 internal::ArraySerializer<MapValue, DataValue>::SerializeElements( | 174 |
| 162 value_iter.begin(), input->size(), buf, result->values.ptr, | 175 auto values_retval = |
| 163 value_validate_params); | 176 internal::ArraySerializer<MapValue, DataValue>::SerializeElements( |
| 164 } | 177 value_iter.begin(), input->size(), buf, result->values.ptr, |
| 165 } | 178 value_validate_params); |
| 166 *output = result; | 179 |
|
viettrungluu
2015/10/09 01:35:19
nit: delete this blank line
vardhan
2015/10/09 22:31:28
Done.
| |
| 167 } else { | 180 if (values_retval != internal::VALIDATION_ERROR_NONE) |
| 168 *output = nullptr; | 181 return values_retval; |
| 169 } | 182 |
| 183 *output = result; | |
| 184 return internal::VALIDATION_ERROR_NONE; | |
| 170 } | 185 } |
| 171 | 186 |
| 172 template <typename MapKey, | 187 template <typename MapKey, |
| 173 typename MapValue, | 188 typename MapValue, |
| 174 typename DataKey, | 189 typename DataKey, |
| 175 typename DataValue> | 190 typename DataValue> |
| 176 inline void Deserialize_(internal::Map_Data<DataKey, DataValue>* input, | 191 inline void Deserialize_(internal::Map_Data<DataKey, DataValue>* input, |
| 177 Map<MapKey, MapValue>* output) { | 192 Map<MapKey, MapValue>* output) { |
| 178 if (input) { | 193 if (input) { |
| 179 Array<MapKey> keys; | 194 Array<MapKey> keys; |
| 180 Array<MapValue> values; | 195 Array<MapValue> values; |
| 181 | 196 |
| 182 Deserialize_(input->keys.ptr, &keys); | 197 Deserialize_(input->keys.ptr, &keys); |
| 183 Deserialize_(input->values.ptr, &values); | 198 Deserialize_(input->values.ptr, &values); |
| 184 | 199 |
| 185 *output = Map<MapKey, MapValue>(keys.Pass(), values.Pass()); | 200 *output = Map<MapKey, MapValue>(keys.Pass(), values.Pass()); |
| 186 } else { | 201 } else { |
| 187 output->reset(); | 202 output->reset(); |
| 188 } | 203 } |
| 189 } | 204 } |
| 190 | 205 |
| 191 } // namespace mojo | 206 } // namespace mojo |
| 192 | 207 |
| 193 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_SERIALIZATION_H_ | 208 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_SERIALIZATION_H_ |
| OLD | NEW |