Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Rename simgrid::exception into simgrid::Exception
[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 /** The location of where an exception has been thrown
28  *
29  *  This is a tuple (__FILE__, __LINE__, __func__) and can be created with
30  *  @ref XBT_THROW_POINT.
31  *
32  *  @ingroup XBT_ex
33  */
34 class ThrowPoint {
35  public:
36   ThrowPoint() = default;
37   explicit ThrowPoint(const char* file, int line, const char* function) : file_(file), line_(line), function_(function)
38   {
39   }
40   const char* file_     = nullptr;
41   int line_             = 0;
42   const char* function_ = nullptr;
43 };
44
45 /** Create a ThrowPoint with (__FILE__, __LINE__, __func__) */
46 #define XBT_THROW_POINT ::simgrid::xbt::ThrowPoint(__FILE__, __LINE__, __func__)
47
48 /** A base class for exceptions with context
49  *
50  *  This is a base class for exceptions which store additional contextual
51  *  information: backtrace, throw point, simulated process name, PID, etc.
52  */
53 class XBT_PUBLIC ContextedException {
54 public:
55   ContextedException() :
56     backtrace_(simgrid::xbt::backtrace()),
57     procname_(xbt_procname()),
58     pid_(xbt_getpid())
59   {}
60   explicit ContextedException(Backtrace bt) : backtrace_(std::move(bt)), procname_(xbt_procname()), pid_(xbt_getpid())
61   {}
62   explicit ContextedException(ThrowPoint throwpoint, Backtrace bt)
63       : backtrace_(std::move(bt)), procname_(xbt_procname()), pid_(xbt_getpid()), throwpoint_(throwpoint)
64   {}
65   virtual ~ContextedException();
66   Backtrace const& backtrace() const
67   {
68     return backtrace_;
69   }
70   int pid() const { return pid_; }
71   std::string const& process_name() const { return procname_; }
72   ThrowPoint& throw_point() { return throwpoint_; }
73
74   // deprecated
75   XBT_ATTRIB_DEPRECATED_v323("Please use WithContextException::process_name()") std::string const& processName() const
76   {
77     return process_name();
78   }
79   XBT_ATTRIB_DEPRECATED_v323("Please use WithContextException::throw_point()") ThrowPoint& throwPoint()
80   {
81     return throw_point();
82   }
83
84 private:
85   Backtrace backtrace_;
86   std::string procname_; /**< Name of the process who thrown this */
87   int pid_;              /**< PID of the process who thrown this */
88   ThrowPoint throwpoint_;
89 };
90 }
91 }
92
93 #endif