LehrFEM++ 1.0.0
A simple Finite Element Library for teaching
lf_assert.h
1
9#ifndef __c3c605c9e48646758bf03fab65d52836
10#define __c3c605c9e48646758bf03fab65d52836
11
12#include <boost/assert.hpp>
13#include <sstream>
14#include <stdexcept>
15#include <string>
16
17namespace lf::base {
18
19void AssertionFailed(const std::string& expr, const std::string& file,
20 long line, const std::string& msg);
21
22} // namespace lf::base
23
33#define LF_VERIFY_MSG(expr, msg) \
34 { \
35 if (!(expr)) { \
36 std::stringstream ss; \
37 ss << msg; /* NOLINT */ \
38 ::lf::base::AssertionFailed(#expr, __FILE__, __LINE__, ss.str()); \
39 throw std::runtime_error("this code should not be reached"); \
40 } \
41 }
42
43#ifdef NDEBUG
44#define LF_ASSERT_MSG_CONSTEXPR(expr, msg) ((void)0)
45#define LF_ASSERT_MSG(expr, msg) ((void)0)
46#else
47#define LF_ASSERT_MSG_CONSTEXPR(expr, msg) \
48 { \
49 if (!(expr)) throw std::runtime_error(msg); \
50 }
51
52#define LF_ASSERT_MSG(expr, msg) \
53 { \
54 if (!(expr)) { \
55 std::stringstream ss; \
56 ss << msg; /* NOLINT */ \
57 ::lf::base::AssertionFailed(#expr, __FILE__, __LINE__, ss.str()); \
58 throw std::runtime_error("this code should not be reached"); \
59 } \
60 }
61#endif
62
63// And now we will redefine eigen_assert if needed:
64#ifdef LF_REDIRECT_ASSERTS
65#ifdef eigen_assert
66#ifndef eigen_assert_redirected
67// eigen_assert has already been defined, but not by us!
68#ifdef _MSC_VER
69#pragma message( \
70 "WARNING: Eigen has been included before all LehrFEM++ headers but LF_REDIRECT_ASSERTS=On in cmake. Not all Eigen Asserts may print a stacktrace! https://craffael.github.io/lehrfempp/eigen_stacktrace_warning.html")
71#else
72#warning \
73 "Eigen has been included before all LehrFEM++ headers but LF_REDIRECT_ASSERTS=On in cmake. Not all Eigen Asserts may print a stacktrace! https://craffael.github.io/lehrfempp/eigen_stacktrace_warning.html"
74#endif
75#undef eigen_assert
76#define eigen_assert(x) LF_ASSERT_MSG(x, "")
77#define eigen_assert_redirected
78#endif
79#else
80#define eigen_assert(x) LF_ASSERT_MSG(x, "")
81#define eigen_assert_redirected
82#endif
83#endif
84#endif // __c3c605c9e48646758bf03fab65d52836
Contains basic functionality that is used by other parts of LehrFEM++.
Definition: base.h:15
void AssertionFailed(const std::string &expr, const std::string &file, long line, const std::string &msg)
Definition: lf_assert.cc:11