OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_TEMPLATE_UTIL_H_ | |
6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_TEMPLATE_UTIL_H_ | |
7 | |
8 namespace mojo { | |
9 namespace internal { | |
10 | |
11 template<class T, T v> | |
12 struct integral_constant { | |
yzshen1
2014/05/27 22:16:59
style nit: Shall we use IntegralConstant / kValue
| |
13 static const T value = v; | |
14 typedef T value_type; | |
15 typedef integral_constant<T, v> type; | |
16 }; | |
17 | |
18 template <class T, T v> const T integral_constant<T, v>::value; | |
19 | |
20 typedef integral_constant<bool, true> true_type; | |
21 typedef integral_constant<bool, false> false_type; | |
22 | |
23 template <class T> struct is_const : false_type {}; | |
24 template <class T> struct is_const<const T> : true_type {}; | |
25 | |
26 template<bool B, typename T = void> | |
27 struct enable_if {}; | |
28 | |
29 template<typename T> | |
30 struct enable_if<true, T> { typedef T type; }; | |
31 | |
32 // Types YesType and NoType are guaranteed such that sizeof(YesType) < | |
33 // sizeof(NoType). | |
34 typedef char YesType; | |
35 | |
36 struct NoType { | |
37 YesType dummy[2]; | |
38 }; | |
39 | |
40 // A helper template to determine if given type is non-const move-only-type, | |
41 // i.e. if a value of the given type should be passed via .Pass() in a | |
42 // destructive way. | |
43 template <typename T> struct IsMoveOnlyType { | |
44 template <typename U> | |
45 static YesType Test(const typename U::MoveOnlyTypeForCPP03*); | |
46 | |
47 template <typename U> | |
48 static NoType Test(...); | |
49 | |
50 static const bool value = sizeof(Test<T>(0)) == sizeof(YesType) && | |
51 !is_const<T>::value; | |
52 }; | |
53 | |
54 template <typename T> | |
55 typename enable_if<!IsMoveOnlyType<T>::value, T>::type& Forward(T& t) { | |
56 return t; | |
57 } | |
58 | |
59 template <typename T> | |
60 typename enable_if<IsMoveOnlyType<T>::value, T>::type Forward(T& t) { | |
61 return t.Pass(); | |
62 } | |
63 | |
64 } // namespace internal | |
65 } // namespace mojo | |
66 | |
67 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_TEMPLATE_UTIL_H_ | |
OLD | NEW |