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 3 // found in the LICENSE file. 4 5 #ifndef PDFIUM_THIRD_PARTY_BASE_SAFE_CONVERSIONS_H_ 6 #define PDFIUM_THIRD_PARTY_BASE_SAFE_CONVERSIONS_H_ 7 8 #include <limits> 9 10 #include "safe_conversions_impl.h" 11 #include "third_party/base/logging.h" 12 13 namespace pdfium { 14 namespace base { 15 16 // Convenience function that returns true if the supplied value is in range 17 // for the destination type. 18 template <typename Dst, typename Src> IsValueInRangeForNumericType(Src value)19inline bool IsValueInRangeForNumericType(Src value) { 20 return internal::DstRangeRelationToSrcRange<Dst>(value) == 21 internal::RANGE_VALID; 22 } 23 24 // checked_cast<> is analogous to static_cast<> for numeric types, 25 // except that it CHECKs that the specified numeric conversion will not 26 // overflow or underflow. NaN source will always trigger a CHECK. 27 template <typename Dst, typename Src> checked_cast(Src value)28inline Dst checked_cast(Src value) { 29 CHECK(IsValueInRangeForNumericType<Dst>(value)); 30 return static_cast<Dst>(value); 31 } 32 33 // saturated_cast<> is analogous to static_cast<> for numeric types, except 34 // that the specified numeric conversion will saturate rather than overflow or 35 // underflow. NaN assignment to an integral will trigger a CHECK condition. 36 template <typename Dst, typename Src> saturated_cast(Src value)37inline Dst saturated_cast(Src value) { 38 // Optimization for floating point values, which already saturate. 39 if (std::numeric_limits<Dst>::is_iec559) 40 return static_cast<Dst>(value); 41 42 switch (internal::DstRangeRelationToSrcRange<Dst>(value)) { 43 case internal::RANGE_VALID: 44 return static_cast<Dst>(value); 45 46 case internal::RANGE_UNDERFLOW: 47 return std::numeric_limits<Dst>::min(); 48 49 case internal::RANGE_OVERFLOW: 50 return std::numeric_limits<Dst>::max(); 51 52 // Should fail only on attempting to assign NaN to a saturated integer. 53 case internal::RANGE_INVALID: 54 CHECK(false); 55 return std::numeric_limits<Dst>::max(); 56 } 57 58 NOTREACHED(); 59 return static_cast<Dst>(value); 60 } 61 62 } // namespace base 63 } // namespace pdfium 64 65 #endif // PDFIUM_THIRD_PARTY_BASE_SAFE_CONVERSIONS_H_ 66 67