From: Gabriel Corona Date: Tue, 12 Jul 2016 10:46:30 +0000 (+0200) Subject: [xbt] Move throwpoint out of xbt_ex in WithContextException X-Git-Tag: v3_14~773 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/fae10815035e4d0d150ff5fdbb7aa8fe25448c57 [xbt] Move throwpoint out of xbt_ex in WithContextException --- diff --git a/include/xbt/ex.hpp b/include/xbt/ex.hpp index 1afc1bba74..c1dcfb8a1a 100644 --- a/include/xbt/ex.hpp +++ b/include/xbt/ex.hpp @@ -48,19 +48,43 @@ #include +/** (Deprecated) Generic exception + * + * An error is defined by a category and a value within that category. + * + * This used to be a structure for C exceptions but it has been retrofitted + * as a C++ exception and some of its data has been moved in the + * WithContextException base class. We should deprecate it and replace it + * with either C++ different exceptions or `std::system_error` which already + * provides this (category + error code) logic. + */ struct XBT_PUBLIC() xbt_ex : public std::runtime_error, public simgrid::xbt::WithContextException { public: - xbt_ex() : std::runtime_error("") {} - xbt_ex(const char* message) : std::runtime_error(message) {} + + xbt_ex() : + std::runtime_error("") + {} + + /** + * + * @param throwpoint Throw point (use XBT_THROW_POINT) + * @param message Exception message + */ + xbt_ex(simgrid::xbt::ThrowPoint throwpoint, const char* message) : + std::runtime_error(message), + simgrid::xbt::WithContextException(throwpoint, simgrid::xbt::backtrace()) + {} + ~xbt_ex() override; - xbt_errcat_t category; /**< category like HTTP (what went wrong) */ - int value; /**< like errno (why did it went wrong) */ - const char *file; /**< Thrown point */ - int line; /**< Thrown point */ - const char *func; /**< Thrown point */ + /** Category (what went wrong) */ + xbt_errcat_t category; + + /** Why did it went wrong */ + int value; + }; #endif diff --git a/include/xbt/exception.hpp b/include/xbt/exception.hpp index c5d13584db..e0807e3353 100644 --- a/include/xbt/exception.hpp +++ b/include/xbt/exception.hpp @@ -22,6 +22,15 @@ namespace xbt { typedef std::vector Backtrace; +struct ThrowPoint { + ThrowPoint() {} + ThrowPoint(const char* file, int line, const char* function) : + file(file), line(line), function(function) {} + const char* file = nullptr; + int line = 0; + const char* function = nullptr; +}; + /** A polymorphic mixin class for adding context to an exception */ XBT_PUBLIC_CLASS WithContextException { public: @@ -35,6 +44,12 @@ public: procname_(xbt_procname()), pid_(xbt_getpid()) {} + WithContextException(ThrowPoint throwpoint, Backtrace bt) : + backtrace_(std::move(bt)), + procname_(xbt_procname()), + pid_(xbt_getpid()), + throwpoint_(throwpoint) + {} virtual ~WithContextException(); Backtrace const& backtrace() const { @@ -42,10 +57,12 @@ public: } int pid() const { return pid_; } std::string const& processName() const { return procname_; } + ThrowPoint& throwPoint() { return throwpoint_; } private: Backtrace backtrace_; std::string procname_; /**< Name of the process who thrown this */ int pid_; /**< PID of the process who thrown this */ + ThrowPoint throwpoint_; }; /** Internal class used to mixin the two classes */ @@ -53,12 +70,17 @@ template class WithContext : public E, public WithContextException { public: - WithContext(E exception) - : E(std::move(exception)) {} - WithContext(E exception, Backtrace backtrace) - : E(std::move(exception)), WithContextException(std::move(backtrace)) {} - WithContext(E exception, WithContextException context) - : E(std::move(exception)), WithContextException(std::move(context)) {} + WithContext(E exception) : + E(std::move(exception)) {} + WithContext(E exception, ThrowPoint throwpoint, Backtrace backtrace) : + E(std::move(exception)), + WithContextException(throwpoint, std::move(backtrace)) {} + WithContext(E exception, Backtrace backtrace) : + E(std::move(exception)), + WithContextException(std::move(backtrace)) {} + WithContext(E exception, WithContextException context) : + E(std::move(exception)), + WithContextException(std::move(context)) {} ~WithContext() override {} }; @@ -75,9 +97,25 @@ throwWithContext( // Thanks to the default argument, we are taking the backtrace in the caller: Backtrace backtrace = simgrid::xbt::backtrace()) { - throw WithContext(std::move(exception), std::move(backtrace)); + throw WithContext(std::move(exception), std::move(backtrace)); +} + +template +[[noreturn]] inline +typename std::enable_if< !std::is_base_of::value >::type +throwWithContext( + E exception, + ThrowPoint throwpoint, + // Thanks to the default argument, we are taking the backtrace in the caller: + Backtrace backtrace = simgrid::xbt::backtrace()) +{ + throw WithContext(std::move(exception), throwpoint, std::move(backtrace)); } +#define XBT_THROW_POINT ::simgrid::xbt::ThrowPoint(__FILE__, __LINE__, __func__) +#define XBT_THROW(e) \ + ::simgrid::xbt::throwWithContext(std::move(e), XBT_THROW_POINT) + } } diff --git a/src/bindings/java/JavaContext.cpp b/src/bindings/java/JavaContext.cpp index 2f63009980..8460d04187 100644 --- a/src/bindings/java/JavaContext.cpp +++ b/src/bindings/java/JavaContext.cpp @@ -82,12 +82,9 @@ JavaContext::JavaContext(std::function code, "Failed to create context #%d. You may want to switch to Java coroutines to increase your limits (error: %s)." "See the Install section of simgrid-java documentation (in doc/install.html) for more on coroutines.", thread_amount, ex.what()); - xbt_ex new_exception(str); + xbt_ex new_exception(XBT_THROW_POINT, str); new_exception.category = ex.category; new_exception.value = ex.value; - new_exception.file = __FILE__; - new_exception.line = __LINE__; - new_exception.func = __func__; std::throw_with_nested(std::move(new_exception)); } } else { diff --git a/src/simix/smx_private.h b/src/simix/smx_private.h index ad1ff192f1..108b770ecb 100644 --- a/src/simix/smx_private.h +++ b/src/simix/smx_private.h @@ -109,12 +109,9 @@ XBT_PUBLIC(void) SIMIX_clean(void); #define SMX_EXCEPTION(issuer, cat, val, msg) \ if (1) { \ smx_process_t _smx_throw_issuer = (issuer); /* evaluate only once */ \ - xbt_ex e(msg); \ + xbt_ex e(XBT_THROW_POINT, msg); \ e.category = cat; \ e.value = val; \ - e.file = __FILE__; \ - e.line = __LINE__; \ - e.func = __func__; \ _smx_throw_issuer->exception = std::make_exception_ptr(e); \ } else ((void)0) diff --git a/src/xbt/cunit.cpp b/src/xbt/cunit.cpp index 3be162f183..e9439c2c22 100644 --- a/src/xbt/cunit.cpp +++ b/src/xbt/cunit.cpp @@ -670,7 +670,7 @@ void _xbt_test_fail(const char *file, int line, const char *fmt, ...) void xbt_test_exception(xbt_ex_t e) { - _xbt_test_fail(e.file, e.line, "Exception %s raised: %s", xbt_ex_catname(e.category), e.what()); + _xbt_test_fail(e.throwPoint().file, e.throwPoint().line, "Exception %s raised: %s", xbt_ex_catname(e.category), e.what()); } void xbt_test_expect_failure(void) diff --git a/src/xbt/ex.cpp b/src/xbt/ex.cpp index 0c11397a21..516e0b630f 100644 --- a/src/xbt/ex.cpp +++ b/src/xbt/ex.cpp @@ -92,13 +92,10 @@ void xbt_throw( char* message, xbt_errcat_t errcat, int value, const char* file, int line, const char* func) { - xbt_ex e(message); + xbt_ex e(simgrid::xbt::ThrowPoint(file, line, func), message); free(message); e.category = errcat; e.value = value; - e.file = file; - e.line = line; - e.func = func; throw e; }