MeVisLab Toolbox Reference
mlErrorMacros.h
Go to the documentation of this file.
1 /*************************************************************************************
2 **
3 ** Copyright 2007, MeVis Medical Solutions AG
4 **
5 ** The user may use this file in accordance with the license agreement provided with
6 ** the Software or, alternatively, in accordance with the terms contained in a
7 ** written agreement between the user and MeVis Medical Solutions AG.
8 **
9 ** For further information use the contact form at https://www.mevislab.de/contact
10 **
11 **************************************************************************************/
12 
13 #ifndef ML_ERROR_MACROS_H
14 #define ML_ERROR_MACROS_H
15 
18 #include <utility>
19 #include <type_traits>
20 #include "mlConfig.h"
21 
22 #include "mlMacros.h"
23 
24 // ------------------------------------------------------
25 // Disabling some bogus warnings in MeVis release mode
26 // ------------------------------------------------------
27 #if defined(_MSC_VER)
28  #pragma warning(push)
29  #if !defined(_DEBUG)
30  #pragma warning(disable: 4701) // local variable *may* be used without init
31  #pragma warning(disable: 4702) // unreachable code
32  #pragma warning(disable: 4710) // private constructors are disallowed
33  #pragma warning(disable: 4097) // typedef-name 'identifier1' used as synonym for class-name 'identifier2'
34  #pragma warning(disable: 4512) // 'class' : assignment operator could not be generated
35  #pragma warning(disable: 4127) // conditional expression is constant
36  #pragma warning(disable: 4711) // selected for automatic inline expansion
37  #endif
38  #pragma warning(disable: 4127) // conditional expression is constant
39 #endif
40 
41 #include "mlLogging.h"
42 
43 #if defined(_MSC_VER)
44  #pragma warning(pop)
45 #endif
47 
48 
49  //----------------------------------------
54  //----------------------------------------
55 #ifdef _DEBUG
56  #define ML_TRY
57  #define ML_CATCH
58  #define ML_CATCH_RETURN
59  #define ML_CATCH_RETURN_FALSE
60  #define ML_CATCH_RETURN_NULL
61  #define ML_CATCH_RETHROW
62  #define ML_CATCH_BLOCK(__paramType) if (MLAlwaysFalse)
63 #else
64  #define ML_TRY try
65  #define ML_CATCH catch(...){ MLPrintAndNotifyFatal(__FILE__, __LINE__, ML_UNKNOWN_EXCEPTION); }
66  #define ML_CATCH_RETURN catch(...){ MLPrintAndNotifyFatal(__FILE__, __LINE__, ML_UNKNOWN_EXCEPTION); return; }
67  #define ML_CATCH_RETURN_FALSE catch(...){ MLPrintAndNotifyFatal(__FILE__, __LINE__, ML_UNKNOWN_EXCEPTION); return false; }
68  #define ML_CATCH_RETURN_NULL catch(...){ MLPrintAndNotifyFatal(__FILE__, __LINE__, ML_UNKNOWN_EXCEPTION); return 0; }
69  #define ML_CATCH_RETHROW catch(...){ MLPrintAndNotifyFatal(__FILE__, __LINE__, ML_UNKNOWN_EXCEPTION); throw; }
70  #define ML_CATCH_BLOCK(__paramType) catch(__paramType)
71 #endif
72 
74 
75 
76  //----------------------------------------
79  //----------------------------------------
80  #define ML_CHECK(x) if (!(x)){ MLPrintAndNotifyFatal(__FILE__, __LINE__, ML_BAD_POINTER_OR_0); }
81 #ifdef _DEBUG
82  #define ML_CHECK_ONLY_IN_DEBUG(x) ML_CHECK(x)
83 #else
84  #define ML_CHECK_ONLY_IN_DEBUG(x) /* Behaves like assert, not compiled in release mode */
85 #endif
86  #define ML_CHECK_RETURN(x) if (!(x)){ MLPrintAndNotifyFatal(__FILE__, __LINE__, ML_BAD_POINTER_OR_0); return; }
87  #define ML_CHECK_RETURN_FALSE(x) if (!(x)){ MLPrintAndNotifyFatal(__FILE__, __LINE__, ML_BAD_POINTER_OR_0); return false; }
88  #define ML_CHECK_RETURN_NULL(x) if (!(x)){ MLPrintAndNotifyFatal(__FILE__, __LINE__, ML_BAD_POINTER_OR_0); return 0; }
89  #define ML_CHECK_THROW(x) if (!(x)){ MLPrintAndNotifyFatal(__FILE__, __LINE__, ML_BAD_POINTER_OR_0); throw ML_BAD_POINTER_OR_0; }
90  #define ML_CHECK_RUNTIME_TYPE(x) if (!(x)){ MLPrintAndNotifyFatal(__FILE__, __LINE__, ML_TYPE_NOT_REGISTERED); throw ML_TYPE_NOT_REGISTERED; }
91 
93  #define ML_HARAKIRI MLPrintAndNotifyFatal(__FILE__, __LINE__, ML_BAD_POINTER_OR_0);
94 
96 
97 
98  //----------------------------------------
101  //----------------------------------------
102  #define ML_CHECK_FLOAT(x) if (MLValueIs0WOM(x)){ MLPrintAndNotifyFatal(__FILE__, __LINE__, ML_BAD_POINTER_OR_0); }
103  #define ML_CHECK_FLOAT_RETURN(x) if (MLValueIs0WOM(x)){ MLPrintAndNotifyFatal(__FILE__, __LINE__, ML_BAD_POINTER_OR_0); return; }
104  #define ML_CHECK_FLOAT_RETURN_FALSE(x) if (MLValueIs0WOM(x)){ MLPrintAndNotifyFatal(__FILE__, __LINE__, ML_BAD_POINTER_OR_0); return false; }
105  #define ML_CHECK_FLOAT_RETURN_NULL(x) if (MLValueIs0WOM(x)){ MLPrintAndNotifyFatal(__FILE__, __LINE__, ML_BAD_POINTER_OR_0); return 0; }
106  #define ML_CHECK_FLOAT_THROW(x) if (MLValueIs0WOM(x)){ MLPrintAndNotifyFatal(__FILE__, __LINE__, ML_BAD_POINTER_OR_0); throw ML_BAD_POINTER_OR_0; }
107  #define ML_CHECK_FLOAT_RUNTIME_TYPE(x) if (MLValueIs0WOM(x)){ MLPrintAndNotifyFatal(__FILE__, __LINE__, ML_TYPE_NOT_REGISTERED); throw ML_TYPE_NOT_REGISTERED; }
108 
110 
111 
112 //-----------------------------------------------------------------------------------------------
115 //-----------------------------------------------------------------------------------------------
120 #define _ML_INFORMATION_ADD_ON(FUNC_NAME, REASON, HANDLING)
121 
126 #define _ML_WARNING_ADD_ON(FUNC_NAME, REASON, HANDLING)
127 
132 #define _ML_ERROR_ADD_ON(FUNC_NAME, REASON, HANDLING)
133 
138 #define _ML_FATAL_ERROR_ADD_ON(FUNC_NAME, REASON, HANDLING)
139 
145 #define _ML_ERROR_SHOW_ASSERT_BOX(PARAM)
147 
148 
149 
150 
151 
152 //--------------------------------------------------------------------------------------------
153 //
156 //
157 //--------------------------------------------------------------------------------------------
158 
159 //--------------------------------------------------------------------------------------------
172 //--------------------------------------------------------------------------------------------
173 #define _ML_PRINT_FATAL_ERROR_DUMP(FUNC_NAME, REASON, HANDLING, RT_OBJ, RTYPE) \
174  { \
175  /* Get runtime information from RTYPE, which could be a crashing expression.*/ \
176  const ML_UTILS_NAMESPACE::RuntimeType *rtt7645 = nullptr; \
177  try{ rtt7645 = RTYPE; if (MLAlwaysFalse){ throw ML_UNKNOWN_EXCEPTION;} } catch(...){ rtt7645 = nullptr; }\
178  _ML_FATAL_ERROR_ADD_ON(FUNC_NAME, REASON, HANDLING) \
179  ML_UTILS_NAMESPACE::MLErrorOutput.printAndNotify(ML_FATAL, \
180  ML_PREFIX, "Function=", FUNC_NAME, REASON, HANDLING, \
181  __FILE__, __LINE__, RT_OBJ, rtt7645); \
182  _ML_ERROR_SHOW_ASSERT_BOX(0); \
183  }
184 
185 //--------------------------------------------------------------------------------------------
198 //--------------------------------------------------------------------------------------------
199 #define _ML_PRINT_ERROR_DUMP(FUNC_NAME, REASON, HANDLING, RT_OBJ, RTYPE) \
200  { \
201  /* Get runtime information from RTYPE which could be a crashing expression.*/ \
202  const ML_UTILS_NAMESPACE::RuntimeType *rtt1987 = nullptr; \
203  try{ rtt1987 = RTYPE; if (MLAlwaysFalse){ throw ML_UNKNOWN_EXCEPTION;} } catch(...){ rtt1987 = nullptr; }\
204  _ML_ERROR_ADD_ON(FUNC_NAME, REASON, HANDLING) \
205  ML_UTILS_NAMESPACE::MLErrorOutput.printAndNotify(ML_ERROR, \
206  ML_PREFIX, "Function=", FUNC_NAME, REASON, HANDLING, \
207  __FILE__, __LINE__, RT_OBJ, rtt1987); \
208  _ML_ERROR_SHOW_ASSERT_BOX(0); \
209  }
210 
211 //--------------------------------------------------------------------------------------------
225 //--------------------------------------------------------------------------------------------
226 #define _ML_PRINT_WARNING_DUMP(FUNC_NAME, REASON, HANDLING, RT_OBJ, RTYPE) \
227  { \
228  /* Get runtime information from RTYPE which could be a crashing expression.*/ \
229  const ML_UTILS_NAMESPACE::RuntimeType *rt9477 = nullptr; \
230  try{ rt9477 = RTYPE; if (MLAlwaysFalse){ throw ML_UNKNOWN_EXCEPTION;} } catch(...){ rt9477 = nullptr; } \
231  _ML_WARNING_ADD_ON(FUNC_NAME, REASON, HANDLING) \
232  ML_UTILS_NAMESPACE::MLErrorOutput.printAndNotify(ML_WARNING, \
233  ML_PREFIX, "Function=", FUNC_NAME, REASON, HANDLING, \
234  __FILE__, __LINE__, RT_OBJ, rt9477); \
235  /* No showing of assert box here. That would be to drastic. */ \
236  }
237 
238 //--------------------------------------------------------------------------------------------
252 //--------------------------------------------------------------------------------------------
253 #define _ML_PRINT_INFORMATION_DUMP(FUNC_NAME, REASON, HANDLING, RT_OBJ, RTYPE) \
254  { \
255  /* Get runtime information from RTYPE which could be a crashing expression.*/ \
256  const ML_UTILS_NAMESPACE::RuntimeType *rtAx598 = nullptr; \
257  try{ rtAx598 = RTYPE; if (MLAlwaysFalse){ throw ML_UNKNOWN_EXCEPTION;} } catch(...){ rtAx598 = nullptr; }\
258  _ML_INFORMATION_ADD_ON(FUNC_NAME, REASON, HANDLING) \
259  ML_UTILS_NAMESPACE::MLErrorOutput.printAndNotify(ML_INFORMATION, \
260  ML_PREFIX, "Function=", FUNC_NAME, REASON, HANDLING, \
261  __FILE__, __LINE__, RT_OBJ, rtAx598); \
262  /* No showing of assert box here. That would be to drastic. */ \
263  }
265 
266 
267 
268 //--------------------------------------------------------------------------------------------
269 //
272 //
273 //--------------------------------------------------------------------------------------------
274 //--------------------------------------------------------------------------------------------
277 //--------------------------------------------------------------------------------------------
278 #define ML_PRINT_FATAL_ERROR_DUMP(FUNC_NAME, REASON, HANDLING, RT_OBJ) \
279  /* Use OR with MLAlwaysFalse to avoid compiler warnings about unreachable code when constant RT_OBJ is passed from other macro. */ \
280  _ML_PRINT_FATAL_ERROR_DUMP(FUNC_NAME, REASON, HANDLING, RT_OBJ, (((RT_OBJ) || MLAlwaysFalse) ? (RT_OBJ)->getTypeId() : nullptr))
281 
282 //--------------------------------------------------------------------------------------------
285 //--------------------------------------------------------------------------------------------
286 #define ML_PRINT_FATAL_ERROR_DUMP_NON_NULL(FUNC_NAME, REASON, HANDLING, RT_OBJ) \
287  _ML_PRINT_FATAL_ERROR_DUMP(FUNC_NAME, REASON, HANDLING, RT_OBJ, (RT_OBJ)->getTypeId())
288 
289 //--------------------------------------------------------------------------------------------
292 //--------------------------------------------------------------------------------------------
293 #define ML_PRINT_ERROR_DUMP(FUNC_NAME, REASON, HANDLING, RT_OBJ) \
294  /* Use OR with MLAlwaysFalse to avoid compiler warnings about unreachable code when constant RT_OBJ is passed from other macro. */ \
295  _ML_PRINT_ERROR_DUMP(FUNC_NAME, REASON, HANDLING, RT_OBJ, (((RT_OBJ) || MLAlwaysFalse) ? (RT_OBJ)->getTypeId() : nullptr))
296 
297 //--------------------------------------------------------------------------------------------
300 //--------------------------------------------------------------------------------------------
301 #define ML_PRINT_ERROR_DUMP_NON_NULL(FUNC_NAME, REASON, HANDLING, RT_OBJ) \
302  _ML_PRINT_ERROR_DUMP(FUNC_NAME, REASON, HANDLING, RT_OBJ, (RT_OBJ)->getTypeId())
303 
304 //--------------------------------------------------------------------------------------------
307 //--------------------------------------------------------------------------------------------
308 #define ML_PRINT_WARNING_DUMP(FUNC_NAME, REASON, HANDLING, RT_OBJ) \
309  /* Use OR with MLAlwaysFalse to avoid compiler warnings about unreachable code when constant RT_OBJ is passed from other macro. */ \
310  _ML_PRINT_WARNING_DUMP(FUNC_NAME, REASON, HANDLING, RT_OBJ, (((RT_OBJ) || MLAlwaysFalse) ? (RT_OBJ)->getTypeId() : nullptr))
311 
312 //--------------------------------------------------------------------------------------------
315 //--------------------------------------------------------------------------------------------
316 #define ML_PRINT_INFORMATION_DUMP(FUNC_NAME, REASON, HANDLING, RT_OBJ) \
317  /* Use OR with MLAlwaysFalse to avoid compiler warnings about unreachable code when constant RT_OBJ is passed from other macro. */ \
318  _ML_PRINT_INFORMATION_DUMP(FUNC_NAME, REASON, HANDLING, RT_OBJ, (((RT_OBJ) || MLAlwaysFalse) ? (RT_OBJ)->getTypeId() : nullptr))
319 
321 
322 
323 
324 //--------------------------------------------------------------------------------------------
325 //
328 //
329 //--------------------------------------------------------------------------------------------
330 
331 //--------------------------------------------------------------------------------------------
336 //--------------------------------------------------------------------------------------------
337 #define ML_PRINT_FATAL_ERROR(FUNC_NAME, REASON, HANDLING) \
338  _ML_PRINT_FATAL_ERROR_DUMP(FUNC_NAME, REASON, HANDLING, nullptr, nullptr)
339 
340 //--------------------------------------------------------------------------------------------
345 //--------------------------------------------------------------------------------------------
346 #define ML_PRINT_ERROR(FUNC_NAME, REASON, HANDLING) \
347  _ML_PRINT_ERROR_DUMP(FUNC_NAME, REASON, HANDLING, nullptr, nullptr)
348 
349 //--------------------------------------------------------------------------------------------
354 //--------------------------------------------------------------------------------------------
355 #define ML_PRINT_WARNING(FUNC_NAME, REASON, HANDLING) \
356  _ML_PRINT_WARNING_DUMP(FUNC_NAME, REASON, HANDLING, nullptr, nullptr)
357 
358 //--------------------------------------------------------------------------------------------
363 //--------------------------------------------------------------------------------------------
364 #define ML_PRINT_INFORMATION(FUNC_NAME, REASON, HANDLING) \
365  _ML_PRINT_INFORMATION_DUMP(FUNC_NAME, REASON, HANDLING, nullptr, nullptr)
366 
367 //--------------------------------------------------------------------------------------------
371 //--------------------------------------------------------------------------------------------
372 #define ML_PRINT_INFO(FUNC_NAME, HANDLING) \
373  _ML_PRINT_INFORMATION_DUMP(FUNC_NAME, ML_RESULT_OK, HANDLING, nullptr, nullptr)
374 
376 
377 
378 
379 
380 #endif
381 
382