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