ESYS13  Revision_
EsysAssert.h
Go to the documentation of this file.
00001 
00002 /*******************************************************
00003 *
00004 * Copyright (c) 2003-2012 by University of Queensland
00005 * Earth Systems Science Computational Center (ESSCC)
00006 * http://www.uq.edu.au/esscc
00007 *
00008 * Primary Business: Queensland, Australia
00009 * Licensed under the Open Software License version 3.0
00010 * http://www.opensource.org/licenses/osl-3.0.php
00011 *
00012 *******************************************************/
00013 
00014 
00015 #if !defined escript_EsysAssert_20040330_H
00016 #define escript_EsysAssert_20040330_H
00017 #include "system_dep.h"
00029 //
00030 // Note that the ANSI C Standard requires all headers to be idempotent except
00031 // <assert.h> which is explicitly required not to be idempotent (section 4.1.2).
00032 // This version of EsysAssert follows this requirement, consequently this
00033 // part of the header is intentionally outside the single pass guard.
00034 //
00035 
00036 #undef EsysAssert
00037 
00038 #if defined DOASSERT
00039 
00040 //
00041 // DOASSERT is defined, replace EsysAssert with Exception throw
00042 //
00043 
00044 #include "EsysAssertException.h"
00045 #include <sstream>
00046 
00047 namespace esysUtils {
00048 
00049   class ErrStream
00050   {
00051     public:
00052     template <typename Tmpl>
00053     ErrStream& operator<<(Tmpl t)
00054     {
00055       std::stringstream str;
00056       str << t;
00057       m_msg += str.str();
00058       
00059       return *this;
00060     }
00061     
00062     inline
00063     const std::string &toString() const
00064     {
00065       return m_msg;
00066     }
00067 
00068     private:
00069       std::string m_msg;
00070   };
00071 
00072   inline
00073   std::ostream& operator<<(std::ostream& oStream,
00074                                   const ErrStream& errStream)
00075   {
00076     oStream << errStream.toString();
00077     return oStream;
00078   }
00079 
00080 }
00081 
00082 #define EsysAssert(AssertTest,AssertMessage) \
00083    (void)((AssertTest) || \
00084            ((esysUtils::EsysAssertException::assertFailure(#AssertTest, __DATE__, __FILE__, __LINE__, \
00085              (esysUtils::ErrStream()<<AssertMessage).toString())),0),0)
00086 
00087 #else
00088 
00089 //
00090 // DOASSERT os not defined, replace EsysAssert with "NO-OP"
00091 //
00092 
00093 #define EsysAssert(AssertTest,AssertMessage) ((void)0)
00094 
00095 #endif
00096 
00097 #endif