Carna  Version 3.3.2
CarnaException.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2010 - 2015 Leonid Kostrykin
3  *
4  * Chair of Medical Engineering (mediTEC)
5  * RWTH Aachen University
6  * Pauwelsstr. 20
7  * 52074 Aachen
8  * Germany
9  *
10  */
11 
12 #ifndef CARNAEXCEPTION_H_6014714286
13 #define CARNAEXCEPTION_H_6014714286
14 
21 #include <Carna/Carna.h>
22 #include <string>
23 #include <sstream>
24 #include <stdexcept>
25 #include <csignal>
26 
27 namespace Carna
28 {
29 
30 namespace base
31 {
32 
33 
34 
35 // ----------------------------------------------------------------------------------
36 // CarnaException
37 // ----------------------------------------------------------------------------------
38 
46 class CARNA_LIB CarnaException
47 {
48 
49 public:
50 
53  CarnaException( const std::string& type, const std::string& message, const std::string& details = "" );
54 
57  CarnaException( const std::logic_error& error, const std::string& details = "" );
58 
61  CarnaException( const std::runtime_error& error, const std::string& details = "" );
62 
63  virtual ~CarnaException();
64 
65 
68  const std::string type;
69 
72  const std::string message;
73 
76  const std::string details;
77 
78 }; // CarnaException
79 
80 
81 
82 // ----------------------------------------------------------------------------------
83 // AssertionFailure
84 // ----------------------------------------------------------------------------------
85 
92 class CARNA_LIB AssertionFailure : public CarnaException
93 {
94 
95 public:
96 
102  explicit AssertionFailure( const std::string& details );
103 
104 }; // AssertionFailure
105 
106 
107 
108 // ----------------------------------------------------------------------------------
109 // BreakForDebug
110 // ----------------------------------------------------------------------------------
111 
120 class CARNA_LIB BreakForDebug
121 {
122 
123  BreakForDebug();
124 
125  static bool enabled;
126 
127 public:
128 
131  static void enable();
132 
135  static void disable();
136 
139  static bool isEnabled();
140 
141 }; // BreakForDebug
142 
143 
144 
145 // ----------------------------------------------------------------------------------
146 // CARNA_BREAK_FOR_DEBUG
147 // ----------------------------------------------------------------------------------
148 
158 #ifndef NDEBUG
159  #ifdef _MSC_VER
160  #define CARNA_BREAK_FOR_DEBUG \
161  if( ::Carna::base::BreakForDebug::isEnabled() ) \
162  { \
163  __debugbreak(); \
164  }
165  #else
166  #define CARNA_BREAK_FOR_DEBUG \
167  if( ::Carna::base::BreakForDebug::isEnabled() ) \
168  { \
169  std::raise( SIGINT ); \
170  }
171  #endif
172 #else
173  #define CARNA_BREAK_FOR_DEBUG
174 #endif
175 
176 
177 
178 // ----------------------------------------------------------------------------------
179 // CARNA_FAIL
180 // ----------------------------------------------------------------------------------
181 
188 #define CARNA_FAIL( description ) \
189  CARNA_BREAK_FOR_DEBUG \
190  { \
191  std::stringstream details; \
192  details \
193  << "Description: " << description << std::endl \
194  << "File: " << __FILE__ << std::endl \
195  << "Line: " << __LINE__; \
196  throw ::Carna::base::AssertionFailure( details.str() ); \
197  }
198 
199 
200 
201 // ----------------------------------------------------------------------------------
202 // CARNA_ASSERT
203 // ----------------------------------------------------------------------------------
204 
212 #define CARNA_ASSERT( expression ) \
213  if( !( expression ) ) \
214  { \
215  CARNA_BREAK_FOR_DEBUG \
216  std::stringstream details; \
217  details \
218  << "Failed expression: " << #expression << std::endl \
219  << "File: " << __FILE__ << std::endl \
220  << "Line: " << __LINE__; \
221  throw ::Carna::base::AssertionFailure( details.str() ); \
222  }
223 
224 
225 
226 // ----------------------------------------------------------------------------------
227 // CARNA_ASSERT_EX
228 // ----------------------------------------------------------------------------------
229 
237 #define CARNA_ASSERT_EX( expression, description ) \
238  if( !( expression ) ) \
239  { \
240  CARNA_BREAK_FOR_DEBUG \
241  std::stringstream details; \
242  details \
243  << "Failed expression: " << #expression << std::endl \
244  << "Description: " << description << std::endl \
245  << "File: " << __FILE__ << std::endl \
246  << "Line: " << __LINE__; \
247  throw ::Carna::base::AssertionFailure( details.str() ); \
248  }
249 
250 
251 
252 } // namespace Carna :: base
253 
254 } // namespace Carna
255 
256 #endif // CARNAEXCEPTION_H_6014714286
Carna exception that indicates a failed assertion.
Base exception class that provides distinct attributes for the type (category) of the exception...
const std::string message
Tells the message of this exception.
Controls whether the CARNA_BREAK_FOR_DEBUG macro produces a break point or not.
const std::string type
Tells the type (category) of this exception.
const std::string details
Tells the details of this exception. May be empty.