00001 // /////////////////////////////////////////////////////////////////////////// 00002 // xrb_assert.hpp by Victor Dods, created 2005/06/25 00003 // /////////////////////////////////////////////////////////////////////////// 00004 // Unless a different license was explicitly granted in writing by the 00005 // copyright holder (Victor Dods), this software is freely distributable under 00006 // the terms of the GNU General Public License, version 2. Any works deriving 00007 // from this work must also be released under the GNU GPL. See the included 00008 // file LICENSE for details. 00009 // /////////////////////////////////////////////////////////////////////////// 00010 00011 #if !defined(_XRB_ASSERT_HPP_) 00012 #define _XRB_ASSERT_HPP_ 00013 00014 // don't include xrb.h here, because xrb.h includes this file. 00015 00016 // NDEBUG is UNdefined in this header, so the assert macro is available. 00017 #include "xrb_debug.hpp" 00018 00019 #include <assert.h> 00020 00021 // /////////////////////////////////////////////////////////////////////////// 00022 // compile-time assert macro 00023 // /////////////////////////////////////////////////////////////////////////// 00024 00026 template <bool condition> struct ThisCompileErrorIsActuallyAFailedCompileTimeAssert; 00027 00028 template <> 00029 struct ThisCompileErrorIsActuallyAFailedCompileTimeAssert<true> 00030 { 00031 enum { BLAH }; 00032 }; 00034 00035 // this assert is intended to be used in the global scope and will produce a 00036 // compile error on conditions that are decidable during compilation 00037 // (e.g. "sizeof(char) == 1"). each call to this macro must supply an assert 00038 // name that is unique to that source file (e.g. CHAR_SIZE_CHECK). 00039 #define GLOBAL_SCOPE_COMPILE_TIME_ASSERT(assert_name, x) \ 00040 namespace { inline int assert_name () { return ThisCompileErrorIsActuallyAFailedCompileTimeAssert<static_cast<bool>(x)>::BLAH; } } 00041 00042 // this assert is intended to be used within the body of a function/method 00043 // and will produce a compile error on conditions that are decidable during 00044 // compilation (e.g. "sizeof(something) == 4"). 00045 #define CODE_SCOPE_COMPILE_TIME_ASSERT(x) \ 00046 ThisCompileErrorIsActuallyAFailedCompileTimeAssert<static_cast<bool>(x)>::BLAH; 00047 00048 // /////////////////////////////////////////////////////////////////////////// 00049 // run-time assert macros 00050 // /////////////////////////////////////////////////////////////////////////// 00051 00052 // assert macro which is ALWAYS compiled in 00053 #define ASSERT0(x) do { assert(x); } while (false) 00054 00055 // normal assert macro, should be used generously in non-speed-critical code 00056 #if XRB_DEBUG_LEVEL >= 1 00057 #define ASSERT1(x) do { assert(x); } while (false) 00058 #else // XRB_DEBUG_LEVEL < 1 00059 #define ASSERT1(x) do { } while (false) 00060 #endif // XRB_DEBUG_LEVEL < 1 00061 00062 // pedantic assert macro (asserts which are there just for correctness' sake, 00063 // but are extremely unlikely to fail). to be used in speed-critical code 00064 // sections. 00065 #if XRB_DEBUG_LEVEL >= 2 00066 #define ASSERT2(x) do { assert(x); } while (false) 00067 #else // XRB_DEBUG_LEVEL < 2 00068 #define ASSERT2(x) do { } while (false) 00069 #endif // XRB_DEBUG_LEVEL < 2 00070 00071 // EXTREMELY pedantic assert macro (asserts which are there just for 00072 // correctness' sake, but are extremely fucking unlikely to fail). to be used 00073 // in the most speed-critical code sections. 00074 #if XRB_DEBUG_LEVEL >= 3 00075 #define ASSERT3(x) do { assert(x); } while (false) 00076 #else // XRB_DEBUG_LEVEL < 3 00077 #define ASSERT3(x) do { } while (false) 00078 #endif // XRB_DEBUG_LEVEL < 3 00079 00080 // assert which is used in Engine2::Entity code for debugging NaNs. 00081 #if defined(XRB_NAN_SANITY_CHECK) 00082 #define ASSERT_NAN_SANITY_CHECK(x) do { assert(x); } while (false) 00083 #else // !defined(XRB_NAN_SANITY_CHECK) 00084 #define ASSERT_NAN_SANITY_CHECK(x) do { } while (false) 00085 #endif // !defined(XRB_NAN_SANITY_CHECK) 00086 00087 #endif // !defined(_XRB_ASSERT_HPP_) 00088