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 // TODO(vardhan): Currently, the logic for serializing a mojom type exists in | 5 // TODO(vardhan): Currently, the logic for serializing a mojom type exists in |
| 6 // two places: the C++ code generator template, and here. However, most types | 6 // two places: the C++ code generator template, and here. However, most types |
| 7 // are serialized the same way within Arrays or outside, with the exception of | 7 // are serialized the same way within Arrays or outside, with the exception of |
| 8 // |bool|. Consider defining serialization/deserialization traits for each | 8 // |bool|. Consider defining serialization/deserialization traits for each |
| 9 // serializable type and call those traits from here. This should help us | 9 // serializable type and call those traits from here. This should help us |
| 10 // remove most of the ArraySerializer<> specializations here. | 10 // remove most of the ArraySerializer<> specializations here. |
| (...skipping 282 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 293 typename std::enable_if< | 293 typename std::enable_if< |
| 294 std::is_pointer<typename WrapperTraits<S>::DataType>::value, | 294 std::is_pointer<typename WrapperTraits<S>::DataType>::value, |
| 295 typename WrapperTraits<S>::DataType>::type, | 295 typename WrapperTraits<S>::DataType>::type, |
| 296 false> { | 296 false> { |
| 297 typedef | 297 typedef |
| 298 typename std::remove_pointer<typename WrapperTraits<S>::DataType>::type | 298 typename std::remove_pointer<typename WrapperTraits<S>::DataType>::type |
| 299 S_Data; | 299 S_Data; |
| 300 static size_t GetSerializedSize(const Array<S>& input) { | 300 static size_t GetSerializedSize(const Array<S>& input) { |
| 301 size_t size = sizeof(Array_Data<S_Data*>) + | 301 size_t size = sizeof(Array_Data<S_Data*>) + |
| 302 input.size() * sizeof(StructPointer<S_Data>); | 302 input.size() * sizeof(StructPointer<S_Data>); |
| 303 for (size_t i = 0; i < input.size(); ++i) | 303 for (size_t i = 0; i < input.size(); ++i) { |
| 304 size += GetSerializedSize_(*(UnwrapConstStructPtr<S>::value(input[i]))); | 304 if (!input[i].is_null()) |
| 305 size += GetSerializedSize_(*(UnwrapConstStructPtr<S>::value(input[i]))); | |
| 306 } | |
| 305 return size; | 307 return size; |
| 306 } | 308 } |
| 307 | 309 |
| 308 template <typename Iterator> | 310 template <typename Iterator> |
| 309 static ValidationError SerializeElements( | 311 static ValidationError SerializeElements( |
| 310 Iterator it, | 312 Iterator it, |
| 311 size_t num_elements, | 313 size_t num_elements, |
| 312 Buffer* buf, | 314 Buffer* buf, |
| 313 Array_Data<S_Data*>* output, | 315 Array_Data<S_Data*>* output, |
| 314 const ArrayValidateParams* validate_params) { | 316 const ArrayValidateParams* validate_params) { |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 399 } | 401 } |
| 400 | 402 |
| 401 // Since Deserialize_ takes in a |Struct*| (not |StructPtr|), we need to | 403 // Since Deserialize_ takes in a |Struct*| (not |StructPtr|), we need to |
| 402 // initialize the |StructPtr| here before deserializing into its underlying | 404 // initialize the |StructPtr| here before deserializing into its underlying |
| 403 // data. | 405 // data. |
| 404 // TODO(vardhan): Either all containers, or just Deserialize_(), should | 406 // TODO(vardhan): Either all containers, or just Deserialize_(), should |
| 405 // support taking in an allocator. | 407 // support taking in an allocator. |
| 406 template <typename T> | 408 template <typename T> |
| 407 static void Run(typename WrapperTraits<StructPtr<T>>::DataType input, | 409 static void Run(typename WrapperTraits<StructPtr<T>>::DataType input, |
| 408 StructPtr<T>* output) { | 410 StructPtr<T>* output) { |
| 409 *output = T::New(); | 411 if (input != nullptr) { |
|
viettrungluu
2015/12/07 22:03:51
nit: probably "if (input)" should be fine and more
vardhan
2015/12/07 22:48:31
Done.
I was trying to be more explicit about what
| |
| 410 Deserialize_(input, output->get()); | 412 *output = T::New(); |
| 413 Deserialize_(input, output->get()); | |
| 414 } | |
| 411 } | 415 } |
| 412 | 416 |
| 413 template <typename T> | 417 template <typename T> |
| 414 static void Run(typename WrapperTraits<InlinedStructPtr<T>>::DataType input, | 418 static void Run(typename WrapperTraits<InlinedStructPtr<T>>::DataType input, |
| 415 InlinedStructPtr<T>* output) { | 419 InlinedStructPtr<T>* output) { |
| 416 *output = T::New(); | 420 if (input != nullptr) { |
|
viettrungluu
2015/12/07 22:03:51
"
vardhan
2015/12/07 22:48:31
Done.
| |
| 417 Deserialize_(input, output->get()); | 421 *output = T::New(); |
| 422 Deserialize_(input, output->get()); | |
| 423 } | |
| 418 } | 424 } |
| 419 }; | 425 }; |
| 420 }; | 426 }; |
| 421 | 427 |
| 422 // Handles serialization and deserialization of arrays of unions. | 428 // Handles serialization and deserialization of arrays of unions. |
| 423 template <typename U, typename U_Data> | 429 template <typename U, typename U_Data> |
| 424 struct ArraySerializer<U, U_Data, true> { | 430 struct ArraySerializer<U, U_Data, true> { |
| 425 static size_t GetSerializedSize(const Array<U>& input) { | 431 static size_t GetSerializedSize(const Array<U>& input) { |
| 426 size_t size = sizeof(Array_Data<U_Data>); | 432 size_t size = sizeof(Array_Data<U_Data>); |
| 427 for (size_t i = 0; i < input.size(); ++i) { | 433 for (size_t i = 0; i < input.size(); ++i) { |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 470 output->Swap(&result); | 476 output->Swap(&result); |
| 471 } | 477 } |
| 472 }; | 478 }; |
| 473 | 479 |
| 474 } // namespace internal | 480 } // namespace internal |
| 475 | 481 |
| 476 template <typename E> | 482 template <typename E> |
| 477 inline size_t GetSerializedSize_(const Array<E>& input) { | 483 inline size_t GetSerializedSize_(const Array<E>& input) { |
| 478 if (!input) | 484 if (!input) |
| 479 return 0; | 485 return 0; |
| 480 typedef typename internal::WrapperTraits<E>::DataType F; | 486 using F = typename internal::WrapperTraits<E>::DataType; |
| 481 return internal::ArraySerializer<E, F>::GetSerializedSize(input); | 487 return internal::ArraySerializer<E, F>::GetSerializedSize(input); |
| 482 } | 488 } |
| 483 | 489 |
| 484 // SerializeArray_ will return ValidationError::NONE on success and set | 490 // SerializeArray_ will return ValidationError::NONE on success and set |
| 485 // |output| accordingly. On failure, |input| will be partially serialized into | 491 // |output| accordingly. On failure, |input| will be partially serialized into |
| 486 // |output| up until an error occurs (which is propagated up and returned by | 492 // |output| up until an error occurs (which is propagated up and returned by |
| 487 // SerializeArray_), in which case |buf| is also partially consumed. | 493 // SerializeArray_), in which case |buf| is also partially consumed. |
| 488 template <typename E, typename F> | 494 template <typename E, typename F> |
| 489 inline internal::ValidationError SerializeArray_( | 495 inline internal::ValidationError SerializeArray_( |
| 490 Array<E>* input, | 496 Array<E>* input, |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 525 if (input) { | 531 if (input) { |
| 526 internal::ArraySerializer<E, F>::DeserializeElements(input, output); | 532 internal::ArraySerializer<E, F>::DeserializeElements(input, output); |
| 527 } else { | 533 } else { |
| 528 output->reset(); | 534 output->reset(); |
| 529 } | 535 } |
| 530 } | 536 } |
| 531 | 537 |
| 532 } // namespace mojo | 538 } // namespace mojo |
| 533 | 539 |
| 534 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_SERIALIZATION_H_ | 540 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_SERIALIZATION_H_ |
| OLD | NEW |