00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #ifndef CGATOOLS_UTIL_EXCEPTION_HPP_
00016 #define CGATOOLS_UTIL_EXCEPTION_HPP_ 1
00017
00020
00021 #include "cgatools/core.hpp"
00022 #include <cstdlib>
00023 #include <exception>
00024 #include <string>
00025 #include <iostream>
00026 #include <sstream>
00027
00028 namespace cgatools { namespace util {
00029
00031 #define CGA_ASSERT_MSG(expr, message) \
00032 do \
00033 { \
00034 if (!(expr)) { \
00035 std::cerr << "assert failed: " << \
00036 __FILE__ << ":" << \
00037 __LINE__ << ": " << \
00038 #expr << std::endl << \
00039 message << std::endl; \
00040 volatile char *p = 0, ch = *p; \
00041 if (ch == '\0') *p = ch; p++; ch = *p; \
00042 std::exit(1); \
00043 } \
00044 } while (0)
00045
00046 #define CGA_WARN_MSG(expr, message) \
00047 do \
00048 { \
00049 if (!(expr)) { \
00050 std::cerr << "Warning. Assertion failed: " << \
00051 __FILE__ << ":" << \
00052 __LINE__ << ": " << \
00053 #expr << std::endl << \
00054 message << std::endl; \
00055 } \
00056 } while (0)
00057
00058 #define CGA_ASSERT(expr) CGA_ASSERT_MSG(expr, "")
00059
00060 #ifdef NDEBUG
00061
00063 #define CGA_DBG_ASSERT(expr) (void(0))
00064
00065 #else // NDEBUG
00066
00068 #define CGA_DBG_ASSERT(expr) CGA_ASSERT(expr)
00069
00070 #endif // NDEBUG
00071
00073 class Exception: public std::exception
00074 {
00075 public:
00076 Exception(const std::string& message);
00077 virtual ~Exception() throw () {}
00078
00079 const char* what() const throw ();
00080
00081 private:
00082 std::string message_;
00083 };
00084
00085
00086 #define CGA_VOUT(var) "{"<<#var<<"="<<var<<"}"
00087 #define CGA_ASSERT_EQ(var1,var2) CGA_ASSERT_MSG((var1)==(var2), CGA_VOUT(var1)<<CGA_VOUT(var2))
00088 #define CGA_ASSERT_NEQ(var1,var2) CGA_ASSERT_MSG((var1)!=(var2), CGA_VOUT(var1)<<CGA_VOUT(var2))
00089 #define CGA_ASSERT_L(var1,var2) CGA_ASSERT_MSG((var1)<(var2), CGA_VOUT(var1)<<CGA_VOUT(var2))
00090 #define CGA_ASSERT_LE(var1,var2) CGA_ASSERT_MSG((var1)<=(var2), CGA_VOUT(var1)<<CGA_VOUT(var2))
00091
00092
00093 #define CGA_ERROR_EX(m_message) \
00094 { \
00095 std::stringstream stx; \
00096 stx << m_message; \
00097 throw cgatools::util::Exception(stx.str()); \
00098 }
00099
00100 } }
00101
00102 #endif // CGATOOLS_UTIL_EXCEPTION_HPP_
00103