HyperHDG
hy_assert.hxx
Go to the documentation of this file.
1 /*!*************************************************************************************************
2  * \file hy_assert.hxx
3  * \brief This file provides the function \c hy_assert.
4  *
5  * This is a wrapper file to provide a function that allows to use assertions that are similar to
6  * those provided by cassert. That is, we define a macro \c hy_assert that implements assert.
7  * If a user wants to use assertions, it is recommended to use \c hy_assert(\c Expr, \c Msg). The
8  * use of the function \c __Hy_Assert is \b not recommended.
9  *
10  * Function \c hy_assert takes two arguments. The first argument is evaluated to a \c boolean and
11  * if this returns \c true, nothing is done. If the argument is \c false, the running program is
12  * terminated and the second argument is displayed as part of an error message. Here, the second
13  * argument is handled as a \c stringstream (without initial \c <<). Thus, for two integers a and b,
14  * a function call might look like: hy_assert( a == b , "Integers have not been the same, since a
15  * turned out to be " << a << " and b was " << b << "." );
16  *
17  * Whether this functionality is active or not can be deduced via setting \c NDEBUG, when the code
18  * is compiled. Using this functionality makes your program significantly slower. However, usage is
19  * highly recommended for testing.
20  *
21  * \authors Guido Kanschat, Heidelberg University, 2020.
22  * \authors Andreas Rupp, Heidelberg University, 2020.
23  **************************************************************************************************/
24 
25 #pragma once // Ensure that file is included only once in a single compilation.
26 
27 #ifndef NDEBUG
28 
29 #include <iostream>
30 #include <sstream>
31 
32 /*!*************************************************************************************************
33  * \brief The assertion to be used within HyperHDG --- deactivate using -DNDEBUG compile flag.
34  *
35  * \param Expr C++ Expression that can be evaluated to \c true or \c false.
36  * \param Msg Message that is to be displayed if \c Expr is evaluated to \c false.
37  **************************************************************************************************/
38 #define hy_assert(Expr, Msg) \
39  { \
40  std::stringstream __hy_assertion_text; \
41  __hy_assertion_text << Msg; \
42  __Hy_Assert(#Expr, Expr, __FILE__, __LINE__, __hy_assertion_text); \
43  } \
44  static_assert(true, "")
45 
46 // -------------------------------------------------------------------------------------------------
48 // -------------------------------------------------------------------------------------------------
49 
50 /*!*************************************************************************************************
51  * \brief This function is not (never) to be used.
52  *
53  * This function is \b not to be used in regular code. It only / solely is defined to allow the use
54  * of function \c hy_assert( \c Expr, \c Msg) which is implemented as a macro in file HyAssert.hxx.
55  *
56  * \authors Guido Kanschat, Heidelberg University, 2020.
57  * \authors Andreas Rupp, Heidelberg University, 2020.
58  **************************************************************************************************/
59 inline void __Hy_Assert(const char* expr_str,
60  bool expr,
61  const char* file,
62  int line,
63  std::stringstream& msg)
64 {
65  if (!expr)
66  {
67  std::cerr << "Assert failed: " << msg.str() << std::endl
68  << "Expected: " << expr_str << std::endl
69  << "Source: " << file << ", line " << line << std::endl;
70  abort();
71  }
72 }
73 
74 #else // alternative branch of ifndef NDEBUG
75 #define hy_assert(Expr, Msg) \
76  { \
77  ; \
78  }
79 #endif // end of ifndef NDEBUG
80 
81 // -------------------------------------------------------------------------------------------------
83 // -------------------------------------------------------------------------------------------------
file
to the best of my is covered under an appropriate open source license and I have the right under that license to submit that work with whether created in whole or in part by under the same open source as indicated in the file
Definition: DeveloperCertificateOfOrigin.txt:27