1 |
jgs |
82 |
/* |
2 |
|
|
****************************************************************************** |
3 |
|
|
* * |
4 |
|
|
* COPYRIGHT ACcESS 2004 - All Rights Reserved * |
5 |
|
|
* * |
6 |
|
|
* This software is the property of ACcESS. No part of this code * |
7 |
|
|
* may be copied in any form or by any means without the expressed written * |
8 |
|
|
* consent of ACcESS. Copying, use or modification of this software * |
9 |
|
|
* by any unauthorised person is illegal unless that person has a software * |
10 |
|
|
* license agreement with ACcESS. * |
11 |
|
|
* * |
12 |
|
|
****************************************************************************** |
13 |
|
|
*/ |
14 |
|
|
|
15 |
jgs |
100 |
#if !defined escript_EsysAssert_20040330_H |
16 |
jgs |
82 |
#define escript_EsysAssert_20040330_H |
17 |
|
|
|
18 |
|
|
/** |
19 |
|
|
\brief |
20 |
|
|
EsysAssert is a MACRO that will throw an exception if the boolean |
21 |
jgs |
100 |
condition is false. |
22 |
jgs |
82 |
|
23 |
|
|
Description: |
24 |
|
|
EsysAssert is conditionally compiled into code only when DOASSERT is |
25 |
|
|
defined. When DOASSERT is not defined, the EsysAssert statement is |
26 |
|
|
entirely removed from code. |
27 |
|
|
*/ |
28 |
|
|
|
29 |
|
|
// |
30 |
|
|
// Note that the ANSI C Standard requires all headers to be idempotent except |
31 |
jgs |
100 |
// <assert.h> which is explicitly required not to be idempotent |
32 |
|
|
// (section 4.1.2). |
33 |
jgs |
82 |
// This version of EsysAssert follows this requirement, consequently this |
34 |
jgs |
100 |
// part of the header is intentionally outside the single pass guard |
35 |
jgs |
82 |
// |
36 |
|
|
|
37 |
|
|
#undef EsysAssert |
38 |
|
|
|
39 |
|
|
#if defined DOASSERT |
40 |
|
|
// |
41 |
|
|
// DOASSERT is defined, replace EsysAssert with Exception throw |
42 |
|
|
// |
43 |
|
|
#include "EsysAssertException.h" |
44 |
|
|
#include <sstream> |
45 |
|
|
|
46 |
|
|
namespace esysUtils { |
47 |
|
|
|
48 |
|
|
class ErrStream |
49 |
|
|
{ |
50 |
|
|
public: |
51 |
|
|
template <typename Tmpl> |
52 |
|
|
ErrStream& operator<<(Tmpl t) |
53 |
|
|
{ |
54 |
|
|
std::stringstream str; |
55 |
|
|
str << t; |
56 |
|
|
m_msg += str.str(); |
57 |
|
|
|
58 |
|
|
return *this; |
59 |
|
|
} |
60 |
|
|
|
61 |
|
|
const std::string &toString() const |
62 |
|
|
{ |
63 |
|
|
return m_msg; |
64 |
|
|
} |
65 |
|
|
|
66 |
|
|
private: |
67 |
|
|
std::string m_msg; |
68 |
|
|
}; |
69 |
|
|
|
70 |
|
|
inline std::ostream& operator<<(std::ostream& oStream, const ErrStream& errStream) |
71 |
|
|
{ |
72 |
|
|
oStream << errStream.toString(); |
73 |
|
|
return oStream; |
74 |
|
|
} |
75 |
|
|
|
76 |
|
|
} |
77 |
|
|
|
78 |
|
|
#define EsysAssert(AssertTest,AssertMessage) \ |
79 |
jgs |
100 |
(void)((AssertTest) || ((esysUtils::EsysAssertException::assertFailure(#AssertTest, __DATE__, \ |
80 |
|
|
__FILE__, __LINE__, \ |
81 |
|
|
(esysUtils::ErrStream()<<AssertMessage).toString())),0),0) |
82 |
jgs |
82 |
|
83 |
|
|
#else |
84 |
|
|
// |
85 |
|
|
// DOASSERT os not defined, replace EsysAssert with "NO-OP" |
86 |
|
|
// |
87 |
|
|
#define EsysAssert(AssertTest,AssertMessage) ((void)0) |
88 |
|
|
|
89 |
|
|
#endif |
90 |
|
|
|
91 |
|
|
#endif |
92 |
jgs |
100 |
|
93 |
|
|
|
94 |
|
|
|
95 |
|
|
|
96 |
|
|
|