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

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

Issue 2802423002: Rename safe_cmp::{Eq,Ne,Lt,Le,Ge,Gt} to Safe{Eq,Ne,Lt,Le,Ge,Gt} (Closed)
Patch Set: Created 3 years, 6 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 | « webrtc/base/checks.h ('k') | webrtc/base/safe_compare_unittest.cc » ('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
11 // This file defines six constexpr functions: 11 // This file defines six constexpr functions:
12 // 12 //
13 // rtc::safe_cmp::Eq // == 13 // rtc::SafeEq // ==
14 // rtc::safe_cmp::Ne // != 14 // rtc::SafeNe // !=
15 // rtc::safe_cmp::Lt // < 15 // rtc::SafeLt // <
16 // rtc::safe_cmp::Le // <= 16 // rtc::SafeLe // <=
17 // rtc::safe_cmp::Gt // > 17 // rtc::SafeGt // >
18 // rtc::safe_cmp::Ge // >= 18 // rtc::SafeGe // >=
19 // 19 //
20 // They each accept two arguments of arbitrary types, and in almost all cases, 20 // They each accept two arguments of arbitrary types, and in almost all cases,
21 // they simply call the appropriate comparison operator. However, if both 21 // they simply call the appropriate comparison operator. However, if both
22 // arguments are integers, they don't compare them using C++'s quirky rules, 22 // arguments are integers, they don't compare them using C++'s quirky rules,
23 // but instead adhere to the true mathematical definitions. It is as if the 23 // but instead adhere to the true mathematical definitions. It is as if the
24 // arguments were first converted to infinite-range signed integers, and then 24 // arguments were first converted to infinite-range signed integers, and then
25 // compared, although of course nothing expensive like that actually takes 25 // compared, although of course nothing expensive like that actually takes
26 // place. In practice, for signed/signed and unsigned/unsigned comparisons and 26 // place. In practice, for signed/signed and unsigned/unsigned comparisons and
27 // some mixed-signed comparisons with a compile-time constant, the overhead is 27 // some mixed-signed comparisons with a compile-time constant, the overhead is
28 // zero; in the remaining cases, it is just a few machine instructions (no 28 // zero; in the remaining cases, it is just a few machine instructions (no
29 // branches). 29 // branches).
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" 40 #include "webrtc/base/type_traits.h"
41 41
42 namespace rtc { 42 namespace rtc {
43 namespace safe_cmp {
44 43
45 namespace safe_cmp_impl { 44 namespace safe_cmp_impl {
46 45
47 template <size_t N> 46 template <size_t N>
48 struct LargerIntImpl : std::false_type {}; 47 struct LargerIntImpl : std::false_type {};
49 template <> 48 template <>
50 struct LargerIntImpl<sizeof(int8_t)> : std::true_type { 49 struct LargerIntImpl<sizeof(int8_t)> : std::true_type {
51 using type = int16_t; 50 using type = int16_t;
52 }; 51 };
53 template <> 52 template <>
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
146 RTC_SAFECMP_MAKE_OP(GtOp, >) 145 RTC_SAFECMP_MAKE_OP(GtOp, >)
147 RTC_SAFECMP_MAKE_OP(GeOp, >=) 146 RTC_SAFECMP_MAKE_OP(GeOp, >=)
148 #undef RTC_SAFECMP_MAKE_OP 147 #undef RTC_SAFECMP_MAKE_OP
149 148
150 } // namespace safe_cmp_impl 149 } // namespace safe_cmp_impl
151 150
152 #define RTC_SAFECMP_MAKE_FUN(name) \ 151 #define RTC_SAFECMP_MAKE_FUN(name) \
153 template <typename T1, typename T2> \ 152 template <typename T1, typename T2> \
154 constexpr \ 153 constexpr \
155 typename std::enable_if<IsIntlike<T1>::value && IsIntlike<T2>::value, \ 154 typename std::enable_if<IsIntlike<T1>::value && IsIntlike<T2>::value, \
156 bool>::type \ 155 bool>::type Safe##name(T1 a, T2 b) { \
157 name(T1 a, T2 b) { \
158 /* Unary plus here turns enums into real integral types. */ \ 156 /* Unary plus here turns enums into real integral types. */ \
159 return safe_cmp_impl::Cmp<safe_cmp_impl::name##Op>(+a, +b); \ 157 return safe_cmp_impl::Cmp<safe_cmp_impl::name##Op>(+a, +b); \
160 } \ 158 } \
161 template <typename T1, typename T2> \ 159 template <typename T1, typename T2> \
162 constexpr \ 160 constexpr \
163 typename std::enable_if<!IsIntlike<T1>::value || !IsIntlike<T2>::value, \ 161 typename std::enable_if<!IsIntlike<T1>::value || !IsIntlike<T2>::value, \
164 bool>::type \ 162 bool>::type Safe##name(const T1& a, \
165 name(const T1& a, const T2& b) { \ 163 const T2& b) { \
166 return safe_cmp_impl::name##Op::Op(a, b); \ 164 return safe_cmp_impl::name##Op::Op(a, b); \
167 } 165 }
168 RTC_SAFECMP_MAKE_FUN(Eq) 166 RTC_SAFECMP_MAKE_FUN(Eq)
169 RTC_SAFECMP_MAKE_FUN(Ne) 167 RTC_SAFECMP_MAKE_FUN(Ne)
170 RTC_SAFECMP_MAKE_FUN(Lt) 168 RTC_SAFECMP_MAKE_FUN(Lt)
171 RTC_SAFECMP_MAKE_FUN(Le) 169 RTC_SAFECMP_MAKE_FUN(Le)
172 RTC_SAFECMP_MAKE_FUN(Gt) 170 RTC_SAFECMP_MAKE_FUN(Gt)
173 RTC_SAFECMP_MAKE_FUN(Ge) 171 RTC_SAFECMP_MAKE_FUN(Ge)
174 #undef RTC_SAFECMP_MAKE_FUN 172 #undef RTC_SAFECMP_MAKE_FUN
175 173
176 } // namespace safe_cmp
177 } // namespace rtc 174 } // namespace rtc
178 175
179 #endif // WEBRTC_BASE_SAFE_COMPARE_H_ 176 #endif // WEBRTC_BASE_SAFE_COMPARE_H_
OLDNEW
« no previous file with comments | « webrtc/base/checks.h ('k') | webrtc/base/safe_compare_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698