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 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
221 } | 221 } |
222 | 222 |
223 template <typename ReturnType, typename ParamType, typename InputUserType> | 223 template <typename ReturnType, typename ParamType, typename InputUserType> |
224 ReturnType CallWithContext(ReturnType (*f)(ParamType), | 224 ReturnType CallWithContext(ReturnType (*f)(ParamType), |
225 InputUserType&& input, | 225 InputUserType&& input, |
226 void* context) { | 226 void* context) { |
227 return f(std::forward<InputUserType>(input)); | 227 return f(std::forward<InputUserType>(input)); |
228 } | 228 } |
229 | 229 |
230 template <typename T, typename MaybeConstUserType> | 230 template <typename T, typename MaybeConstUserType> |
231 struct HasGetBeginMethod { | |
232 template <typename U> | |
233 static char Test(decltype(U::GetBegin(std::declval<MaybeConstUserType&>()))*); | |
234 template <typename U> | |
235 static int Test(...); | |
236 static const bool value = sizeof(Test<T>(0)) == sizeof(char); | |
237 | |
238 private: | |
239 EnsureTypeIsComplete<T> check_t_; | |
240 }; | |
241 | |
242 template <typename T, typename MaybeConstUserType> | |
231 struct HasGetDataMethod { | 243 struct HasGetDataMethod { |
yzshen1
2016/06/10 16:21:32
nit: to be consistent with the surrounding code, p
Fady Samuel
2016/06/10 19:05:33
Done.
| |
232 template <typename U> | 244 template <typename U> |
233 static char Test(decltype(U::GetData(std::declval<MaybeConstUserType&>()))*); | 245 static char Test(decltype(U::GetData(std::declval<MaybeConstUserType&>()))*); |
234 template <typename U> | 246 template <typename U> |
235 static int Test(...); | 247 static int Test(...); |
236 static const bool value = sizeof(Test<T>(0)) == sizeof(char); | 248 static const bool value = sizeof(Test<T>(0)) == sizeof(char); |
237 | 249 |
238 private: | 250 private: |
239 EnsureTypeIsComplete<T> check_t_; | 251 EnsureTypeIsComplete<T> check_t_; |
240 }; | 252 }; |
241 | 253 |
242 template < | 254 template < |
243 typename Traits, | 255 typename Traits, |
244 typename MaybeConstUserType, | 256 typename MaybeConstUserType, |
245 typename std::enable_if< | 257 typename std::enable_if< |
258 HasGetBeginMethod<Traits, MaybeConstUserType>::value>::type* = nullptr> | |
259 decltype(Traits::GetBegin(std::declval<MaybeConstUserType&>())) | |
260 CallGetBeginIfExists(MaybeConstUserType& input) { | |
261 return Traits::GetBegin(input); | |
262 } | |
263 | |
264 template < | |
265 typename Traits, | |
266 typename MaybeConstUserType, | |
267 typename std::enable_if< | |
268 !HasGetBeginMethod<Traits, MaybeConstUserType>::value>::type* = nullptr> | |
269 size_t CallGetBeginIfExists(MaybeConstUserType& input) { | |
270 return 0; | |
271 } | |
272 | |
273 template < | |
274 typename Traits, | |
275 typename MaybeConstUserType, | |
276 typename std::enable_if< | |
246 HasGetDataMethod<Traits, MaybeConstUserType>::value>::type* = nullptr> | 277 HasGetDataMethod<Traits, MaybeConstUserType>::value>::type* = nullptr> |
247 decltype(Traits::GetData(std::declval<MaybeConstUserType&>())) | 278 decltype(Traits::GetData(std::declval<MaybeConstUserType&>())) |
248 CallGetDataIfExists(MaybeConstUserType& input) { | 279 CallGetDataIfExists(MaybeConstUserType& input) { |
249 return Traits::GetData(input); | 280 return Traits::GetData(input); |
250 } | 281 } |
251 | 282 |
252 template < | 283 template < |
253 typename Traits, | 284 typename Traits, |
254 typename MaybeConstUserType, | 285 typename MaybeConstUserType, |
255 typename std::enable_if< | 286 typename std::enable_if< |
256 !HasGetDataMethod<Traits, MaybeConstUserType>::value>::type* = nullptr> | 287 !HasGetDataMethod<Traits, MaybeConstUserType>::value>::type* = nullptr> |
257 void* CallGetDataIfExists(MaybeConstUserType& input) { | 288 void* CallGetDataIfExists(MaybeConstUserType& input) { |
258 return nullptr; | 289 return nullptr; |
259 } | 290 } |
260 | 291 |
261 } // namespace internal | 292 } // namespace internal |
262 } // namespace mojo | 293 } // namespace mojo |
263 | 294 |
264 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_SERIALIZATION_UTIL_H_ | 295 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_SERIALIZATION_UTIL_H_ |
OLD | NEW |