| 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 11 matching lines...) Expand all Loading... |
| 22 inline size_t GetSerializedSize_(const Array<E>& input); | 22 inline size_t GetSerializedSize_(const Array<E>& input); |
| 23 | 23 |
| 24 template <typename E, typename F> | 24 template <typename E, typename F> |
| 25 inline void SerializeArray_( | 25 inline void SerializeArray_( |
| 26 Array<E> input, | 26 Array<E> input, |
| 27 internal::Buffer* buf, | 27 internal::Buffer* buf, |
| 28 internal::Array_Data<F>** output, | 28 internal::Array_Data<F>** output, |
| 29 const internal::ArrayValidateParams* validate_params); | 29 const internal::ArrayValidateParams* validate_params); |
| 30 | 30 |
| 31 template <typename E, typename F> | 31 template <typename E, typename F> |
| 32 inline void Deserialize_(internal::Array_Data<F>* data, Array<E>* output); | 32 inline void Deserialize_(internal::Array_Data<F>* data, |
| 33 Array<E>* output, |
| 34 internal::SerializationContext* context); |
| 33 | 35 |
| 34 namespace internal { | 36 namespace internal { |
| 35 | 37 |
| 36 template <typename E, | 38 template <typename E, |
| 37 typename F, | 39 typename F, |
| 38 bool is_union = | 40 bool is_union = |
| 39 IsUnionDataType<typename RemovePointer<F>::type>::value> | 41 IsUnionDataType<typename RemovePointer<F>::type>::value> |
| 40 struct ArraySerializer; | 42 struct ArraySerializer; |
| 41 | 43 |
| 42 // Handles serialization and deserialization of arrays of pod types. | 44 // Handles serialization and deserialization of arrays of pod types. |
| 43 template <typename E, typename F> | 45 template <typename E, typename F> |
| 44 struct ArraySerializer<E, F, false> { | 46 struct ArraySerializer<E, F, false> { |
| 45 static_assert(sizeof(E) == sizeof(F), "Incorrect array serializer"); | 47 static_assert(sizeof(E) == sizeof(F), "Incorrect array serializer"); |
| 46 static size_t GetSerializedSize(const Array<E>& input) { | 48 static size_t GetSerializedSize(const Array<E>& input) { |
| 47 return sizeof(Array_Data<F>) + Align(input.size() * sizeof(E)); | 49 return sizeof(Array_Data<F>) + Align(input.size() * sizeof(E)); |
| 48 } | 50 } |
| 49 | 51 |
| 50 static void SerializeElements(Array<E> input, | 52 static void SerializeElements(Array<E> input, |
| 51 Buffer* buf, | 53 Buffer* buf, |
| 52 Array_Data<F>* output, | 54 Array_Data<F>* output, |
| 53 const ArrayValidateParams* validate_params) { | 55 const ArrayValidateParams* validate_params) { |
| 54 MOJO_DCHECK(!validate_params->element_is_nullable) | 56 MOJO_DCHECK(!validate_params->element_is_nullable) |
| 55 << "Primitive type should be non-nullable"; | 57 << "Primitive type should be non-nullable"; |
| 56 MOJO_DCHECK(!validate_params->element_validate_params) | 58 MOJO_DCHECK(!validate_params->element_validate_params) |
| 57 << "Primitive type should not have array validate params"; | 59 << "Primitive type should not have array validate params"; |
| 58 | 60 |
| 59 if (input.size()) | 61 if (input.size()) |
| 60 memcpy(output->storage(), &input.storage()[0], input.size() * sizeof(E)); | 62 memcpy(output->storage(), &input.storage()[0], input.size() * sizeof(E)); |
| 61 } | 63 } |
| 62 static void DeserializeElements(Array_Data<F>* input, Array<E>* output) { | 64 static void DeserializeElements(Array_Data<F>* input, |
| 65 Array<E>* output, |
| 66 SerializationContext* context) { |
| 63 std::vector<E> result(input->size()); | 67 std::vector<E> result(input->size()); |
| 64 if (input->size()) | 68 if (input->size()) |
| 65 memcpy(&result[0], input->storage(), input->size() * sizeof(E)); | 69 memcpy(&result[0], input->storage(), input->size() * sizeof(E)); |
| 66 output->Swap(&result); | 70 output->Swap(&result); |
| 67 } | 71 } |
| 68 }; | 72 }; |
| 69 | 73 |
| 70 // Serializes and deserializes arrays of bools. | 74 // Serializes and deserializes arrays of bools. |
| 71 template <> | 75 template <> |
| 72 struct ArraySerializer<bool, bool, false> { | 76 struct ArraySerializer<bool, bool, false> { |
| 73 static size_t GetSerializedSize(const Array<bool>& input) { | 77 static size_t GetSerializedSize(const Array<bool>& input) { |
| 74 return sizeof(Array_Data<bool>) + Align((input.size() + 7) / 8); | 78 return sizeof(Array_Data<bool>) + Align((input.size() + 7) / 8); |
| 75 } | 79 } |
| 76 | 80 |
| 77 static void SerializeElements(Array<bool> input, | 81 static void SerializeElements(Array<bool> input, |
| 78 Buffer* buf, | 82 Buffer* buf, |
| 79 Array_Data<bool>* output, | 83 Array_Data<bool>* output, |
| 80 const ArrayValidateParams* validate_params) { | 84 const ArrayValidateParams* validate_params) { |
| 81 MOJO_DCHECK(!validate_params->element_is_nullable) | 85 MOJO_DCHECK(!validate_params->element_is_nullable) |
| 82 << "Primitive type should be non-nullable"; | 86 << "Primitive type should be non-nullable"; |
| 83 MOJO_DCHECK(!validate_params->element_validate_params) | 87 MOJO_DCHECK(!validate_params->element_validate_params) |
| 84 << "Primitive type should not have array validate params"; | 88 << "Primitive type should not have array validate params"; |
| 85 | 89 |
| 86 // TODO(darin): Can this be a memcpy somehow instead of a bit-by-bit copy? | 90 // TODO(darin): Can this be a memcpy somehow instead of a bit-by-bit copy? |
| 87 for (size_t i = 0; i < input.size(); ++i) | 91 for (size_t i = 0; i < input.size(); ++i) |
| 88 output->at(i) = input[i]; | 92 output->at(i) = input[i]; |
| 89 } | 93 } |
| 90 static void DeserializeElements(Array_Data<bool>* input, | 94 static void DeserializeElements(Array_Data<bool>* input, |
| 91 Array<bool>* output) { | 95 Array<bool>* output, |
| 96 SerializationContext* context) { |
| 92 Array<bool> result(input->size()); | 97 Array<bool> result(input->size()); |
| 93 // TODO(darin): Can this be a memcpy somehow instead of a bit-by-bit copy? | 98 // TODO(darin): Can this be a memcpy somehow instead of a bit-by-bit copy? |
| 94 for (size_t i = 0; i < input->size(); ++i) | 99 for (size_t i = 0; i < input->size(); ++i) |
| 95 result.at(i) = input->at(i); | 100 result.at(i) = input->at(i); |
| 96 output->Swap(&result); | 101 output->Swap(&result); |
| 97 } | 102 } |
| 98 }; | 103 }; |
| 99 | 104 |
| 100 // Serializes and deserializes arrays of handles. | 105 // Serializes and deserializes arrays of handles. |
| 101 template <typename H> | 106 template <typename H> |
| (...skipping 13 matching lines...) Expand all Loading... |
| 115 output->at(i) = input[i].release(); // Transfer ownership of the handle. | 120 output->at(i) = input[i].release(); // Transfer ownership of the handle. |
| 116 MOJO_INTERNAL_DLOG_SERIALIZATION_WARNING( | 121 MOJO_INTERNAL_DLOG_SERIALIZATION_WARNING( |
| 117 !validate_params->element_is_nullable && !output->at(i).is_valid(), | 122 !validate_params->element_is_nullable && !output->at(i).is_valid(), |
| 118 VALIDATION_ERROR_UNEXPECTED_INVALID_HANDLE, | 123 VALIDATION_ERROR_UNEXPECTED_INVALID_HANDLE, |
| 119 MakeMessageWithArrayIndex( | 124 MakeMessageWithArrayIndex( |
| 120 "invalid handle in array expecting valid handles", input.size(), | 125 "invalid handle in array expecting valid handles", input.size(), |
| 121 i)); | 126 i)); |
| 122 } | 127 } |
| 123 } | 128 } |
| 124 static void DeserializeElements(Array_Data<H>* input, | 129 static void DeserializeElements(Array_Data<H>* input, |
| 125 Array<ScopedHandleBase<H>>* output) { | 130 Array<ScopedHandleBase<H>>* output, |
| 131 SerializationContext* context) { |
| 126 Array<ScopedHandleBase<H>> result(input->size()); | 132 Array<ScopedHandleBase<H>> result(input->size()); |
| 127 for (size_t i = 0; i < input->size(); ++i) | 133 for (size_t i = 0; i < input->size(); ++i) |
| 128 result.at(i) = MakeScopedHandle(FetchAndReset(&input->at(i))); | 134 result.at(i) = MakeScopedHandle(FetchAndReset(&input->at(i))); |
| 129 output->Swap(&result); | 135 output->Swap(&result); |
| 130 } | 136 } |
| 131 }; | 137 }; |
| 132 | 138 |
| 133 // This template must only apply to pointer mojo entity (structs and arrays). | 139 // This template must only apply to pointer mojo entity (structs and arrays). |
| 134 // This is done by ensuring that WrapperTraits<S>::DataType is a pointer. | 140 // This is done by ensuring that WrapperTraits<S>::DataType is a pointer. |
| 135 template <typename S> | 141 template <typename S> |
| (...skipping 22 matching lines...) Expand all Loading... |
| 158 validate_params->element_validate_params); | 164 validate_params->element_validate_params); |
| 159 output->at(i) = element; | 165 output->at(i) = element; |
| 160 MOJO_INTERNAL_DLOG_SERIALIZATION_WARNING( | 166 MOJO_INTERNAL_DLOG_SERIALIZATION_WARNING( |
| 161 !validate_params->element_is_nullable && !element, | 167 !validate_params->element_is_nullable && !element, |
| 162 VALIDATION_ERROR_UNEXPECTED_NULL_POINTER, | 168 VALIDATION_ERROR_UNEXPECTED_NULL_POINTER, |
| 163 MakeMessageWithArrayIndex("null in array expecting valid pointers", | 169 MakeMessageWithArrayIndex("null in array expecting valid pointers", |
| 164 input.size(), i)); | 170 input.size(), i)); |
| 165 } | 171 } |
| 166 } | 172 } |
| 167 static void DeserializeElements(Array_Data<S_Data*>* input, | 173 static void DeserializeElements(Array_Data<S_Data*>* input, |
| 168 Array<S>* output) { | 174 Array<S>* output, |
| 175 SerializationContext* context) { |
| 169 Array<S> result(input->size()); | 176 Array<S> result(input->size()); |
| 170 for (size_t i = 0; i < input->size(); ++i) { | 177 for (size_t i = 0; i < input->size(); ++i) { |
| 171 Deserialize_(input->at(i), &result[i]); | 178 Deserialize_(input->at(i), &result[i], context); |
| 172 } | 179 } |
| 173 output->Swap(&result); | 180 output->Swap(&result); |
| 174 } | 181 } |
| 175 | 182 |
| 176 private: | 183 private: |
| 177 template <typename T> | 184 template <typename T> |
| 178 struct SerializeCaller { | 185 struct SerializeCaller { |
| 179 static void Run(T input, | 186 static void Run(T input, |
| 180 Buffer* buf, | 187 Buffer* buf, |
| 181 typename WrapperTraits<T>::DataType* output, | 188 typename WrapperTraits<T>::DataType* output, |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 229 U_Data* result = output->storage() + i; | 236 U_Data* result = output->storage() + i; |
| 230 SerializeUnion_(input[i].Pass(), buf, &result, true); | 237 SerializeUnion_(input[i].Pass(), buf, &result, true); |
| 231 MOJO_INTERNAL_DLOG_SERIALIZATION_WARNING( | 238 MOJO_INTERNAL_DLOG_SERIALIZATION_WARNING( |
| 232 !validate_params->element_is_nullable && output->at(i).is_null(), | 239 !validate_params->element_is_nullable && output->at(i).is_null(), |
| 233 VALIDATION_ERROR_UNEXPECTED_NULL_POINTER, | 240 VALIDATION_ERROR_UNEXPECTED_NULL_POINTER, |
| 234 MakeMessageWithArrayIndex("null in array expecting valid unions", | 241 MakeMessageWithArrayIndex("null in array expecting valid unions", |
| 235 input.size(), i)); | 242 input.size(), i)); |
| 236 } | 243 } |
| 237 } | 244 } |
| 238 | 245 |
| 239 static void DeserializeElements(Array_Data<U_Data>* input, Array<U>* output) { | 246 static void DeserializeElements(Array_Data<U_Data>* input, |
| 247 Array<U>* output, |
| 248 SerializationContext* context) { |
| 240 Array<U> result(input->size()); | 249 Array<U> result(input->size()); |
| 241 for (size_t i = 0; i < input->size(); ++i) { | 250 for (size_t i = 0; i < input->size(); ++i) { |
| 242 Deserialize_(&input->at(i), &result[i]); | 251 Deserialize_(&input->at(i), &result[i], context); |
| 243 } | 252 } |
| 244 output->Swap(&result); | 253 output->Swap(&result); |
| 245 } | 254 } |
| 246 }; | 255 }; |
| 247 | 256 |
| 248 // Handles serialization and deserialization of arrays of strings. | 257 // Handles serialization and deserialization of arrays of strings. |
| 249 template <> | 258 template <> |
| 250 struct ArraySerializer<String, String_Data*> { | 259 struct ArraySerializer<String, String_Data*> { |
| 251 static size_t GetSerializedSize(const Array<String>& input) { | 260 static size_t GetSerializedSize(const Array<String>& input) { |
| 252 size_t size = | 261 size_t size = |
| (...skipping 19 matching lines...) Expand all Loading... |
| 272 Serialize_(input[i], buf, &element); | 281 Serialize_(input[i], buf, &element); |
| 273 output->at(i) = element; | 282 output->at(i) = element; |
| 274 MOJO_INTERNAL_DLOG_SERIALIZATION_WARNING( | 283 MOJO_INTERNAL_DLOG_SERIALIZATION_WARNING( |
| 275 !validate_params->element_is_nullable && !element, | 284 !validate_params->element_is_nullable && !element, |
| 276 VALIDATION_ERROR_UNEXPECTED_NULL_POINTER, | 285 VALIDATION_ERROR_UNEXPECTED_NULL_POINTER, |
| 277 MakeMessageWithArrayIndex("null in array expecting valid strings", | 286 MakeMessageWithArrayIndex("null in array expecting valid strings", |
| 278 input.size(), i)); | 287 input.size(), i)); |
| 279 } | 288 } |
| 280 } | 289 } |
| 281 static void DeserializeElements(Array_Data<String_Data*>* input, | 290 static void DeserializeElements(Array_Data<String_Data*>* input, |
| 282 Array<String>* output) { | 291 Array<String>* output, |
| 292 SerializationContext* context) { |
| 283 Array<String> result(input->size()); | 293 Array<String> result(input->size()); |
| 284 for (size_t i = 0; i < input->size(); ++i) | 294 for (size_t i = 0; i < input->size(); ++i) |
| 285 Deserialize_(input->at(i), &result[i]); | 295 Deserialize_(input->at(i), &result[i], context); |
| 286 output->Swap(&result); | 296 output->Swap(&result); |
| 287 } | 297 } |
| 288 }; | 298 }; |
| 289 | 299 |
| 290 } // namespace internal | 300 } // namespace internal |
| 291 | 301 |
| 292 template <typename E> | 302 template <typename E> |
| 293 inline size_t GetSerializedSize_(const Array<E>& input) { | 303 inline size_t GetSerializedSize_(const Array<E>& input) { |
| 294 if (!input) | 304 if (!input) |
| 295 return 0; | 305 return 0; |
| (...skipping 22 matching lines...) Expand all Loading... |
| 318 internal::ArraySerializer<E, F>::SerializeElements( | 328 internal::ArraySerializer<E, F>::SerializeElements( |
| 319 internal::Forward(input), buf, result, validate_params); | 329 internal::Forward(input), buf, result, validate_params); |
| 320 } | 330 } |
| 321 *output = result; | 331 *output = result; |
| 322 } else { | 332 } else { |
| 323 *output = nullptr; | 333 *output = nullptr; |
| 324 } | 334 } |
| 325 } | 335 } |
| 326 | 336 |
| 327 template <typename E, typename F> | 337 template <typename E, typename F> |
| 328 inline void Deserialize_(internal::Array_Data<F>* input, Array<E>* output) { | 338 inline void Deserialize_(internal::Array_Data<F>* input, |
| 339 Array<E>* output, |
| 340 internal::SerializationContext* context) { |
| 329 if (input) { | 341 if (input) { |
| 330 internal::ArraySerializer<E, F>::DeserializeElements(input, output); | 342 internal::ArraySerializer<E, F>::DeserializeElements(input, output, |
| 343 context); |
| 331 } else { | 344 } else { |
| 332 output->reset(); | 345 output->reset(); |
| 333 } | 346 } |
| 334 } | 347 } |
| 335 | 348 |
| 336 } // namespace mojo | 349 } // namespace mojo |
| 337 | 350 |
| 338 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_SERIALIZATION_H_ | 351 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_SERIALIZATION_H_ |
| OLD | NEW |