ESScript  Revision_
EsysAssert.h
Go to the documentation of this file.
1 
2 /*****************************************************************************
3 *
4 * Copyright (c) 2003-2013 by University of Queensland
5 * http://www.uq.edu.au
6 *
7 * Primary Business: Queensland, Australia
8 * Licensed under the Open Software License version 3.0
9 * http://www.opensource.org/licenses/osl-3.0.php
10 *
11 * Development until 2012 by Earth Systems Science Computational Center (ESSCC)
12 * Development since 2012 by School of Earth Sciences
13 *
14 *****************************************************************************/
15 
16 
17 #if !defined escript_EsysAssert_20040330_H
18 #define escript_EsysAssert_20040330_H
19 #include "system_dep.h"
31 //
32 // Note that the ANSI C Standard requires all headers to be idempotent except
33 // <assert.h> which is explicitly required not to be idempotent (section 4.1.2).
34 // This version of EsysAssert follows this requirement, consequently this
35 // part of the header is intentionally outside the single pass guard.
36 //
37 
38 #undef EsysAssert
39 
40 #if defined DOASSERT
41 
42 //
43 // DOASSERT is defined, replace EsysAssert with Exception throw
44 //
45 
46 #include "EsysAssertException.h"
47 #include <sstream>
48 
49 namespace esysUtils {
50 
51  class ErrStream
52  {
53  public:
54  template <typename Tmpl>
55  ErrStream& operator<<(Tmpl t)
56  {
57  std::stringstream str;
58  str << t;
59  m_msg += str.str();
60 
61  return *this;
62  }
63 
64  inline
65  const std::string &toString() const
66  {
67  return m_msg;
68  }
69 
70  private:
71  std::string m_msg;
72  };
73 
74  inline
75  std::ostream& operator<<(std::ostream& oStream,
76  const ErrStream& errStream)
77  {
78  oStream << errStream.toString();
79  return oStream;
80  }
81 
82 }
83 
84 #define EsysAssert(AssertTest,AssertMessage) \
85  (void)((AssertTest) || \
86  ((esysUtils::EsysAssertException::assertFailure(#AssertTest, __DATE__, __FILE__, __LINE__, \
87  (esysUtils::ErrStream()<<AssertMessage).toString())),0),0)
88 
89 #else
90 
91 //
92 // DOASSERT os not defined, replace EsysAssert with "NO-OP"
93 //
94 
95 #define EsysAssert(AssertTest,AssertMessage) ((void)0)
96 
97 #endif
98 
99 #endif