mirror of
https://github.com/microsoft/GSL.git
synced 2024-11-03 17:56:43 -05:00
Merge pull request #873 from londey/narrow-throw
Changed implementation of gsl::narrow to throw gsl::narrowing_error
This commit is contained in:
commit
601b55ea61
@ -96,6 +96,10 @@ constexpr T narrow_cast(U&& u) noexcept
|
|||||||
return static_cast<T>(std::forward<U>(u));
|
return static_cast<T>(std::forward<U>(u));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct narrowing_error : public std::exception
|
||||||
|
{
|
||||||
|
};
|
||||||
|
|
||||||
namespace details
|
namespace details
|
||||||
{
|
{
|
||||||
template <class T, class U>
|
template <class T, class U>
|
||||||
@ -115,9 +119,9 @@ constexpr
|
|||||||
T narrow(U u) noexcept(false)
|
T narrow(U u) noexcept(false)
|
||||||
{
|
{
|
||||||
T t = narrow_cast<T>(u);
|
T t = narrow_cast<T>(u);
|
||||||
if (static_cast<U>(t) != u) gsl::details::terminate();
|
if (static_cast<U>(t) != u) throw narrowing_error{};
|
||||||
if (!details::is_same_signedness<T, U>::value && ((t < T{}) != (u < U{})))
|
if (!details::is_same_signedness<T, U>::value && ((t < T{}) != (u < U{})))
|
||||||
gsl::details::terminate();
|
throw narrowing_error{};
|
||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -19,7 +19,6 @@
|
|||||||
|
|
||||||
#include <gsl/gsl_assert> // for Expects
|
#include <gsl/gsl_assert> // for Expects
|
||||||
#include <gsl/gsl_byte> // for byte
|
#include <gsl/gsl_byte> // for byte
|
||||||
#include <gsl/gsl_util> // for narrow_cast, narrow
|
|
||||||
|
|
||||||
#include <array> // for array
|
#include <array> // for array
|
||||||
#include <cstddef> // for ptrdiff_t, size_t, nullptr_t
|
#include <cstddef> // for ptrdiff_t, size_t, nullptr_t
|
||||||
|
@ -264,5 +264,4 @@ function(add_gsl_test_noexcept name)
|
|||||||
set_property(TARGET ${name} PROPERTY FOLDER "GSL_tests_noexcept")
|
set_property(TARGET ${name} PROPERTY FOLDER "GSL_tests_noexcept")
|
||||||
endfunction()
|
endfunction()
|
||||||
|
|
||||||
add_gsl_test_noexcept(no_exception_throw_tests)
|
|
||||||
add_gsl_test_noexcept(no_exception_ensure_tests)
|
add_gsl_test_noexcept(no_exception_ensure_tests)
|
||||||
|
@ -1,48 +0,0 @@
|
|||||||
///////////////////////////////////////////////////////////////////////////////
|
|
||||||
//
|
|
||||||
// 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.
|
|
||||||
//
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
#include <cstdlib> // for std::exit
|
|
||||||
#include <gsl/gsl_assert> // for get_terminate
|
|
||||||
#include <gsl/gsl_util> // for narrow
|
|
||||||
|
|
||||||
int narrow_no_throw()
|
|
||||||
{
|
|
||||||
const long long bigNumber = 0x0fffffffffffffff;
|
|
||||||
return gsl::narrow<int>(bigNumber);
|
|
||||||
}
|
|
||||||
|
|
||||||
[[noreturn]] void test_terminate() { std::exit(0); }
|
|
||||||
|
|
||||||
void setup_termination_handler() noexcept
|
|
||||||
{
|
|
||||||
#if defined(GSL_MSVC_USE_STL_NOEXCEPTION_WORKAROUND)
|
|
||||||
|
|
||||||
auto& handler = gsl::details::get_terminate_handler();
|
|
||||||
handler = &test_terminate;
|
|
||||||
|
|
||||||
#else
|
|
||||||
|
|
||||||
std::set_terminate(test_terminate);
|
|
||||||
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
int main()
|
|
||||||
{
|
|
||||||
setup_termination_handler();
|
|
||||||
narrow_no_throw();
|
|
||||||
return -1;
|
|
||||||
}
|
|
@ -29,7 +29,6 @@ using namespace gsl;
|
|||||||
|
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
static constexpr char deathstring[] = "Expected Death";
|
|
||||||
void f(int& i) { i += 1; }
|
void f(int& i) { i += 1; }
|
||||||
static int j = 0;
|
static int j = 0;
|
||||||
void g() { j += 1; }
|
void g() { j += 1; }
|
||||||
@ -104,17 +103,12 @@ TEST(utils_tests, narrow_cast)
|
|||||||
|
|
||||||
TEST(utils_tests, narrow)
|
TEST(utils_tests, narrow)
|
||||||
{
|
{
|
||||||
std::set_terminate([] {
|
|
||||||
std::cerr << "Expected Death. narrow";
|
|
||||||
std::abort();
|
|
||||||
});
|
|
||||||
|
|
||||||
int n = 120;
|
int n = 120;
|
||||||
const char c = narrow<char>(n);
|
const char c = narrow<char>(n);
|
||||||
EXPECT_TRUE(c == 120);
|
EXPECT_TRUE(c == 120);
|
||||||
|
|
||||||
n = 300;
|
n = 300;
|
||||||
EXPECT_DEATH(narrow<char>(n), deathstring);
|
EXPECT_THROW(narrow<char>(n), narrowing_error);
|
||||||
|
|
||||||
const auto int32_max = std::numeric_limits<int32_t>::max();
|
const auto int32_max = std::numeric_limits<int32_t>::max();
|
||||||
const auto int32_min = std::numeric_limits<int32_t>::min();
|
const auto int32_min = std::numeric_limits<int32_t>::min();
|
||||||
@ -123,14 +117,13 @@ TEST(utils_tests, narrow)
|
|||||||
EXPECT_TRUE(narrow<uint32_t>(int32_t(1)) == 1);
|
EXPECT_TRUE(narrow<uint32_t>(int32_t(1)) == 1);
|
||||||
EXPECT_TRUE(narrow<uint32_t>(int32_max) == static_cast<uint32_t>(int32_max));
|
EXPECT_TRUE(narrow<uint32_t>(int32_max) == static_cast<uint32_t>(int32_max));
|
||||||
|
|
||||||
EXPECT_DEATH(narrow<uint32_t>(int32_t(-1)), deathstring);
|
EXPECT_THROW(narrow<uint32_t>(int32_t(-1)), narrowing_error);
|
||||||
EXPECT_DEATH(narrow<uint32_t>(int32_min), deathstring);
|
EXPECT_THROW(narrow<uint32_t>(int32_min), narrowing_error);
|
||||||
|
|
||||||
n = -42;
|
n = -42;
|
||||||
EXPECT_DEATH(narrow<unsigned>(n), deathstring);
|
EXPECT_THROW(narrow<unsigned>(n), narrowing_error);
|
||||||
|
|
||||||
#if GSL_CONSTEXPR_NARROW
|
#if GSL_CONSTEXPR_NARROW
|
||||||
static_assert(narrow<char>(120) == 120, "Fix GSL_CONSTEXPR_NARROW");
|
static_assert(narrow<char>(120) == 120, "Fix GSL_CONSTEXPR_NARROW");
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user