| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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_TEMPLATE_UTIL_H_ | 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_TEMPLATE_UTIL_H_ |
| 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_TEMPLATE_UTIL_H_ | 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_TEMPLATE_UTIL_H_ |
| 7 | 7 |
| 8 #include <type_traits> |
| 9 |
| 8 namespace mojo { | 10 namespace mojo { |
| 9 namespace internal { | 11 namespace internal { |
| 10 | 12 |
| 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) < | 13 // Types YesType and NoType are guaranteed such that sizeof(YesType) < |
| 41 // sizeof(NoType). | 14 // sizeof(NoType). |
| 42 typedef char YesType; | 15 typedef char YesType; |
| 43 | 16 |
| 44 struct NoType { | 17 struct NoType { |
| 45 YesType dummy[2]; | 18 YesType dummy[2]; |
| 46 }; | 19 }; |
| 47 | 20 |
| 48 // A helper template to determine if given type is non-const move-only-type, | 21 // 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 | 22 // i.e. if a value of the given type should be passed via .Pass() in a |
| 50 // destructive way. | 23 // destructive way. |
| 51 template <typename T> | 24 template <typename T> |
| 52 struct IsMoveOnlyType { | 25 struct IsMoveOnlyType { |
| 53 template <typename U> | 26 template <typename U> |
| 54 static YesType Test(const typename U::MoveOnlyTypeForCPP03*); | 27 static YesType Test(const typename U::MoveOnlyTypeForCPP03*); |
| 55 | 28 |
| 56 template <typename U> | 29 template <typename U> |
| 57 static NoType Test(...); | 30 static NoType Test(...); |
| 58 | 31 |
| 59 static const bool value = | 32 static const bool value = |
| 60 sizeof(Test<T>(0)) == sizeof(YesType) && !IsConst<T>::value; | 33 sizeof(Test<T>(0)) == sizeof(YesType) && !std::is_const<T>::value; |
| 61 }; | 34 }; |
| 62 | 35 |
| 63 // Returns a reference to |t| when T is not a move-only type. | 36 // Returns a reference to |t| when T is not a move-only type. |
| 64 template <typename T> | 37 template <typename T> |
| 65 typename EnableIf<!IsMoveOnlyType<T>::value, T>::type& Forward(T& t) { | 38 typename std::enable_if<!IsMoveOnlyType<T>::value, T>::type& Forward(T& t) { |
| 66 return t; | 39 return t; |
| 67 } | 40 } |
| 68 | 41 |
| 69 // Returns the result of t.Pass() when T is a move-only type. | 42 // Returns the result of t.Pass() when T is a move-only type. |
| 70 template <typename T> | 43 template <typename T> |
| 71 typename EnableIf<IsMoveOnlyType<T>::value, T>::type Forward(T& t) { | 44 typename std::enable_if<IsMoveOnlyType<T>::value, T>::type Forward(T& t) { |
| 72 return t.Pass(); | 45 return t.Pass(); |
| 73 } | 46 } |
| 74 | 47 |
| 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> | 48 template <template <typename...> class Template, typename T> |
| 112 struct IsSpecializationOf : FalseType {}; | 49 struct IsSpecializationOf : std::false_type {}; |
| 113 | 50 |
| 114 template <template <typename...> class Template, typename... Args> | 51 template <template <typename...> class Template, typename... Args> |
| 115 struct IsSpecializationOf<Template, Template<Args...>> : TrueType {}; | 52 struct IsSpecializationOf<Template, Template<Args...>> : std::true_type {}; |
| 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 | 53 |
| 127 } // namespace internal | 54 } // namespace internal |
| 128 } // namespace mojo | 55 } // namespace mojo |
| 129 | 56 |
| 130 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_TEMPLATE_UTIL_H_ | 57 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_TEMPLATE_UTIL_H_ |
| OLD | NEW |