LibCarna Version 3.4.0
Loading...
Searching...
No Matches
LibCarnaException.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2010 - 2016 Leonid Kostrykin
3 *
4 * Chair of Medical Engineering (mediTEC)
5 * RWTH Aachen University
6 * Pauwelsstr. 20
7 * 52074 Aachen
8 * Germany
9 *
10 *
11 * Copyright (C) 2021 - 2025 Leonid Kostrykin
12 *
13 */
14
15#ifndef LIBCARNAEXCEPTION_H_6014714286
16#define LIBCARNAEXCEPTION_H_6014714286
17
23#include <memory>
24#include <LibCarna/LibCarna.hpp>
25#include <string>
26#include <sstream>
27#include <stdexcept>
28#include <csignal>
29
30namespace LibCarna
31{
32
33namespace base
34{
35
36
37
38// ----------------------------------------------------------------------------------
39// LibCarnaException
40// ----------------------------------------------------------------------------------
41
48class LIBCARNA LibCarnaException
49{
50
51 struct Details;
52 const std::unique_ptr< Details > pimpl;
53
54public:
55
58 LibCarnaException( const std::string& type, const std::string& message, const std::string& details = "" );
59
62 LibCarnaException( const std::logic_error& error, const std::string& details = "" );
63
66 LibCarnaException( const std::runtime_error& error, const std::string& details = "" );
67
71
72 virtual ~LibCarnaException();
73
74
77 const std::string type;
78
81 const std::string message;
82
85 const std::string details;
86
87
90 const char* what() const;
91
92}; // LibCarnaException
93
94
95
96// ----------------------------------------------------------------------------------
97// AssertionFailure
98// ----------------------------------------------------------------------------------
99
105class LIBCARNA AssertionFailure : public LibCarnaException
106{
107
108public:
109
115 explicit AssertionFailure( const std::string& details );
116
117}; // AssertionFailure
118
119
120
121// ----------------------------------------------------------------------------------
122// BreakForDebug
123// ----------------------------------------------------------------------------------
124
132class LIBCARNA BreakForDebug
133{
134
136
137 static bool enabled;
138
139public:
140
143 static void enable();
144
147 static void disable();
148
151 static bool isEnabled();
152
153}; // BreakForDebug
154
155
156
157// ----------------------------------------------------------------------------------
158// LIBCARNA_BREAK_FOR_DEBUG
159// ----------------------------------------------------------------------------------
160
169#ifndef NDEBUG
170 #ifdef _MSC_VER
171 #define LIBCARNA_BREAK_FOR_DEBUG \
172 if( ::LibCarna::base::BreakForDebug::isEnabled() ) \
173 { \
174 __debugbreak(); \
175 }
176 #else
177 #define LIBCARNA_BREAK_FOR_DEBUG \
178 if( ::LibCarna::base::BreakForDebug::isEnabled() ) \
179 { \
180 std::raise( SIGINT ); \
181 }
182 #endif
183#else
184 #define LIBCARNA_BREAK_FOR_DEBUG
185#endif
186
187
188
189// ----------------------------------------------------------------------------------
190// LIBCARNA_FAIL
191// ----------------------------------------------------------------------------------
192
198#define LIBCARNA_FAIL( description ) \
199 LIBCARNA_BREAK_FOR_DEBUG \
200 { \
201 std::stringstream details; \
202 details \
203 << "Description: " << description << std::endl \
204 << "Where: " << __FILE__ << ":" << __LINE__; \
205 throw ::LibCarna::base::AssertionFailure( details.str() ); \
206 }
207
208
209
210// ----------------------------------------------------------------------------------
211// LIBCARNA_ASSERT
212// ----------------------------------------------------------------------------------
213
220#define LIBCARNA_ASSERT( expression ) \
221 if( !( expression ) ) \
222 { \
223 LIBCARNA_BREAK_FOR_DEBUG \
224 std::stringstream details; \
225 details \
226 << "Failed expression: " << #expression << std::endl \
227 << "Where: " << __FILE__ << ":" << __LINE__; \
228 throw ::LibCarna::base::AssertionFailure( details.str() ); \
229 }
230
231
232
233// ----------------------------------------------------------------------------------
234// LIBCARNA_ASSERT_EX
235// ----------------------------------------------------------------------------------
236
243#define LIBCARNA_ASSERT_EX( expression, description ) \
244 if( !( expression ) ) \
245 { \
246 LIBCARNA_BREAK_FOR_DEBUG \
247 std::stringstream details; \
248 details \
249 << "Failed expression: " << #expression << std::endl \
250 << "Description: " << description << std::endl \
251 << "Where: " << __FILE__ << ":" << __LINE__; \
252 throw ::LibCarna::base::AssertionFailure( details.str() ); \
253 }
254
255
256
257} // namespace LibCarna :: base
258
259} // namespace LibCarna
260
261#endif // LIBCARNAEXCEPTION_H_6014714286
Contains forward-declarations.
Carna exception that indicates a failed assertion.
AssertionFailure(const std::string &details)
Instantiates.
Represents an association.
Controls whether the LIBCARNA_BREAK_FOR_DEBUG macro produces a break point or not.
static void enable()
Makes future invocations of LIBCARNA_BREAK_FOR_DEBUG will produce break point.
static void disable()
Prevents future invocations of LIBCARNA_BREAK_FOR_DEBUG from producing break point.
static bool isEnabled()
Tells whether future invocations of LIBCARNA_BREAK_FOR_DEBUG will produce break point.
Base exception class that provides distinct attributes for the type (category) of the exception,...
const std::string type
Tells the type (category) of this exception.
const char * what() const
Tells the message and the details of this exception.
const std::string message
Tells the message of this exception.
LibCarnaException(const LibCarnaException &other)
Instantiates as a copy of the other exception.
LibCarnaException(const std::string &type, const std::string &message, const std::string &details="")
Initializes the attributes plainly from the arguments.
const std::string details
Tells the details of this exception. May be empty.
LibCarnaException(const std::logic_error &error, const std::string &details="")
Initializes the type as ''Assertion Error'' and the message from the given exception.
LibCarnaException(const std::runtime_error &error, const std::string &details="")
Initializes the type as ''Unhandled Exception'' and the message from the given exception.