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
23 typedef std::vector<xbt_backtrace_location_t> Backtrace;
27 ThrowPoint(const char* file, int line, const char* function) :
28 file(file), line(line), function(function) {}
29 const char* file = nullptr;
31 const char* function = nullptr;
34 /** A polymorphic mixin class for adding context to an exception */
35 XBT_PUBLIC_CLASS WithContextException {
37 WithContextException() :
38 backtrace_(simgrid::xbt::backtrace()),
39 procname_(xbt_procname()),
42 WithContextException(Backtrace bt) :
43 backtrace_(std::move(bt)),
44 procname_(xbt_procname()),
47 WithContextException(ThrowPoint throwpoint, Backtrace bt) :
48 backtrace_(std::move(bt)),
49 procname_(xbt_procname()),
51 throwpoint_(throwpoint)
53 virtual ~WithContextException();
54 Backtrace const& backtrace() const
58 int pid() const { return pid_; }
59 std::string const& processName() const { return procname_; }
60 ThrowPoint& throwPoint() { return throwpoint_; }
63 std::string procname_; /**< Name of the process who thrown this */
64 int pid_; /**< PID of the process who thrown this */
65 ThrowPoint throwpoint_;
68 /** Internal class used to mixin the two classes */
70 class WithContext : public E, public WithContextException
73 WithContext(E exception) :
74 E(std::move(exception)) {}
75 WithContext(E exception, ThrowPoint throwpoint, Backtrace backtrace) :
76 E(std::move(exception)),
77 WithContextException(throwpoint, std::move(backtrace)) {}
78 WithContext(E exception, Backtrace backtrace) :
79 E(std::move(exception)),
80 WithContextException(std::move(backtrace)) {}
81 WithContext(E exception, WithContextException context) :
82 E(std::move(exception)),
83 WithContextException(std::move(context)) {}
84 ~WithContext() override {}
87 /** Throw a given exception a context
89 * @param exception exception to throw
90 * @param backtrace backtrace to attach
94 typename std::enable_if< !std::is_base_of<WithContextException,E>::value >::type
97 // Thanks to the default argument, we are taking the backtrace in the caller:
98 Backtrace backtrace = simgrid::xbt::backtrace())
100 throw WithContext<E>(std::move(exception), std::move(backtrace));
105 typename std::enable_if< !std::is_base_of<WithContextException,E>::value >::type
108 ThrowPoint throwpoint,
109 // Thanks to the default argument, we are taking the backtrace in the caller:
110 Backtrace backtrace = simgrid::xbt::backtrace())
112 throw WithContext<E>(std::move(exception), throwpoint, std::move(backtrace));
115 #define XBT_THROW_POINT ::simgrid::xbt::ThrowPoint(__FILE__, __LINE__, __func__)
116 #define XBT_THROW(e) \
117 ::simgrid::xbt::throwWithContext(std::move(e), XBT_THROW_POINT)