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 a90570dd285ee1e184159c3073767ad5bf69df29..827cb3f11207548977a27ca56bb6916038054d41 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 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)); |
} |
@@ -141,7 +148,8 @@ struct ArraySerializer< |
S, |
typename EnableIf<IsPointer<typename WrapperTraits<S>::DataType>::value, |
typename WrapperTraits<S>::DataType>::type, |
- true> { |
+ true, |
+ false> { |
typedef |
typename RemovePointer<typename WrapperTraits<S>::DataType>::type S_Data; |
static size_t GetSerializedSize(const Array<S>& input) { |
@@ -171,9 +179,7 @@ struct ArraySerializer< |
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); |
} |
@@ -210,6 +216,44 @@ struct ArraySerializer< |
}; |
}; |
+// 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) { |
+ // 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], false); |
+ } |
+ 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) { |