| 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 <string.h> // For |memcpy()|. | 8 #include <string.h> // For |memcpy()|. |
| 9 | 9 |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 27 matching lines...) Expand all Loading... |
| 38 inline void SerializeMap_( | 38 inline void SerializeMap_( |
| 39 Map<KeyWrapperType, ValueWrapperType> input, | 39 Map<KeyWrapperType, ValueWrapperType> input, |
| 40 internal::Buffer* buf, | 40 internal::Buffer* buf, |
| 41 internal::Map_Data<KeySerializationType, ValueSerializationType>** output); | 41 internal::Map_Data<KeySerializationType, ValueSerializationType>** output); |
| 42 | 42 |
| 43 template <typename E, typename F> | 43 template <typename E, typename F> |
| 44 inline void Deserialize_(internal::Array_Data<F>* data, Array<E>* output); | 44 inline void Deserialize_(internal::Array_Data<F>* data, Array<E>* output); |
| 45 | 45 |
| 46 namespace internal { | 46 namespace internal { |
| 47 | 47 |
| 48 template <typename E, typename F, bool move_only = IsMoveOnlyType<E>::value> | 48 template <typename E, |
| 49 typename F, |
| 50 bool move_only = IsMoveOnlyType<E>::value, |
| 51 bool is_union = |
| 52 IsUnionDataType<typename RemovePointer<F>::type>::value> |
| 49 struct ArraySerializer; | 53 struct ArraySerializer; |
| 50 | 54 |
| 55 // Handles serialization and deserialization of arrays of pod types. |
| 51 template <typename E, typename F> | 56 template <typename E, typename F> |
| 52 struct ArraySerializer<E, F, false> { | 57 struct ArraySerializer<E, F, false, false> { |
| 53 static_assert(sizeof(E) == sizeof(F), "Incorrect array serializer"); | 58 static_assert(sizeof(E) == sizeof(F), "Incorrect array serializer"); |
| 54 static size_t GetSerializedSize(const Array<E>& input) { | 59 static size_t GetSerializedSize(const Array<E>& input) { |
| 55 return sizeof(Array_Data<F>) + Align(input.size() * sizeof(E)); | 60 return sizeof(Array_Data<F>) + Align(input.size() * sizeof(E)); |
| 56 } | 61 } |
| 57 template <bool element_is_nullable, typename ElementValidateParams> | 62 template <bool element_is_nullable, typename ElementValidateParams> |
| 58 static void SerializeElements(Array<E> input, | 63 static void SerializeElements(Array<E> input, |
| 59 Buffer* buf, | 64 Buffer* buf, |
| 60 Array_Data<F>* output) { | 65 Array_Data<F>* output) { |
| 61 static_assert(!element_is_nullable, | 66 static_assert(!element_is_nullable, |
| 62 "Primitive type should be non-nullable"); | 67 "Primitive type should be non-nullable"); |
| 63 static_assert((IsSame<ElementValidateParams, NoValidateParams>::value), | 68 static_assert((IsSame<ElementValidateParams, NoValidateParams>::value), |
| 64 "Primitive type should not have array validate params"); | 69 "Primitive type should not have array validate params"); |
| 65 | 70 |
| 66 if (input.size()) | 71 if (input.size()) |
| 67 memcpy(output->storage(), &input.storage()[0], input.size() * sizeof(E)); | 72 memcpy(output->storage(), &input.storage()[0], input.size() * sizeof(E)); |
| 68 } | 73 } |
| 69 static void DeserializeElements(Array_Data<F>* input, Array<E>* output) { | 74 static void DeserializeElements(Array_Data<F>* input, Array<E>* output) { |
| 70 std::vector<E> result(input->size()); | 75 std::vector<E> result(input->size()); |
| 71 if (input->size()) | 76 if (input->size()) |
| 72 memcpy(&result[0], input->storage(), input->size() * sizeof(E)); | 77 memcpy(&result[0], input->storage(), input->size() * sizeof(E)); |
| 73 output->Swap(&result); | 78 output->Swap(&result); |
| 74 } | 79 } |
| 75 }; | 80 }; |
| 76 | 81 |
| 82 // Serializes and deserializes arrays of bools. |
| 77 template <> | 83 template <> |
| 78 struct ArraySerializer<bool, bool, false> { | 84 struct ArraySerializer<bool, bool, false, false> { |
| 79 static size_t GetSerializedSize(const Array<bool>& input) { | 85 static size_t GetSerializedSize(const Array<bool>& input) { |
| 80 return sizeof(Array_Data<bool>) + Align((input.size() + 7) / 8); | 86 return sizeof(Array_Data<bool>) + Align((input.size() + 7) / 8); |
| 81 } | 87 } |
| 82 template <bool element_is_nullable, typename ElementValidateParams> | 88 template <bool element_is_nullable, typename ElementValidateParams> |
| 83 static void SerializeElements(Array<bool> input, | 89 static void SerializeElements(Array<bool> input, |
| 84 Buffer* buf, | 90 Buffer* buf, |
| 85 Array_Data<bool>* output) { | 91 Array_Data<bool>* output) { |
| 86 static_assert(!element_is_nullable, | 92 static_assert(!element_is_nullable, |
| 87 "Primitive type should be non-nullable"); | 93 "Primitive type should be non-nullable"); |
| 88 static_assert((IsSame<ElementValidateParams, NoValidateParams>::value), | 94 static_assert((IsSame<ElementValidateParams, NoValidateParams>::value), |
| 89 "Primitive type should not have array validate params"); | 95 "Primitive type should not have array validate params"); |
| 90 | 96 |
| 91 // TODO(darin): Can this be a memcpy somehow instead of a bit-by-bit copy? | 97 // TODO(darin): Can this be a memcpy somehow instead of a bit-by-bit copy? |
| 92 for (size_t i = 0; i < input.size(); ++i) | 98 for (size_t i = 0; i < input.size(); ++i) |
| 93 output->at(i) = input[i]; | 99 output->at(i) = input[i]; |
| 94 } | 100 } |
| 95 static void DeserializeElements(Array_Data<bool>* input, | 101 static void DeserializeElements(Array_Data<bool>* input, |
| 96 Array<bool>* output) { | 102 Array<bool>* output) { |
| 97 Array<bool> result(input->size()); | 103 Array<bool> result(input->size()); |
| 98 // TODO(darin): Can this be a memcpy somehow instead of a bit-by-bit copy? | 104 // TODO(darin): Can this be a memcpy somehow instead of a bit-by-bit copy? |
| 99 for (size_t i = 0; i < input->size(); ++i) | 105 for (size_t i = 0; i < input->size(); ++i) |
| 100 result.at(i) = input->at(i); | 106 result.at(i) = input->at(i); |
| 101 output->Swap(&result); | 107 output->Swap(&result); |
| 102 } | 108 } |
| 103 }; | 109 }; |
| 104 | 110 |
| 111 // Serializes and deserializes arrays of handles. |
| 105 template <typename H> | 112 template <typename H> |
| 106 struct ArraySerializer<ScopedHandleBase<H>, H, true> { | 113 struct ArraySerializer<ScopedHandleBase<H>, H, true, false> { |
| 107 static size_t GetSerializedSize(const Array<ScopedHandleBase<H>>& input) { | 114 static size_t GetSerializedSize(const Array<ScopedHandleBase<H>>& input) { |
| 108 return sizeof(Array_Data<H>) + Align(input.size() * sizeof(H)); | 115 return sizeof(Array_Data<H>) + Align(input.size() * sizeof(H)); |
| 109 } | 116 } |
| 110 template <bool element_is_nullable, typename ElementValidateParams> | 117 template <bool element_is_nullable, typename ElementValidateParams> |
| 111 static void SerializeElements(Array<ScopedHandleBase<H>> input, | 118 static void SerializeElements(Array<ScopedHandleBase<H>> input, |
| 112 Buffer* buf, | 119 Buffer* buf, |
| 113 Array_Data<H>* output) { | 120 Array_Data<H>* output) { |
| 114 static_assert((IsSame<ElementValidateParams, NoValidateParams>::value), | 121 static_assert((IsSame<ElementValidateParams, NoValidateParams>::value), |
| 115 "Handle type should not have array validate params"); | 122 "Handle type should not have array validate params"); |
| 116 | 123 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 134 } | 141 } |
| 135 }; | 142 }; |
| 136 | 143 |
| 137 // This template must only apply to pointer mojo entity (structs and arrays). | 144 // This template must only apply to pointer mojo entity (structs and arrays). |
| 138 // This is done by ensuring that WrapperTraits<S>::DataType is a pointer. | 145 // This is done by ensuring that WrapperTraits<S>::DataType is a pointer. |
| 139 template <typename S> | 146 template <typename S> |
| 140 struct ArraySerializer< | 147 struct ArraySerializer< |
| 141 S, | 148 S, |
| 142 typename EnableIf<IsPointer<typename WrapperTraits<S>::DataType>::value, | 149 typename EnableIf<IsPointer<typename WrapperTraits<S>::DataType>::value, |
| 143 typename WrapperTraits<S>::DataType>::type, | 150 typename WrapperTraits<S>::DataType>::type, |
| 144 true> { | 151 true, |
| 152 false> { |
| 145 typedef | 153 typedef |
| 146 typename RemovePointer<typename WrapperTraits<S>::DataType>::type S_Data; | 154 typename RemovePointer<typename WrapperTraits<S>::DataType>::type S_Data; |
| 147 static size_t GetSerializedSize(const Array<S>& input) { | 155 static size_t GetSerializedSize(const Array<S>& input) { |
| 148 size_t size = sizeof(Array_Data<S_Data*>) + | 156 size_t size = sizeof(Array_Data<S_Data*>) + |
| 149 input.size() * sizeof(StructPointer<S_Data>); | 157 input.size() * sizeof(StructPointer<S_Data>); |
| 150 for (size_t i = 0; i < input.size(); ++i) | 158 for (size_t i = 0; i < input.size(); ++i) |
| 151 size += GetSerializedSize_(input[i]); | 159 size += GetSerializedSize_(input[i]); |
| 152 return size; | 160 return size; |
| 153 } | 161 } |
| 154 template <bool element_is_nullable, typename ElementValidateParams> | 162 template <bool element_is_nullable, typename ElementValidateParams> |
| 155 static void SerializeElements(Array<S> input, | 163 static void SerializeElements(Array<S> input, |
| 156 Buffer* buf, | 164 Buffer* buf, |
| 157 Array_Data<S_Data*>* output) { | 165 Array_Data<S_Data*>* output) { |
| 158 for (size_t i = 0; i < input.size(); ++i) { | 166 for (size_t i = 0; i < input.size(); ++i) { |
| 159 S_Data* element; | 167 S_Data* element; |
| 160 SerializeCaller<S, ElementValidateParams>::Run( | 168 SerializeCaller<S, ElementValidateParams>::Run( |
| 161 input[i].Pass(), buf, &element); | 169 input[i].Pass(), buf, &element); |
| 162 output->at(i) = element; | 170 output->at(i) = element; |
| 163 MOJO_INTERNAL_DLOG_SERIALIZATION_WARNING( | 171 MOJO_INTERNAL_DLOG_SERIALIZATION_WARNING( |
| 164 !element_is_nullable && !element, | 172 !element_is_nullable && !element, |
| 165 VALIDATION_ERROR_UNEXPECTED_NULL_POINTER, | 173 VALIDATION_ERROR_UNEXPECTED_NULL_POINTER, |
| 166 MakeMessageWithArrayIndex( | 174 MakeMessageWithArrayIndex( |
| 167 "null in array expecting valid pointers", input.size(), i)); | 175 "null in array expecting valid pointers", input.size(), i)); |
| 168 } | 176 } |
| 169 } | 177 } |
| 170 static void DeserializeElements(Array_Data<S_Data*>* input, | 178 static void DeserializeElements(Array_Data<S_Data*>* input, |
| 171 Array<S>* output) { | 179 Array<S>* output) { |
| 172 Array<S> result(input->size()); | 180 Array<S> result(input->size()); |
| 173 for (size_t i = 0; i < input->size(); ++i) { | 181 for (size_t i = 0; i < input->size(); ++i) { |
| 174 S element; | 182 Deserialize_(input->at(i), &result[i]); |
| 175 Deserialize_(input->at(i), &element); | |
| 176 result[i] = element.Pass(); | |
| 177 } | 183 } |
| 178 output->Swap(&result); | 184 output->Swap(&result); |
| 179 } | 185 } |
| 180 | 186 |
| 181 private: | 187 private: |
| 182 template <typename T, typename Params> | 188 template <typename T, typename Params> |
| 183 struct SerializeCaller { | 189 struct SerializeCaller { |
| 184 static void Run(T input, | 190 static void Run(T input, |
| 185 Buffer* buf, | 191 Buffer* buf, |
| 186 typename WrapperTraits<T>::DataType* output) { | 192 typename WrapperTraits<T>::DataType* output) { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 203 template <typename T, typename U, typename Params> | 209 template <typename T, typename U, typename Params> |
| 204 struct SerializeCaller<Map<T, U>, Params> { | 210 struct SerializeCaller<Map<T, U>, Params> { |
| 205 static void Run(Map<T, U> input, | 211 static void Run(Map<T, U> input, |
| 206 Buffer* buf, | 212 Buffer* buf, |
| 207 typename Map<T, U>::Data_** output) { | 213 typename Map<T, U>::Data_** output) { |
| 208 SerializeMap_<Params>(input.Pass(), buf, output); | 214 SerializeMap_<Params>(input.Pass(), buf, output); |
| 209 } | 215 } |
| 210 }; | 216 }; |
| 211 }; | 217 }; |
| 212 | 218 |
| 219 // Handles serialization and deserialization of arrays of unions. |
| 220 template <typename U, typename U_Data> |
| 221 struct ArraySerializer<U, U_Data, true, true> { |
| 222 static size_t GetSerializedSize(const Array<U>& input) { |
| 223 size_t size = sizeof(Array_Data<U_Data>); |
| 224 for (size_t i = 0; i < input.size(); ++i) { |
| 225 // GetSerializedSize_ will account for both the data in the union and the |
| 226 // space in the array used to hold the union. |
| 227 size += GetSerializedSize_(input[i], false); |
| 228 } |
| 229 return size; |
| 230 } |
| 231 |
| 232 template <bool element_is_nullable, typename ElementValidateParams> |
| 233 static void SerializeElements(Array<U> input, |
| 234 Buffer* buf, |
| 235 Array_Data<U_Data>* output) { |
| 236 for (size_t i = 0; i < input.size(); ++i) { |
| 237 U_Data* result = output->storage() + i; |
| 238 SerializeUnion_(input[i].Pass(), buf, &result, true); |
| 239 MOJO_INTERNAL_DLOG_SERIALIZATION_WARNING( |
| 240 !element_is_nullable && output->at(i).is_null(), |
| 241 VALIDATION_ERROR_UNEXPECTED_NULL_POINTER, |
| 242 MakeMessageWithArrayIndex("null in array expecting valid unions", |
| 243 input.size(), i)); |
| 244 } |
| 245 } |
| 246 |
| 247 static void DeserializeElements(Array_Data<U_Data>* input, Array<U>* output) { |
| 248 Array<U> result(input->size()); |
| 249 for (size_t i = 0; i < input->size(); ++i) { |
| 250 Deserialize_(&input->at(i), &result[i]); |
| 251 } |
| 252 output->Swap(&result); |
| 253 } |
| 254 }; |
| 255 |
| 256 // Handles serialization and deserialization of arrays of strings. |
| 213 template <> | 257 template <> |
| 214 struct ArraySerializer<String, String_Data*, false> { | 258 struct ArraySerializer<String, String_Data*, false> { |
| 215 static size_t GetSerializedSize(const Array<String>& input) { | 259 static size_t GetSerializedSize(const Array<String>& input) { |
| 216 size_t size = | 260 size_t size = |
| 217 sizeof(Array_Data<String_Data*>) + input.size() * sizeof(StringPointer); | 261 sizeof(Array_Data<String_Data*>) + input.size() * sizeof(StringPointer); |
| 218 for (size_t i = 0; i < input.size(); ++i) | 262 for (size_t i = 0; i < input.size(); ++i) |
| 219 size += GetSerializedSize_(input[i]); | 263 size += GetSerializedSize_(input[i]); |
| 220 return size; | 264 return size; |
| 221 } | 265 } |
| 222 template <bool element_is_nullable, typename ElementValidateParams> | 266 template <bool element_is_nullable, typename ElementValidateParams> |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 291 if (input) { | 335 if (input) { |
| 292 internal::ArraySerializer<E, F>::DeserializeElements(input, output); | 336 internal::ArraySerializer<E, F>::DeserializeElements(input, output); |
| 293 } else { | 337 } else { |
| 294 output->reset(); | 338 output->reset(); |
| 295 } | 339 } |
| 296 } | 340 } |
| 297 | 341 |
| 298 } // namespace mojo | 342 } // namespace mojo |
| 299 | 343 |
| 300 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_SERIALIZATION_H_ | 344 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_SERIALIZATION_H_ |
| OLD | NEW |