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 |
(...skipping 28 matching lines...) Expand all Loading... |
39 | 39 |
40 explicit ArrayIterator(MaybeConstUserType& input) | 40 explicit ArrayIterator(MaybeConstUserType& input) |
41 : input_(input), iter_(CallGetBeginIfExists<Traits>(input)) {} | 41 : input_(input), iter_(CallGetBeginIfExists<Traits>(input)) {} |
42 ~ArrayIterator() {} | 42 ~ArrayIterator() {} |
43 | 43 |
44 size_t GetSize() const { return Traits::GetSize(input_); } | 44 size_t GetSize() const { return Traits::GetSize(input_); } |
45 | 45 |
46 using GetNextResult = | 46 using GetNextResult = |
47 decltype(Traits::GetValue(std::declval<IteratorType&>())); | 47 decltype(Traits::GetValue(std::declval<IteratorType&>())); |
48 GetNextResult GetNext() { | 48 GetNextResult GetNext() { |
49 auto& value = Traits::GetValue(iter_); | 49 GetNextResult value = Traits::GetValue(iter_); |
50 Traits::AdvanceIterator(iter_); | 50 Traits::AdvanceIterator(iter_); |
51 return value; | 51 return value; |
52 } | 52 } |
53 | 53 |
54 using GetDataIfExistsResult = decltype( | 54 using GetDataIfExistsResult = decltype( |
55 CallGetDataIfExists<Traits>(std::declval<MaybeConstUserType&>())); | 55 CallGetDataIfExists<Traits>(std::declval<MaybeConstUserType&>())); |
56 GetDataIfExistsResult GetDataIfExists() { | 56 GetDataIfExistsResult GetDataIfExists() { |
57 return CallGetDataIfExists<Traits>(input_); | 57 return CallGetDataIfExists<Traits>(input_); |
58 } | 58 } |
59 | 59 |
(...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
299 static void SerializeElements(UserTypeIterator* input, | 299 static void SerializeElements(UserTypeIterator* input, |
300 Buffer* buf, | 300 Buffer* buf, |
301 Data* output, | 301 Data* output, |
302 const ContainerValidateParams* validate_params, | 302 const ContainerValidateParams* validate_params, |
303 SerializationContext* context) { | 303 SerializationContext* context) { |
304 DCHECK(!validate_params->element_validate_params) | 304 DCHECK(!validate_params->element_validate_params) |
305 << "Handle or interface type should not have array validate params"; | 305 << "Handle or interface type should not have array validate params"; |
306 | 306 |
307 size_t size = input->GetSize(); | 307 size_t size = input->GetSize(); |
308 for (size_t i = 0; i < size; ++i) { | 308 for (size_t i = 0; i < size; ++i) { |
309 Serialize<Element>(input->GetNext(), &output->at(i), context); | 309 typename UserTypeIterator::GetNextResult next = input->GetNext(); |
| 310 Serialize<Element>(next, &output->at(i), context); |
310 | 311 |
311 static const ValidationError kError = | 312 static const ValidationError kError = |
312 BelongsTo<Element, | 313 BelongsTo<Element, |
313 MojomTypeCategory::ASSOCIATED_INTERFACE | | 314 MojomTypeCategory::ASSOCIATED_INTERFACE | |
314 MojomTypeCategory::ASSOCIATED_INTERFACE_REQUEST>::value | 315 MojomTypeCategory::ASSOCIATED_INTERFACE_REQUEST>::value |
315 ? VALIDATION_ERROR_UNEXPECTED_INVALID_INTERFACE_ID | 316 ? VALIDATION_ERROR_UNEXPECTED_INVALID_INTERFACE_ID |
316 : VALIDATION_ERROR_UNEXPECTED_INVALID_HANDLE; | 317 : VALIDATION_ERROR_UNEXPECTED_INVALID_HANDLE; |
317 MOJO_INTERNAL_DLOG_SERIALIZATION_WARNING( | 318 MOJO_INTERNAL_DLOG_SERIALIZATION_WARNING( |
318 !validate_params->element_is_nullable && | 319 !validate_params->element_is_nullable && |
319 !IsHandleOrInterfaceValid(output->at(i)), | 320 !IsHandleOrInterfaceValid(output->at(i)), |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
354 using UserType = typename std::remove_const<MaybeConstUserType>::type; | 355 using UserType = typename std::remove_const<MaybeConstUserType>::type; |
355 using Data = typename MojomTypeTraits<MojomType>::Data; | 356 using Data = typename MojomTypeTraits<MojomType>::Data; |
356 using Element = typename MojomType::Element; | 357 using Element = typename MojomType::Element; |
357 using DataElementPtr = typename MojomTypeTraits<Element>::Data*; | 358 using DataElementPtr = typename MojomTypeTraits<Element>::Data*; |
358 using Traits = ArrayTraits<UserType>; | 359 using Traits = ArrayTraits<UserType>; |
359 | 360 |
360 static size_t GetSerializedSize(UserTypeIterator* input, | 361 static size_t GetSerializedSize(UserTypeIterator* input, |
361 SerializationContext* context) { | 362 SerializationContext* context) { |
362 size_t element_count = input->GetSize(); | 363 size_t element_count = input->GetSize(); |
363 size_t size = sizeof(Data) + element_count * sizeof(typename Data::Element); | 364 size_t size = sizeof(Data) + element_count * sizeof(typename Data::Element); |
364 for (size_t i = 0; i < element_count; ++i) | 365 for (size_t i = 0; i < element_count; ++i) { |
365 size += PrepareToSerialize<Element>(input->GetNext(), context); | 366 typename UserTypeIterator::GetNextResult next = input->GetNext(); |
| 367 size += PrepareToSerialize<Element>(next, context); |
| 368 } |
366 return size; | 369 return size; |
367 } | 370 } |
368 | 371 |
369 static void SerializeElements(UserTypeIterator* input, | 372 static void SerializeElements(UserTypeIterator* input, |
370 Buffer* buf, | 373 Buffer* buf, |
371 Data* output, | 374 Data* output, |
372 const ContainerValidateParams* validate_params, | 375 const ContainerValidateParams* validate_params, |
373 SerializationContext* context) { | 376 SerializationContext* context) { |
374 size_t size = input->GetSize(); | 377 size_t size = input->GetSize(); |
375 for (size_t i = 0; i < size; ++i) { | 378 for (size_t i = 0; i < size; ++i) { |
376 DataElementPtr data_ptr; | 379 DataElementPtr data_ptr; |
377 SerializeCaller<Element>::Run(input->GetNext(), buf, &data_ptr, | 380 typename UserTypeIterator::GetNextResult next = input->GetNext(); |
| 381 SerializeCaller<Element>::Run(next, buf, &data_ptr, |
378 validate_params->element_validate_params, | 382 validate_params->element_validate_params, |
379 context); | 383 context); |
380 output->at(i).Set(data_ptr); | 384 output->at(i).Set(data_ptr); |
381 MOJO_INTERNAL_DLOG_SERIALIZATION_WARNING( | 385 MOJO_INTERNAL_DLOG_SERIALIZATION_WARNING( |
382 !validate_params->element_is_nullable && !data_ptr, | 386 !validate_params->element_is_nullable && !data_ptr, |
383 VALIDATION_ERROR_UNEXPECTED_NULL_POINTER, | 387 VALIDATION_ERROR_UNEXPECTED_NULL_POINTER, |
384 MakeMessageWithArrayIndex("null in array expecting valid pointers", | 388 MakeMessageWithArrayIndex("null in array expecting valid pointers", |
385 size, i)); | 389 size, i)); |
386 } | 390 } |
387 } | 391 } |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
448 typename Traits::Element>::value, | 452 typename Traits::Element>::value, |
449 "Incorrect array serializer"); | 453 "Incorrect array serializer"); |
450 | 454 |
451 static size_t GetSerializedSize(UserTypeIterator* input, | 455 static size_t GetSerializedSize(UserTypeIterator* input, |
452 SerializationContext* context) { | 456 SerializationContext* context) { |
453 size_t element_count = input->GetSize(); | 457 size_t element_count = input->GetSize(); |
454 size_t size = sizeof(Data); | 458 size_t size = sizeof(Data); |
455 for (size_t i = 0; i < element_count; ++i) { | 459 for (size_t i = 0; i < element_count; ++i) { |
456 // Call with |inlined| set to false, so that it will account for both the | 460 // Call with |inlined| set to false, so that it will account for both the |
457 // data in the union and the space in the array used to hold the union. | 461 // data in the union and the space in the array used to hold the union. |
458 size += PrepareToSerialize<Element>(input->GetNext(), false, context); | 462 typename UserTypeIterator::GetNextResult next = input->GetNext(); |
| 463 size += PrepareToSerialize<Element>(next, false, context); |
459 } | 464 } |
460 return size; | 465 return size; |
461 } | 466 } |
462 | 467 |
463 static void SerializeElements(UserTypeIterator* input, | 468 static void SerializeElements(UserTypeIterator* input, |
464 Buffer* buf, | 469 Buffer* buf, |
465 Data* output, | 470 Data* output, |
466 const ContainerValidateParams* validate_params, | 471 const ContainerValidateParams* validate_params, |
467 SerializationContext* context) { | 472 SerializationContext* context) { |
468 size_t size = input->GetSize(); | 473 size_t size = input->GetSize(); |
469 for (size_t i = 0; i < size; ++i) { | 474 for (size_t i = 0; i < size; ++i) { |
470 typename Data::Element* result = output->storage() + i; | 475 typename Data::Element* result = output->storage() + i; |
471 Serialize<Element>(input->GetNext(), buf, &result, true, context); | 476 typename UserTypeIterator::GetNextResult next = input->GetNext(); |
| 477 Serialize<Element>(next, buf, &result, true, context); |
472 MOJO_INTERNAL_DLOG_SERIALIZATION_WARNING( | 478 MOJO_INTERNAL_DLOG_SERIALIZATION_WARNING( |
473 !validate_params->element_is_nullable && output->at(i).is_null(), | 479 !validate_params->element_is_nullable && output->at(i).is_null(), |
474 VALIDATION_ERROR_UNEXPECTED_NULL_POINTER, | 480 VALIDATION_ERROR_UNEXPECTED_NULL_POINTER, |
475 MakeMessageWithArrayIndex("null in array expecting valid unions", | 481 MakeMessageWithArrayIndex("null in array expecting valid unions", |
476 size, i)); | 482 size, i)); |
477 } | 483 } |
478 } | 484 } |
479 | 485 |
480 static bool DeserializeElements(Data* input, | 486 static bool DeserializeElements(Data* input, |
481 UserType* output, | 487 UserType* output, |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
539 if (!input) | 545 if (!input) |
540 return CallSetToNullIfExists<Traits>(output); | 546 return CallSetToNullIfExists<Traits>(output); |
541 return Impl::DeserializeElements(input, output, context); | 547 return Impl::DeserializeElements(input, output, context); |
542 } | 548 } |
543 }; | 549 }; |
544 | 550 |
545 } // namespace internal | 551 } // namespace internal |
546 } // namespace mojo | 552 } // namespace mojo |
547 | 553 |
548 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_SERIALIZATION_H_ | 554 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_SERIALIZATION_H_ |
OLD | NEW |