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> |
11 | 11 |
12 #include "mojo/public/c/system/macros.h" | 12 #include "mojo/public/c/system/macros.h" |
13 #include "mojo/public/cpp/bindings/lib/array_internal.h" | 13 #include "mojo/public/cpp/bindings/lib/array_internal.h" |
| 14 #include "mojo/public/cpp/bindings/lib/bindings_internal.h" |
14 #include "mojo/public/cpp/bindings/lib/string_serialization.h" | 15 #include "mojo/public/cpp/bindings/lib/string_serialization.h" |
15 #include "mojo/public/cpp/bindings/lib/template_util.h" | 16 #include "mojo/public/cpp/bindings/lib/template_util.h" |
16 #include "mojo/public/cpp/bindings/lib/validation_errors.h" | 17 #include "mojo/public/cpp/bindings/lib/validation_errors.h" |
17 | 18 |
18 namespace mojo { | 19 namespace mojo { |
19 | 20 |
20 template <typename E> | 21 template <typename E> |
21 inline size_t GetSerializedSize_(const Array<E>& input); | 22 inline size_t GetSerializedSize_(const Array<E>& input); |
22 | 23 |
23 // Because ValidateParams requires explicit argument specification, the | 24 // Because ValidateParams requires explicit argument specification, the |
24 // argument-dependent loopup technique used to omit namespace when calling | 25 // argument-dependent loopup technique used to omit namespace when calling |
25 // Serialize_() doesn't seem to work. Therefore, this function is named | 26 // Serialize_() doesn't seem to work. Therefore, this function is named |
26 // differently from those Serialize_() overloads. | 27 // differently from those Serialize_() overloads. |
27 template <typename ValidateParams, typename E, typename F> | 28 template <typename ValidateParams, typename E, typename F> |
28 inline void SerializeArray_(Array<E> input, internal::Buffer* buf, | 29 inline void SerializeArray_(Array<E> input, internal::Buffer* buf, |
29 internal::Array_Data<F>** output); | 30 internal::Array_Data<F>** output); |
30 | 31 |
31 template <typename E, typename F> | 32 template <typename E, typename F> |
32 inline void Deserialize_(internal::Array_Data<F>* data, Array<E>* output); | 33 inline void Deserialize_(internal::Array_Data<F>* data, Array<E>* output); |
33 | 34 |
| 35 template <typename ValueValidateParams, |
| 36 typename KeyWrapperType, |
| 37 typename ValueWrapperType, |
| 38 typename KeySerializationType, |
| 39 typename ValueSerializationType> |
| 40 inline void SerializeMap_( |
| 41 Map<KeyWrapperType, ValueWrapperType> input, |
| 42 internal::Buffer* buf, |
| 43 internal::MapPointerPair<KeySerializationType, |
| 44 ValueSerializationType>* output); |
| 45 |
34 namespace internal { | 46 namespace internal { |
35 | 47 |
36 template <typename E, typename F, bool move_only = IsMoveOnlyType<E>::value> | 48 template <typename E, typename F, bool move_only = IsMoveOnlyType<E>::value> |
37 struct ArraySerializer; | 49 struct ArraySerializer; |
38 | 50 |
39 template <typename E, typename F> struct ArraySerializer<E, F, false> { | 51 template <typename E, typename F> struct ArraySerializer<E, F, false> { |
40 MOJO_COMPILE_ASSERT(sizeof(E) == sizeof(F), wrong_array_serializer); | 52 MOJO_COMPILE_ASSERT(sizeof(E) == sizeof(F), wrong_array_serializer); |
41 static size_t GetSerializedSize(const Array<E>& input) { | 53 static size_t GetSerializedSize(const Array<E>& input) { |
42 return sizeof(Array_Data<F>) + Align(input.size() * sizeof(E)); | 54 return sizeof(Array_Data<F>) + Align(input.size() * sizeof(E)); |
43 } | 55 } |
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
205 } | 217 } |
206 static void DeserializeElements( | 218 static void DeserializeElements( |
207 Array_Data<String_Data*>* input, Array<String>* output) { | 219 Array_Data<String_Data*>* input, Array<String>* output) { |
208 Array<String> result(input->size()); | 220 Array<String> result(input->size()); |
209 for (size_t i = 0; i < input->size(); ++i) | 221 for (size_t i = 0; i < input->size(); ++i) |
210 Deserialize_(input->at(i), &result[i]); | 222 Deserialize_(input->at(i), &result[i]); |
211 output->Swap(&result); | 223 output->Swap(&result); |
212 } | 224 } |
213 }; | 225 }; |
214 | 226 |
| 227 template <typename KeyWrapperType, typename ValueWrapperType, |
| 228 typename KeySerializationType, typename ValueSerializationType> |
| 229 struct ArraySerializer< |
| 230 Map<KeyWrapperType, ValueWrapperType>, |
| 231 MapPointerPair<KeySerializationType, ValueSerializationType>, |
| 232 true> { |
| 233 static size_t GetSerializedSize( |
| 234 const Array<Map<KeyWrapperType, ValueWrapperType>>& input) { |
| 235 size_t size = sizeof(ArrayHeader) + |
| 236 input.size() * sizeof(MapPointerPair<KeySerializationType, |
| 237 ValueSerializationType>); |
| 238 for (size_t i = 0; i < input.size(); ++i) |
| 239 size += GetSerializedSize_(input[i]); |
| 240 return size; |
| 241 } |
| 242 |
| 243 template <bool element_is_nullable, typename ElementValidateParams> |
| 244 static void SerializeElements( |
| 245 Array<Map<KeyWrapperType, ValueWrapperType>> input, |
| 246 Buffer* buf, |
| 247 Array_Data<MapPointerPair<KeySerializationType, |
| 248 ValueSerializationType>>* output) { |
| 249 for (size_t i = 0; i < input.size(); ++i) { |
| 250 MapPointerPair<KeySerializationType, ValueSerializationType> element; |
| 251 SerializeMap_<ElementValidateParams>(input[i].Pass(), buf, &element); |
| 252 output->at(i) = element; |
| 253 MOJO_DCHECK(!!element.keys.ptr == !!element.values.ptr); |
| 254 MOJO_INTERNAL_DLOG_SERIALIZATION_WARNING( |
| 255 !element_is_nullable && !element.keys.ptr, |
| 256 VALIDATION_ERROR_UNEXPECTED_NULL_POINTER, |
| 257 MakeMessageWithArrayIndex( |
| 258 "null in array expecting valid pointers", input.size(), i)); |
| 259 } |
| 260 } |
| 261 |
| 262 static void DeserializeElements( |
| 263 Array_Data< |
| 264 MapPointerPair<KeySerializationType, ValueSerializationType>>* input, |
| 265 Array<Map<KeyWrapperType, ValueWrapperType>>* output) { |
| 266 Array<Map<KeyWrapperType, ValueWrapperType>> result(input->size()); |
| 267 for (size_t i = 0; i < input->size(); ++i) { |
| 268 Map<KeyWrapperType, ValueWrapperType> element; |
| 269 Deserialize_(&input->at(i), &element); |
| 270 result[i] = element.Pass(); |
| 271 } |
| 272 output->Swap(&result); |
| 273 } |
| 274 }; |
| 275 |
215 } // namespace internal | 276 } // namespace internal |
216 | 277 |
217 template <typename E> | 278 template <typename E> |
218 inline size_t GetSerializedSize_(const Array<E>& input) { | 279 inline size_t GetSerializedSize_(const Array<E>& input) { |
219 if (!input) | 280 if (!input) |
220 return 0; | 281 return 0; |
221 typedef typename internal::WrapperTraits<E>::DataType F; | 282 typedef typename internal::WrapperTraits<E>::DataType F; |
222 return internal::ArraySerializer<E, F>::GetSerializedSize(input); | 283 return internal::ArraySerializer<E, F>::GetSerializedSize(input); |
223 } | 284 } |
224 | 285 |
(...skipping 28 matching lines...) Expand all Loading... |
253 if (input) { | 314 if (input) { |
254 internal::ArraySerializer<E, F>::DeserializeElements(input, output); | 315 internal::ArraySerializer<E, F>::DeserializeElements(input, output); |
255 } else { | 316 } else { |
256 output->reset(); | 317 output->reset(); |
257 } | 318 } |
258 } | 319 } |
259 | 320 |
260 } // namespace mojo | 321 } // namespace mojo |
261 | 322 |
262 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_SERIALIZATION_H_ | 323 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_SERIALIZATION_H_ |
OLD | NEW |