| 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 <stddef.h> | 8 #include <stddef.h> |
| 9 #include <string.h> // For |memcpy()|. | 9 #include <string.h> // For |memcpy()|. |
| 10 | 10 |
| 11 #include <limits> | 11 #include <limits> |
| 12 #include <type_traits> | 12 #include <type_traits> |
| 13 #include <utility> | 13 #include <utility> |
| 14 #include <vector> | 14 #include <vector> |
| 15 | 15 |
| 16 #include "base/logging.h" | 16 #include "base/logging.h" |
| 17 #include "mojo/public/cpp/bindings/array.h" | 17 #include "mojo/public/cpp/bindings/array_data_view.h" |
| 18 #include "mojo/public/cpp/bindings/lib/array_internal.h" | 18 #include "mojo/public/cpp/bindings/lib/array_internal.h" |
| 19 #include "mojo/public/cpp/bindings/lib/serialization_forward.h" | 19 #include "mojo/public/cpp/bindings/lib/serialization_forward.h" |
| 20 #include "mojo/public/cpp/bindings/lib/template_util.h" | 20 #include "mojo/public/cpp/bindings/lib/template_util.h" |
| 21 #include "mojo/public/cpp/bindings/lib/validation_errors.h" | 21 #include "mojo/public/cpp/bindings/lib/validation_errors.h" |
| 22 #include "mojo/public/cpp/bindings/map.h" | 22 #include "mojo/public/cpp/bindings/map.h" |
| 23 | 23 |
| 24 namespace mojo { | 24 namespace mojo { |
| 25 namespace internal { | 25 namespace internal { |
| 26 | 26 |
| 27 template <typename Traits, | 27 template <typename Traits, |
| (...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 280 MojomTypeCategory::ASSOCIATED_INTERFACE | | 280 MojomTypeCategory::ASSOCIATED_INTERFACE | |
| 281 MojomTypeCategory::ASSOCIATED_INTERFACE_REQUEST | | 281 MojomTypeCategory::ASSOCIATED_INTERFACE_REQUEST | |
| 282 MojomTypeCategory::HANDLE | | 282 MojomTypeCategory::HANDLE | |
| 283 MojomTypeCategory::INTERFACE | | 283 MojomTypeCategory::INTERFACE | |
| 284 MojomTypeCategory::INTERFACE_REQUEST>::value>::type> { | 284 MojomTypeCategory::INTERFACE_REQUEST>::value>::type> { |
| 285 using UserType = typename std::remove_const<MaybeConstUserType>::type; | 285 using UserType = typename std::remove_const<MaybeConstUserType>::type; |
| 286 using Data = typename MojomTypeTraits<MojomType>::Data; | 286 using Data = typename MojomTypeTraits<MojomType>::Data; |
| 287 using Element = typename MojomType::Element; | 287 using Element = typename MojomType::Element; |
| 288 using Traits = ArrayTraits<UserType>; | 288 using Traits = ArrayTraits<UserType>; |
| 289 | 289 |
| 290 static_assert(std::is_same<Element, typename Traits::Element>::value, | |
| 291 "Incorrect array serializer"); | |
| 292 | |
| 293 static size_t GetSerializedSize(UserTypeIterator* input, | 290 static size_t GetSerializedSize(UserTypeIterator* input, |
| 294 SerializationContext* context) { | 291 SerializationContext* context) { |
| 295 return sizeof(Data) + | 292 return sizeof(Data) + |
| 296 Align(input->GetSize() * sizeof(typename Data::Element)); | 293 Align(input->GetSize() * sizeof(typename Data::Element)); |
| 297 } | 294 } |
| 298 | 295 |
| 299 static void SerializeElements(UserTypeIterator* input, | 296 static void SerializeElements(UserTypeIterator* input, |
| 300 Buffer* buf, | 297 Buffer* buf, |
| 301 Data* output, | 298 Data* output, |
| 302 const ContainerValidateParams* validate_params, | 299 const ContainerValidateParams* validate_params, |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 441 MojomType, | 438 MojomType, |
| 442 MaybeConstUserType, | 439 MaybeConstUserType, |
| 443 UserTypeIterator, | 440 UserTypeIterator, |
| 444 typename std::enable_if<BelongsTo<typename MojomType::Element, | 441 typename std::enable_if<BelongsTo<typename MojomType::Element, |
| 445 MojomTypeCategory::UNION>::value>::type> { | 442 MojomTypeCategory::UNION>::value>::type> { |
| 446 using UserType = typename std::remove_const<MaybeConstUserType>::type; | 443 using UserType = typename std::remove_const<MaybeConstUserType>::type; |
| 447 using Data = typename MojomTypeTraits<MojomType>::Data; | 444 using Data = typename MojomTypeTraits<MojomType>::Data; |
| 448 using Element = typename MojomType::Element; | 445 using Element = typename MojomType::Element; |
| 449 using Traits = ArrayTraits<UserType>; | 446 using Traits = ArrayTraits<UserType>; |
| 450 | 447 |
| 451 static_assert(std::is_same<typename MojomType::Element, | |
| 452 typename Traits::Element>::value, | |
| 453 "Incorrect array serializer"); | |
| 454 | |
| 455 static size_t GetSerializedSize(UserTypeIterator* input, | 448 static size_t GetSerializedSize(UserTypeIterator* input, |
| 456 SerializationContext* context) { | 449 SerializationContext* context) { |
| 457 size_t element_count = input->GetSize(); | 450 size_t element_count = input->GetSize(); |
| 458 size_t size = sizeof(Data); | 451 size_t size = sizeof(Data); |
| 459 for (size_t i = 0; i < element_count; ++i) { | 452 for (size_t i = 0; i < element_count; ++i) { |
| 460 // Call with |inlined| set to false, so that it will account for both the | 453 // Call with |inlined| set to false, so that it will account for both the |
| 461 // data in the union and the space in the array used to hold the union. | 454 // data in the union and the space in the array used to hold the union. |
| 462 typename UserTypeIterator::GetNextResult next = input->GetNext(); | 455 typename UserTypeIterator::GetNextResult next = input->GetNext(); |
| 463 size += PrepareToSerialize<Element>(next, false, context); | 456 size += PrepareToSerialize<Element>(next, false, context); |
| 464 } | 457 } |
| (...skipping 26 matching lines...) Expand all Loading... |
| 491 ArrayIterator<Traits, UserType> iterator(*output); | 484 ArrayIterator<Traits, UserType> iterator(*output); |
| 492 for (size_t i = 0; i < input->size(); ++i) { | 485 for (size_t i = 0; i < input->size(); ++i) { |
| 493 if (!Deserialize<Element>(&input->at(i), &iterator.GetNext(), context)) | 486 if (!Deserialize<Element>(&input->at(i), &iterator.GetNext(), context)) |
| 494 return false; | 487 return false; |
| 495 } | 488 } |
| 496 return true; | 489 return true; |
| 497 } | 490 } |
| 498 }; | 491 }; |
| 499 | 492 |
| 500 template <typename Element, typename MaybeConstUserType> | 493 template <typename Element, typename MaybeConstUserType> |
| 501 struct Serializer<Array<Element>, MaybeConstUserType> { | 494 struct Serializer<ArrayDataView<Element>, MaybeConstUserType> { |
| 502 using UserType = typename std::remove_const<MaybeConstUserType>::type; | 495 using UserType = typename std::remove_const<MaybeConstUserType>::type; |
| 503 using Traits = ArrayTraits<UserType>; | 496 using Traits = ArrayTraits<UserType>; |
| 504 using Impl = ArraySerializer<Array<Element>, | 497 using Impl = ArraySerializer<ArrayDataView<Element>, |
| 505 MaybeConstUserType, | 498 MaybeConstUserType, |
| 506 ArrayIterator<Traits, MaybeConstUserType>>; | 499 ArrayIterator<Traits, MaybeConstUserType>>; |
| 507 using Data = typename MojomTypeTraits<Array<Element>>::Data; | 500 using Data = typename MojomTypeTraits<ArrayDataView<Element>>::Data; |
| 508 | 501 |
| 509 static size_t PrepareToSerialize(MaybeConstUserType& input, | 502 static size_t PrepareToSerialize(MaybeConstUserType& input, |
| 510 SerializationContext* context) { | 503 SerializationContext* context) { |
| 511 if (CallIsNullIfExists<Traits>(input)) | 504 if (CallIsNullIfExists<Traits>(input)) |
| 512 return 0; | 505 return 0; |
| 513 ArrayIterator<Traits, MaybeConstUserType> iterator(input); | 506 ArrayIterator<Traits, MaybeConstUserType> iterator(input); |
| 514 return Impl::GetSerializedSize(&iterator, context); | 507 return Impl::GetSerializedSize(&iterator, context); |
| 515 } | 508 } |
| 516 | 509 |
| 517 static void Serialize(MaybeConstUserType& input, | 510 static void Serialize(MaybeConstUserType& input, |
| (...skipping 27 matching lines...) Expand all Loading... |
| 545 if (!input) | 538 if (!input) |
| 546 return CallSetToNullIfExists<Traits>(output); | 539 return CallSetToNullIfExists<Traits>(output); |
| 547 return Impl::DeserializeElements(input, output, context); | 540 return Impl::DeserializeElements(input, output, context); |
| 548 } | 541 } |
| 549 }; | 542 }; |
| 550 | 543 |
| 551 } // namespace internal | 544 } // namespace internal |
| 552 } // namespace mojo | 545 } // namespace mojo |
| 553 | 546 |
| 554 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_SERIALIZATION_H_ | 547 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_SERIALIZATION_H_ |
| OLD | NEW |