1 | // |
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo |
---|
3 | // |
---|
4 | // The contents of this file are covered under the licence agreement in the |
---|
5 | // file "LICENCE" distributed with Cforall. |
---|
6 | // |
---|
7 | // assert.h -- |
---|
8 | // |
---|
9 | // Author : Peter A. Buhr |
---|
10 | // Created On : Thu Aug 18 13:19:26 2016 |
---|
11 | // Last Modified By : Andrew Beach |
---|
12 | // Last Modified On : Thu May 23 15:30:00 2017 |
---|
13 | // Update Count : 17 |
---|
14 | // |
---|
15 | |
---|
16 | #pragma once |
---|
17 | // Pragmas for header cleanup tool |
---|
18 | // IWYU pragma: private, include <cassert> |
---|
19 | |
---|
20 | #include_next <cassert> |
---|
21 | |
---|
22 | #include <string> |
---|
23 | |
---|
24 | template < typename ... Params > |
---|
25 | std::string toString( const Params & ... params ); |
---|
26 | |
---|
27 | #ifdef NDEBUG |
---|
28 | |
---|
29 | #define assertf(expr, fmt, ...) (__ASSERT_VOID_ASSERT (0)) |
---|
30 | |
---|
31 | #else |
---|
32 | |
---|
33 | #define assertf(expr, fmt, ...) ((expr) \ |
---|
34 | ? (__ASSERT_VOID_CAST (0)) \ |
---|
35 | : __assert_fail_f( #expr, __FILE__, __LINE__, \ |
---|
36 | __ASSERT_FUNCTION, fmt, ## __VA_ARGS__ )) |
---|
37 | |
---|
38 | void __assert_fail_f( const char *assertion, const char *file, |
---|
39 | unsigned int line, const char *function, |
---|
40 | const char *fmt, ... |
---|
41 | ) __attribute__((noreturn, format(printf, 5, 6))); |
---|
42 | |
---|
43 | #endif |
---|
44 | |
---|
45 | enum StrictAllowNull {NonNull, AllowNull}; |
---|
46 | |
---|
47 | template<typename T, StrictAllowNull nullable = NonNull, typename U> |
---|
48 | static inline T strict_dynamic_cast( const U & src ) { |
---|
49 | if (nullable == AllowNull && src == nullptr) { |
---|
50 | return nullptr; |
---|
51 | } |
---|
52 | assert(src); |
---|
53 | T ret = dynamic_cast<T>(src); |
---|
54 | assertf(ret, "%s", toString(src).c_str()); |
---|
55 | return ret; |
---|
56 | } |
---|
57 | |
---|
58 | extern void abort(const char *fmt, ... ) noexcept __attribute__((noreturn, format(printf, 1, 2))); |
---|
59 | // Local Variables: // |
---|
60 | // tab-width: 4 // |
---|
61 | // mode: c++ // |
---|
62 | // compile-command: "make install" // |
---|
63 | // End: // |
---|