Chromium Code Reviews| Index: mojo/public/cpp/bindings/lib/array_serialization.h |
| diff --git a/mojo/public/cpp/bindings/lib/array_serialization.h b/mojo/public/cpp/bindings/lib/array_serialization.h |
| index eacf298ec80ea46fffebe0afa499a32f83325316..065063b78799bd2f14062f3bcb102bab4348eed2 100644 |
| --- a/mojo/public/cpp/bindings/lib/array_serialization.h |
| +++ b/mojo/public/cpp/bindings/lib/array_serialization.h |
| @@ -45,11 +45,16 @@ inline void Deserialize_(internal::Array_Data<F>* data, Array<E>* output); |
| namespace internal { |
| -template <typename E, typename F, bool move_only = IsMoveOnlyType<E>::value> |
| +template <typename E, |
| + typename F, |
| + bool move_only = IsMoveOnlyType<E>::value, |
| + bool is_union = |
| + IsUnionDataType<typename internal::RemovePointer<F>::type>::value> |
|
yzshen1
2015/03/26 07:30:14
nit: internal:: is not needed, is it?
azani
2015/03/26 22:27:39
Done.
|
| struct ArraySerializer; |
| +// Handles serialization and deserialization of arrays of pod types. |
| template <typename E, typename F> |
| -struct ArraySerializer<E, F, false> { |
| +struct ArraySerializer<E, F, false, false> { |
| static_assert(sizeof(E) == sizeof(F), "Incorrect array serializer"); |
| static size_t GetSerializedSize(const Array<E>& input) { |
| return sizeof(Array_Data<F>) + Align(input.size() * sizeof(E)); |
| @@ -74,8 +79,9 @@ struct ArraySerializer<E, F, false> { |
| } |
| }; |
| +// Serializes and deserializes arrays of bools. |
| template <> |
| -struct ArraySerializer<bool, bool, false> { |
| +struct ArraySerializer<bool, bool, false, false> { |
| static size_t GetSerializedSize(const Array<bool>& input) { |
| return sizeof(Array_Data<bool>) + Align((input.size() + 7) / 8); |
| } |
| @@ -102,8 +108,9 @@ struct ArraySerializer<bool, bool, false> { |
| } |
| }; |
| +// Serializes and deserializes arrays of handles. |
| template <typename H> |
| -struct ArraySerializer<ScopedHandleBase<H>, H, true> { |
| +struct ArraySerializer<ScopedHandleBase<H>, H, true, false> { |
| static size_t GetSerializedSize(const Array<ScopedHandleBase<H>>& input) { |
| return sizeof(Array_Data<H>) + Align(input.size() * sizeof(H)); |
| } |
| @@ -142,7 +149,8 @@ struct ArraySerializer<S, |
| internal::IsPointer<typename internal::WrapperTraits< |
| S>::DataType>::value, |
| typename internal::WrapperTraits<S>::DataType>::type, |
| - true> { |
| + true, |
| + false> { |
| typedef typename internal::RemovePointer< |
| typename internal::WrapperTraits<S>::DataType>::type S_Data; |
| static size_t GetSerializedSize(const Array<S>& input) { |
| @@ -172,9 +180,7 @@ struct ArraySerializer<S, |
| Array<S>* output) { |
| Array<S> result(input->size()); |
| for (size_t i = 0; i < input->size(); ++i) { |
| - S element; |
| - Deserialize_(input->at(i), &element); |
| - result[i] = element.Pass(); |
| + Deserialize_(input->at(i), &result[i]); |
| } |
| output->Swap(&result); |
| } |
| @@ -211,6 +217,43 @@ struct ArraySerializer<S, |
| }; |
| }; |
| +// Handles serialization and deserialization of arrays of unions. |
| +template <typename U, typename U_Data> |
| +struct ArraySerializer<U, U_Data, true, true> { |
| + static size_t GetSerializedSize(const Array<U>& input) { |
| + size_t size = sizeof(Array_Data<U_Data>); |
| + for (size_t i = 0; i < input.size(); ++i) |
|
yzshen1
2015/03/26 07:30:14
Please add {}. The body of the for-statement are m
azani
2015/03/26 22:27:39
Done.
|
| + // GetSerializedSize_ will account for both the data in the union and the |
| + // space in the array used to hold the union. |
| + size += GetSerializedSize_(input[i]); |
| + return size; |
| + } |
| + |
| + template <bool element_is_nullable, typename ElementValidateParams> |
| + static void SerializeElements(Array<U> input, |
| + Buffer* buf, |
| + Array_Data<U_Data>* output) { |
| + for (size_t i = 0; i < input.size(); ++i) { |
| + U_Data* result = output->storage() + i; |
| + SerializeUnion_(input[i].Pass(), buf, &result, true); |
| + MOJO_INTERNAL_DLOG_SERIALIZATION_WARNING( |
| + !element_is_nullable && output->at(i).is_null(), |
| + VALIDATION_ERROR_UNEXPECTED_NULL_POINTER, |
| + MakeMessageWithArrayIndex("null in array expecting valid unions", |
| + input.size(), i)); |
| + } |
| + } |
| + |
| + static void DeserializeElements(Array_Data<U_Data>* input, Array<U>* output) { |
| + Array<U> result(input->size()); |
| + for (size_t i = 0; i < input->size(); ++i) { |
| + Deserialize_(&input->at(i), &result[i]); |
| + } |
| + output->Swap(&result); |
| + } |
| +}; |
| + |
| +// Handles serialization and deserialization of arrays of strings. |
| template <> |
| struct ArraySerializer<String, String_Data*, false> { |
| static size_t GetSerializedSize(const Array<String>& input) { |