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.
This commit is contained in:
vjacquet 2016-06-26 23:58:13 +02:00
parent a3fde02d06
commit 28ec4ea699
2 changed files with 44 additions and 5 deletions

View File

@ -45,18 +45,31 @@
namespace gsl 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<typename E>
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) #if defined(GSL_THROW_ON_CONTRACT_VIOLATION)
#define Expects(cond) if (!(cond)) \ #define Expects(cond) if (!(cond)) \
throw gsl::fail_fast("GSL: Precondition failure at " __FILE__ ": " GSL_STRINGIFY(__LINE__)); throw gsl::details::contract_violation<std::logic_error>("GSL: Precondition failure at " __FILE__ ": " GSL_STRINGIFY(__LINE__));
#define Ensures(cond) if (!(cond)) \ #define Ensures(cond) if (!(cond)) \
throw gsl::fail_fast("GSL: Postcondition failure at " __FILE__ ": " GSL_STRINGIFY(__LINE__)); throw gsl::details::contract_violation<std::runtime_error>("GSL: Postcondition failure at " __FILE__ ": " GSL_STRINGIFY(__LINE__));
#elif defined(GSL_TERMINATE_ON_CONTRACT_VIOLATION) #elif defined(GSL_TERMINATE_ON_CONTRACT_VIOLATION)

View File

@ -31,6 +31,19 @@ SUITE(assertion_tests)
{ {
CHECK(f(2) == 2); CHECK(f(2) == 2);
CHECK_THROW(f(10), fail_fast); 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) int g(int i)
@ -44,7 +57,20 @@ SUITE(assertion_tests)
{ {
CHECK(g(2) == 3); CHECK(g(2) == 3);
CHECK_THROW(g(9), fail_fast); 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 *[]) int main(int, const char *[])