Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
May 1, 2011, 9:02:38 PM (14 years ago)
Author:
rgrieder
Message:

Improved output of OrxAssert (line number, function name and file) and added OrxVerify.
You can use the latter like OrxAssert, but the condition will also be evaluated in release mode. However abort() will still only be called in debug mode.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/trunk/src/libraries/util/OrxAssert.h

    r7401 r8372  
    4040
    4141#include <cassert>
    42 #include "OutputHandler.h"
     42#include "Debug.h"
    4343
    4444#ifndef NDEBUG
     45
    4546/** Run time assertion like assert(), but with an embedded message.
    4647@details
     
    5152    @endcode
    5253*/
    53 #define OrxAssert(Assertion, ErrorMessage) \
    54     Assertion ? ((void)0) : (void)(orxonox::OutputHandler::getOutStream(1) << ErrorMessage << std::endl); \
    55     assert(Assertion)
     54#define OrxAssert(condition, errorMessage) \
     55    do \
     56    { \
     57        if (!(condition)) \
     58        { \
     59            COUT(1) << __FILE__ << "(" << __LINE__ << "): "; \
     60            COUT(1) << "Assertion failed in " << __FUNCTIONNAME__ << std::endl; \
     61            COUT(1) << "Expression: " << #condition << std::endl; \
     62            COUT(1) << "Message   : " << errorMessage << std::endl; \
     63            /* Don't use the condition again to avoid double evaluation */ \
     64            /* Instead, stringify the expression and negate it */ \
     65            assert(!#condition); \
     66        } \
     67    } while (false)
     68
     69/** Works like OrxAssert in debug mode, but also checks the condition in release
     70    mode (no abort() triggered then).
     71@details
     72    The message will be printed as error with COUT(1). <br>
     73    You can use the same magic here as you can with \ref ThrowException
     74    @code
     75        OrxVerify(condition, "Text: " << number << " more text");
     76    @endcode
     77*/
     78#define OrxVerify(condition, errorMessage) \
     79    do \
     80    { \
     81        if (!(condition)) \
     82        { \
     83            COUT(1) << __FILE__ << "(" << __LINE__ << "): "; \
     84            COUT(1) << "Verification failed in " << __FUNCTIONNAME__ << std::endl; \
     85            COUT(1) << "Expression: " << #condition << std::endl; \
     86            COUT(1) << "Message   : " << errorMessage << std::endl; \
     87            /* Don't use the condition again to avoid double evaluation */ \
     88            /* Instead, stringify the expression and negate it */ \
     89            assert(!#condition); \
     90        } \
     91    } while (false)
     92
    5693#else
     94
    5795#define OrxAssert(condition, errorMessage)  ((void)0)
     96
     97#define OrxVerify(condition, errorMessage) \
     98    do \
     99    { \
     100        if (!(condition)) \
     101        { \
     102            COUT(1) << __FILE__ << "(" << __LINE__ << "): "; \
     103            COUT(1) << "Verification failed in " << __FUNCTIONNAME__ << std::endl; \
     104            COUT(1) << "Expression: " << #condition << std::endl; \
     105            COUT(1) << "Message   : " << errorMessage << std::endl; \
     106            /* No assert() in release configuration */ \
     107        } \
     108    } while (false)
     109
    58110#endif
    59111
Note: See TracChangeset for help on using the changeset viewer.