OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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_UTIL_H_ | 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_SERIALIZATION_UTIL_H_ |
6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_SERIALIZATION_UTIL_H_ | 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_SERIALIZATION_UTIL_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 #include <stdint.h> | 9 #include <stdint.h> |
10 | 10 |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
95 | 95 |
96 template <typename T> | 96 template <typename T> |
97 inline void InterfaceDataToPointer(Interface_Data* input, | 97 inline void InterfaceDataToPointer(Interface_Data* input, |
98 InterfacePtr<T>* output, | 98 InterfacePtr<T>* output, |
99 SerializationContext* context) { | 99 SerializationContext* context) { |
100 output->Bind(InterfacePtrInfo<T>( | 100 output->Bind(InterfacePtrInfo<T>( |
101 context->handles.TakeHandleAs<mojo::MessagePipeHandle>(input->handle), | 101 context->handles.TakeHandleAs<mojo::MessagePipeHandle>(input->handle), |
102 input->version)); | 102 input->version)); |
103 } | 103 } |
104 | 104 |
105 // TODO(yzshen): Unify StructTraits::Read*() methods and remove | |
106 // HasReadFromDataViewMethod. | |
107 template <typename T> | |
108 struct HasReadFromDataViewMethod { | |
109 template <typename U> | |
110 static char Test(decltype(U::ReadFromDataView)*); | |
111 template <typename U> | |
112 static int Test(...); | |
113 static const bool value = sizeof(Test<T>(0)) == sizeof(char); | |
114 | |
115 private: | |
116 EnsureTypeIsComplete<T> check_t_; | |
117 }; | |
118 | |
119 template <typename T> | 105 template <typename T> |
120 struct HasIsNullMethod { | 106 struct HasIsNullMethod { |
121 template <typename U> | 107 template <typename U> |
122 static char Test(decltype(U::IsNull)*); | 108 static char Test(decltype(U::IsNull)*); |
123 template <typename U> | 109 template <typename U> |
124 static int Test(...); | 110 static int Test(...); |
125 static const bool value = sizeof(Test<T>(0)) == sizeof(char); | 111 static const bool value = sizeof(Test<T>(0)) == sizeof(char); |
126 | 112 |
127 private: | 113 private: |
128 EnsureTypeIsComplete<T> check_t_; | 114 EnsureTypeIsComplete<T> check_t_; |
129 }; | 115 }; |
130 | 116 |
131 template < | 117 template < |
132 typename Traits, | 118 typename Traits, |
133 typename UserType, | 119 typename UserType, |
134 typename std::enable_if<HasIsNullMethod<Traits>::value>::type* = nullptr> | 120 typename std::enable_if<HasIsNullMethod<Traits>::value>::type* = nullptr> |
135 bool CallIsNullIfExists(const UserType& input) { | 121 bool CallIsNullIfExists(const UserType& input) { |
136 return Traits::IsNull(input); | 122 return Traits::IsNull(input); |
137 } | 123 } |
138 | 124 |
139 template < | 125 template < |
140 typename Traits, | 126 typename Traits, |
141 typename UserType, | 127 typename UserType, |
142 typename std::enable_if<!HasIsNullMethod<Traits>::value>::type* = nullptr> | 128 typename std::enable_if<!HasIsNullMethod<Traits>::value>::type* = nullptr> |
143 bool CallIsNullIfExists(const UserType& input) { | 129 bool CallIsNullIfExists(const UserType& input) { |
144 return false; | 130 return false; |
145 } | 131 } |
| 132 template <typename T> |
| 133 struct HasSetToNullMethod { |
| 134 template <typename U> |
| 135 static char Test(decltype(U::SetToNull)*); |
| 136 template <typename U> |
| 137 static int Test(...); |
| 138 static const bool value = sizeof(Test<T>(0)) == sizeof(char); |
| 139 |
| 140 private: |
| 141 EnsureTypeIsComplete<T> check_t_; |
| 142 }; |
| 143 |
| 144 template < |
| 145 typename Traits, |
| 146 typename UserType, |
| 147 typename std::enable_if<HasSetToNullMethod<Traits>::value>::type* = nullptr> |
| 148 bool CallSetToNullIfExists(UserType* output) { |
| 149 Traits::SetToNull(output); |
| 150 return true; |
| 151 } |
| 152 |
| 153 template <typename Traits, |
| 154 typename UserType, |
| 155 typename std::enable_if<!HasSetToNullMethod<Traits>::value>::type* = |
| 156 nullptr> |
| 157 bool CallSetToNullIfExists(UserType* output) { |
| 158 LOG(ERROR) << "A null value is received. But the Struct/Array/StringTraits " |
| 159 << "class doesn't define a SetToNull() function and therefore is " |
| 160 << "unable to deserialize the value."; |
| 161 return false; |
| 162 } |
146 | 163 |
147 template <typename T> | 164 template <typename T> |
148 struct HasSetUpContextMethod { | 165 struct HasSetUpContextMethod { |
149 template <typename U> | 166 template <typename U> |
150 static char Test(decltype(U::SetUpContext)*); | 167 static char Test(decltype(U::SetUpContext)*); |
151 template <typename U> | 168 template <typename U> |
152 static int Test(...); | 169 static int Test(...); |
153 static const bool value = sizeof(Test<T>(0)) == sizeof(char); | 170 static const bool value = sizeof(Test<T>(0)) == sizeof(char); |
154 | 171 |
155 private: | 172 private: |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
209 ReturnType CallWithContext(ReturnType (*f)(ParamType), | 226 ReturnType CallWithContext(ReturnType (*f)(ParamType), |
210 MaybeConstUserType& input, | 227 MaybeConstUserType& input, |
211 void* context) { | 228 void* context) { |
212 return f(input); | 229 return f(input); |
213 } | 230 } |
214 | 231 |
215 } // namespace internal | 232 } // namespace internal |
216 } // namespace mojo | 233 } // namespace mojo |
217 | 234 |
218 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_SERIALIZATION_UTIL_H_ | 235 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_SERIALIZATION_UTIL_H_ |
OLD | NEW |