/* * Assert.hpp * * Created on: Mar 18, 2010 * Author: crueger */ #ifndef ASSERT_HPP_ #define ASSERT_HPP_ #include #include namespace Assert{ typedef void (*hook_t)(void); enum Action {Ask,Abort,Throw,Ignore,MAX_ACTION}; extern const char ActionKeys[MAX_ACTION]; extern const char* ActionNames[MAX_ACTION]; class AssertionFailure{ public: AssertionFailure(std::string _condition, std::string _file, int _line, std::string _message); std::string getFile(); int getLine(); std::string getMessage(); std::ostream& operator<<(std::ostream&); private: std::string condition; std::string file; int line; std::string message; }; } #ifndef NDEBUG #ifndef STRINGIFY #define STRINGIFY(x) #x #endif #ifdef __GNUC__ // on gcc we know how to exit to the Debugger #define DEBUG_BREAK __builtin_trap() #else #define DEBUG_BREAK exit(1) #endif #define ASSERT(condition,message) \ do{\ static bool ignore = false;\ if(!ignore){\ if(_my_assert::check((condition),STRINGIFY(condition),(message),__FILE__,__LINE__,ignore)){\ _my_assert::doHooks();\ DEBUG_BREAK;\ }\ } \ }while(0) #define ASSERT_DO(action) do{_my_assert::setDefault(action);}while(0) #define ASSERT_HOOK(hook) do{_my_assert::addHook(hook);}while(0) #define ASSERT_UNHOOK(hook) do{_my_assert::removeHook(hook);}while(0) #define ASSERT_DEFAULT (_myAssert::printDefault()) #else // we need to do something, so this is the usual solution (e.g. assert.h) #define ASSERT(condition,message) (void)(0) #define ASSERT_DO(action) (void)(0) #define ASSERT_HOOK(hook) (void)(0) #define ASSERT_UNHOOK(hook) (void)(0) #define ASSERT_DEFAULT std::string("Deactivated") #endif //! @cond class _my_assert{ public: static bool check(const bool res, const char* condition, const char* message, const char* filename, const int line, bool& ignore); static void addHook(Assert::hook_t hook); static void removeHook(Assert::hook_t hook); static void doHooks(); static void setDefault(Assert::Action); static Assert::Action getDefault(); static std::string printDefault(); private: static Assert::Action defaultAction; static std::vector hooks; }; //! @endcond #endif /* ASSERT_HPP_ */