OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2016 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2016 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
138 } \ | 138 } \ |
139 }; | 139 }; |
140 RTC_SAFECMP_MAKE_OP(EqOp, ==) | 140 RTC_SAFECMP_MAKE_OP(EqOp, ==) |
141 RTC_SAFECMP_MAKE_OP(NeOp, !=) | 141 RTC_SAFECMP_MAKE_OP(NeOp, !=) |
142 RTC_SAFECMP_MAKE_OP(LtOp, <) | 142 RTC_SAFECMP_MAKE_OP(LtOp, <) |
143 RTC_SAFECMP_MAKE_OP(LeOp, <=) | 143 RTC_SAFECMP_MAKE_OP(LeOp, <=) |
144 RTC_SAFECMP_MAKE_OP(GtOp, >) | 144 RTC_SAFECMP_MAKE_OP(GtOp, >) |
145 RTC_SAFECMP_MAKE_OP(GeOp, >=) | 145 RTC_SAFECMP_MAKE_OP(GeOp, >=) |
146 #undef RTC_SAFECMP_MAKE_OP | 146 #undef RTC_SAFECMP_MAKE_OP |
147 | 147 |
148 // Determines if the given type is an enum that converts implicitly to | |
149 // an integral type. | |
150 template <typename T> | |
151 struct IsIntEnum { | |
152 private: | |
153 // This overload is used if the type is an enum, and unary plus | |
154 // compiles and turns it into an integral type. | |
155 template <typename X, | |
156 typename std::enable_if< | |
157 std::is_enum<X>::value && | |
158 std::is_integral<decltype(+std::declval<X>())>::value>::type* = | |
159 nullptr> | |
160 static int Test(int); | |
161 | |
162 // Otherwise, this overload is used. | |
163 template <typename> | |
164 static char Test(...); | |
ossu
2016/11/28 14:14:34
I tried to find an answer to this, and clearly thi
kwiberg-webrtc
2016/11/28 23:20:37
Yes, they can be the same size, but I think they'd
ossu
2016/11/29 10:28:23
Cool beans! Then I'm satisfied. :)
| |
165 | |
166 public: | |
167 static constexpr bool value = | |
168 std::is_same<decltype(Test<typename std::remove_reference<T>::type>(0)), | |
169 int>::value; | |
170 }; | |
171 | |
172 // Determines if the given type is integral, or an enum that | |
173 // converts implicitly to an integral type. | |
174 template <typename T> | |
175 struct IsIntlike { | |
176 private: | |
177 using X = typename std::remove_reference<T>::type; | |
178 | |
179 public: | |
180 static constexpr bool value = | |
181 std::is_integral<X>::value || IsIntEnum<X>::value; | |
182 }; | |
183 | |
184 namespace test_enum_intlike { | |
185 | |
186 enum E1 { e1 }; | |
187 enum { e2 }; | |
188 enum class E3 { e3 }; | |
189 struct S {}; | |
190 | |
191 static_assert(IsIntEnum<E1>::value, ""); | |
192 static_assert(IsIntEnum<decltype(e2)>::value, ""); | |
193 static_assert(!IsIntEnum<E3>::value, ""); | |
194 static_assert(!IsIntEnum<int>::value, ""); | |
195 static_assert(!IsIntEnum<float>::value, ""); | |
196 static_assert(!IsIntEnum<S>::value, ""); | |
197 | |
198 static_assert(IsIntlike<E1>::value, ""); | |
199 static_assert(IsIntlike<decltype(e2)>::value, ""); | |
200 static_assert(!IsIntlike<E3>::value, ""); | |
201 static_assert(IsIntlike<int>::value, ""); | |
202 static_assert(!IsIntlike<float>::value, ""); | |
203 static_assert(!IsIntlike<S>::value, ""); | |
204 | |
205 } // test_enum_intlike | |
148 } // namespace safe_cmp_impl | 206 } // namespace safe_cmp_impl |
149 | 207 |
150 #define RTC_SAFECMP_MAKE_FUN(name) \ | 208 #define RTC_SAFECMP_MAKE_FUN(name) \ |
151 template < \ | 209 template <typename T1, typename T2, \ |
152 typename T1, typename T2, \ | 210 typename std::enable_if< \ |
153 typename std::enable_if< \ | 211 safe_cmp_impl::IsIntlike<T1>::value && \ |
154 std::is_integral<typename std::remove_reference<T1>::type>::value && \ | 212 safe_cmp_impl::IsIntlike<T2>::value>::type* = nullptr> \ |
155 std::is_integral<typename std::remove_reference<T2>::type>::value>:: \ | 213 inline bool name(T1 a, T2 b) { \ |
156 type* = nullptr> \ | 214 /* Unary plus here turns enums into real integral types. */ \ |
157 inline bool name(T1 a, T2 b) { \ | 215 return safe_cmp_impl::Cmp<safe_cmp_impl::name##Op>(+a, +b); \ |
158 return safe_cmp_impl::Cmp<safe_cmp_impl::name##Op>(a, b); \ | 216 } \ |
159 } \ | 217 template <typename T1, typename T2, \ |
160 template <typename T1, typename T2, \ | 218 typename std::enable_if< \ |
161 typename std::enable_if< \ | 219 !safe_cmp_impl::IsIntlike<T1>::value || \ |
162 !std::is_integral< \ | 220 !safe_cmp_impl::IsIntlike<T2>::value>::type* = nullptr> \ |
163 typename std::remove_reference<T1>::type>::value || \ | 221 inline bool name(T1&& a, T2&& b) { \ |
164 !std::is_integral<typename std::remove_reference<T2>::type>:: \ | 222 return safe_cmp_impl::name##Op::Op(a, b); \ |
165 value>::type* = nullptr> \ | |
166 inline bool name(T1&& a, T2&& b) { \ | |
167 return safe_cmp_impl::name##Op::Op(a, b); \ | |
168 } | 223 } |
169 RTC_SAFECMP_MAKE_FUN(Eq) | 224 RTC_SAFECMP_MAKE_FUN(Eq) |
170 RTC_SAFECMP_MAKE_FUN(Ne) | 225 RTC_SAFECMP_MAKE_FUN(Ne) |
171 RTC_SAFECMP_MAKE_FUN(Lt) | 226 RTC_SAFECMP_MAKE_FUN(Lt) |
172 RTC_SAFECMP_MAKE_FUN(Le) | 227 RTC_SAFECMP_MAKE_FUN(Le) |
173 RTC_SAFECMP_MAKE_FUN(Gt) | 228 RTC_SAFECMP_MAKE_FUN(Gt) |
174 RTC_SAFECMP_MAKE_FUN(Ge) | 229 RTC_SAFECMP_MAKE_FUN(Ge) |
175 #undef RTC_SAFECMP_MAKE_FUN | 230 #undef RTC_SAFECMP_MAKE_FUN |
176 | 231 |
177 } // namespace safe_cmp | 232 } // namespace safe_cmp |
178 } // namespace rtc | 233 } // namespace rtc |
179 | 234 |
180 #endif // WEBRTC_BASE_SAFE_COMPARE_H_ | 235 #endif // WEBRTC_BASE_SAFE_COMPARE_H_ |
OLD | NEW |