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..6aafbb5dfb1fc1fad16ac3148460c654e3f7aafa 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> |
| 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) { |
| @@ -211,6 +219,44 @@ 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>); |
|
viettrungluu
2015/03/04 20:58:13
I don't understand why this doesn't account for th
azani
2015/03/05 23:59:25
See lower.
|
| + for (size_t i = 0; i < input.size(); ++i) { |
|
viettrungluu
2015/03/04 20:58:13
nit: Other one-line for statements in this file do
azani
2015/03/05 23:59:25
Done.
|
| + size += GetSerializedSize_(input[i]); |
|
viettrungluu
2015/03/04 20:58:13
(I suppose might include the inline part, but that
azani
2015/03/05 23:59:25
It does. I've added a comment to clarify.
|
| + } |
| + 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) { |
| + U element; |
| + Deserialize_(&input->at(i), &element); |
|
viettrungluu
2015/03/04 20:58:13
Why don't you just do &result[i], instead of going
azani
2015/03/05 23:59:25
Done.
|
| + result[i] = element.Pass(); |
| + } |
| + 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) { |