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_ARRAY_SERIALIZATION_H_ | 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_SERIALIZATION_H_ |
| 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_SERIALIZATION_H_ | 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_SERIALIZATION_H_ |
| 7 | 7 |
| 8 #include <string.h> // For |memcpy()|. | 8 #include <string.h> // For |memcpy()|. |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 38 | 38 |
| 39 // Handles serialization and deserialization of arrays of pod types. | 39 // Handles serialization and deserialization of arrays of pod types. |
| 40 template <typename E, typename F> | 40 template <typename E, typename F> |
| 41 struct ArraySerializer<E, F, false> { | 41 struct ArraySerializer<E, F, false> { |
| 42 static_assert(sizeof(E) == sizeof(F), "Incorrect array serializer"); | 42 static_assert(sizeof(E) == sizeof(F), "Incorrect array serializer"); |
| 43 static size_t GetSerializedSize(const Array<E>& input) { | 43 static size_t GetSerializedSize(const Array<E>& input) { |
| 44 return sizeof(Array_Data<F>) + Align(input.size() * sizeof(E)); | 44 return sizeof(Array_Data<F>) + Align(input.size() * sizeof(E)); |
| 45 } | 45 } |
| 46 | 46 |
| 47 template <typename Iterator> | 47 template <typename Iterator> |
| 48 static void SerializeElements(Iterator it, | 48 static ValidationError SerializeElements( |
| 49 size_t num_elements, | 49 Iterator it, |
| 50 Buffer* buf, | 50 size_t num_elements, |
| 51 Array_Data<F>* output, | 51 Buffer* buf, |
| 52 const ArrayValidateParams* validate_params) { | 52 Array_Data<F>* output, |
| 53 const ArrayValidateParams* validate_params) { | |
| 53 MOJO_DCHECK(!validate_params->element_is_nullable) | 54 MOJO_DCHECK(!validate_params->element_is_nullable) |
| 54 << "Primitive type should be non-nullable"; | 55 << "Primitive type should be non-nullable"; |
| 55 MOJO_DCHECK(!validate_params->element_validate_params) | 56 MOJO_DCHECK(!validate_params->element_validate_params) |
| 56 << "Primitive type should not have array validate params"; | 57 << "Primitive type should not have array validate params"; |
| 57 for (size_t i = 0; i < num_elements; ++i, ++it) | 58 for (size_t i = 0; i < num_elements; ++i, ++it) |
| 58 output->at(i) = *it; | 59 output->at(i) = *it; |
| 60 | |
| 61 return ValidationError::VALIDATION_ERROR_NONE; | |
|
viettrungluu
2015/10/06 16:40:06
Since ValidationError is (not yet!) a class enum,
vardhan
2015/10/06 22:57:14
That was my plan, but since ValidationError is use
viettrungluu
2015/10/08 15:51:25
OK, but for now, ValidationError::VALIDATION_ERROR
vardhan
2015/10/08 20:53:51
Done.
| |
| 59 } | 62 } |
| 60 | 63 |
| 61 // We can optimize serializing PODs by |memcpy|ing directly. | 64 // We can optimize serializing PODs by |memcpy|ing directly. |
| 62 // Note that this has precedence over its templated sibling defined above. | 65 // Note that this has precedence over its templated sibling defined above. |
| 63 static void SerializeElements(typename Array<E>::Iterator it, | 66 static ValidationError SerializeElements( |
| 64 size_t num_elements, | 67 typename Array<E>::Iterator it, |
| 65 Buffer* buf, | 68 size_t num_elements, |
| 66 Array_Data<F>* output, | 69 Buffer* buf, |
| 67 const ArrayValidateParams* validate_params) { | 70 Array_Data<F>* output, |
| 71 const ArrayValidateParams* validate_params) { | |
| 68 MOJO_DCHECK(!validate_params->element_is_nullable) | 72 MOJO_DCHECK(!validate_params->element_is_nullable) |
| 69 << "Primitive type should be non-nullable"; | 73 << "Primitive type should be non-nullable"; |
| 70 MOJO_DCHECK(!validate_params->element_validate_params) | 74 MOJO_DCHECK(!validate_params->element_validate_params) |
| 71 << "Primitive type should not have array validate params"; | 75 << "Primitive type should not have array validate params"; |
| 72 if (num_elements) | 76 if (num_elements) |
| 73 memcpy(output->storage(), &(*it), num_elements * sizeof(E)); | 77 memcpy(output->storage(), &(*it), num_elements * sizeof(E)); |
| 78 | |
| 79 return ValidationError::VALIDATION_ERROR_NONE; | |
| 74 } | 80 } |
| 75 | 81 |
| 76 static void DeserializeElements(Array_Data<F>* input, Array<E>* output) { | 82 static void DeserializeElements(Array_Data<F>* input, Array<E>* output) { |
| 77 std::vector<E> result(input->size()); | 83 std::vector<E> result(input->size()); |
| 78 if (input->size()) | 84 if (input->size()) |
| 79 memcpy(&result[0], input->storage(), input->size() * sizeof(E)); | 85 memcpy(&result[0], input->storage(), input->size() * sizeof(E)); |
| 80 output->Swap(&result); | 86 output->Swap(&result); |
| 81 } | 87 } |
| 82 }; | 88 }; |
| 83 | 89 |
| 84 // Serializes and deserializes arrays of bools. | 90 // Serializes and deserializes arrays of bools. |
| 85 template <> | 91 template <> |
| 86 struct ArraySerializer<bool, bool, false> { | 92 struct ArraySerializer<bool, bool, false> { |
| 87 static size_t GetSerializedSize(const Array<bool>& input) { | 93 static size_t GetSerializedSize(const Array<bool>& input) { |
| 88 return sizeof(Array_Data<bool>) + Align((input.size() + 7) / 8); | 94 return sizeof(Array_Data<bool>) + Align((input.size() + 7) / 8); |
| 89 } | 95 } |
| 90 | 96 |
| 91 template <typename Iterator> | 97 template <typename Iterator> |
| 92 static void SerializeElements(Iterator it, | 98 static ValidationError SerializeElements( |
| 93 size_t num_elements, | 99 Iterator it, |
| 94 Buffer* buf, | 100 size_t num_elements, |
| 95 Array_Data<bool>* output, | 101 Buffer* buf, |
| 96 const ArrayValidateParams* validate_params) { | 102 Array_Data<bool>* output, |
| 103 const ArrayValidateParams* validate_params) { | |
| 97 MOJO_DCHECK(!validate_params->element_is_nullable) | 104 MOJO_DCHECK(!validate_params->element_is_nullable) |
| 98 << "Primitive type should be non-nullable"; | 105 << "Primitive type should be non-nullable"; |
| 99 MOJO_DCHECK(!validate_params->element_validate_params) | 106 MOJO_DCHECK(!validate_params->element_validate_params) |
| 100 << "Primitive type should not have array validate params"; | 107 << "Primitive type should not have array validate params"; |
| 101 | 108 |
| 102 // TODO(darin): Can this be a memcpy somehow instead of a bit-by-bit copy? | 109 // TODO(darin): Can this be a memcpy somehow instead of a bit-by-bit copy? |
| 103 for (size_t i = 0; i < num_elements; ++i, ++it) | 110 for (size_t i = 0; i < num_elements; ++i, ++it) |
| 104 output->at(i) = *it; | 111 output->at(i) = *it; |
| 112 | |
| 113 return ValidationError::VALIDATION_ERROR_NONE; | |
| 105 } | 114 } |
| 106 | 115 |
| 107 static void DeserializeElements(Array_Data<bool>* input, | 116 static void DeserializeElements(Array_Data<bool>* input, |
| 108 Array<bool>* output) { | 117 Array<bool>* output) { |
| 109 Array<bool> result(input->size()); | 118 Array<bool> result(input->size()); |
| 110 // TODO(darin): Can this be a memcpy somehow instead of a bit-by-bit copy? | 119 // TODO(darin): Can this be a memcpy somehow instead of a bit-by-bit copy? |
| 111 for (size_t i = 0; i < input->size(); ++i) | 120 for (size_t i = 0; i < input->size(); ++i) |
| 112 result.at(i) = input->at(i); | 121 result.at(i) = input->at(i); |
| 113 output->Swap(&result); | 122 output->Swap(&result); |
| 114 } | 123 } |
| 115 }; | 124 }; |
| 116 | 125 |
| 117 // Serializes and deserializes arrays of handles. | 126 // Serializes and deserializes arrays of handles. |
| 118 template <typename H> | 127 template <typename H> |
| 119 struct ArraySerializer<ScopedHandleBase<H>, H, false> { | 128 struct ArraySerializer<ScopedHandleBase<H>, H, false> { |
| 120 static size_t GetSerializedSize(const Array<ScopedHandleBase<H>>& input) { | 129 static size_t GetSerializedSize(const Array<ScopedHandleBase<H>>& input) { |
| 121 return sizeof(Array_Data<H>) + Align(input.size() * sizeof(H)); | 130 return sizeof(Array_Data<H>) + Align(input.size() * sizeof(H)); |
| 122 } | 131 } |
| 123 | 132 |
| 124 template <typename Iterator> | 133 template <typename Iterator> |
| 125 static void SerializeElements(Iterator it, | 134 static ValidationError SerializeElements( |
| 126 size_t num_elements, | 135 Iterator it, |
| 127 Buffer* buf, | 136 size_t num_elements, |
| 128 Array_Data<H>* output, | 137 Buffer* buf, |
| 129 const ArrayValidateParams* validate_params) { | 138 Array_Data<H>* output, |
| 139 const ArrayValidateParams* validate_params) { | |
| 130 MOJO_DCHECK(!validate_params->element_validate_params) | 140 MOJO_DCHECK(!validate_params->element_validate_params) |
| 131 << "Handle type should not have array validate params"; | 141 << "Handle type should not have array validate params"; |
| 132 | 142 |
| 133 for (size_t i = 0; i < num_elements; ++i, ++it) { | 143 for (size_t i = 0; i < num_elements; ++i, ++it) { |
| 134 // Transfer ownership of the handle. | 144 // Transfer ownership of the handle. |
| 135 output->at(i) = it->release(); | 145 output->at(i) = it->release(); |
| 136 MOJO_INTERNAL_DLOG_SERIALIZATION_WARNING( | 146 MOJO_INTERNAL_SERIALIZATION_CHECK_AND_RETURN( |
| 137 !validate_params->element_is_nullable && !output->at(i).is_valid(), | 147 !validate_params->element_is_nullable && !output->at(i).is_valid(), |
| 138 VALIDATION_ERROR_UNEXPECTED_INVALID_HANDLE, | 148 ValidationError::VALIDATION_ERROR_UNEXPECTED_INVALID_HANDLE, |
| 139 MakeMessageWithArrayIndex( | 149 MakeMessageWithArrayIndex( |
| 140 "invalid handle in array expecting valid handles", num_elements, | 150 "invalid handle in array expecting valid handles", num_elements, |
| 141 i)); | 151 i)); |
| 142 } | 152 } |
| 153 | |
| 154 return ValidationError::VALIDATION_ERROR_NONE; | |
| 143 } | 155 } |
| 144 | 156 |
| 145 static void DeserializeElements(Array_Data<H>* input, | 157 static void DeserializeElements(Array_Data<H>* input, |
| 146 Array<ScopedHandleBase<H>>* output) { | 158 Array<ScopedHandleBase<H>>* output) { |
| 147 Array<ScopedHandleBase<H>> result(input->size()); | 159 Array<ScopedHandleBase<H>> result(input->size()); |
| 148 for (size_t i = 0; i < input->size(); ++i) | 160 for (size_t i = 0; i < input->size(); ++i) |
| 149 result.at(i) = MakeScopedHandle(FetchAndReset(&input->at(i))); | 161 result.at(i) = MakeScopedHandle(FetchAndReset(&input->at(i))); |
| 150 output->Swap(&result); | 162 output->Swap(&result); |
| 151 } | 163 } |
| 152 }; | 164 }; |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 164 typename RemovePointer<typename WrapperTraits<S>::DataType>::type S_Data; | 176 typename RemovePointer<typename WrapperTraits<S>::DataType>::type S_Data; |
| 165 static size_t GetSerializedSize(const Array<S>& input) { | 177 static size_t GetSerializedSize(const Array<S>& input) { |
| 166 size_t size = sizeof(Array_Data<S_Data*>) + | 178 size_t size = sizeof(Array_Data<S_Data*>) + |
| 167 input.size() * sizeof(StructPointer<S_Data>); | 179 input.size() * sizeof(StructPointer<S_Data>); |
| 168 for (size_t i = 0; i < input.size(); ++i) | 180 for (size_t i = 0; i < input.size(); ++i) |
| 169 size += GetSerializedSize_(*(UnwrapConstStructPtr<S>::value(input[i]))); | 181 size += GetSerializedSize_(*(UnwrapConstStructPtr<S>::value(input[i]))); |
| 170 return size; | 182 return size; |
| 171 } | 183 } |
| 172 | 184 |
| 173 template <typename Iterator> | 185 template <typename Iterator> |
| 174 static void SerializeElements(Iterator it, | 186 static ValidationError SerializeElements( |
| 175 size_t num_elements, | 187 Iterator it, |
| 176 Buffer* buf, | 188 size_t num_elements, |
| 177 Array_Data<S_Data*>* output, | 189 Buffer* buf, |
| 178 const ArrayValidateParams* validate_params) { | 190 Array_Data<S_Data*>* output, |
| 191 const ArrayValidateParams* validate_params) { | |
| 179 for (size_t i = 0; i < num_elements; ++i, ++it) { | 192 for (size_t i = 0; i < num_elements; ++i, ++it) { |
| 180 S_Data* element; | 193 S_Data* element; |
| 181 SerializeCaller::Run(&(*it), buf, &element, | 194 auto retval = SerializeCaller::Run( |
| 182 validate_params->element_validate_params); | 195 &(*it), buf, &element, validate_params->element_validate_params); |
| 196 if (retval != ValidationError::VALIDATION_ERROR_NONE) | |
| 197 return retval; | |
| 198 | |
| 183 output->at(i) = element; | 199 output->at(i) = element; |
| 184 MOJO_INTERNAL_DLOG_SERIALIZATION_WARNING( | 200 MOJO_INTERNAL_SERIALIZATION_CHECK_AND_RETURN( |
| 185 !validate_params->element_is_nullable && !element, | 201 !validate_params->element_is_nullable && !element, |
| 186 VALIDATION_ERROR_UNEXPECTED_NULL_POINTER, | 202 ValidationError::VALIDATION_ERROR_UNEXPECTED_NULL_POINTER, |
| 187 MakeMessageWithArrayIndex("null in array expecting valid pointers", | 203 MakeMessageWithArrayIndex("null in array expecting valid pointers", |
| 188 num_elements, i)); | 204 num_elements, i)); |
| 189 } | 205 } |
| 206 | |
| 207 return ValidationError::VALIDATION_ERROR_NONE; | |
| 190 } | 208 } |
| 191 | 209 |
| 192 static void DeserializeElements(Array_Data<S_Data*>* input, | 210 static void DeserializeElements(Array_Data<S_Data*>* input, |
| 193 Array<S>* output) { | 211 Array<S>* output) { |
| 194 Array<S> result(input->size()); | 212 Array<S> result(input->size()); |
| 195 for (size_t i = 0; i < input->size(); ++i) { | 213 for (size_t i = 0; i < input->size(); ++i) { |
| 196 DeserializeCaller::Run(input->at(i), &result[i]); | 214 DeserializeCaller::Run(input->at(i), &result[i]); |
| 197 } | 215 } |
| 198 output->Swap(&result); | 216 output->Swap(&result); |
| 199 } | 217 } |
| 200 | 218 |
| 201 private: | 219 private: |
| 202 // SerializeCaller template is used by |ArraySerializer| to dispatch a | 220 // SerializeCaller template is used by |ArraySerializer| to dispatch a |
| 203 // serialize call on a non-POD type. This template is defined outside | 221 // serialize call on a non-POD type. This template is defined outside |
| 204 // |ArraySerializer| since you cannot specialize a struct within a class | 222 // |ArraySerializer| since you cannot specialize a struct within a class |
| 205 // definition. | 223 // definition. |
| 206 struct SerializeCaller { | 224 struct SerializeCaller { |
| 207 // This template needs to be suppressed if |T| is |String|, otherwise it | 225 // This template needs to be suppressed if |T| is |String|, otherwise it |
| 208 // takes precedence over the |String|-overloaded Run() below. | 226 // takes precedence over the |String|-overloaded Run() below. |
| 209 template <typename T, | 227 template <typename T, |
| 210 typename = typename EnableIf<!IsSame<T, String>::value, T>::type> | 228 typename = typename EnableIf<!IsSame<T, String>::value, T>::type> |
| 211 static void Run(T* input, | 229 static ValidationError Run(T* input, |
| 212 Buffer* buf, | 230 Buffer* buf, |
| 213 typename WrapperTraits<T>::DataType* output, | 231 typename WrapperTraits<T>::DataType* output, |
| 214 const ArrayValidateParams* validate_params) { | 232 const ArrayValidateParams* validate_params) { |
| 215 MOJO_DCHECK(!validate_params) | 233 MOJO_DCHECK(!validate_params) |
| 216 << "Struct type should not have array validate params"; | 234 << "Struct type should not have array validate params"; |
| 217 Serialize_(UnwrapStructPtr<T>::value(*input), buf, output); | 235 return Serialize_(UnwrapStructPtr<T>::value(*input), buf, output); |
| 218 } | 236 } |
| 219 | 237 |
| 220 static void Run(const String* input, | 238 static ValidationError Run(const String* input, |
| 221 Buffer* buf, | 239 Buffer* buf, |
| 222 String_Data** output, | 240 String_Data** output, |
| 223 const ArrayValidateParams* validate_params) { | 241 const ArrayValidateParams* validate_params) { |
| 224 MOJO_DCHECK(validate_params && | 242 MOJO_DCHECK(validate_params && |
| 225 !validate_params->element_validate_params && | 243 !validate_params->element_validate_params && |
| 226 !validate_params->element_is_nullable && | 244 !validate_params->element_is_nullable && |
| 227 validate_params->expected_num_elements == 0) | 245 validate_params->expected_num_elements == 0) |
| 228 << "String type has unexpected array validate params"; | 246 << "String type has unexpected array validate params"; |
| 229 SerializeString_(*input, buf, output); | 247 SerializeString_(*input, buf, output); |
| 248 return ValidationError::VALIDATION_ERROR_NONE; | |
| 230 } | 249 } |
| 231 | 250 |
| 232 template <typename T> | 251 template <typename T> |
| 233 static void Run(Array<T>* input, | 252 static ValidationError Run(Array<T>* input, |
| 234 Buffer* buf, | 253 Buffer* buf, |
| 235 typename Array<T>::Data_** output, | 254 typename Array<T>::Data_** output, |
| 236 const ArrayValidateParams* validate_params) { | 255 const ArrayValidateParams* validate_params) { |
| 237 SerializeArray_(input, buf, output, validate_params); | 256 return SerializeArray_(input, buf, output, validate_params); |
| 238 } | 257 } |
| 239 | 258 |
| 240 template <typename Key, typename Value> | 259 template <typename Key, typename Value> |
| 241 static void Run(Map<Key, Value>* input, | 260 static ValidationError Run(Map<Key, Value>* input, |
| 242 Buffer* buf, | 261 Buffer* buf, |
| 243 typename Map<Key, Value>::Data_** output, | 262 typename Map<Key, Value>::Data_** output, |
| 244 const ArrayValidateParams* validate_params) { | 263 const ArrayValidateParams* validate_params) { |
| 245 SerializeMap_(input, buf, output, validate_params); | 264 return SerializeMap_(input, buf, output, validate_params); |
| 246 } | 265 } |
| 247 }; | 266 }; |
| 248 | 267 |
| 249 struct DeserializeCaller { | 268 struct DeserializeCaller { |
| 250 template <typename T> | 269 template <typename T> |
| 251 static void Run(typename WrapperTraits<T>::DataType input, T* output) { | 270 static void Run(typename WrapperTraits<T>::DataType input, T* output) { |
| 252 Deserialize_(input, output); | 271 Deserialize_(input, output); |
| 253 } | 272 } |
| 254 | 273 |
| 255 // Since Deserialize_ takes in a |Struct*| (not |StructPtr|), we need to | 274 // Since Deserialize_ takes in a |Struct*| (not |StructPtr|), we need to |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 280 size_t size = sizeof(Array_Data<U_Data>); | 299 size_t size = sizeof(Array_Data<U_Data>); |
| 281 for (size_t i = 0; i < input.size(); ++i) { | 300 for (size_t i = 0; i < input.size(); ++i) { |
| 282 // GetSerializedSize_ will account for both the data in the union and the | 301 // GetSerializedSize_ will account for both the data in the union and the |
| 283 // space in the array used to hold the union. | 302 // space in the array used to hold the union. |
| 284 size += GetSerializedSize_(input[i], false); | 303 size += GetSerializedSize_(input[i], false); |
| 285 } | 304 } |
| 286 return size; | 305 return size; |
| 287 } | 306 } |
| 288 | 307 |
| 289 template <typename Iterator> | 308 template <typename Iterator> |
| 290 static void SerializeElements(Iterator it, | 309 static ValidationError SerializeElements( |
| 291 size_t num_elements, | 310 Iterator it, |
| 292 Buffer* buf, | 311 size_t num_elements, |
| 293 Array_Data<U_Data>* output, | 312 Buffer* buf, |
| 294 const ArrayValidateParams* validate_params) { | 313 Array_Data<U_Data>* output, |
| 314 const ArrayValidateParams* validate_params) { | |
| 295 for (size_t i = 0; i < num_elements; ++i, ++it) { | 315 for (size_t i = 0; i < num_elements; ++i, ++it) { |
| 296 U_Data* result = output->storage() + i; | 316 U_Data* result = output->storage() + i; |
| 297 SerializeUnion_(it->get(), buf, &result, true); | 317 auto retval = SerializeUnion_(it->get(), buf, &result, true); |
| 298 MOJO_INTERNAL_DLOG_SERIALIZATION_WARNING( | 318 if (retval != ValidationError::VALIDATION_ERROR_NONE) |
| 319 return retval; | |
| 320 MOJO_INTERNAL_SERIALIZATION_CHECK_AND_RETURN( | |
| 299 !validate_params->element_is_nullable && output->at(i).is_null(), | 321 !validate_params->element_is_nullable && output->at(i).is_null(), |
| 300 VALIDATION_ERROR_UNEXPECTED_NULL_POINTER, | 322 ValidationError::VALIDATION_ERROR_UNEXPECTED_NULL_POINTER, |
| 301 MakeMessageWithArrayIndex("null in array expecting valid unions", | 323 MakeMessageWithArrayIndex("null in array expecting valid unions", |
| 302 num_elements, i)); | 324 num_elements, i)); |
| 303 } | 325 } |
| 326 | |
| 327 return ValidationError::VALIDATION_ERROR_NONE; | |
| 304 } | 328 } |
| 305 | 329 |
| 306 static void DeserializeElements(Array_Data<U_Data>* input, Array<U>* output) { | 330 static void DeserializeElements(Array_Data<U_Data>* input, Array<U>* output) { |
| 307 Array<U> result(input->size()); | 331 Array<U> result(input->size()); |
| 308 for (size_t i = 0; i < input->size(); ++i) { | 332 for (size_t i = 0; i < input->size(); ++i) { |
| 309 auto& elem = input->at(i); | 333 auto& elem = input->at(i); |
| 310 if (!elem.is_null()) { | 334 if (!elem.is_null()) { |
| 311 using UnwrapedUnionType = typename RemoveStructPtr<U>::type; | 335 using UnwrapedUnionType = typename RemoveStructPtr<U>::type; |
| 312 result[i] = UnwrapedUnionType::New(); | 336 result[i] = UnwrapedUnionType::New(); |
| 313 Deserialize_(&elem, result[i].get()); | 337 Deserialize_(&elem, result[i].get()); |
| 314 } | 338 } |
| 315 } | 339 } |
| 316 output->Swap(&result); | 340 output->Swap(&result); |
| 317 } | 341 } |
| 318 }; | 342 }; |
| 319 | 343 |
| 320 } // namespace internal | 344 } // namespace internal |
| 321 | 345 |
| 322 template <typename E> | 346 template <typename E> |
| 323 inline size_t GetSerializedSize_(const Array<E>& input) { | 347 inline size_t GetSerializedSize_(const Array<E>& input) { |
| 324 if (!input) | 348 if (!input) |
| 325 return 0; | 349 return 0; |
| 326 typedef typename internal::WrapperTraits<E>::DataType F; | 350 typedef typename internal::WrapperTraits<E>::DataType F; |
| 327 return internal::ArraySerializer<E, F>::GetSerializedSize(input); | 351 return internal::ArraySerializer<E, F>::GetSerializedSize(input); |
| 328 } | 352 } |
| 329 | 353 |
| 330 template <typename E, typename F> | 354 template <typename E, typename F> |
| 331 inline void SerializeArray_( | 355 inline internal::ValidationError SerializeArray_( |
| 332 Array<E>* input, | 356 Array<E>* input, |
| 333 internal::Buffer* buf, | 357 internal::Buffer* buf, |
| 334 internal::Array_Data<F>** output, | 358 internal::Array_Data<F>** output, |
| 335 const internal::ArrayValidateParams* validate_params) { | 359 const internal::ArrayValidateParams* validate_params) { |
| 336 MOJO_DCHECK(input); | 360 MOJO_DCHECK(input); |
| 337 if (*input) { | 361 if (*input) { |
| 338 MOJO_INTERNAL_DLOG_SERIALIZATION_WARNING( | 362 MOJO_INTERNAL_SERIALIZATION_CHECK_AND_RETURN( |
| 339 validate_params->expected_num_elements != 0 && | 363 validate_params->expected_num_elements != 0 && |
| 340 input->size() != validate_params->expected_num_elements, | 364 input->size() != validate_params->expected_num_elements, |
| 341 internal::VALIDATION_ERROR_UNEXPECTED_ARRAY_HEADER, | 365 internal::ValidationError::VALIDATION_ERROR_UNEXPECTED_ARRAY_HEADER, |
| 342 internal::MakeMessageWithExpectedArraySize( | 366 internal::MakeMessageWithExpectedArraySize( |
| 343 "fixed-size array has wrong number of elements", input->size(), | 367 "fixed-size array has wrong number of elements", input->size(), |
| 344 validate_params->expected_num_elements)); | 368 validate_params->expected_num_elements)); |
| 345 | 369 |
| 346 internal::Array_Data<F>* result = | 370 internal::Array_Data<F>* result = |
| 347 internal::Array_Data<F>::New(input->size(), buf); | 371 internal::Array_Data<F>::New(input->size(), buf); |
| 348 if (result) { | 372 if (result) { |
| 349 internal::ArraySerializer<E, F>::SerializeElements( | 373 auto retval = internal::ArraySerializer<E, F>::SerializeElements( |
| 350 input->begin(), input->size(), buf, result, validate_params); | 374 input->begin(), input->size(), buf, result, validate_params); |
| 375 if (retval != internal::ValidationError::VALIDATION_ERROR_NONE) { | |
| 376 return retval; | |
| 377 } | |
| 351 } | 378 } |
| 352 *output = result; | 379 *output = result; |
| 353 } else { | 380 } else { |
| 381 // It is up to the caller to make the given |Array| is not null if it is | |
| 382 // not nullable. | |
| 354 *output = nullptr; | 383 *output = nullptr; |
| 355 } | 384 } |
| 385 | |
| 386 return internal::ValidationError::VALIDATION_ERROR_NONE; | |
| 356 } | 387 } |
| 357 | 388 |
| 358 template <typename E, typename F> | 389 template <typename E, typename F> |
| 359 inline void Deserialize_(internal::Array_Data<F>* input, Array<E>* output) { | 390 inline void Deserialize_(internal::Array_Data<F>* input, Array<E>* output) { |
| 360 if (input) { | 391 if (input) { |
| 361 internal::ArraySerializer<E, F>::DeserializeElements(input, output); | 392 internal::ArraySerializer<E, F>::DeserializeElements(input, output); |
| 362 } else { | 393 } else { |
| 363 output->reset(); | 394 output->reset(); |
| 364 } | 395 } |
| 365 } | 396 } |
| 366 | 397 |
| 367 } // namespace mojo | 398 } // namespace mojo |
| 368 | 399 |
| 369 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_SERIALIZATION_H_ | 400 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_SERIALIZATION_H_ |
| OLD | NEW |