CrossFramework Library
|
00001 00006 #if defined(XBASE_INCLUDED_RUNTIMEASSERT_HPP) 00007 #else 00008 #define XBASE_INCLUDED_RUNTIMEASSERT_HPP 00009 00010 //----------------------------------------------------------- 00011 #include <XBase/BuiltInTypes.hpp> 00012 #include <XBase/Console.hpp> 00013 #include <XBase/PointerCheck.hpp> 00014 #include <XBase/RuntimeError.hpp> 00015 #include <XBase/ShortString.hpp> 00016 #include <XBase/ToShortString.hpp> 00017 #include <XBase/ValueInRange.hpp> 00018 00019 //------------------------------------------------------------ 00020 namespace XBase { 00021 // 実行時Assertを扱う空間。 00022 struct RuntimeAssert 00023 { 00024 static const char* const Separator; // 分離する文字列。 00025 static const char* const Header; // ヘッダ。 00026 static const char* const FileLineFmt; // ファイルと行数。 00027 static const char* const LabelCondition; // ラベル:Condition。 00028 static const char* const LabelMessage; // ラベル:Message。 00029 }; 00030 } 00031 00032 // 実行時Assertの本体。XBASE_CONFIG_ENABLE_RUNTIME_ERRORが定義されているときに動作。 00033 #if defined(XBASE_CONFIG_ENABLE_RUNTIME_ERROR) 00034 #define XBASE_RUNTIME_ASSERT( aCond , ... ) \ 00035 do \ 00036 { \ 00037 if( !(aCond) ) \ 00038 { \ 00039 XBASE_COUT_LINE( ::XBase::RuntimeAssert::Separator ); \ 00040 XBASE_COUT_LINE_WITH_TIME( ::XBase::RuntimeAssert::Header ); \ 00041 XBASE_COUTFMT_LINE( ::XBase::RuntimeAssert::FileLineFmt , __FILE__ , __LINE__ ); \ 00042 XBASE_COUT( ::XBase::RuntimeAssert::LabelCondition ); \ 00043 XBASE_COUT_LINE( #aCond ); \ 00044 XBASE_COUT( ::XBase::RuntimeAssert::LabelMessage ); \ 00045 XBASE_COUTFMT_LINE( __VA_ARGS__ , #aCond ); \ 00046 XBASE_COUT_LINE( ::XBase::RuntimeAssert::Separator ); \ 00047 ::XBase::RuntimeError::OnError(); \ 00048 while(1){} \ 00049 } \ 00050 }while(0) 00051 00052 #else 00053 #define XBASE_RUNTIME_ASSERT( aCond , ... ) do{}while(false) 00054 #endif 00055 00056 00057 //----------------------------------------------------------- 00059 00060 00061 00062 00073 #define XBASE_ASSERT_MSG( aCond , aMsg ) XBASE_RUNTIME_ASSERT( aCond , "%s" , aMsg ) 00074 00085 #define XBASE_ASSERT_MSGFMT( aCond , aFormat , ... ) XBASE_RUNTIME_ASSERT( aCond , aFormat , __VA_ARGS__ ) 00086 00096 #define XBASE_ASSERT( aCond ) XBASE_ASSERT_MSG( aCond , #aCond ) 00097 00107 #define XBASE_SHOULD_NULL_ASSERT( aVal ) \ 00108 XBASE_ASSERT_MSG( (aVal)==0 \ 00109 , "Value is not Null (%s)\n" \ 00110 , XBASE_TO_SHORT_STRING( aVal ).readPtr() \ 00111 ) 00112 00122 #define XBASE_NOT_NULL_ASSERT( aVal ) XBASE_ASSERT_MSG( (aVal)!=0 , "Value is Null\n" ) 00123 00133 #define XBASE_POINTER_ASSERT( aVal ) do { if ( !::XBase::PointerCheck::IsValid( aVal ) ) { XBASE_INVALID_VALUE_ERROR( reinterpret_cast< ::XBase::pword_t >( aVal ) ); } } while(false) 00134 00144 #define XBASE_NOT_REACH_ASSERT_MSGFMT( aFormat , ... ) XBASE_ASSERT_MSGFMT( false , aFormat , __VA_ARGS__ ) 00145 #define XBASE_NOT_REACH_ASSERT_MSG( aMsg ) XBASE_ASSERT_MSG( false , aMsg ) 00146 00155 #define XBASE_NOT_REACH_ASSERT() XBASE_NOT_REACH_ASSERT_MSG( "Should nod reach here.\n" ) 00156 00165 #define XBASE_INVALID_VALUE_ERROR( aVal ) \ 00166 XBASE_NOT_REACH_ASSERT_MSGFMT( \ 00167 "%s is invalid value(%s)\n" \ 00168 , #aVal \ 00169 , XBASE_TO_SHORT_STRING(aVal).readPtr() \ 00170 ) 00171 00182 #define XBASE_EQUALS_ASSERT( aVal1 , aVal2 ) \ 00183 XBASE_ASSERT_MSGFMT( aVal1 == aVal2 \ 00184 , "%s(%s) is not equals %s(%s).\n" \ 00185 , #aVal1 , XBASE_TO_SHORT_STRING( aVal1 ).readPtr() \ 00186 , #aVal2 , XBASE_TO_SHORT_STRING( aVal2 ).readPtr() \ 00187 ) 00188 00199 #define XBASE_NOT_EQUALS_ASSERT( aVal1 , aVal2 ) \ 00200 XBASE_ASSERT_MSGFMT( aVal1 != aVal2 \ 00201 , "%s(%s) is quals %s(%s).\n" \ 00202 , #aVal1 , XBASE_TO_SHORT_STRING( aVal1 ).readPtr() \ 00203 , #aVal2 , XBASE_TO_SHORT_STRING( aVal2 ).readPtr() \ 00204 ) 00205 00215 #if defined(XBASE_CONFIG_ENABLE_RUNTIME_ERROR) 00216 #define XBASE_TEST( aCond ) (aCond) 00217 #else 00218 #define XBASE_TEST( aCond ) do{}while(false) 00219 #endif 00220 00230 #define XBASE_RANGE_ASSERT_EMAX( aVal , aMaxVal ) XBASE_RANGE_ASSERT_CORE_FVM( XBASE_IS_VALUE_IN_RANGE_EMAX , aVal , aMaxVal ) 00231 00240 #define XBASE_RANGE_ASSERT_EMIN( aMinVal , aVal ) XBASE_RANGE_ASSERT_CORE_FMV( XBASE_IS_VALUE_IN_RANGE_EMIN , aMinVal , aVal ) 00241 00250 #define XBASE_RANGE_ASSERT_EMIN_EMAX( aMinVal , aVal , aMaxVal ) XBASE_RANGE_ASSERT_CORE_FMVM( XBASE_IS_VALUE_IN_RANGE_EMIN_EMAX , aMinVal , aVal , aMaxVal ) 00251 00260 #define XBASE_RANGE_ASSERT_EMIN_MAX( aMinVal , aVal , aMaxVal ) XBASE_RANGE_ASSERT_CORE_FMVM( XBASE_IS_VALUE_IN_RANGE_EMIN_MAX , aMinVal , aVal , aMaxVal ) 00261 00270 #define XBASE_RANGE_ASSERT_MAX( aVal , aMaxVal ) XBASE_RANGE_ASSERT_CORE_FVM( XBASE_IS_VALUE_IN_RANGE_MAX , aVal , aMaxVal ) 00271 00272 00281 #define XBASE_RANGE_ASSERT_MIN( aMinVal , aVal ) XBASE_RANGE_ASSERT_CORE_FMV( XBASE_IS_VALUE_IN_RANGE_MIN , aMinVal , aVal ) 00282 00291 #define XBASE_RANGE_ASSERT_MIN_EMAX( aMinVal , aVal , aMaxVal ) XBASE_RANGE_ASSERT_CORE_FMVM( XBASE_IS_VALUE_IN_RANGE_MIN_EMAX , aMinVal , aVal , aMaxVal ) 00292 00298 #define XBASE_ENUM_ASSERT( aEnumType , aEnumValue ) XBASE_RANGE_ASSERT_EMIN_EMAX( int( aEnumType##_MIN ) , int( aEnumValue ) , int( aEnumType##_MAX ) ) 00299 00301 00302 00303 // 範囲チェックアサートの実装。 00304 #define XBASE_RANGE_ASSERT_CORE_FVM( func , aVal , aMaxVal ) \ 00305 XBASE_ASSERT_MSGFMT( func( aVal , aMaxVal ) \ 00306 , "Value is not in range.\n" \ 00307 "aVal : %s \naMaxVal : %s\n" \ 00308 , XBASE_TO_SHORT_STRING( aVal ).readPtr() \ 00309 , XBASE_TO_SHORT_STRING( aMaxVal ).readPtr() \ 00310 ) 00311 #define XBASE_RANGE_ASSERT_CORE_FMV( func , aMinVal , aVal ) \ 00312 XBASE_ASSERT_MSGFMT( func( aMinVal , aVal ) \ 00313 , "Value is not in range.\n" \ 00314 "aMinVal : %s \naVal : %s\n" \ 00315 , XBASE_TO_SHORT_STRING( aMinVal ).readPtr() \ 00316 , XBASE_TO_SHORT_STRING( aVal ).readPtr() \ 00317 ) 00318 #define XBASE_RANGE_ASSERT_CORE_FMVM( func , aMinVal , aVal , aMaxVal ) \ 00319 XBASE_ASSERT_MSGFMT( func( aMinVal , aVal , aMaxVal ) \ 00320 , "Value is not in range.\n" \ 00321 "aMinVal : %s \naVal : %s \naMaxVal : %s\n" \ 00322 , XBASE_TO_SHORT_STRING( aVal ).readPtr() \ 00323 , XBASE_TO_SHORT_STRING( aMaxVal ).readPtr() \ 00324 , XBASE_TO_SHORT_STRING( aMinVal ).readPtr() \ 00325 ) 00326 00327 //----------------------------------------------------------- 00328 #endif 00329 // EOF