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 Serialize<Element>(input->GetNext(), &output->at(i), context); |
yzshen1
2016/07/28 18:25:19
We need to do
typename UserTypeIterator::GetNextRe
Fady Samuel
2016/07/28 19:53:31
Done.
| |
310 | 310 |
311 static const ValidationError kError = | 311 static const ValidationError kError = |
312 BelongsTo<Element, | 312 BelongsTo<Element, |
313 MojomTypeCategory::ASSOCIATED_INTERFACE | | 313 MojomTypeCategory::ASSOCIATED_INTERFACE | |
314 MojomTypeCategory::ASSOCIATED_INTERFACE_REQUEST>::value | 314 MojomTypeCategory::ASSOCIATED_INTERFACE_REQUEST>::value |
315 ? VALIDATION_ERROR_UNEXPECTED_INVALID_INTERFACE_ID | 315 ? VALIDATION_ERROR_UNEXPECTED_INVALID_INTERFACE_ID |
316 : VALIDATION_ERROR_UNEXPECTED_INVALID_HANDLE; | 316 : VALIDATION_ERROR_UNEXPECTED_INVALID_HANDLE; |
317 MOJO_INTERNAL_DLOG_SERIALIZATION_WARNING( | 317 MOJO_INTERNAL_DLOG_SERIALIZATION_WARNING( |
318 !validate_params->element_is_nullable && | 318 !validate_params->element_is_nullable && |
319 !IsHandleOrInterfaceValid(output->at(i)), | 319 !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; | 354 using UserType = typename std::remove_const<MaybeConstUserType>::type; |
355 using Data = typename MojomTypeTraits<MojomType>::Data; | 355 using Data = typename MojomTypeTraits<MojomType>::Data; |
356 using Element = typename MojomType::Element; | 356 using Element = typename MojomType::Element; |
357 using DataElementPtr = typename MojomTypeTraits<Element>::Data*; | 357 using DataElementPtr = typename MojomTypeTraits<Element>::Data*; |
358 using Traits = ArrayTraits<UserType>; | 358 using Traits = ArrayTraits<UserType>; |
359 | 359 |
360 static size_t GetSerializedSize(UserTypeIterator* input, | 360 static size_t GetSerializedSize(UserTypeIterator* input, |
361 SerializationContext* context) { | 361 SerializationContext* context) { |
362 size_t element_count = input->GetSize(); | 362 size_t element_count = input->GetSize(); |
363 size_t size = sizeof(Data) + element_count * sizeof(typename Data::Element); | 363 size_t size = sizeof(Data) + element_count * sizeof(typename Data::Element); |
364 for (size_t i = 0; i < element_count; ++i) | 364 for (size_t i = 0; i < element_count; ++i) { |
365 size += PrepareToSerialize<Element>(input->GetNext(), context); | 365 typename UserTypeIterator::GetNextResult next = input->GetNext(); |
yzshen1
2016/07/28 18:25:19
Please also update the documentation of array_trai
Fady Samuel
2016/07/28 19:53:31
Done.
| |
366 size += PrepareToSerialize<Element>(next, context); | |
367 } | |
366 return size; | 368 return size; |
367 } | 369 } |
368 | 370 |
369 static void SerializeElements(UserTypeIterator* input, | 371 static void SerializeElements(UserTypeIterator* input, |
370 Buffer* buf, | 372 Buffer* buf, |
371 Data* output, | 373 Data* output, |
372 const ContainerValidateParams* validate_params, | 374 const ContainerValidateParams* validate_params, |
373 SerializationContext* context) { | 375 SerializationContext* context) { |
374 size_t size = input->GetSize(); | 376 size_t size = input->GetSize(); |
375 for (size_t i = 0; i < size; ++i) { | 377 for (size_t i = 0; i < size; ++i) { |
376 DataElementPtr data_ptr; | 378 DataElementPtr data_ptr; |
377 SerializeCaller<Element>::Run(input->GetNext(), buf, &data_ptr, | 379 typename UserTypeIterator::GetNextResult next = input->GetNext(); |
380 SerializeCaller<Element>::Run(next, buf, &data_ptr, | |
378 validate_params->element_validate_params, | 381 validate_params->element_validate_params, |
379 context); | 382 context); |
380 output->at(i).Set(data_ptr); | 383 output->at(i).Set(data_ptr); |
381 MOJO_INTERNAL_DLOG_SERIALIZATION_WARNING( | 384 MOJO_INTERNAL_DLOG_SERIALIZATION_WARNING( |
382 !validate_params->element_is_nullable && !data_ptr, | 385 !validate_params->element_is_nullable && !data_ptr, |
383 VALIDATION_ERROR_UNEXPECTED_NULL_POINTER, | 386 VALIDATION_ERROR_UNEXPECTED_NULL_POINTER, |
384 MakeMessageWithArrayIndex("null in array expecting valid pointers", | 387 MakeMessageWithArrayIndex("null in array expecting valid pointers", |
385 size, i)); | 388 size, i)); |
386 } | 389 } |
387 } | 390 } |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
448 typename Traits::Element>::value, | 451 typename Traits::Element>::value, |
449 "Incorrect array serializer"); | 452 "Incorrect array serializer"); |
450 | 453 |
451 static size_t GetSerializedSize(UserTypeIterator* input, | 454 static size_t GetSerializedSize(UserTypeIterator* input, |
452 SerializationContext* context) { | 455 SerializationContext* context) { |
453 size_t element_count = input->GetSize(); | 456 size_t element_count = input->GetSize(); |
454 size_t size = sizeof(Data); | 457 size_t size = sizeof(Data); |
455 for (size_t i = 0; i < element_count; ++i) { | 458 for (size_t i = 0; i < element_count; ++i) { |
456 // Call with |inlined| set to false, so that it will account for both the | 459 // 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. | 460 // data in the union and the space in the array used to hold the union. |
458 size += PrepareToSerialize<Element>(input->GetNext(), false, context); | 461 size += PrepareToSerialize<Element>(input->GetNext(), false, context); |
yzshen1
2016/07/28 18:25:19
Please also do
typename UserTypeIterator::GetNextR
Fady Samuel
2016/07/28 19:53:31
Done.
| |
459 } | 462 } |
460 return size; | 463 return size; |
461 } | 464 } |
462 | 465 |
463 static void SerializeElements(UserTypeIterator* input, | 466 static void SerializeElements(UserTypeIterator* input, |
464 Buffer* buf, | 467 Buffer* buf, |
465 Data* output, | 468 Data* output, |
466 const ContainerValidateParams* validate_params, | 469 const ContainerValidateParams* validate_params, |
467 SerializationContext* context) { | 470 SerializationContext* context) { |
468 size_t size = input->GetSize(); | 471 size_t size = input->GetSize(); |
469 for (size_t i = 0; i < size; ++i) { | 472 for (size_t i = 0; i < size; ++i) { |
470 typename Data::Element* result = output->storage() + i; | 473 typename Data::Element* result = output->storage() + i; |
471 Serialize<Element>(input->GetNext(), buf, &result, true, context); | 474 Serialize<Element>(input->GetNext(), buf, &result, true, context); |
yzshen1
2016/07/28 18:25:19
typename UserTypeIterator::GetNextResult next = in
Fady Samuel
2016/07/28 19:53:32
Done.
| |
472 MOJO_INTERNAL_DLOG_SERIALIZATION_WARNING( | 475 MOJO_INTERNAL_DLOG_SERIALIZATION_WARNING( |
473 !validate_params->element_is_nullable && output->at(i).is_null(), | 476 !validate_params->element_is_nullable && output->at(i).is_null(), |
474 VALIDATION_ERROR_UNEXPECTED_NULL_POINTER, | 477 VALIDATION_ERROR_UNEXPECTED_NULL_POINTER, |
475 MakeMessageWithArrayIndex("null in array expecting valid unions", | 478 MakeMessageWithArrayIndex("null in array expecting valid unions", |
476 size, i)); | 479 size, i)); |
477 } | 480 } |
478 } | 481 } |
479 | 482 |
480 static bool DeserializeElements(Data* input, | 483 static bool DeserializeElements(Data* input, |
481 UserType* output, | 484 UserType* output, |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
539 if (!input) | 542 if (!input) |
540 return CallSetToNullIfExists<Traits>(output); | 543 return CallSetToNullIfExists<Traits>(output); |
541 return Impl::DeserializeElements(input, output, context); | 544 return Impl::DeserializeElements(input, output, context); |
542 } | 545 } |
543 }; | 546 }; |
544 | 547 |
545 } // namespace internal | 548 } // namespace internal |
546 } // namespace mojo | 549 } // namespace mojo |
547 | 550 |
548 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_SERIALIZATION_H_ | 551 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_SERIALIZATION_H_ |
OLD | NEW |