X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/a5c5765ba9e8336b55101d248a8f43fbf9cd0c0c..5a00610e05eab122e23f56b589d5bb240e6ceda7:/include/xbt/exception.hpp diff --git a/include/xbt/exception.hpp b/include/xbt/exception.hpp index c5d13584db..4391ffec6d 100644 --- a/include/xbt/exception.hpp +++ b/include/xbt/exception.hpp @@ -17,12 +17,54 @@ #include // xbt_procname #include // xbt_getpid +/** @addtogroup XBT_ex + * @brief Exceptions support + */ + namespace simgrid { namespace xbt { +/** A backtrace + * + * This is used (among other things) in exceptions to store the associated + * backtrace. + * + * @ingroup XBT_ex + */ typedef std::vector Backtrace; -/** A polymorphic mixin class for adding context to an exception */ +/** The location of where an exception has been throwed + * + * This is a tuple (__FILE__, __LINE__, __func__) and can be created with + * @ref XBT_THROW_POINT. + * + * @ingroup XBT_ex + */ +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; +}; + +/** Create a ThrowPoint with (__FILE__, __LINE__, __func__) */ +#define XBT_THROW_POINT ::simgrid::xbt::ThrowPoint(__FILE__, __LINE__, __func__) + +/** A base class for exceptions with context + * + * This is a base class for exceptions which store additional contextual + * infomations about them: backtrace, throw point, simulated process name + * and PID, etc. + * + * You are not expected to inherit from it. Instead of you use should + * @ref XBT_THROW an exception which will throw a subclass of your original + * exception with those additional features. + * + * However, you can try `dynamic_cast` an exception to this type in order to + * get contextual information about the exception. + */ XBT_PUBLIC_CLASS WithContextException { public: WithContextException() : @@ -35,6 +77,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,41 +90,52 @@ 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 */ +/** Internal class used to mixin an exception E with WithContextException */ 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)) {} + + static_assert(!std::is_base_of::value, + "Trying to appli WithContext twice"); + + 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 {} }; -/** Throw a given exception a context +/** Throw a C++ exception with some context * - * @param exception exception to throw - * @param backtrace backtrace to attach + * @param e Exception to throw + * @ingroup XBT_ex */ -template -[[noreturn]] inline -typename std::enable_if< !std::is_base_of::value >::type -throwWithContext( - E exception, - // 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)); -} +#define XBT_THROW(e) \ + throw WithContext(std::move(exception), throwpoint, simgrid::xbt::backtrace()) + +/** Throw a C++ exception with a context and a nexted exception/cause + * + * @param e Exception to throw + * @ingroup XBT_ex + */ +#define XBT_THROW_NESTED(e) \ + std::throw_with_nested(WithContext(std::move(exception), throwpoint, simgrid::xbt::backtrace())) } }