base_assert.hh

Go to the documentation of this file.
00001 /*
00011  * LEGAL:   COPYRIGHT (C) 2004 JIM E. BROOKS
00012  *          THIS SOURCE CODE IS RELEASED UNDER THE TERMS
00013  *          OF THE GNU GENERAL PUBLIC LICENSE VERSION 2 (GPL 2).
00014  *****************************************************************************/
00015 
00016 #ifndef BASE_ASSERT_HH
00017 #define BASE_ASSERT_HH 1
00018 
00019 
00020 
00021 
00022 using namespace std;
00023 
00024 // make DEBUG=1 enables assertions, otherwise disabled.
00025 #if ! DEBUG && ! defined(NDEBUG)
00026 #define NDEBUG  // disable assert()
00027 #endif
00028   // affected by NDEBUG
00029 
00030 namespace base {
00031 
00035 class AssertException : public Exception
00036 {
00037 public:
00038                 AssertException( const string& fileName, int fileLine );
00039     string      GetText( void ) const { return mText; }
00040 private:
00041     string  mText;
00042 };
00043 
00044 
00045 // __LINE__ is a decimal constant, not a string.
00046 #if DEBUG
00047 #define ASSERT( COND ) base::restrict::Assert( (COND), __FILE__, __LINE__ )
00048 namespace restrict { void Assert( bool cond, const char* fileName, int fileLine ); }
00049 #else
00050 #define ASSERT( COND )
00051 #endif
00052 
00053 // ASSERT2() is for pedantic checks that would slow down the normal debug build.
00054 #if DEBUG == 2  // extra debug level
00055 #define ASSERT2( COND ) ASSERT( (COND) )
00056 #else
00057 #define ASSERT2( COND )
00058 #endif
00059 
00060 #if DEBUG
00061 #define ASSERT_UNLESS_EXITING( COND ) base::restrict::AssertUnlessExiting( (COND), __FILE__, __LINE__ )
00062 namespace restrict { void AssertUnlessExiting( bool cond, const char* fileName, int fileLine ); }
00063 #else
00064 #define ASSERT_UNLESS_EXITING( COND )
00065 #endif
00066 
00067 // Assert that a function runs only once.
00068 #if DEBUG
00069 #define ASSERT_RUN_ONCE {{ PERSISTENT int once = 0; ++once; ASSERT( once == 1 ); }}
00070 #else
00071 #define ASSERT_RUN_ONCE
00072 #endif
00073 
00074 // Assert that one instance of a class exists.  Pass Class::msInstanceCnt.
00075 #if DEBUG
00076 #define ASSERT_ONE_INSTANCE_CTOR( INSTANCE_CNT ) {{ ASSERT( (INSTANCE_CNT) == 0 ); ++(INSTANCE_CNT); }}
00077 #define ASSERT_ONE_INSTANCE_DTOR( INSTANCE_CNT ) {{ ASSERT( (INSTANCE_CNT) == 1 ); --(INSTANCE_CNT); }}
00078 #else
00079 #define ASSERT_ONE_INSTANCE_CTOR( INSTANCE_CNT )
00080 #define ASSERT_ONE_INSTANCE_DTOR( INSTANCE_CNT )
00081 #endif
00082 
00083 // AssertOr*() is for error conditions where the program might be able to continue.
00084 // The OR case is in case assert() is disabled by NDEBUG.
00085 #define ASSERT_OR_RETURN( COND )          {{ ASSERT( (COND) ); if ( !(COND) ) return; }}
00086 #define ASSERT_OR_RETURN_VAL( COND, VAL ) {{ ASSERT( (COND) ); if ( !(COND) ) return (VAL); }}
00087 #define ASSERT_OR_GOTO( COND, LABEL )     {{ ASSERT( (COND) ); if ( !(COND) ) goto (LABEL); }}
00088 
00089 // See base_asert.cc
00090 #if DEBUG
00091 void ASSERT_STACK_OBJECT( const void* obj );
00092 void ASSERT_PERSISTENT_OBJECT( const void* obj );
00093 #else
00094 #define ASSERT_STACK_OBJECT( OBJ_PTR )
00095 #define ASSERT_PERSISTENT_OBJECT( OBJ_PTR )
00096 #endif
00097 
00098 #if DEBUG && COMPILE_THREADS
00099 void ASSERT_MAIN_THREAD( void );
00100 #else
00101 #define ASSERT_MAIN_THREAD()
00102 #endif
00103 
00104 } // namespace base
00105 
00106 #endif // BASE_ASSERT_HH
00107 
Palomino 3D Engine documents generated by doxygen 1.5.3 on Fri Nov 23 11:26:06 2007