OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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_SERIALIZATION_FORWARD_H_ | 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_SERIALIZATION_FORWARD_H_ |
6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_SERIALIZATION_FORWARD_H_ | 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_SERIALIZATION_FORWARD_H_ |
7 | 7 |
| 8 #include "base/optional.h" |
8 #include "mojo/public/cpp/bindings/array_traits.h" | 9 #include "mojo/public/cpp/bindings/array_traits.h" |
9 #include "mojo/public/cpp/bindings/enum_traits.h" | 10 #include "mojo/public/cpp/bindings/enum_traits.h" |
| 11 #include "mojo/public/cpp/bindings/lib/template_util.h" |
10 #include "mojo/public/cpp/bindings/map_traits.h" | 12 #include "mojo/public/cpp/bindings/map_traits.h" |
11 #include "mojo/public/cpp/bindings/string_traits.h" | 13 #include "mojo/public/cpp/bindings/string_traits.h" |
12 #include "mojo/public/cpp/bindings/struct_traits.h" | 14 #include "mojo/public/cpp/bindings/struct_traits.h" |
13 | 15 |
14 // This file is included by serialization implementation files to avoid circular | 16 // This file is included by serialization implementation files to avoid circular |
15 // includes. | 17 // includes. |
16 // Users of the serialization funtions should include serialization.h (and also | 18 // Users of the serialization funtions should include serialization.h (and also |
17 // wtf_serialization.h if necessary). | 19 // wtf_serialization.h if necessary). |
18 | 20 |
19 namespace mojo { | 21 namespace mojo { |
20 namespace internal { | 22 namespace internal { |
21 | 23 |
22 template <typename MojomType, typename MaybeConstUserType> | 24 template <typename MojomType, typename MaybeConstUserType> |
23 struct Serializer; | 25 struct Serializer; |
24 | 26 |
| 27 template <typename T> |
| 28 struct IsOptionalWrapper { |
| 29 static const bool value = IsSpecializationOf< |
| 30 base::Optional, |
| 31 typename std::remove_const< |
| 32 typename std::remove_reference<T>::type>::type>::value; |
| 33 }; |
| 34 |
25 // PrepareToSerialize() must be matched by a Serialize() for the same input | 35 // PrepareToSerialize() must be matched by a Serialize() for the same input |
26 // later. Moreover, within the same SerializationContext if PrepareToSerialize() | 36 // later. Moreover, within the same SerializationContext if PrepareToSerialize() |
27 // is called for |input_1|, ..., |input_n|, Serialize() must be called for | 37 // is called for |input_1|, ..., |input_n|, Serialize() must be called for |
28 // those objects in the exact same order. | 38 // those objects in the exact same order. |
29 template <typename MojomType, typename InputUserType, typename... Args> | 39 template <typename MojomType, |
| 40 typename InputUserType, |
| 41 typename... Args, |
| 42 typename std::enable_if< |
| 43 !IsOptionalWrapper<InputUserType>::value>::type* = nullptr> |
30 size_t PrepareToSerialize(InputUserType&& input, Args&&... args) { | 44 size_t PrepareToSerialize(InputUserType&& input, Args&&... args) { |
31 return Serializer<MojomType, | 45 return Serializer<MojomType, |
32 typename std::remove_reference<InputUserType>::type>:: | 46 typename std::remove_reference<InputUserType>::type>:: |
33 PrepareToSerialize(std::forward<InputUserType>(input), | 47 PrepareToSerialize(std::forward<InputUserType>(input), |
34 std::forward<Args>(args)...); | 48 std::forward<Args>(args)...); |
35 } | 49 } |
36 | 50 |
37 template <typename MojomType, typename InputUserType, typename... Args> | 51 template <typename MojomType, |
| 52 typename InputUserType, |
| 53 typename... Args, |
| 54 typename std::enable_if< |
| 55 !IsOptionalWrapper<InputUserType>::value>::type* = nullptr> |
38 void Serialize(InputUserType&& input, Args&&... args) { | 56 void Serialize(InputUserType&& input, Args&&... args) { |
39 Serializer<MojomType, typename std::remove_reference<InputUserType>::type>:: | 57 Serializer<MojomType, typename std::remove_reference<InputUserType>::type>:: |
40 Serialize(std::forward<InputUserType>(input), | 58 Serialize(std::forward<InputUserType>(input), |
41 std::forward<Args>(args)...); | 59 std::forward<Args>(args)...); |
42 } | 60 } |
43 | 61 |
44 template <typename MojomType, | 62 template <typename MojomType, |
45 typename DataType, | 63 typename DataType, |
46 typename InputUserType, | 64 typename InputUserType, |
47 typename... Args> | 65 typename... Args, |
| 66 typename std::enable_if< |
| 67 !IsOptionalWrapper<InputUserType>::value>::type* = nullptr> |
48 bool Deserialize(DataType&& input, InputUserType* output, Args&&... args) { | 68 bool Deserialize(DataType&& input, InputUserType* output, Args&&... args) { |
49 return Serializer<MojomType, InputUserType>::Deserialize( | 69 return Serializer<MojomType, InputUserType>::Deserialize( |
50 std::forward<DataType>(input), output, std::forward<Args>(args)...); | 70 std::forward<DataType>(input), output, std::forward<Args>(args)...); |
51 } | 71 } |
52 | 72 |
| 73 // Specialization that unwraps base::Optional<>. |
| 74 template <typename MojomType, |
| 75 typename InputUserType, |
| 76 typename... Args, |
| 77 typename std::enable_if< |
| 78 IsOptionalWrapper<InputUserType>::value>::type* = nullptr> |
| 79 size_t PrepareToSerialize(InputUserType&& input, Args&&... args) { |
| 80 if (!input) |
| 81 return 0; |
| 82 return PrepareToSerialize<MojomType>(*input, std::forward<Args>(args)...); |
| 83 } |
| 84 |
| 85 template <typename MojomType, |
| 86 typename InputUserType, |
| 87 typename DataType, |
| 88 typename... Args, |
| 89 typename std::enable_if< |
| 90 IsOptionalWrapper<InputUserType>::value>::type* = nullptr> |
| 91 void Serialize(InputUserType&& input, |
| 92 Buffer* buffer, |
| 93 DataType** output, |
| 94 Args&&... args) { |
| 95 if (!input) { |
| 96 *output = nullptr; |
| 97 return; |
| 98 } |
| 99 Serialize<MojomType>(*input, buffer, output, std::forward<Args>(args)...); |
| 100 } |
| 101 |
| 102 template <typename MojomType, |
| 103 typename DataType, |
| 104 typename InputUserType, |
| 105 typename... Args, |
| 106 typename std::enable_if< |
| 107 IsOptionalWrapper<InputUserType>::value>::type* = nullptr> |
| 108 bool Deserialize(DataType&& input, InputUserType* output, Args&&... args) { |
| 109 if (!input) { |
| 110 *output = base::nullopt; |
| 111 return true; |
| 112 } |
| 113 if (!*output) |
| 114 output->emplace(); |
| 115 return Deserialize<MojomType>(std::forward<DataType>(input), &output->value(), |
| 116 std::forward<Args>(args)...); |
| 117 } |
| 118 |
53 } // namespace internal | 119 } // namespace internal |
54 } // namespace mojo | 120 } // namespace mojo |
55 | 121 |
56 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_SERIALIZATION_FORWARD_H_ | 122 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_SERIALIZATION_FORWARD_H_ |
OLD | NEW |