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 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 template <typename T> | |
64 typename EnableIf<!IsMoveOnlyType<T>::value, T>::type& Forward(T& t) { | |
65 return t; | |
66 } | |
67 | |
68 template <typename T> | |
69 typename EnableIf<IsMoveOnlyType<T>::value, T>::type Forward(T& t) { | |
70 return t.Pass(); | |
71 } | |
72 | |
73 // This goop is a trick used to implement a template that can be used to | |
74 // determine if a given class is the base class of another given class. | |
75 template <typename, typename> | |
76 struct IsSame { | |
77 static bool const value = false; | |
78 }; | |
79 template <typename A> | |
80 struct IsSame<A, A> { | |
81 static bool const value = true; | |
82 }; | |
83 template <typename Base, typename Derived> | |
84 struct IsBaseOf { | |
85 private: | |
86 // This class doesn't work correctly with forward declarations. | |
87 // Because sizeof cannot be applied to incomplete types, this line prevents us | |
88 // from passing in forward declarations. | |
89 typedef char (*EnsureTypesAreComplete)[sizeof(Base) + sizeof(Derived)]; | |
90 | |
91 static Derived* CreateDerived(); | |
92 static char(&Check(Base*))[1]; | |
93 static char(&Check(...))[2]; | |
94 | |
95 public: | |
96 static bool const value = sizeof Check(CreateDerived()) == 1 && | |
97 !IsSame<Base const, void const>::value; | |
98 }; | |
99 | |
100 template <class T> | |
101 struct RemovePointer {}; | |
102 template <class T> | |
103 struct RemovePointer<T*> { | |
104 typedef T type; | |
105 }; | |
106 | |
107 template <template <typename...> class Template, typename T> | |
108 struct IsSpecializationOf : FalseType {}; | |
109 | |
110 template <template <typename...> class Template, typename... Args> | |
111 struct IsSpecializationOf<Template, Template<Args...>> : TrueType {}; | |
112 | |
113 } // namespace internal | |
114 } // namespace mojo | |
115 | |
116 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_TEMPLATE_UTIL_H_ | |
OLD | NEW |