BornAgain  1.19.79
Open-source research software to simulate and fit neutron and x-ray reflectometry and grazing-incidence small-angle scattering
Assert.h
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file Base/Util/Assert.h
6 //! @brief Defines the macro ASSERT.
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2018
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
15 #ifdef SWIG
16 #error no need to expose this header to Swig
17 #endif
18 
19 #ifndef USER_API
20 #ifndef BORNAGAIN_BASE_UTIL_ASSERT_H
21 #define BORNAGAIN_BASE_UTIL_ASSERT_H
22 
23 // ASSERT must be declared as a macro, not a function, in order for the error
24 // message to correctly report the source line where the assertion failed.
25 
26 // For an alternative implementation that calls qFatal, see Base/Utils/Assert.h before 29oct20.
27 
28 
29 #ifdef BA_DEBUG
30 
31 #include <csignal>
32 #include <iostream>
33 #define ASSERT(condition) \
34  if (!(condition)) { \
35  std::cerr << "Assertion " << (#condition) << " failed in " << __FILE__ << ", line " \
36  << __LINE__ << std::endl; \
37  std::raise(11); /* abort so that we can inspect the backtrace */ \
38  throw std::runtime_error("assertion failed ... and we should never get here"); \
39  }
40 
41 #else // not BA_DEBUG
42 
43 #include <sstream>
44 #include <stdexcept>
45 #define ASSERT(condition) \
46  if (!(condition)) { \
47  std::stringstream msg; \
48  msg << "Assertion " << (#condition) << " failed in " << __FILE__ << ", line " << __LINE__; \
49  throw std::runtime_error(msg.str()); \
50  }
51 
52 #endif // BA_DEBUG
53 
54 #endif // BORNAGAIN_BASE_UTIL_ASSERT_H
55 #endif // USER_API