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 && *input) { |
| 137 internal::Map_Data<DataKey, DataValue>* result = | 137 internal::Map_Data<DataKey, DataValue>* result = |
|
viettrungluu
2015/10/06 16:40:06
Drive-by: When does the New() fail? Is this an err
vardhan
2015/10/06 22:57:14
New() currently check-fails if buf->Allocate() doe
| |
| 138 internal::Map_Data<DataKey, DataValue>::New(buf); | 138 internal::Map_Data<DataKey, DataValue>::New(buf); |
| 139 internal::Array_Data<DataKey>* keys_data = | 139 internal::Array_Data<DataKey>* keys_data = |
| 140 internal::Array_Data<DataKey>::New(input->size(), buf); | 140 internal::Array_Data<DataKey>::New(input->size(), buf); |
| 141 | 141 |
| 142 if (result && keys_data) { | 142 if (result && keys_data) { |
| 143 result->keys.ptr = keys_data; | 143 result->keys.ptr = keys_data; |
| 144 | 144 |
| 145 // We *must* serialize the keys before we allocate an Array_Data for the | 145 // We *must* serialize the keys before we allocate an Array_Data for the |
| 146 // values. | 146 // values. |
| 147 internal::MapKeyIterator<MapKey, MapValue> key_iter(input); | 147 internal::MapKeyIterator<MapKey, MapValue> key_iter(input); |
| 148 const internal::ArrayValidateParams* key_validate_params = | 148 const internal::ArrayValidateParams* key_validate_params = |
| 149 internal::MapKeyValidateParamsFactory<DataKey>::Get(); | 149 internal::MapKeyValidateParamsFactory<DataKey>::Get(); |
| 150 | 150 |
| 151 internal::ArraySerializer<MapKey, DataKey>::SerializeElements( | 151 { |
| 152 key_iter.begin(), input->size(), buf, result->keys.ptr, | 152 auto retval = |
| 153 key_validate_params); | 153 internal::ArraySerializer<MapKey, DataKey>::SerializeElements( |
| 154 key_iter.begin(), input->size(), buf, result->keys.ptr, | |
| 155 key_validate_params); | |
| 156 if (retval != internal::ValidationError::VALIDATION_ERROR_NONE) | |
| 157 return retval; | |
|
viettrungluu
2015/10/06 16:40:06
Are you supposed to set *output in this case? (Dit
vardhan
2015/10/06 22:57:14
No, not in this case -- setting "*output = nullptr
| |
| 158 } | |
| 154 | 159 |
| 155 // Now we try allocate an Array_Data for the values | 160 // Now we try allocate an Array_Data for the values |
| 156 internal::Array_Data<DataValue>* values_data = | 161 internal::Array_Data<DataValue>* values_data = |
| 157 internal::Array_Data<DataValue>::New(input->size(), buf); | 162 internal::Array_Data<DataValue>::New(input->size(), buf); |
| 158 if (values_data) { | 163 if (values_data) { |
| 159 result->values.ptr = values_data; | 164 result->values.ptr = values_data; |
| 160 internal::MapValueIterator<MapKey, MapValue> value_iter(input); | 165 internal::MapValueIterator<MapKey, MapValue> value_iter(input); |
| 161 internal::ArraySerializer<MapValue, DataValue>::SerializeElements( | 166 { |
|
viettrungluu
2015/10/06 16:40:06
These braces seem redundant.
But more generally,
vardhan
2015/10/06 22:57:14
This way, I could reuse 'retval' as the name for t
| |
| 162 value_iter.begin(), input->size(), buf, result->values.ptr, | 167 auto retval = |
| 163 value_validate_params); | 168 internal::ArraySerializer<MapValue, DataValue>::SerializeElements( |
| 169 value_iter.begin(), input->size(), buf, result->values.ptr, | |
| 170 value_validate_params); | |
| 171 if (retval != internal::ValidationError::VALIDATION_ERROR_NONE) | |
| 172 return retval; | |
| 173 } | |
| 164 } | 174 } |
| 165 } | 175 } |
| 166 *output = result; | 176 *output = result; |
| 167 } else { | 177 } else { |
| 168 *output = nullptr; | 178 *output = nullptr; |
| 169 } | 179 } |
| 180 return internal::ValidationError::VALIDATION_ERROR_NONE; | |
| 170 } | 181 } |
| 171 | 182 |
| 172 template <typename MapKey, | 183 template <typename MapKey, |
| 173 typename MapValue, | 184 typename MapValue, |
| 174 typename DataKey, | 185 typename DataKey, |
| 175 typename DataValue> | 186 typename DataValue> |
| 176 inline void Deserialize_(internal::Map_Data<DataKey, DataValue>* input, | 187 inline void Deserialize_(internal::Map_Data<DataKey, DataValue>* input, |
| 177 Map<MapKey, MapValue>* output) { | 188 Map<MapKey, MapValue>* output) { |
| 178 if (input) { | 189 if (input) { |
| 179 Array<MapKey> keys; | 190 Array<MapKey> keys; |
| 180 Array<MapValue> values; | 191 Array<MapValue> values; |
| 181 | 192 |
| 182 Deserialize_(input->keys.ptr, &keys); | 193 Deserialize_(input->keys.ptr, &keys); |
| 183 Deserialize_(input->values.ptr, &values); | 194 Deserialize_(input->values.ptr, &values); |
| 184 | 195 |
| 185 *output = Map<MapKey, MapValue>(keys.Pass(), values.Pass()); | 196 *output = Map<MapKey, MapValue>(keys.Pass(), values.Pass()); |
| 186 } else { | 197 } else { |
| 187 output->reset(); | 198 output->reset(); |
| 188 } | 199 } |
| 189 } | 200 } |
| 190 | 201 |
| 191 } // namespace mojo | 202 } // namespace mojo |
| 192 | 203 |
| 193 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_SERIALIZATION_H_ | 204 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_SERIALIZATION_H_ |
| OLD | NEW |