 Chromium Code Reviews
 Chromium Code Reviews Issue 2810483002:
  Add SafeMin() and SafeMax(), which accept args of different types  (Closed)
    
  
    Issue 2810483002:
  Add SafeMin() and SafeMax(), which accept args of different types  (Closed) 
  | OLD | NEW | 
|---|---|
| (Empty) | |
| 1 /* | |
| 2 * Copyright 2017 The WebRTC Project Authors. All rights reserved. | |
| 3 * | |
| 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 | |
| 6 * tree. An additional intellectual property rights grant can be found | |
| 7 * in the file PATENTS. All contributing project authors may | |
| 8 * be found in the AUTHORS file in the root of the source tree. | |
| 9 */ | |
| 10 | |
| 11 // Minimum and maximum | |
| 12 // =================== | |
| 13 // | |
| 14 // rtc::SafeMin(x, y) | |
| 15 // rtc::SafeMax(x, y) | |
| 16 // | |
| 17 // Accept two arguments of any mix of integral and floating-point types, and | |
| 18 // return the smaller and larger value, respectively, with no truncation or | |
| 19 // wrap-around. If only one of the input types is statically guaranteed to be | |
| 20 // able to represent the result, the return type is that type; if either one | |
| 21 // would do, the result type is the smaller type. (One of these two cases | |
| 22 // always applies.) | |
| 23 // | |
| 24 // Requesting a specific return type | |
| 25 // ================================= | |
| 26 // | |
| 27 // Both functions allow callers to explicitly specify the return type as a | |
| 28 // template parameter, overriding the default return type. E.g. | |
| 29 // | |
| 30 // rtc::SafeMin<int>(x, y) // returns an int | |
| 31 // | |
| 32 // If the requested type is statically guaranteed to be able to represent the | |
| 33 // result, then everything's fine, and the return type is as requested. But if | |
| 34 // the requested type is too small, a static_assert is triggered. | |
| 35 | |
| 36 #ifndef WEBRTC_BASE_SAFE_MINMAX_H_ | |
| 37 #define WEBRTC_BASE_SAFE_MINMAX_H_ | |
| 38 | |
| 39 #include <limits> | |
| 40 #include <type_traits> | |
| 41 | |
| 42 #include "webrtc/base/checks.h" | |
| 43 #include "webrtc/base/safe_compare.h" | |
| 44 #include "webrtc/base/type_traits.h" | |
| 45 | |
| 46 namespace rtc { | |
| 47 | |
| 48 namespace safe_minmax_impl { | |
| 49 | |
| 50 // Make the range of a type available via something other than a constexpr | |
| 51 // function, to work around MSVC limitations. See | |
| 52 // https://blogs.msdn.microsoft.com/vcblog/2015/12/02/partial-support-for-expres sion-sfinae-in-vs-2015-update-1/ | |
| 53 template <typename T> | |
| 54 struct Limits { | |
| 55 static constexpr T lowest = std::numeric_limits<T>::lowest(); | |
| 56 static constexpr T max = std::numeric_limits<T>::max(); | |
| 57 }; | |
| 58 | |
| 59 // Given two types T1 and T2, find types that can hold the smallest (in | |
| 60 // ::min_t) and the largest (in ::max_t) of the two values. | |
| 61 template <typename T1, | |
| 62 typename T2, | |
| 63 bool all_int = IsIntlike<T1>::value&& IsIntlike<T2>::value> | |
| 
aleloi
2017/04/10 11:40:09
IsIntLike seems to also handle int-based enums. Is
 
ossu
2017/04/10 13:50:05
+Space before &&, it's not an rvalue-reference but
 
kwiberg-webrtc
2017/04/12 18:21:20
Yes, a unit test with enum values would be good. W
 
kwiberg-webrtc
2017/04/12 18:21:21
I agree. Unfortunately, clang-format does not. OK
 | |
| 64 struct MType; | |
| 65 | |
| 66 // Specialization for when at least one of the types is floating-point. | |
| 67 template <typename T1, typename T2> | |
| 68 struct MType<T1, T2, false> { | |
| 69 using min_t = typename std::common_type<T1, T2>::type; | |
| 70 static_assert(std::is_same<min_t, T1>::value || | |
| 71 std::is_same<min_t, T2>::value, | |
| 72 ""); | |
| 73 | |
| 74 using max_t = typename std::common_type<T1, T2>::type; | |
| 75 static_assert(std::is_same<max_t, T1>::value || | |
| 76 std::is_same<max_t, T2>::value, | |
| 77 ""); | |
| 78 }; | |
| 79 | |
| 80 // Specialization for when both types are integral. | |
| 81 template <typename T1, typename T2> | |
| 82 struct MType<T1, T2, true> { | |
| 83 // The type with the lowest minimum value. In case of a tie, the type with | |
| 84 // the lowest maximum value. In case that too is a tie, the types have the | |
| 85 // same range, and we arbitrarily pick T1. | |
| 86 using min_t = typename std::conditional< | |
| 87 safe_cmp::Lt(Limits<T1>::lowest, Limits<T2>::lowest), | |
| 88 T1, | |
| 89 typename std::conditional< | |
| 90 safe_cmp::Gt(Limits<T1>::lowest, Limits<T2>::lowest), | |
| 91 T2, | |
| 92 typename std::conditional<safe_cmp::Le(Limits<T1>::max, | |
| 93 Limits<T2>::max), | |
| 94 T1, | |
| 95 T2>::type>::type>::type; | |
| 96 static_assert(std::is_same<min_t, T1>::value || | |
| 97 std::is_same<min_t, T2>::value, | |
| 98 ""); | |
| 99 | |
| 100 // The type with the highest maximum value. In case of a tie, the types have | |
| 101 // the same range (because in C++, integer types with the same maximum also | |
| 102 // have the same minimum). | |
| 103 static_assert(safe_cmp::Ne(Limits<T1>::max, Limits<T2>::max) || | |
| 104 safe_cmp::Eq(Limits<T1>::lowest, Limits<T2>::lowest), | |
| 105 "integer types with the same max should have the same min"); | |
| 106 using max_t = typename std:: | |
| 107 conditional<safe_cmp::Ge(Limits<T1>::max, Limits<T2>::max), T1, T2>::type; | |
| 108 static_assert(std::is_same<max_t, T1>::value || | |
| 109 std::is_same<max_t, T2>::value, | |
| 110 ""); | |
| 111 }; | |
| 112 | |
| 113 // A dummy type that we pass around at compile time but never actually use. | |
| 114 // Declared but not defined. | |
| 115 struct DefaultType; | |
| 
aleloi
2017/04/10 11:40:09
Is this a common pattern? Is there something that
 
kwiberg-webrtc
2017/04/12 18:21:20
Dunno. I haven't seen it before, I think.
 | |
| 116 | |
| 117 // ::type is A, except we fall back to B if A is DefaultType. We static_assert | |
| 118 // that the chosen type can hold all values that B can hold. | |
| 119 template <typename A, typename B> | |
| 120 struct TypeOr { | |
| 121 using type = typename std:: | |
| 122 conditional<std::is_same<A, DefaultType>::value, B, A>::type; | |
| 123 static_assert(safe_cmp::Le(Limits<type>::lowest, Limits<B>::lowest) && | |
| 124 safe_cmp::Ge(Limits<type>::max, Limits<B>::max), | |
| 125 "The specified type isn't large enough"); | |
| 126 }; | |
| 127 | |
| 128 } // namespace safe_minmax_impl | |
| 129 | |
| 130 template <typename R = safe_minmax_impl::DefaultType, | |
| 131 typename T1 = safe_minmax_impl::DefaultType, | |
| 132 typename T2 = safe_minmax_impl::DefaultType, | |
| 
aleloi
2017/04/10 11:40:09
I wonder if the function signature can be made sim
 
kwiberg-webrtc
2017/04/12 18:21:21
The only other good-ish alternative I see is to no
 | |
| 133 typename R2 = typename safe_minmax_impl:: | |
| 134 TypeOr<R, typename safe_minmax_impl::MType<T1, T2>::min_t>::type> | |
| 135 constexpr R2 SafeMin(T1 a, T2 b) { | |
| 136 static_assert(IsIntlike<T1>::value || std::is_floating_point<T1>::value, | |
| 137 "The first argument must be integral or floating-point"); | |
| 138 static_assert(IsIntlike<T2>::value || std::is_floating_point<T2>::value, | |
| 139 "The second argument must be integral or floating-point"); | |
| 140 return safe_cmp::Lt(a, b) ? static_cast<R2>(a) : static_cast<R2>(b); | |
| 141 } | |
| 142 | |
| 143 template <typename R = safe_minmax_impl::DefaultType, | |
| 144 typename T1 = safe_minmax_impl::DefaultType, | |
| 145 typename T2 = safe_minmax_impl::DefaultType, | |
| 146 typename R2 = typename safe_minmax_impl:: | |
| 147 TypeOr<R, typename safe_minmax_impl::MType<T1, T2>::max_t>::type> | |
| 148 constexpr R2 SafeMax(T1 a, T2 b) { | |
| 
aleloi
2017/04/10 11:40:09
Same comment as above.
 | |
| 149 static_assert(IsIntlike<T1>::value || std::is_floating_point<T1>::value, | |
| 150 "The first argument must be integral or floating-point"); | |
| 151 static_assert(IsIntlike<T2>::value || std::is_floating_point<T2>::value, | |
| 152 "The second argument must be integral or floating-point"); | |
| 153 return safe_cmp::Gt(a, b) ? static_cast<R2>(a) : static_cast<R2>(b); | |
| 154 } | |
| 155 | |
| 156 } // namespace rtc | |
| 157 | |
| 158 #endif // WEBRTC_BASE_SAFE_MINMAX_H_ | |
| OLD | NEW |