| 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 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 106 "Incorrect array serializer"); | 106 "Incorrect array serializer"); |
| 107 | 107 |
| 108 static size_t GetSerializedSize(UserTypeReader* input, | 108 static size_t GetSerializedSize(UserTypeReader* input, |
| 109 SerializationContext* context) { | 109 SerializationContext* context) { |
| 110 return sizeof(Data) + Align(input->GetSize() * sizeof(DataElement)); | 110 return sizeof(Data) + Align(input->GetSize() * sizeof(DataElement)); |
| 111 } | 111 } |
| 112 | 112 |
| 113 static void SerializeElements(UserTypeReader* input, | 113 static void SerializeElements(UserTypeReader* input, |
| 114 Buffer* buf, | 114 Buffer* buf, |
| 115 Data* output, | 115 Data* output, |
| 116 const ArrayValidateParams* validate_params, | 116 const ContainerValidateParams* validate_params, |
| 117 SerializationContext* context) { | 117 SerializationContext* context) { |
| 118 DCHECK(!validate_params->element_is_nullable) | 118 DCHECK(!validate_params->element_is_nullable) |
| 119 << "Primitive type should be non-nullable"; | 119 << "Primitive type should be non-nullable"; |
| 120 DCHECK(!validate_params->element_validate_params) | 120 DCHECK(!validate_params->element_validate_params) |
| 121 << "Primitive type should not have array validate params"; | 121 << "Primitive type should not have array validate params"; |
| 122 | 122 |
| 123 size_t size = input->GetSize(); | 123 size_t size = input->GetSize(); |
| 124 if (size == 0) | 124 if (size == 0) |
| 125 return; | 125 return; |
| 126 | 126 |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 166 "Incorrect array serializer"); | 166 "Incorrect array serializer"); |
| 167 | 167 |
| 168 static size_t GetSerializedSize(UserTypeReader* input, | 168 static size_t GetSerializedSize(UserTypeReader* input, |
| 169 SerializationContext* context) { | 169 SerializationContext* context) { |
| 170 return sizeof(Data) + Align((input->GetSize() + 7) / 8); | 170 return sizeof(Data) + Align((input->GetSize() + 7) / 8); |
| 171 } | 171 } |
| 172 | 172 |
| 173 static void SerializeElements(UserTypeReader* input, | 173 static void SerializeElements(UserTypeReader* input, |
| 174 Buffer* buf, | 174 Buffer* buf, |
| 175 Data* output, | 175 Data* output, |
| 176 const ArrayValidateParams* validate_params, | 176 const ContainerValidateParams* validate_params, |
| 177 SerializationContext* context) { | 177 SerializationContext* context) { |
| 178 DCHECK(!validate_params->element_is_nullable) | 178 DCHECK(!validate_params->element_is_nullable) |
| 179 << "Primitive type should be non-nullable"; | 179 << "Primitive type should be non-nullable"; |
| 180 DCHECK(!validate_params->element_validate_params) | 180 DCHECK(!validate_params->element_validate_params) |
| 181 << "Primitive type should not have array validate params"; | 181 << "Primitive type should not have array validate params"; |
| 182 | 182 |
| 183 size_t size = input->GetSize(); | 183 size_t size = input->GetSize(); |
| 184 for (size_t i = 0; i < size; ++i) | 184 for (size_t i = 0; i < size; ++i) |
| 185 output->at(i) = input->GetNext(); | 185 output->at(i) = input->GetNext(); |
| 186 } | 186 } |
| (...skipping 25 matching lines...) Expand all Loading... |
| 212 | 212 |
| 213 static size_t GetSerializedSize(UserTypeReader* input, | 213 static size_t GetSerializedSize(UserTypeReader* input, |
| 214 SerializationContext* context) { | 214 SerializationContext* context) { |
| 215 return sizeof(Data) + | 215 return sizeof(Data) + |
| 216 Align(input->GetSize() * sizeof(typename Data::Element)); | 216 Align(input->GetSize() * sizeof(typename Data::Element)); |
| 217 } | 217 } |
| 218 | 218 |
| 219 static void SerializeElements(UserTypeReader* input, | 219 static void SerializeElements(UserTypeReader* input, |
| 220 Buffer* buf, | 220 Buffer* buf, |
| 221 Data* output, | 221 Data* output, |
| 222 const ArrayValidateParams* validate_params, | 222 const ContainerValidateParams* validate_params, |
| 223 SerializationContext* context) { | 223 SerializationContext* context) { |
| 224 DCHECK(!validate_params->element_validate_params) | 224 DCHECK(!validate_params->element_validate_params) |
| 225 << "Handle type should not have array validate params"; | 225 << "Handle type should not have array validate params"; |
| 226 | 226 |
| 227 size_t size = input->GetSize(); | 227 size_t size = input->GetSize(); |
| 228 for (size_t i = 0; i < size; ++i) { | 228 for (size_t i = 0; i < size; ++i) { |
| 229 // Transfer ownership of the handle. | 229 // Transfer ownership of the handle. |
| 230 output->at(i) = context->handles.AddHandle(input->GetNext().release()); | 230 output->at(i) = context->handles.AddHandle(input->GetNext().release()); |
| 231 MOJO_INTERNAL_DLOG_SERIALIZATION_WARNING( | 231 MOJO_INTERNAL_DLOG_SERIALIZATION_WARNING( |
| 232 !validate_params->element_is_nullable && !output->at(i).is_valid(), | 232 !validate_params->element_is_nullable && !output->at(i).is_valid(), |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 271 element_count * | 271 element_count * |
| 272 sizeof(Pointer<typename std::remove_pointer<DataElement>::type>); | 272 sizeof(Pointer<typename std::remove_pointer<DataElement>::type>); |
| 273 for (size_t i = 0; i < element_count; ++i) | 273 for (size_t i = 0; i < element_count; ++i) |
| 274 size += PrepareToSerialize<Element>(input->GetNext(), context); | 274 size += PrepareToSerialize<Element>(input->GetNext(), context); |
| 275 return size; | 275 return size; |
| 276 } | 276 } |
| 277 | 277 |
| 278 static void SerializeElements(UserTypeReader* input, | 278 static void SerializeElements(UserTypeReader* input, |
| 279 Buffer* buf, | 279 Buffer* buf, |
| 280 Data* output, | 280 Data* output, |
| 281 const ArrayValidateParams* validate_params, | 281 const ContainerValidateParams* validate_params, |
| 282 SerializationContext* context) { | 282 SerializationContext* context) { |
| 283 size_t size = input->GetSize(); | 283 size_t size = input->GetSize(); |
| 284 for (size_t i = 0; i < size; ++i) { | 284 for (size_t i = 0; i < size; ++i) { |
| 285 DataElement element; | 285 DataElement element; |
| 286 SerializeCaller<Element>::Run(input->GetNext(), buf, &element, | 286 SerializeCaller<Element>::Run(input->GetNext(), buf, &element, |
| 287 validate_params->element_validate_params, | 287 validate_params->element_validate_params, |
| 288 context); | 288 context); |
| 289 output->at(i) = element; | 289 output->at(i) = element; |
| 290 MOJO_INTERNAL_DLOG_SERIALIZATION_WARNING( | 290 MOJO_INTERNAL_DLOG_SERIALIZATION_WARNING( |
| 291 !validate_params->element_is_nullable && !element, | 291 !validate_params->element_is_nullable && !element, |
| (...skipping 21 matching lines...) Expand all Loading... |
| 313 | 313 |
| 314 private: | 314 private: |
| 315 template <typename T, | 315 template <typename T, |
| 316 bool is_array_or_map = IsSpecializationOf<Array, T>::value || | 316 bool is_array_or_map = IsSpecializationOf<Array, T>::value || |
| 317 IsSpecializationOf<Map, T>::value> | 317 IsSpecializationOf<Map, T>::value> |
| 318 struct SerializeCaller { | 318 struct SerializeCaller { |
| 319 template <typename InputElementType> | 319 template <typename InputElementType> |
| 320 static void Run(InputElementType&& input, | 320 static void Run(InputElementType&& input, |
| 321 Buffer* buf, | 321 Buffer* buf, |
| 322 DataElement* output, | 322 DataElement* output, |
| 323 const ArrayValidateParams* validate_params, | 323 const ContainerValidateParams* validate_params, |
| 324 SerializationContext* context) { | 324 SerializationContext* context) { |
| 325 Serialize<T>(std::forward<InputElementType>(input), buf, output, context); | 325 Serialize<T>(std::forward<InputElementType>(input), buf, output, context); |
| 326 } | 326 } |
| 327 }; | 327 }; |
| 328 | 328 |
| 329 template <typename T> | 329 template <typename T> |
| 330 struct SerializeCaller<T, true> { | 330 struct SerializeCaller<T, true> { |
| 331 template <typename InputElementType> | 331 template <typename InputElementType> |
| 332 static void Run(InputElementType&& input, | 332 static void Run(InputElementType&& input, |
| 333 Buffer* buf, | 333 Buffer* buf, |
| 334 DataElement* output, | 334 DataElement* output, |
| 335 const ArrayValidateParams* validate_params, | 335 const ContainerValidateParams* validate_params, |
| 336 SerializationContext* context) { | 336 SerializationContext* context) { |
| 337 Serialize<T>(std::forward<InputElementType>(input), buf, output, | 337 Serialize<T>(std::forward<InputElementType>(input), buf, output, |
| 338 validate_params, context); | 338 validate_params, context); |
| 339 } | 339 } |
| 340 }; | 340 }; |
| 341 }; | 341 }; |
| 342 | 342 |
| 343 // Handles serialization and deserialization of arrays of unions. | 343 // Handles serialization and deserialization of arrays of unions. |
| 344 template <typename MojomType, | 344 template <typename MojomType, |
| 345 typename MaybeConstUserType, | 345 typename MaybeConstUserType, |
| (...skipping 19 matching lines...) Expand all Loading... |
| 365 // Call with |inlined| set to false, so that it will account for both the | 365 // Call with |inlined| set to false, so that it will account for both the |
| 366 // data in the union and the space in the array used to hold the union. | 366 // data in the union and the space in the array used to hold the union. |
| 367 size += PrepareToSerialize<Element>(input->GetNext(), false, context); | 367 size += PrepareToSerialize<Element>(input->GetNext(), false, context); |
| 368 } | 368 } |
| 369 return size; | 369 return size; |
| 370 } | 370 } |
| 371 | 371 |
| 372 static void SerializeElements(UserTypeReader* input, | 372 static void SerializeElements(UserTypeReader* input, |
| 373 Buffer* buf, | 373 Buffer* buf, |
| 374 Data* output, | 374 Data* output, |
| 375 const ArrayValidateParams* validate_params, | 375 const ContainerValidateParams* validate_params, |
| 376 SerializationContext* context) { | 376 SerializationContext* context) { |
| 377 size_t size = input->GetSize(); | 377 size_t size = input->GetSize(); |
| 378 for (size_t i = 0; i < size; ++i) { | 378 for (size_t i = 0; i < size; ++i) { |
| 379 typename Data::Element* result = output->storage() + i; | 379 typename Data::Element* result = output->storage() + i; |
| 380 Serialize<Element>(input->GetNext(), buf, &result, true, context); | 380 Serialize<Element>(input->GetNext(), buf, &result, true, context); |
| 381 MOJO_INTERNAL_DLOG_SERIALIZATION_WARNING( | 381 MOJO_INTERNAL_DLOG_SERIALIZATION_WARNING( |
| 382 !validate_params->element_is_nullable && output->at(i).is_null(), | 382 !validate_params->element_is_nullable && output->at(i).is_null(), |
| 383 VALIDATION_ERROR_UNEXPECTED_NULL_POINTER, | 383 VALIDATION_ERROR_UNEXPECTED_NULL_POINTER, |
| 384 MakeMessageWithArrayIndex("null in array expecting valid unions", | 384 MakeMessageWithArrayIndex("null in array expecting valid unions", |
| 385 size, i)); | 385 size, i)); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 417 SerializationContext* context) { | 417 SerializationContext* context) { |
| 418 if (CallIsNullIfExists<Traits>(input)) | 418 if (CallIsNullIfExists<Traits>(input)) |
| 419 return 0; | 419 return 0; |
| 420 ArrayReader<MaybeConstUserType> reader(input); | 420 ArrayReader<MaybeConstUserType> reader(input); |
| 421 return Impl::GetSerializedSize(&reader, context); | 421 return Impl::GetSerializedSize(&reader, context); |
| 422 } | 422 } |
| 423 | 423 |
| 424 static void Serialize(MaybeConstUserType& input, | 424 static void Serialize(MaybeConstUserType& input, |
| 425 Buffer* buf, | 425 Buffer* buf, |
| 426 Data** output, | 426 Data** output, |
| 427 const ArrayValidateParams* validate_params, | 427 const ContainerValidateParams* validate_params, |
| 428 SerializationContext* context) { | 428 SerializationContext* context) { |
| 429 if (!CallIsNullIfExists<Traits>(input)) { | 429 if (!CallIsNullIfExists<Traits>(input)) { |
| 430 MOJO_INTERNAL_DLOG_SERIALIZATION_WARNING( | 430 MOJO_INTERNAL_DLOG_SERIALIZATION_WARNING( |
| 431 validate_params->expected_num_elements != 0 && | 431 validate_params->expected_num_elements != 0 && |
| 432 Traits::GetSize(input) != validate_params->expected_num_elements, | 432 Traits::GetSize(input) != validate_params->expected_num_elements, |
| 433 internal::VALIDATION_ERROR_UNEXPECTED_ARRAY_HEADER, | 433 internal::VALIDATION_ERROR_UNEXPECTED_ARRAY_HEADER, |
| 434 internal::MakeMessageWithExpectedArraySize( | 434 internal::MakeMessageWithExpectedArraySize( |
| 435 "fixed-size array has wrong number of elements", | 435 "fixed-size array has wrong number of elements", |
| 436 Traits::GetSize(input), validate_params->expected_num_elements)); | 436 Traits::GetSize(input), validate_params->expected_num_elements)); |
| 437 Data* result = Data::New(Traits::GetSize(input), buf); | 437 Data* result = Data::New(Traits::GetSize(input), buf); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 451 if (!input) | 451 if (!input) |
| 452 return CallSetToNullIfExists<Traits>(output); | 452 return CallSetToNullIfExists<Traits>(output); |
| 453 return Impl::DeserializeElements(input, output, context); | 453 return Impl::DeserializeElements(input, output, context); |
| 454 } | 454 } |
| 455 }; | 455 }; |
| 456 | 456 |
| 457 } // namespace internal | 457 } // namespace internal |
| 458 } // namespace mojo | 458 } // namespace mojo |
| 459 | 459 |
| 460 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_SERIALIZATION_H_ | 460 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_SERIALIZATION_H_ |
| OLD | NEW |