From 28ec4ea69984c823e18b06c235ecb8f789613724 Mon Sep 17 00:00:00 2001 From: vjacquet Date: Sun, 26 Jun 2016 23:58:13 +0200 Subject: [PATCH] Differentiating exception type for Expects and Ensures Expects throw std::logic_error when Ensures throws std::runtime_error. Uses multiple inheritance so that the exception thrown is both the standard and the gsl::fail_fast. --- include/gsl_assert.h | 21 +++++++++++++++++---- tests/assertion_tests.cpp | 28 +++++++++++++++++++++++++++- 2 files changed, 44 insertions(+), 5 deletions(-) diff --git a/include/gsl_assert.h b/include/gsl_assert.h index 51e8ab6..17a2a75 100644 --- a/include/gsl_assert.h +++ b/include/gsl_assert.h @@ -45,18 +45,31 @@ namespace gsl { -struct fail_fast : public std::runtime_error +struct fail_fast { - explicit fail_fast(char const* const message) : std::runtime_error(message) {} + virtual char const* what() const = 0; }; + +namespace details +{ + template + struct contract_violation : public E, public fail_fast + { + explicit contract_violation(char const* const message) : E(message) {} + virtual char const* what() const override + { + return E::what(); + } + }; +} } #if defined(GSL_THROW_ON_CONTRACT_VIOLATION) #define Expects(cond) if (!(cond)) \ - throw gsl::fail_fast("GSL: Precondition failure at " __FILE__ ": " GSL_STRINGIFY(__LINE__)); + throw gsl::details::contract_violation("GSL: Precondition failure at " __FILE__ ": " GSL_STRINGIFY(__LINE__)); #define Ensures(cond) if (!(cond)) \ - throw gsl::fail_fast("GSL: Postcondition failure at " __FILE__ ": " GSL_STRINGIFY(__LINE__)); + throw gsl::details::contract_violation("GSL: Postcondition failure at " __FILE__ ": " GSL_STRINGIFY(__LINE__)); #elif defined(GSL_TERMINATE_ON_CONTRACT_VIOLATION) diff --git a/tests/assertion_tests.cpp b/tests/assertion_tests.cpp index acd381a..cdcd2e0 100644 --- a/tests/assertion_tests.cpp +++ b/tests/assertion_tests.cpp @@ -31,6 +31,19 @@ SUITE(assertion_tests) { CHECK(f(2) == 2); CHECK_THROW(f(10), fail_fast); + try { f(10); } + catch (fail_fast const& e) { + cstring_span<> prefix = ensure_z("GSL: Precondition failure"); + cstring_span<> what = ensure_z(e.what()).first(prefix.length()); + CHECK(prefix == what); + } + CHECK_THROW(f(10), std::logic_error); + try { f(10); } + catch (std::logic_error const& e) { + cstring_span<> prefix = ensure_z("GSL: Precondition failure"); + cstring_span<> what = ensure_z(e.what()).first(prefix.length()); + CHECK(prefix == what); + } } int g(int i) @@ -44,7 +57,20 @@ SUITE(assertion_tests) { CHECK(g(2) == 3); CHECK_THROW(g(9), fail_fast); - } + try { g(9); } + catch (fail_fast const& e) { + cstring_span<> prefix = ensure_z("GSL: Postcondition failure"); + cstring_span<> what = ensure_z(e.what()).first(prefix.length()); + CHECK(prefix == what); + } + CHECK_THROW(g(9), std::runtime_error); + try { g(9); } + catch (std::runtime_error const& e) { + cstring_span<> prefix = ensure_z("GSL: Postcondition failure"); + cstring_span<> what = ensure_z(e.what()).first(prefix.length()); + CHECK(prefix == what); + } + } } int main(int, const char *[])