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