2020-10-29 20:38:48 -04:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Copyright (c) 2015 Microsoft Corporation. All rights reserved.
|
|
|
|
//
|
|
|
|
// This code is licensed under the MIT License (MIT).
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
// THE SOFTWARE.
|
|
|
|
//
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#ifndef GSL_NARROW_H
|
|
|
|
#define GSL_NARROW_H
|
2022-04-28 12:49:10 -04:00
|
|
|
#include "assert" // for GSL_SUPPRESS
|
2022-04-26 17:41:10 -04:00
|
|
|
#include "util" // for narrow_cast
|
|
|
|
#include <exception> // for std::exception
|
2020-10-29 20:38:48 -04:00
|
|
|
namespace gsl
|
|
|
|
{
|
|
|
|
struct narrowing_error : public std::exception
|
|
|
|
{
|
|
|
|
const char* what() const noexcept override { return "narrowing_error"; }
|
|
|
|
};
|
|
|
|
|
|
|
|
// narrow() : a checked version of narrow_cast() that throws if the cast changed the value
|
2021-06-23 18:28:45 -04:00
|
|
|
template <class T, class U, typename std::enable_if<std::is_arithmetic<T>::value>::type* = nullptr>
|
2020-10-29 20:38:48 -04:00
|
|
|
// clang-format off
|
|
|
|
GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
|
|
|
|
GSL_SUPPRESS(f.6) // NO-FORMAT: attribute // TODO: MSVC /analyze does not recognise noexcept(false)
|
2021-05-20 21:18:08 -04:00
|
|
|
// clang-format on
|
2020-10-29 20:38:48 -04:00
|
|
|
constexpr T narrow(U u) noexcept(false)
|
|
|
|
{
|
|
|
|
constexpr const bool is_different_signedness =
|
|
|
|
(std::is_signed<T>::value != std::is_signed<U>::value);
|
|
|
|
|
2022-01-26 19:44:07 -05:00
|
|
|
GSL_SUPPRESS(es.103) // NO-FORMAT: attribute // don't overflow
|
|
|
|
GSL_SUPPRESS(es.104) // NO-FORMAT: attribute // don't underflow
|
|
|
|
GSL_SUPPRESS(p.2) // NO-FORMAT: attribute // don't rely on undefined behavior
|
|
|
|
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
|
|
|
|
// and cannot fit into the destination integral type), the resultant behavior is benign on the platforms
|
|
|
|
// that we target (i.e., no hardware trap representations are hit).
|
2020-10-29 20:38:48 -04:00
|
|
|
|
2022-04-14 14:08:28 -04:00
|
|
|
#if defined(__clang__) || defined(__GNUC__)
|
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wfloat-equal"
|
|
|
|
#endif
|
2020-10-29 20:38:48 -04:00
|
|
|
if (static_cast<U>(t) != u || (is_different_signedness && ((t < T{}) != (u < U{}))))
|
|
|
|
{
|
|
|
|
throw narrowing_error{};
|
|
|
|
}
|
2022-04-14 14:08:28 -04:00
|
|
|
#if defined(__clang__) || defined(__GNUC__)
|
|
|
|
#pragma GCC diagnostic pop
|
|
|
|
#endif
|
2020-10-29 20:38:48 -04:00
|
|
|
|
|
|
|
return t;
|
|
|
|
}
|
2021-06-23 18:28:45 -04:00
|
|
|
|
|
|
|
template <class T, class U, typename std::enable_if<!std::is_arithmetic<T>::value>::type* = nullptr>
|
|
|
|
// clang-format off
|
|
|
|
GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
|
|
|
|
GSL_SUPPRESS(f.6) // NO-FORMAT: attribute // TODO: MSVC /analyze does not recognise noexcept(false)
|
|
|
|
// clang-format on
|
|
|
|
constexpr T narrow(U u) noexcept(false)
|
|
|
|
{
|
|
|
|
const T t = narrow_cast<T>(u);
|
|
|
|
|
|
|
|
if (static_cast<U>(t) != u)
|
|
|
|
{
|
|
|
|
throw narrowing_error{};
|
|
|
|
}
|
|
|
|
|
|
|
|
return t;
|
|
|
|
}
|
2020-10-29 20:38:48 -04:00
|
|
|
} // namespace gsl
|
|
|
|
#endif // GSL_NARROW_H
|