• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
3 // found in the LICENSE file.
4 
5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_SERIALIZATION_FORWARD_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_SERIALIZATION_FORWARD_H_
7 
8 #include "base/optional.h"
9 #include "mojo/public/cpp/bindings/array_traits.h"
10 #include "mojo/public/cpp/bindings/enum_traits.h"
11 #include "mojo/public/cpp/bindings/lib/template_util.h"
12 #include "mojo/public/cpp/bindings/map_traits.h"
13 #include "mojo/public/cpp/bindings/string_traits.h"
14 #include "mojo/public/cpp/bindings/struct_traits.h"
15 
16 // This file is included by serialization implementation files to avoid circular
17 // includes.
18 // Users of the serialization funtions should include serialization.h (and also
19 // wtf_serialization.h if necessary).
20 
21 namespace mojo {
22 namespace internal {
23 
24 template <typename MojomType, typename MaybeConstUserType>
25 struct Serializer;
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 
35 // PrepareToSerialize() must be matched by a Serialize() for the same input
36 // later. Moreover, within the same SerializationContext if PrepareToSerialize()
37 // is called for |input_1|, ..., |input_n|, Serialize() must be called for
38 // those objects in the exact same order.
39 template <typename MojomType,
40           typename InputUserType,
41           typename... Args,
42           typename std::enable_if<
43               !IsOptionalWrapper<InputUserType>::value>::type* = nullptr>
PrepareToSerialize(InputUserType && input,Args &&...args)44 size_t PrepareToSerialize(InputUserType&& input, Args&&... args) {
45   return Serializer<MojomType,
46                     typename std::remove_reference<InputUserType>::type>::
47       PrepareToSerialize(std::forward<InputUserType>(input),
48                          std::forward<Args>(args)...);
49 }
50 
51 template <typename MojomType,
52           typename InputUserType,
53           typename... Args,
54           typename std::enable_if<
55               !IsOptionalWrapper<InputUserType>::value>::type* = nullptr>
Serialize(InputUserType && input,Args &&...args)56 void Serialize(InputUserType&& input, Args&&... args) {
57   Serializer<MojomType, typename std::remove_reference<InputUserType>::type>::
58       Serialize(std::forward<InputUserType>(input),
59                 std::forward<Args>(args)...);
60 }
61 
62 template <typename MojomType,
63           typename DataType,
64           typename InputUserType,
65           typename... Args,
66           typename std::enable_if<
67               !IsOptionalWrapper<InputUserType>::value>::type* = nullptr>
Deserialize(DataType && input,InputUserType * output,Args &&...args)68 bool Deserialize(DataType&& input, InputUserType* output, Args&&... args) {
69   return Serializer<MojomType, InputUserType>::Deserialize(
70       std::forward<DataType>(input), output, std::forward<Args>(args)...);
71 }
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>
PrepareToSerialize(InputUserType && input,Args &&...args)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>
Serialize(InputUserType && input,Buffer * buffer,DataType ** output,Args &&...args)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>
Deserialize(DataType && input,InputUserType * output,Args &&...args)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 
119 }  // namespace internal
120 }  // namespace mojo
121 
122 #endif  // MOJO_PUBLIC_CPP_BINDINGS_LIB_SERIALIZATION_FORWARD_H_
123