The Sparta Modeling Framework
Loading...
Searching...
No Matches
SpartaException.hpp
Go to the documentation of this file.
1// <SpartaException.hpp> -*- C++ -*-
2
11#pragma once
12
13#include <exception>
14#include <sstream>
15#include <string>
16#include <memory>
17
30#define THROW_IF_NOT_UNWINDING(exclass, msg_construct) \
31 if(std::uncaught_exceptions() > 0){ \
32 std::cerr << msg_construct << std::endl; \
33 }else{ \
34 throw exclass() << msg_construct; \
35 }
36
37#define DO_PRAGMA(x) _Pragma(#x)
38#define TERMINATING_THROW(exclass, msg_construct) \
39 DO_PRAGMA(GCC diagnostic push) \
40 DO_PRAGMA(GCC diagnostic ignored "-Wterminate") \
41 throw exclass() << msg_construct; \
42 DO_PRAGMA(GCC diagnostic pop)
43
44namespace sparta
45{
46 namespace app
47 {
48 class BacktraceData;
49 }
50
63 class SpartaException : public std::exception
64 {
65 public:
66
76
83 SpartaException(const std::string & reason);
84
89
91 virtual ~SpartaException() noexcept;
92
97 const char* what() const noexcept {
98 reason_str_ = reason_.str();
99 return reason_str_.c_str();
100 }
101
105 std::string backtrace() const noexcept;
106
111 std::string rawReason() const {
112 return raw_reason_;
113 }
114
133 template<class T>
134 SpartaException & operator<<(const T & msg) {
135 reason_ << msg;
136 return *this;
137 }
138
139 private:
140 // The raw reason without file/line information
141 std::string raw_reason_;
142
143 // The reason/explanation for the exception
144 std::stringstream reason_;
145
146 // Backtrace at the time of the exception
147 std::unique_ptr<app::BacktraceData> bt_;
148
149 // Need to keep a local copy of the string formed in the
150 // string stream for the 'what' call
151 mutable std::string reason_str_;
152 };
153
154
167 {
168 public:
169
178
183 SpartaCriticalError(const std::string & reason) :
184 SpartaException(reason)
185 { }
186 };
187
203 {
204 public:
205
214
219 SpartaFatalError(const std::string & reason) :
220 SpartaException(reason)
221 { }
222 };
223
224}
225
Indicates something went seriously wrong and likely indicates corruption in simulator runtime state.
SpartaCriticalError(const std::string &reason)
Construct a SpartaCriticalException object.
SpartaCriticalError()
Construct a SpartaCriticalException object.
Used to construct and throw a standard C++ exception. Inherits from std::exception.
const char * what() const noexcept
Overload from std::exception.
virtual ~SpartaException() noexcept
Destroy!
SpartaException(const std::string &reason)
Construct a SpartaException object.
SpartaException()
Construct a SpartaException object with empty reason.
SpartaException(const SpartaException &orig)
Copy construct a SpartaException object.
SpartaException & operator<<(const T &msg)
Append additional information to the message.
std::string rawReason() const
Return the raw reason without file, line information.
std::string backtrace() const noexcept
Returns the backtrace at the time this exception was generated.
Indicates something went seriously wrong and likely indicates unrecoverable corruption in simulator r...
SpartaFatalError(const std::string &reason)
Construct a SpartaFatalError object.
SpartaFatalError()
Construct a SpartaFatalError object.
Macros for handling exponential backoff.