| 1 |
/////////////////////////////////////////////////////////////////////////////// |
| 2 |
// |
| 3 |
// Copyright (c) 2015 Microsoft Corporation. All rights reserved. |
| 4 |
// |
| 5 |
// This code is licensed under the MIT License (MIT). |
| 6 |
// |
| 7 |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 8 |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 9 |
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 10 |
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 11 |
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 12 |
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 13 |
// THE SOFTWARE. |
| 14 |
// |
| 15 |
/////////////////////////////////////////////////////////////////////////////// |
| 16 |
|
| 17 |
#ifndef GSL_NARROW_H |
| 18 |
#define GSL_NARROW_H |
| 19 |
#include "./assert" // for GSL_SUPPRESS |
| 20 |
#include "./util" // for narrow_cast |
| 21 |
#include <exception> // for std::exception |
| 22 |
namespace gsl |
| 23 |
{ |
| 24 |
struct narrowing_error : public std::exception |
| 25 |
{ |
| 26 |
const char* what() const noexcept override { return "narrowing_error"; } |
| 27 |
}; |
| 28 |
|
| 29 |
// narrow() : a checked version of narrow_cast() that throws if the cast changed the value |
| 30 |
template <class T, class U, typename std::enable_if<std::is_arithmetic<T>::value>::type* = nullptr> |
| 31 |
// clang-format off |
| 32 |
GSL_SUPPRESS(type.1) // NO-FORMAT: attribute |
| 33 |
GSL_SUPPRESS(es.46) // NO-FORMAT: attribute // The warning suggests that a floating->unsigned conversion can occur |
| 34 |
// in the static_cast below, and that gsl::narrow should be used instead. |
| 35 |
// Suppress this warning, since gsl::narrow is defined in terms of |
| 36 |
// static_cast |
| 37 |
// clang-format on |
| 38 |
constexpr T narrow(U u) |
| 39 |
{ |
| 40 |
constexpr const bool is_different_signedness = |
| 41 |
(std::is_signed<T>::value != std::is_signed<U>::value); |
| 42 |
|
| 43 |
GSL_SUPPRESS(es.103) // NO-FORMAT: attribute // don't overflow |
| 44 |
GSL_SUPPRESS(es.104) // NO-FORMAT: attribute // don't underflow |
| 45 |
GSL_SUPPRESS(p.2) // NO-FORMAT: attribute // don't rely on undefined behavior |
| 46 |
const T t = narrow_cast<T>(u); // While this is technically undefined behavior in some cases (i.e., if the source value is of floating-point type |
| 47 |
// and cannot fit into the destination integral type), the resultant behavior is benign on the platforms |
| 48 |
// that we target (i.e., no hardware trap representations are hit). |
| 49 |
|
| 50 |
#if defined(__clang__) || defined(__GNUC__) |
| 51 |
#pragma GCC diagnostic push |
| 52 |
#pragma GCC diagnostic ignored "-Wfloat-equal" |
| 53 |
#endif |
| 54 |
// Note: NaN will always throw, since NaN != NaN |
| 55 |
if (static_cast<U>(t) != u || (is_different_signedness && ((t < T{}) != (u < U{})))) |
| 56 |
{ |
| 57 |
throw narrowing_error{}; |
| 58 |
} |
| 59 |
#if defined(__clang__) || defined(__GNUC__) |
| 60 |
#pragma GCC diagnostic pop |
| 61 |
#endif |
| 62 |
|
| 63 |
return t; |
| 64 |
} |
| 65 |
|
| 66 |
template <class T, class U, typename std::enable_if<!std::is_arithmetic<T>::value>::type* = nullptr> |
| 67 |
// clang-format off |
| 68 |
GSL_SUPPRESS(type.1) // NO-FORMAT: attribute |
| 69 |
// clang-format on |
| 70 |
constexpr T narrow(U u) |
| 71 |
{ |
| 72 |
const T t = narrow_cast<T>(u); |
| 73 |
|
| 74 |
if (static_cast<U>(t) != u) |
| 75 |
{ |
| 76 |
throw narrowing_error{}; |
| 77 |
} |
| 78 |
|
| 79 |
return t; |
| 80 |
} |
| 81 |
} // namespace gsl |
| 82 |
#endif // GSL_NARROW_H |