/* * Assert.hpp * * Created on: Mar 18, 2010 * Author: crueger */ #ifndef ASSERT_HPP_ #define ASSERT_HPP_ #include #include /** * \file Helpers/Assert.hpp *

ASSERT Howto

* *

Introduction

* * ASSERT() is a small macro that allows easier debugging, when it is widely used. The custom * ASSERT macro defined in this file works mainly the same way as the assert() macro that * is defined in the Ansi-C standard, but includes a few nice additions. * *

What ASSERT() does

* * ASSERT can be used to make sure that a condition that always needs to be true for the code to * work correctly is holding. If you have a function that takes a value greater than 0 and a value * smaller than 0 indicates a mistake you should always do it the following way:
* @code * void foo(int a) // a should be greater 0 * { * ASSERT(a>0,"Parameter passed to foo was smaller than 0"); * ... * } * @endcode * * (Note: some people say, that assertions like these should not be used to check function parameters. * This is mainly due to the reason, that a failed assertion will show up inside the function. The buggy * code however is at a completely different place, i.e. at the callers side. Always put the * Assertions as close to the code that produces the value as possible, when looking at function * parameters however this would mean, that any code calling foo would have an ASSERT(...) before * it, which makes it easy to forget the Assertion at some places. Also this makes an easy example.) * * If the condition inside the ASSERT does not evaluate to true the user is shown a message, including * the condition that failed, the line in which the failure was observed and the message of the assertion. * In the above case that would look something like this:
* @code * Assertion "a>0" failed in foo.cpp in line 3. * Assertion Message: Parameter passed to foo was smaller than 0 * @endcode * * In normal conditions, i.e. when no default action is set (see below for default actions) the user * is then shown a short choice menu, on how to handle the assertion. The user can choose to abort the * program, throw an exception of type AssertionFailure that contains the file, line and message, * ignore the assertion or even to always ignore the assertion at that point (i.e. the ASSERT() macro * at this file and line is fully disabled). * * Both ASSERT() and assert() handle debugging in the same way, i.e. they are only used when the * NDEBUG macro is not defined. If the NDEBUG macro is defined, for example using a CXXFLAG then * all asserts and ASSERTs will be disabled in the compiled program. That way in a end-user version * all assertions can be removed with a single switch, thus not hassling the end-user with potential * bugs. * *

Special functions of ASSERT()

* * Compared to the standard assert() macro the custom ASSERT() contains a few special functions. As * first it is possible to set a global default behavior that is used anytime an assertion fails. * This default behavior can be either of Assert::Ask, Assert::Abort, Assert::Throw or Assert::ignore. * The default behavior is set using the ASSERT_DO() macro. For example if you want to check in a * unittest that wrong code at another point actually makes a certain assert fail you could set * ASSERT_DO(Assert::Throw) to make sure a exception is thrown and catch that exception using * the CPPUNIT_ASSERT_THROW() macro. The current set default behavior can be queried as a string * using the ASSERT_DEFAULT macro. * * As a second enhancement it is possible to install callback functions as hooks that will be executed * when an assertion aborts the program. These callback functions could for example be used to flush * any open streams, thus making sure files on the disk are not corrupted by a unexpected abortion. * It would also be possible to install functions that produce some kind of "coredump" of important * internal data-structures, thus giving the person looking for the bug some valuable information. * These assertion hooks should however not be used to clean up the reserved memory of the program, * because a) this memory is under normal circumstances reclaimed by the OS anyway, once the program * has aborted and b) the memory might still contain some hints that could be useful when running * the program inside a debugger and which could be destroyed by the clean-up. To use the hooking * mechanism you can simply use the ASSERT_HOOK() macro, passing this macro any kind of void function. * For example:
* @code * void foo(){ * // produce a coredump * ... * // close and flush all open handles * ... * } * * int main(int argc, char **argv){ * ASSERT_HOOK(foo); * ... * return 0; * } * @endcode * * All hooks will be executed in the reverse order of hooking, i.e. the function hooked last will be * executed first when the abortion is handled. It is also possible to remove a hook to any function * using the ASSERT_UNHOOK() macro and passing it the pointer to the function one wants to remove. * * Assertion hooks will only be executed when the program is terminated by an assertion using the * abort mechanism. They will not be executed when the program exits in any other way. They also * wont be executed when the assertion is ignored or an exception is thrown (even when the exception * is not caught and thus terminates the program). * *

Rules for using ASSERT()

* * The rules for using ASSERT() are basically the same ones that can be used as guidlines for the * standard assert() macro. So if you think you know those guidelines you can skip the following. * * * *

Differences between ASSERT() and assert()

* * This chapter is to explain why a custom ASSERT() macro was introduced and should be used in place * of the standard assert(). Here are the main differences between ASSERT() and assert(). * * * *

Tips and tricks and FAQ

* * */ 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_ */