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> | 13 template <class T, T v> |
12 struct IntegralConstant { | 14 struct IntegralConstant { |
13 static const T value = v; | 15 static const T value = v; |
14 }; | 16 }; |
15 | 17 |
16 template <class T, T v> | 18 template <class T, T v> |
17 const T IntegralConstant<T, v>::value; | 19 const T IntegralConstant<T, v>::value; |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
111 template <bool B, typename T, typename F> | 113 template <bool B, typename T, typename F> |
112 struct Conditional { | 114 struct Conditional { |
113 typedef T type; | 115 typedef T type; |
114 }; | 116 }; |
115 | 117 |
116 template <typename T, typename F> | 118 template <typename T, typename F> |
117 struct Conditional<false, T, F> { | 119 struct Conditional<false, T, F> { |
118 typedef F type; | 120 typedef F type; |
119 }; | 121 }; |
120 | 122 |
| 123 template <typename T> |
| 124 struct HasCloneMethod { |
| 125 template <typename U> |
| 126 static char Test(decltype(&U::Clone)); |
| 127 template <typename U> |
| 128 static int Test(...); |
| 129 static const bool value = sizeof(Test<T>(0)) == sizeof(char); |
| 130 |
| 131 private: |
| 132 EnsureTypeIsComplete<T> check_t_; |
| 133 }; |
| 134 |
| 135 template <typename T, |
| 136 typename std::enable_if<HasCloneMethod<T>::value>::type* = nullptr> |
| 137 T Clone(const T& input) { |
| 138 return input.Clone(); |
| 139 }; |
| 140 |
| 141 template <typename T, |
| 142 typename std::enable_if<!HasCloneMethod<T>::value>::type* = nullptr> |
| 143 T Clone(const T& input) { |
| 144 return input; |
| 145 } |
| 146 |
| 147 template <typename T> |
| 148 struct HasEqualsMethod { |
| 149 template <typename U> |
| 150 static char Test(decltype(&U::Equals)); |
| 151 template <typename U> |
| 152 static int Test(...); |
| 153 static const bool value = sizeof(Test<T>(0)) == sizeof(char); |
| 154 |
| 155 private: |
| 156 EnsureTypeIsComplete<T> check_t_; |
| 157 }; |
| 158 |
| 159 template <typename T, |
| 160 typename std::enable_if<HasEqualsMethod<T>::value>::type* = nullptr> |
| 161 bool Equals(const T& a, const T& b) { |
| 162 return a.Equals(b); |
| 163 }; |
| 164 |
| 165 template <typename T, |
| 166 typename std::enable_if<!HasEqualsMethod<T>::value>::type* = nullptr> |
| 167 bool Equals(const T& a, const T& b) { |
| 168 return a == b; |
| 169 } |
| 170 |
121 } // namespace internal | 171 } // namespace internal |
122 } // namespace mojo | 172 } // namespace mojo |
123 | 173 |
124 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_TEMPLATE_UTIL_H_ | 174 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_TEMPLATE_UTIL_H_ |
OLD | NEW |