| 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 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 96 Buffer* buf, | 96 Buffer* buf, |
| 97 Array_Data<H>* output) { | 97 Array_Data<H>* output) { |
| 98 MOJO_COMPILE_ASSERT( | 98 MOJO_COMPILE_ASSERT( |
| 99 (IsSame<ElementValidateParams, NoValidateParams>::value), | 99 (IsSame<ElementValidateParams, NoValidateParams>::value), |
| 100 Handle_type_should_not_have_array_validate_params); | 100 Handle_type_should_not_have_array_validate_params); |
| 101 | 101 |
| 102 for (size_t i = 0; i < input.size(); ++i) { | 102 for (size_t i = 0; i < input.size(); ++i) { |
| 103 output->at(i) = input[i].release(); // Transfer ownership of the handle. | 103 output->at(i) = input[i].release(); // Transfer ownership of the handle. |
| 104 MOJO_INTERNAL_DLOG_SERIALIZATION_WARNING( | 104 MOJO_INTERNAL_DLOG_SERIALIZATION_WARNING( |
| 105 !element_is_nullable && !output->at(i).is_valid(), | 105 !element_is_nullable && !output->at(i).is_valid(), |
| 106 VALIDATION_ERROR_UNEXPECTED_INVALID_HANDLE); | 106 VALIDATION_ERROR_UNEXPECTED_INVALID_HANDLE, |
| 107 MakeMessageWithArrayIndex( |
| 108 "invalid handle in array expecting valid handles", |
| 109 input.size(), i)); |
| 107 } | 110 } |
| 108 } | 111 } |
| 109 static void DeserializeElements( | 112 static void DeserializeElements( |
| 110 Array_Data<H>* input, Array<ScopedHandleBase<H> >* output) { | 113 Array_Data<H>* input, Array<ScopedHandleBase<H> >* output) { |
| 111 Array<ScopedHandleBase<H> > result(input->size()); | 114 Array<ScopedHandleBase<H> > result(input->size()); |
| 112 for (size_t i = 0; i < input->size(); ++i) | 115 for (size_t i = 0; i < input->size(); ++i) |
| 113 result.at(i) = MakeScopedHandle(FetchAndReset(&input->at(i))); | 116 result.at(i) = MakeScopedHandle(FetchAndReset(&input->at(i))); |
| 114 output->Swap(&result); | 117 output->Swap(&result); |
| 115 } | 118 } |
| 116 }; | 119 }; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 127 static void SerializeElements(Array<S> input, | 130 static void SerializeElements(Array<S> input, |
| 128 Buffer* buf, | 131 Buffer* buf, |
| 129 Array_Data<typename S::Data_*>* output) { | 132 Array_Data<typename S::Data_*>* output) { |
| 130 for (size_t i = 0; i < input.size(); ++i) { | 133 for (size_t i = 0; i < input.size(); ++i) { |
| 131 typename S::Data_* element; | 134 typename S::Data_* element; |
| 132 SerializeCaller<S, ElementValidateParams>::Run( | 135 SerializeCaller<S, ElementValidateParams>::Run( |
| 133 input[i].Pass(), buf, &element); | 136 input[i].Pass(), buf, &element); |
| 134 output->at(i) = element; | 137 output->at(i) = element; |
| 135 MOJO_INTERNAL_DLOG_SERIALIZATION_WARNING( | 138 MOJO_INTERNAL_DLOG_SERIALIZATION_WARNING( |
| 136 !element_is_nullable && !element, | 139 !element_is_nullable && !element, |
| 137 VALIDATION_ERROR_UNEXPECTED_NULL_POINTER); | 140 VALIDATION_ERROR_UNEXPECTED_NULL_POINTER, |
| 141 MakeMessageWithArrayIndex( |
| 142 "null in array expecting valid pointers", input.size(), i)); |
| 138 } | 143 } |
| 139 } | 144 } |
| 140 static void DeserializeElements( | 145 static void DeserializeElements( |
| 141 Array_Data<typename S::Data_*>* input, Array<S>* output) { | 146 Array_Data<typename S::Data_*>* input, Array<S>* output) { |
| 142 Array<S> result(input->size()); | 147 Array<S> result(input->size()); |
| 143 for (size_t i = 0; i < input->size(); ++i) { | 148 for (size_t i = 0; i < input->size(); ++i) { |
| 144 S element; | 149 S element; |
| 145 Deserialize_(input->at(i), &element); | 150 Deserialize_(input->at(i), &element); |
| 146 result[i] = element.Pass(); | 151 result[i] = element.Pass(); |
| 147 } | 152 } |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 186 (IsSame<ElementValidateParams, | 191 (IsSame<ElementValidateParams, |
| 187 ArrayValidateParams<0, false, NoValidateParams> >::value), | 192 ArrayValidateParams<0, false, NoValidateParams> >::value), |
| 188 String_type_has_unexpected_array_validate_params); | 193 String_type_has_unexpected_array_validate_params); |
| 189 | 194 |
| 190 for (size_t i = 0; i < input.size(); ++i) { | 195 for (size_t i = 0; i < input.size(); ++i) { |
| 191 String_Data* element; | 196 String_Data* element; |
| 192 Serialize_(input[i], buf, &element); | 197 Serialize_(input[i], buf, &element); |
| 193 output->at(i) = element; | 198 output->at(i) = element; |
| 194 MOJO_INTERNAL_DLOG_SERIALIZATION_WARNING( | 199 MOJO_INTERNAL_DLOG_SERIALIZATION_WARNING( |
| 195 !element_is_nullable && !element, | 200 !element_is_nullable && !element, |
| 196 VALIDATION_ERROR_UNEXPECTED_NULL_POINTER); | 201 VALIDATION_ERROR_UNEXPECTED_NULL_POINTER, |
| 202 MakeMessageWithArrayIndex( |
| 203 "null in array expecting valid strings", input.size(), i)); |
| 197 } | 204 } |
| 198 } | 205 } |
| 199 static void DeserializeElements( | 206 static void DeserializeElements( |
| 200 Array_Data<String_Data*>* input, Array<String>* output) { | 207 Array_Data<String_Data*>* input, Array<String>* output) { |
| 201 Array<String> result(input->size()); | 208 Array<String> result(input->size()); |
| 202 for (size_t i = 0; i < input->size(); ++i) | 209 for (size_t i = 0; i < input->size(); ++i) |
| 203 Deserialize_(input->at(i), &result[i]); | 210 Deserialize_(input->at(i), &result[i]); |
| 204 output->Swap(&result); | 211 output->Swap(&result); |
| 205 } | 212 } |
| 206 }; | 213 }; |
| 207 | 214 |
| 208 } // namespace internal | 215 } // namespace internal |
| 209 | 216 |
| 210 template <typename E> | 217 template <typename E> |
| 211 inline size_t GetSerializedSize_(const Array<E>& input) { | 218 inline size_t GetSerializedSize_(const Array<E>& input) { |
| 212 if (!input) | 219 if (!input) |
| 213 return 0; | 220 return 0; |
| 214 typedef typename internal::WrapperTraits<E>::DataType F; | 221 typedef typename internal::WrapperTraits<E>::DataType F; |
| 215 return internal::ArraySerializer<E, F>::GetSerializedSize(input); | 222 return internal::ArraySerializer<E, F>::GetSerializedSize(input); |
| 216 } | 223 } |
| 217 | 224 |
| 218 template <typename ValidateParams, typename E, typename F> | 225 template <typename ValidateParams, typename E, typename F> |
| 219 inline void SerializeArray_(Array<E> input, internal::Buffer* buf, | 226 inline void SerializeArray_(Array<E> input, internal::Buffer* buf, |
| 220 internal::Array_Data<F>** output) { | 227 internal::Array_Data<F>** output) { |
| 221 if (input) { | 228 if (input) { |
| 222 MOJO_INTERNAL_DLOG_SERIALIZATION_WARNING( | 229 MOJO_INTERNAL_DLOG_SERIALIZATION_WARNING( |
| 223 ValidateParams::expected_num_elements != 0 && | 230 ValidateParams::expected_num_elements != 0 && |
| 224 input.size() != ValidateParams::expected_num_elements, | 231 input.size() != ValidateParams::expected_num_elements, |
| 225 internal::VALIDATION_ERROR_UNEXPECTED_ARRAY_HEADER); | 232 internal::VALIDATION_ERROR_UNEXPECTED_ARRAY_HEADER, |
| 233 internal::MakeMessageWithExpectedArraySize( |
| 234 "fixed-size array has wrong number of elements", |
| 235 input.size(), ValidateParams::expected_num_elements)); |
| 226 | 236 |
| 227 internal::Array_Data<F>* result = | 237 internal::Array_Data<F>* result = |
| 228 internal::Array_Data<F>::New(input.size(), buf); | 238 internal::Array_Data<F>::New(input.size(), buf); |
| 229 if (result) { | 239 if (result) { |
| 230 internal::ArraySerializer<E, F>::template SerializeElements< | 240 internal::ArraySerializer<E, F>::template SerializeElements< |
| 231 ValidateParams::element_is_nullable, | 241 ValidateParams::element_is_nullable, |
| 232 typename ValidateParams::ElementValidateParams>( | 242 typename ValidateParams::ElementValidateParams>( |
| 233 internal::Forward(input), buf, result); | 243 internal::Forward(input), buf, result); |
| 234 } | 244 } |
| 235 *output = result; | 245 *output = result; |
| 236 } else { | 246 } else { |
| 237 *output = NULL; | 247 *output = NULL; |
| 238 } | 248 } |
| 239 } | 249 } |
| 240 | 250 |
| 241 template <typename E, typename F> | 251 template <typename E, typename F> |
| 242 inline void Deserialize_(internal::Array_Data<F>* input, Array<E>* output) { | 252 inline void Deserialize_(internal::Array_Data<F>* input, Array<E>* output) { |
| 243 if (input) { | 253 if (input) { |
| 244 internal::ArraySerializer<E, F>::DeserializeElements(input, output); | 254 internal::ArraySerializer<E, F>::DeserializeElements(input, output); |
| 245 } else { | 255 } else { |
| 246 output->reset(); | 256 output->reset(); |
| 247 } | 257 } |
| 248 } | 258 } |
| 249 | 259 |
| 250 } // namespace mojo | 260 } // namespace mojo |
| 251 | 261 |
| 252 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_SERIALIZATION_H_ | 262 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_SERIALIZATION_H_ |
| OLD | NEW |