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 THIRD_PARTY_MOJO_SRC_MOJO_PUBLIC_CPP_BINDINGS_LIB_TEMPLATE_UTIL_H_ | |
6 #define THIRD_PARTY_MOJO_SRC_MOJO_PUBLIC_CPP_BINDINGS_LIB_TEMPLATE_UTIL_H_ | |
7 | |
8 namespace mojo { | |
9 namespace internal { | |
10 | |
11 template <class T, T v> | |
12 struct IntegralConstant { | |
13 static const T value = v; | |
14 }; | |
15 | |
16 template <class T, T v> | |
17 const T IntegralConstant<T, v>::value; | |
18 | |
19 typedef IntegralConstant<bool, true> TrueType; | |
20 typedef IntegralConstant<bool, false> FalseType; | |
21 | |
22 template <class T> | |
23 struct IsConst : FalseType {}; | |
24 template <class T> | |
25 struct IsConst<const T> : TrueType {}; | |
26 | |
27 template <class T> | |
28 struct IsPointer : FalseType {}; | |
29 template <class T> | |
30 struct IsPointer<T*> : TrueType {}; | |
31 | |
32 template <bool B, typename T = void> | |
33 struct EnableIf {}; | |
34 | |
35 template <typename T> | |
36 struct EnableIf<true, T> { | |
37 typedef T type; | |
38 }; | |
39 | |
40 // Types YesType and NoType are guaranteed such that sizeof(YesType) < | |
41 // sizeof(NoType). | |
42 typedef char YesType; | |
43 | |
44 struct NoType { | |
45 YesType dummy[2]; | |
46 }; | |
47 | |
48 // A helper template to determine if given type is non-const move-only-type, | |
49 // i.e. if a value of the given type should be passed via .Pass() in a | |
50 // destructive way. | |
51 template <typename T> | |
52 struct IsMoveOnlyType { | |
53 template <typename U> | |
54 static YesType Test(const typename U::MoveOnlyTypeForCPP03*); | |
55 | |
56 template <typename U> | |
57 static NoType Test(...); | |
58 | |
59 static const bool value = | |
60 sizeof(Test<T>(0)) == sizeof(YesType) && !IsConst<T>::value; | |
61 }; | |
62 | |
63 // Returns a reference to |t| when T is not a move-only type. | |
64 template <typename T> | |
65 typename EnableIf<!IsMoveOnlyType<T>::value, T>::type& Forward(T& t) { | |
66 return t; | |
67 } | |
68 | |
69 // Returns the result of t.Pass() when T is a move-only type. | |
70 template <typename T> | |
71 typename EnableIf<IsMoveOnlyType<T>::value, T>::type Forward(T& t) { | |
72 return t.Pass(); | |
73 } | |
74 | |
75 // This goop is a trick used to implement a template that can be used to | |
76 // determine if a given class is the base class of another given class. | |
77 template <typename, typename> | |
78 struct IsSame { | |
79 static bool const value = false; | |
80 }; | |
81 template <typename A> | |
82 struct IsSame<A, A> { | |
83 static bool const value = true; | |
84 }; | |
85 template <typename Base, typename Derived> | |
86 struct IsBaseOf { | |
87 private: | |
88 // This class doesn't work correctly with forward declarations. | |
89 // Because sizeof cannot be applied to incomplete types, this line prevents us | |
90 // from passing in forward declarations. | |
91 typedef char (*EnsureTypesAreComplete)[sizeof(Base) + sizeof(Derived)]; | |
92 | |
93 static Derived* CreateDerived(); | |
94 static char(&Check(Base*))[1]; | |
95 static char(&Check(...))[2]; | |
96 | |
97 public: | |
98 static bool const value = sizeof Check(CreateDerived()) == 1 && | |
99 !IsSame<Base const, void const>::value; | |
100 }; | |
101 | |
102 template <class T> | |
103 struct RemovePointer { | |
104 typedef T type; | |
105 }; | |
106 template <class T> | |
107 struct RemovePointer<T*> { | |
108 typedef T type; | |
109 }; | |
110 | |
111 template <template <typename...> class Template, typename T> | |
112 struct IsSpecializationOf : FalseType {}; | |
113 | |
114 template <template <typename...> class Template, typename... Args> | |
115 struct IsSpecializationOf<Template, Template<Args...>> : TrueType {}; | |
116 | |
117 template <bool B, typename T, typename F> | |
118 struct Conditional { | |
119 typedef T type; | |
120 }; | |
121 | |
122 template <typename T, typename F> | |
123 struct Conditional<false, T, F> { | |
124 typedef F type; | |
125 }; | |
126 | |
127 } // namespace internal | |
128 } // namespace mojo | |
129 | |
130 #endif // THIRD_PARTY_MOJO_SRC_MOJO_PUBLIC_CPP_BINDINGS_LIB_TEMPLATE_UTIL_H_ | |
OLD | NEW |