Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(78)

Side by Side Diff: webrtc/base/safe_compare.h

Issue 2809513002: Move IsIntlike to type_traits.h (Closed)
Patch Set: Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | webrtc/base/type_traits.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 19 matching lines...) Expand all
30 30
31 #ifndef WEBRTC_BASE_SAFE_COMPARE_H_ 31 #ifndef WEBRTC_BASE_SAFE_COMPARE_H_
32 #define WEBRTC_BASE_SAFE_COMPARE_H_ 32 #define WEBRTC_BASE_SAFE_COMPARE_H_
33 33
34 #include <stddef.h> 34 #include <stddef.h>
35 #include <stdint.h> 35 #include <stdint.h>
36 36
37 #include <type_traits> 37 #include <type_traits>
38 #include <utility> 38 #include <utility>
39 39
40 #include "webrtc/base/type_traits.h"
41
40 namespace rtc { 42 namespace rtc {
41 namespace safe_cmp { 43 namespace safe_cmp {
42 44
43 namespace safe_cmp_impl { 45 namespace safe_cmp_impl {
44 46
45 template <size_t N> 47 template <size_t N>
46 struct LargerIntImpl : std::false_type {}; 48 struct LargerIntImpl : std::false_type {};
47 template <> 49 template <>
48 struct LargerIntImpl<sizeof(int8_t)> : std::true_type { 50 struct LargerIntImpl<sizeof(int8_t)> : std::true_type {
49 using type = int16_t; 51 using type = int16_t;
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
138 } \ 140 } \
139 }; 141 };
140 RTC_SAFECMP_MAKE_OP(EqOp, ==) 142 RTC_SAFECMP_MAKE_OP(EqOp, ==)
141 RTC_SAFECMP_MAKE_OP(NeOp, !=) 143 RTC_SAFECMP_MAKE_OP(NeOp, !=)
142 RTC_SAFECMP_MAKE_OP(LtOp, <) 144 RTC_SAFECMP_MAKE_OP(LtOp, <)
143 RTC_SAFECMP_MAKE_OP(LeOp, <=) 145 RTC_SAFECMP_MAKE_OP(LeOp, <=)
144 RTC_SAFECMP_MAKE_OP(GtOp, >) 146 RTC_SAFECMP_MAKE_OP(GtOp, >)
145 RTC_SAFECMP_MAKE_OP(GeOp, >=) 147 RTC_SAFECMP_MAKE_OP(GeOp, >=)
146 #undef RTC_SAFECMP_MAKE_OP 148 #undef RTC_SAFECMP_MAKE_OP
147 149
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(...);
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
206 } // namespace safe_cmp_impl 150 } // namespace safe_cmp_impl
207 151
208 #define RTC_SAFECMP_MAKE_FUN(name) \ 152 #define RTC_SAFECMP_MAKE_FUN(name) \
209 template <typename T1, typename T2, \ 153 template <typename T1, typename T2, \
210 typename std::enable_if< \ 154 typename std::enable_if<IsIntlike<T1>::value && \
211 safe_cmp_impl::IsIntlike<T1>::value && \ 155 IsIntlike<T2>::value>::type* = nullptr> \
212 safe_cmp_impl::IsIntlike<T2>::value>::type* = nullptr> \ 156 inline bool name(T1 a, T2 b) { \
213 inline bool name(T1 a, T2 b) { \ 157 /* Unary plus here turns enums into real integral types. */ \
214 /* Unary plus here turns enums into real integral types. */ \ 158 return safe_cmp_impl::Cmp<safe_cmp_impl::name##Op>(+a, +b); \
215 return safe_cmp_impl::Cmp<safe_cmp_impl::name##Op>(+a, +b); \ 159 } \
216 } \ 160 template <typename T1, typename T2, \
217 template <typename T1, typename T2, \ 161 typename std::enable_if<!IsIntlike<T1>::value || \
218 typename std::enable_if< \ 162 !IsIntlike<T2>::value>::type* = nullptr> \
219 !safe_cmp_impl::IsIntlike<T1>::value || \ 163 inline bool name(T1&& a, T2&& b) { \
220 !safe_cmp_impl::IsIntlike<T2>::value>::type* = nullptr> \ 164 return safe_cmp_impl::name##Op::Op(a, b); \
221 inline bool name(T1&& a, T2&& b) { \
222 return safe_cmp_impl::name##Op::Op(a, b); \
223 } 165 }
224 RTC_SAFECMP_MAKE_FUN(Eq) 166 RTC_SAFECMP_MAKE_FUN(Eq)
225 RTC_SAFECMP_MAKE_FUN(Ne) 167 RTC_SAFECMP_MAKE_FUN(Ne)
226 RTC_SAFECMP_MAKE_FUN(Lt) 168 RTC_SAFECMP_MAKE_FUN(Lt)
227 RTC_SAFECMP_MAKE_FUN(Le) 169 RTC_SAFECMP_MAKE_FUN(Le)
228 RTC_SAFECMP_MAKE_FUN(Gt) 170 RTC_SAFECMP_MAKE_FUN(Gt)
229 RTC_SAFECMP_MAKE_FUN(Ge) 171 RTC_SAFECMP_MAKE_FUN(Ge)
230 #undef RTC_SAFECMP_MAKE_FUN 172 #undef RTC_SAFECMP_MAKE_FUN
231 173
232 } // namespace safe_cmp 174 } // namespace safe_cmp
233 } // namespace rtc 175 } // namespace rtc
234 176
235 #endif // WEBRTC_BASE_SAFE_COMPARE_H_ 177 #endif // WEBRTC_BASE_SAFE_COMPARE_H_
OLDNEW
« no previous file with comments | « no previous file | webrtc/base/type_traits.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698