1 /* Copyright (c) 2005-2018. 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 thrown
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)
49 const char* file_ = nullptr;
51 const char* function_ = nullptr;
54 /** Create a ThrowPoint with (__FILE__, __LINE__, __func__) */
55 #define XBT_THROW_POINT ::simgrid::xbt::ThrowPoint(__FILE__, __LINE__, __func__)
57 /** A base class for exceptions with context
59 * This is a base class for exceptions which store additional contextual
60 * information: backtrace, throw point, simulated process name, PID, etc.
62 class XBT_PUBLIC WithContextException {
64 WithContextException() :
65 backtrace_(simgrid::xbt::backtrace()),
66 procname_(xbt_procname()),
69 explicit WithContextException(Backtrace bt) : backtrace_(std::move(bt)), procname_(xbt_procname()), pid_(xbt_getpid())
71 explicit WithContextException(ThrowPoint throwpoint, Backtrace bt)
72 : backtrace_(std::move(bt)), procname_(xbt_procname()), pid_(xbt_getpid()), throwpoint_(throwpoint)
74 virtual ~WithContextException();
75 Backtrace const& backtrace() const
79 int pid() const { return pid_; }
80 std::string const& process_name() const { return procname_; }
81 ThrowPoint& throw_point() { return throwpoint_; }
84 XBT_ATTRIB_DEPRECATED_v323("Please use WithContextException::process_name()") std::string const& processName() const
86 return process_name();
88 XBT_ATTRIB_DEPRECATED_v323("Please use WithContextException::throw_point()") ThrowPoint& throwPoint()
95 std::string procname_; /**< Name of the process who thrown this */
96 int pid_; /**< PID of the process who thrown this */
97 ThrowPoint throwpoint_;