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 
00027 namespace cgatools { namespace util {
00028 
00030     #define CGA_ASSERT_MSG(expr, message)                               \
00031         do                                                              \
00032         {                                                               \
00033             if (!(expr)) {                                              \
00034                 std::cerr << "assert failed: " <<                       \
00035                     __FILE__ << ":" <<                                  \
00036                     __LINE__ << ": " <<                                 \
00037                     #expr   << std::endl <<                             \
00038                     message << std::endl;                               \
00039                 volatile char *p = 0, ch = *p;                          \
00040                 if (ch == '\0') *p = ch; p++; ch = *p;                  \
00041                 std::exit(1);                                           \
00042             }                                                           \
00043         } while (0)
00044 
00045     #define CGA_WARN_MSG(expr, message)                                 \
00046         do                                                              \
00047         {                                                               \
00048             if (!(expr)) {                                              \
00049                 std::cerr << "Warning. Assertion failed: " <<           \
00050                     __FILE__ << ":" <<                                  \
00051                     __LINE__ << ": " <<                                 \
00052                     #expr   << std::endl <<                             \
00053                     message << std::endl;                               \
00054             }                                                           \
00055         } while (0)
00056 
00057     #define CGA_ASSERT(expr) CGA_ASSERT_MSG(expr, "")
00058 
00059     #ifdef NDEBUG
00060 
00062         #define CGA_DBG_ASSERT(expr) (void(0))
00063 
00064     #else // NDEBUG
00065 
00067         #define CGA_DBG_ASSERT(expr) CGA_ASSERT(expr)
00068 
00069     #endif // NDEBUG
00070 
00072     class Exception: public std::exception
00073     {
00074     public:
00075         Exception(const std::string& message);
00076         virtual ~Exception() throw () {}
00077 
00078         const char* what() const throw ();
00079 
00080     private:
00081         std::string message_;
00082     };
00083 
00084     
00085     #define CGA_VOUT(var) "{"<<#var<<"="<<var<<"}"
00086     #define CGA_ASSERT_EQ(var1,var2) CGA_ASSERT_MSG((var1)==(var2), CGA_VOUT(var1)<<CGA_VOUT(var2))
00087     #define CGA_ASSERT_NEQ(var1,var2) CGA_ASSERT_MSG((var1)!=(var2), CGA_VOUT(var1)<<CGA_VOUT(var2))
00088     #define CGA_ASSERT_L(var1,var2) CGA_ASSERT_MSG((var1)<(var2), CGA_VOUT(var1)<<CGA_VOUT(var2))
00089     #define CGA_ASSERT_LE(var1,var2) CGA_ASSERT_MSG((var1)<=(var2), CGA_VOUT(var1)<<CGA_VOUT(var2))
00090 
00091     
00092     #define CGA_ERROR_EX(m_message) \
00093     { \
00094         std::stringstream stx; \
00095         stx << m_message; \
00096         throw cgatools::util::Exception(stx.str()); \
00097     }
00098 
00099 } } 
00100 
00101 #endif // CGATOOLS_UTIL_EXCEPTION_HPP_
00102