Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fix leak of memory caused to some Events
[simgrid.git] / include / xbt / exception.hpp
1 /* Copyright (c) 2005-2016. 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   ThrowPoint(const char* file, int line, const char* function) :
47     file(file), line(line), function(function) {}
48   const char* file = nullptr;
49   int line = 0;
50   const char* function = nullptr;
51 };
52
53 /** Create a ThrowPoint with (__FILE__, __LINE__, __func__) */
54 #define XBT_THROW_POINT ::simgrid::xbt::ThrowPoint(__FILE__, __LINE__, __func__)
55
56 /** A base class for exceptions with context
57  *
58  *  This is a base class for exceptions which store additional contextual
59  *  infomations about them: backtrace, throw point, simulated process name
60  *  and PID, etc.
61  *
62  *  You are not expected to inherit from it. Instead of you use should
63  *  @ref XBT_THROW an exception which will throw a subclass of your original
64  *  exception with those additional features.
65  * 
66  *  However, you can try `dynamic_cast` an exception to this type in order to
67  *  get contextual information about the exception.
68  */
69 XBT_PUBLIC_CLASS WithContextException {
70 public:
71   WithContextException() :
72     backtrace_(simgrid::xbt::backtrace()),
73     procname_(xbt_procname()),
74     pid_(xbt_getpid())
75   {}
76   WithContextException(Backtrace bt) :
77     backtrace_(std::move(bt)),
78     procname_(xbt_procname()),
79     pid_(xbt_getpid())
80   {}
81   WithContextException(ThrowPoint throwpoint, Backtrace bt) :
82     backtrace_(std::move(bt)),
83     procname_(xbt_procname()),
84     pid_(xbt_getpid()),
85     throwpoint_(throwpoint)
86   {}
87   virtual ~WithContextException();
88   Backtrace const& backtrace() const
89   {
90     return backtrace_;
91   }
92   int pid() const { return pid_; }
93   std::string const& processName() const { return procname_; }
94   ThrowPoint& throwPoint() { return throwpoint_; }
95 private:
96   Backtrace backtrace_;
97   std::string procname_; /**< Name of the process who thrown this */
98   int pid_;              /**< PID of the process who thrown this */
99   ThrowPoint throwpoint_;
100 };
101
102 /** Internal class used to mixin an exception E with WithContextException */
103 template<class E>
104 class WithContext : public E, public WithContextException
105 {
106 public:
107
108   static_assert(!std::is_base_of<WithContextException,E>::value,
109     "Trying to appli WithContext twice");
110
111   WithContext(E exception) :
112     E(std::move(exception)) {}
113   WithContext(E exception, ThrowPoint throwpoint, Backtrace backtrace) :
114     E(std::move(exception)),
115     WithContextException(throwpoint, std::move(backtrace)) {}
116   WithContext(E exception, Backtrace backtrace) :
117     E(std::move(exception)),
118     WithContextException(std::move(backtrace)) {}
119   WithContext(E exception, WithContextException context) :
120     E(std::move(exception)),
121     WithContextException(std::move(context)) {}
122   ~WithContext() override {}
123 };
124
125 /** Throw a C++ exception with some context
126  *
127  *  @param e Exception to throw
128  *  @ingroup XBT_ex
129  */
130 #define XBT_THROW(e) \
131   throw WithContext<E>(std::move(exception), throwpoint, simgrid::xbt::backtrace())
132
133 /** Throw a C++ exception with a context and a nexted exception/cause
134  *
135  *  @param e Exception to throw
136  *  @ingroup XBT_ex
137  */
138 #define XBT_THROW_NESTED(e) \
139   std::throw_with_nested(WithContext<E>(std::move(exception), throwpoint, simgrid::xbt::backtrace()))
140
141 }
142 }
143
144 #endif