/
sassert.h
67 lines (57 loc) · 1.98 KB
/
sassert.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#ifndef SASSERT_H_
#define SASSERT_H_
#undef assert
/**
* Exceptions of this class are raised when assert() is called with a false
* argument, unless asserts are disabled with NDEBUG. */
class assertexception
{
public:
/**
* Returns a string referencing the assert condition in source
*/
virtual std::string what(void) const
{
std::stringstream ss;
ss << "ASSERT FAILURE: [" << expression_ << "], function " << function_
<< " at " << file_ << ":" << line_ << std::endl;
return ss.str();
}
/**
* The constructor will output a message to out immediately. Execution
* continues as normal.
*
* @param ex The expression that evaluated to false
* @param fl The file in which the assert statement resides
* @param fn The function in which the assert statement resides
* @param ln The line on which the assert statement resides
* @param out The stream to which the assert message is written (defaults to std::cerr)
*/
assertexception(const char* ex, const char* fl, const char* fn,
int ln, std::ostream& out = std::cerr )
: expression_(ex), file_(fl), function_(fn), line_(ln), out_(out)
{
// Send the warning message to the output stream
out_ << what();
}
/**
* Doesn't do anything.
*/
virtual ~assertexception() {}
protected:
const std::string expression_; /**< The failing expression */
const int line_; /**< The line number of the assert() */
const std::string function_; /**< The function calling assert() */
const std::string file_; /**< The file containing the call to assert */
std::ostream& out_; /**< The stream to which the warning message is written */
};
#ifndef NDEBUG
/**
* The assert() macro is provided as a convenient way of throwing an
* assertexception
*/
#define assert(x) (x)?(void)0:throw assertexception(#x,__FILE__,__func__,__LINE__)
#else
#define assert(x) (void)0
#endif
#endif