Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Cover with a test Mailbox::ready() method introduced in commit 1ed0e64dc405fbb627ff8a...
[simgrid.git] / include / xbt / exception.hpp
1 /* Copyright (c) 2005-2018. The SimGrid Team.All rights reserved. */
2
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. */
5
6 #ifndef SIMGRID_XBT_EXCEPTION_HPP
7 #define SIMGRID_XBT_EXCEPTION_HPP
8
9 #include <string>
10 #include <type_traits>
11 #include <vector>
12
13 #include <xbt/base.h>
14 #include <xbt/backtrace.h>
15 #include <xbt/backtrace.hpp>
16 #include <xbt/log.h>
17 #include <xbt/misc.h>  // xbt_procname
18 #include <xbt/virtu.h> // xbt_getpid
19
20 /** @addtogroup XBT_ex
21  *  @brief Exceptions support
22  */
23
24 namespace simgrid {
25 namespace xbt {
26
27 /** A backtrace
28  *
29  *  This is used (among other things) in exceptions to store the associated
30  *  backtrace.
31  *
32  *  @ingroup XBT_ex
33  */
34 typedef std::vector<xbt_backtrace_location_t> Backtrace;
35
36 /** The location of where an exception has been throwed
37  *
38  *  This is a tuple (__FILE__, __LINE__, __func__) and can be created with
39  *  @ref XBT_THROW_POINT.
40  *
41  *  @ingroup XBT_ex
42  */
43 class ThrowPoint {
44  public:
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;
48   int line = 0;
49   const char* function = nullptr;
50 };
51
52 /** Create a ThrowPoint with (__FILE__, __LINE__, __func__) */
53 #define XBT_THROW_POINT ::simgrid::xbt::ThrowPoint(__FILE__, __LINE__, __func__)
54
55 /** A base class for exceptions with context
56  *
57  *  This is a base class for exceptions which store additional contextual
58  *  infomations about them: backtrace, throw point, simulated process name
59  *  and PID, etc.
60  *
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.
64  *
65  *  However, you can try `dynamic_cast` an exception to this type in order to
66  *  get contextual information about the exception.
67  */
68 class XBT_PUBLIC WithContextException {
69 public:
70   WithContextException() :
71     backtrace_(simgrid::xbt::backtrace()),
72     procname_(xbt_procname()),
73     pid_(xbt_getpid())
74   {}
75   explicit WithContextException(Backtrace bt) : backtrace_(std::move(bt)), procname_(xbt_procname()), pid_(xbt_getpid())
76   {}
77   explicit WithContextException(ThrowPoint throwpoint, Backtrace bt)
78       : backtrace_(std::move(bt)), procname_(xbt_procname()), pid_(xbt_getpid()), throwpoint_(throwpoint)
79   {}
80   virtual ~WithContextException();
81   Backtrace const& backtrace() const
82   {
83     return backtrace_;
84   }
85   int pid() const { return pid_; }
86   std::string const& process_name() const { return procname_; }
87   ThrowPoint& throw_point() { return throwpoint_; }
88
89   // deprecated
90   XBT_ATTRIB_DEPRECATED_v323("Please use WithContextException::process_name()") std::string const& processName() const
91   {
92     return process_name();
93   }
94   XBT_ATTRIB_DEPRECATED_v323("Please use WithContextException::throw_point()") ThrowPoint& throwPoint()
95   {
96     return throw_point();
97   }
98
99 private:
100   Backtrace backtrace_;
101   std::string procname_; /**< Name of the process who thrown this */
102   int pid_;              /**< PID of the process who thrown this */
103   ThrowPoint throwpoint_;
104 };
105
106 /** Internal class used to mixin an exception E with WithContextException */
107 template<class E>
108 class WithContext : public E, public WithContextException
109 {
110 public:
111   static_assert(not std::is_base_of<WithContextException, E>::value, "Trying to appli WithContext twice");
112
113   explicit WithContext(E exception) : E(std::move(exception)) {}
114   WithContext(E exception, ThrowPoint throwpoint, Backtrace backtrace) :
115     E(std::move(exception)),
116     WithContextException(throwpoint, std::move(backtrace)) {}
117   WithContext(E exception, Backtrace backtrace) :
118     E(std::move(exception)),
119     WithContextException(std::move(backtrace)) {}
120   WithContext(E exception, WithContextException context) :
121     E(std::move(exception)),
122     WithContextException(std::move(context)) {}
123   ~WithContext() override = default;
124 };
125
126 /** Throw a C++ exception with some context
127  *
128  *  @param e Exception to throw
129  *  @ingroup XBT_ex
130  */
131 #define XBT_THROW(e) \
132   throw WithContext<E>(std::move(exception), throwpoint, simgrid::xbt::backtrace())
133
134 /** Throw a C++ exception with a context and a nexted exception/cause
135  *
136  *  @param e Exception to throw
137  *  @ingroup XBT_ex
138  */
139 #define XBT_THROW_NESTED(e) \
140   std::throw_with_nested(WithContext<E>(std::move(exception), throwpoint, simgrid::xbt::backtrace()))
141
142 }
143 }
144
145 #endif