1 /* Copyright (c) 2005-2016. The SimGrid Team.All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
6 #ifndef SIMGRID_XBT_EXCEPTION_HPP
7 #define SIMGRID_XBT_EXCEPTION_HPP
10 #include <type_traits>
14 #include <xbt/backtrace.h>
15 #include <xbt/backtrace.hpp>
17 #include <xbt/misc.h> // xbt_procname
18 #include <xbt/virtu.h> // xbt_getpid
20 /** @addtogroup XBT_ex
21 * @brief Exceptions support
29 * This is used (among other things) in exceptions to store the associated
34 typedef std::vector<xbt_backtrace_location_t> Backtrace;
36 /** The location of where an exception has been throwed
38 * This is a tuple (__FILE__, __LINE__, __func__) and can be created with
39 * @ref XBT_THROW_POINT.
45 ThrowPoint() = default;
46 explicit ThrowPoint(const char* file, int line, const char* function) : file(file), line(line), function(function) {}
47 const char* file = nullptr;
49 const char* function = nullptr;
52 /** Create a ThrowPoint with (__FILE__, __LINE__, __func__) */
53 #define XBT_THROW_POINT ::simgrid::xbt::ThrowPoint(__FILE__, __LINE__, __func__)
55 /** A base class for exceptions with context
57 * This is a base class for exceptions which store additional contextual
58 * infomations about them: backtrace, throw point, simulated process name
61 * You are not expected to inherit from it. Instead of you use should
62 * @ref XBT_THROW an exception which will throw a subclass of your original
63 * exception with those additional features.
65 * However, you can try `dynamic_cast` an exception to this type in order to
66 * get contextual information about the exception.
68 XBT_PUBLIC_CLASS WithContextException {
70 WithContextException() :
71 backtrace_(simgrid::xbt::backtrace()),
72 procname_(xbt_procname()),
75 WithContextException(Backtrace bt) :
76 backtrace_(std::move(bt)),
77 procname_(xbt_procname()),
80 WithContextException(ThrowPoint throwpoint, Backtrace bt) :
81 backtrace_(std::move(bt)),
82 procname_(xbt_procname()),
84 throwpoint_(throwpoint)
86 virtual ~WithContextException();
87 Backtrace const& backtrace() const
91 int pid() const { return pid_; }
92 std::string const& processName() const { return procname_; }
93 ThrowPoint& throwPoint() { return throwpoint_; }
96 std::string procname_; /**< Name of the process who thrown this */
97 int pid_; /**< PID of the process who thrown this */
98 ThrowPoint throwpoint_;
101 /** Internal class used to mixin an exception E with WithContextException */
103 class WithContext : public E, public WithContextException
106 static_assert(not std::is_base_of<WithContextException, E>::value, "Trying to appli WithContext twice");
108 WithContext(E exception) :
109 E(std::move(exception)) {}
110 WithContext(E exception, ThrowPoint throwpoint, Backtrace backtrace) :
111 E(std::move(exception)),
112 WithContextException(throwpoint, std::move(backtrace)) {}
113 WithContext(E exception, Backtrace backtrace) :
114 E(std::move(exception)),
115 WithContextException(std::move(backtrace)) {}
116 WithContext(E exception, WithContextException context) :
117 E(std::move(exception)),
118 WithContextException(std::move(context)) {}
119 ~WithContext() override {}
122 /** Throw a C++ exception with some context
124 * @param e Exception to throw
127 #define XBT_THROW(e) \
128 throw WithContext<E>(std::move(exception), throwpoint, simgrid::xbt::backtrace())
130 /** Throw a C++ exception with a context and a nexted exception/cause
132 * @param e Exception to throw
135 #define XBT_THROW_NESTED(e) \
136 std::throw_with_nested(WithContext<E>(std::move(exception), throwpoint, simgrid::xbt::backtrace()))