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