LAMA
/home/brandes/workspace/LAMA/src/lama/exception/LAMAAssert.hpp
Go to the documentation of this file.
00001 
00034 #ifndef LAMA_ASSERT_HPP_
00035 #define LAMA_ASSERT_HPP_
00036 
00037 #include <lama/exception/Exception.hpp>
00038 
00039 #include <sstream>
00040 #include <cstdio>
00041 #include <iostream>
00042 
00043 //Little help for the Eclipse Parser
00044 #ifdef __CDT_PARSER__
00045     #define LAMA_ASSERT_LEVEL_DEBUG
00046 #endif
00047 
00048 #ifndef LAMA_ASSERT_LEVEL_OFF
00049 #    ifndef LAMA_CHECK_ASSERTS
00050 
00059 #        define LAMA_CHECK_ASSERTS
00060 #    endif
00061 #endif // NDEBUG
00062 
00065 template<typename T>
00066 static inline void unused( T const )
00067 {
00068     // do nothing
00069 }
00070 
00071 template<typename T1, typename T2>
00072 static inline void unused(const T1& , const T2&)
00073 {
00074     // do nothing
00075 }
00076 
00077 #ifndef LAMA_CHECK_ASSERTS
00078 
00096 #define LAMA_ASSERT(exp, msg) unused( exp );
00097 
00098 #define LAMA_ASSERT_EQUAL(exp1, exp2) unused( exp1, exp2 );
00099 
00115 #define LAMA_CHECK_ERROR(msg)
00116 
00117 #else // LAMACHECKASSERTS DEFINED
00118 
00119 #define LAMA_ASSERT(exp, msg)                                                           \
00120     if (!(exp))                                                                         \
00121     {                                                                                   \
00122         std::ostringstream errorStr;                                                    \
00123         errorStr << "Assertion Failed in line " << __LINE__;                            \
00124         errorStr << " of file " << __FILE__ << "\n";                                    \
00125         errorStr << "    Message: " << msg << "\n";                                     \
00126         lama::Exception::addCallStack( errorStr );                                      \
00127         throw lama::Exception( errorStr.str() );                                        \
00128     }
00129 
00130 #define LAMA_ASSERT_EQUAL(exp1, exp2)                                                   \
00131         LAMA_ASSERT(exp1 == exp2, #exp1 " = " << exp1                                   \
00132                     << " must be equal to " #exp2 " = " << exp2 )
00133 
00134 #endif // LAMA_CHECK_ASSERTS
00135 
00136 #if defined(LAMA_ASSERT_LEVEL_OFF)
00137 
00138 #define LAMA_ASSERT_ERROR(exp, msg)                                                  \
00139 {                                                                               \
00140     unused( exp );                                                              \
00141     if ( false )                                                                \
00142     {                                                                           \
00143         std::cout<<msg;                                                         \
00144     }                                                                           \
00145 }
00146 #define LAMA_ASSERT_EQUAL_ERROR(exp1, exp2) unused( exp1, exp2 );
00147 #define LAMA_ASSERT_DEBUG(exp, msg)                                                  \
00148 {                                                                               \
00149     unused( exp );                                                              \
00150     if ( false )                                                                \
00151     {                                                                           \
00152         std::cout<<msg;                                                         \
00153     }                                                                           \
00154 }
00155 #define LAMA_ASSERT_EQUAL_DEBUG(exp1, exp2) unused(exp1, exp2 );
00156 
00157 #elif defined(LAMA_ASSERT_LEVEL_ERROR)
00158 
00162 #define LAMA_ASSERT_ERROR(exp, msg)  LAMA_ASSERT(exp, msg)
00163 #define LAMA_ASSERT_DEBUG(exp, msg)  unused( exp );
00164 #define LAMA_ASSERT_EQUAL_ERROR(exp1, exp2)  LAMA_ASSERT_EQUAL(exp1, exp2)
00165 #define LAMA_ASSERT_EQUAL_DEBUG(exp1, exp2)  unused( exp1, exp2 );
00166 
00167 #elif defined(LAMA_ASSERT_LEVEL_DEBUG)
00168 
00172 #define LAMA_ASSERT_DEBUG(exp, msg)  LAMA_ASSERT(exp, msg)
00173 #define LAMA_ASSERT_ERROR(exp, msg)  LAMA_ASSERT(exp, msg)
00174 
00175 #define LAMA_ASSERT_EQUAL_ERROR(exp1, exp2)  LAMA_ASSERT_EQUAL(exp1, exp2)
00176 #define LAMA_ASSERT_EQUAL_DEBUG(exp1, exp2)  LAMA_ASSERT_EQUAL(exp1, exp2)
00177 
00178 #else
00179 
00180 #pragma message( "Please specify LAMA_ASSERT_LEVEL_xxx with xxx = DEBUG, ERROR, or OFF" )
00181 #pragma message( "Will use LAMA_ASSERT_LEVEL_ERROR by default." )
00182 
00186 #define LAMA_ASSERT_ERROR(exp, msg)  LAMA_ASSERT(exp, msg)
00187 #define LAMA_ASSERT_DEBUG(exp, msg)  unused( exp );
00188 #define LAMA_ASSERT_EQUAL_ERROR(exp1, exp2)  LAMA_ASSERT_EQUAL(exp1, exp2)
00189 #define LAMA_ASSERT_EQUAL_DEBUG(exp1, exp2)  unused( exp1, exp2 );
00190 
00191 #endif
00192 
00193 #endif // LAMA_ASSERT_HPP_