Warning, /include/gsl/narrow is written in an unsupported language. File is not indexed.
0001 ///////////////////////////////////////////////////////////////////////////////
0002 //
0003 // Copyright (c) 2015 Microsoft Corporation. All rights reserved.
0004 //
0005 // This code is licensed under the MIT License (MIT).
0006 //
0007 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0008 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0009 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
0010 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
0011 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
0012 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
0013 // THE SOFTWARE.
0014 //
0015 ///////////////////////////////////////////////////////////////////////////////
0016
0017 #ifndef GSL_NARROW_H
0018 #define GSL_NARROW_H
0019 #include "./assert" // for GSL_SUPPRESS
0020 #include "./util" // for narrow_cast
0021 #include <exception> // for std::exception
0022 namespace gsl
0023 {
0024 namespace details
0025 {
0026 template <class T, class U>
0027 constexpr bool static_cast_is_defined(U u, /*is_floating_point_to_integral=*/std::true_type)
0028 {
0029 if (std::is_same<typename std::remove_cv<T>::type, bool>::value) { return true; }
0030
0031 U upper_bound{1};
0032 for (int i = 0; i < std::numeric_limits<T>::digits; i++)
0033 {
0034 upper_bound *= std::numeric_limits<T>::radix;
0035 }
0036
0037 if (u >= U{}) { return u < upper_bound; }
0038 if (!std::is_signed<T>::value) { return u > U{-1}; }
0039
0040 return u + upper_bound > U{-1};
0041 }
0042
0043 template <class T, class U>
0044 constexpr bool static_cast_is_defined(U, /*is_floating_point_to_integral=*/std::false_type)
0045 {
0046 return true;
0047 }
0048 } // namespace details
0049
0050 struct narrowing_error : public std::exception
0051 {
0052 const char* what() const noexcept override { return "narrowing_error"; }
0053 };
0054
0055 // narrow() : a checked version of narrow_cast() that throws if the cast changed the value
0056 template <class T, class U, typename std::enable_if<std::is_arithmetic<T>::value>::type* = nullptr>
0057 GSL_SUPPRESS(type.1) constexpr T narrow(U u)
0058 {
0059 constexpr const bool is_different_signedness =
0060 (std::is_signed<T>::value != std::is_signed<U>::value);
0061
0062 using is_floating_point_to_integral =
0063 std::integral_constant<bool,
0064 std::is_integral<T>::value && std::is_floating_point<U>::value>;
0065 if (!details::static_cast_is_defined<T, U>(u, is_floating_point_to_integral{}))
0066 {
0067 throw narrowing_error{};
0068 }
0069
0070 GSL_SUPPRESS(es.103) // don't overflow
0071 GSL_SUPPRESS(es.104) // don't underflow
0072 const T t = narrow_cast<T>(u);
0073
0074 #if defined(__clang__) || defined(__GNUC__)
0075 #pragma GCC diagnostic push
0076 #pragma GCC diagnostic ignored "-Wfloat-equal"
0077 #endif
0078 // Note: NaN will always throw, since NaN != NaN
0079 if (static_cast<U>(t) != u || (is_different_signedness && ((t < T{}) != (u < U{}))))
0080 {
0081 throw narrowing_error{};
0082 }
0083 #if defined(__clang__) || defined(__GNUC__)
0084 #pragma GCC diagnostic pop
0085 #endif
0086
0087 return t;
0088 }
0089
0090 template <class T, class U, typename std::enable_if<!std::is_arithmetic<T>::value>::type* = nullptr>
0091 GSL_SUPPRESS(type.1) constexpr T narrow(U u)
0092 {
0093 const T t = narrow_cast<T>(u);
0094
0095 if (static_cast<U>(t) != u) { throw narrowing_error{}; }
0096
0097 return t;
0098 }
0099 } // namespace gsl
0100 #endif // GSL_NARROW_H