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