CrossFramework Library

XBase/RuntimeAssert.hpp

説明を見る。
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 
00176 #define XBASE_INVALID_ENUM_ERROR( aVal ) XBASE_INVALID_VALUE_ERROR( int( aVal ) )
00177 
00188 #define XBASE_EQUALS_ASSERT( aVal1 , aVal2 ) \
00189     XBASE_ASSERT_MSGFMT( aVal1 == aVal2 \
00190     , "%s(%s) is not equals %s(%s).\n" \
00191     , #aVal1 , XBASE_TO_SHORT_STRING( aVal1 ).readPtr() \
00192     , #aVal2 , XBASE_TO_SHORT_STRING( aVal2 ).readPtr() \
00193     )
00194 
00205 #define XBASE_NOT_EQUALS_ASSERT( aVal1 , aVal2 ) \
00206     XBASE_ASSERT_MSGFMT( aVal1 != aVal2 \
00207     , "%s(%s) is quals %s(%s).\n" \
00208     , #aVal1 , XBASE_TO_SHORT_STRING( aVal1 ).readPtr() \
00209     , #aVal2 , XBASE_TO_SHORT_STRING( aVal2 ).readPtr() \
00210     )
00211 
00221 #if defined(XBASE_CONFIG_ENABLE_RUNTIME_ERROR)
00222     #define XBASE_TEST( aCond ) (aCond)
00223 #else
00224     #define XBASE_TEST( aCond ) do{}while(false)
00225 #endif
00226 
00236 #define XBASE_RANGE_ASSERT_EMAX( aVal , aMaxVal ) XBASE_RANGE_ASSERT_CORE_FVM( XBASE_IS_VALUE_IN_RANGE_EMAX , aVal , aMaxVal )
00237 
00246 #define XBASE_RANGE_ASSERT_EMIN( aMinVal , aVal ) XBASE_RANGE_ASSERT_CORE_FMV( XBASE_IS_VALUE_IN_RANGE_EMIN , aMinVal , aVal )
00247 
00256 #define XBASE_RANGE_ASSERT_EMIN_EMAX( aMinVal , aVal , aMaxVal ) XBASE_RANGE_ASSERT_CORE_FMVM( XBASE_IS_VALUE_IN_RANGE_EMIN_EMAX , aMinVal , aVal , aMaxVal )
00257 
00266 #define XBASE_RANGE_ASSERT_EMIN_MAX( aMinVal , aVal , aMaxVal ) XBASE_RANGE_ASSERT_CORE_FMVM( XBASE_IS_VALUE_IN_RANGE_EMIN_MAX , aMinVal , aVal , aMaxVal )
00267 
00276 #define XBASE_RANGE_ASSERT_MAX( aVal , aMaxVal ) XBASE_RANGE_ASSERT_CORE_FVM( XBASE_IS_VALUE_IN_RANGE_MAX , aVal , aMaxVal )
00277 
00278 
00287 #define XBASE_RANGE_ASSERT_MIN( aMinVal , aVal ) XBASE_RANGE_ASSERT_CORE_FMV( XBASE_IS_VALUE_IN_RANGE_MIN , aMinVal , aVal )
00288 
00297 #define XBASE_RANGE_ASSERT_MIN_EMAX( aMinVal , aVal , aMaxVal ) XBASE_RANGE_ASSERT_CORE_FMVM( XBASE_IS_VALUE_IN_RANGE_MIN_EMAX , aMinVal , aVal , aMaxVal )
00298  
00304 #define XBASE_ENUM_ASSERT( aEnumType , aEnumValue ) XBASE_RANGE_ASSERT_EMIN_EMAX( int( aEnumType##_MIN ) , int( aEnumValue ) , int( aEnumType##_MAX ) )
00305 
00307 
00308     
00309 // 範囲チェックアサートの実装。
00310 #define XBASE_RANGE_ASSERT_CORE_FVM( func , aVal , aMaxVal ) \
00311     XBASE_ASSERT_MSGFMT( func( aVal , aMaxVal ) \
00312     , "Value is not in range.\n" \
00313       "aVal : %s \naMaxVal : %s\n" \
00314     , XBASE_TO_SHORT_STRING( aVal ).readPtr() \
00315     , XBASE_TO_SHORT_STRING( aMaxVal ).readPtr() \
00316     )
00317 #define XBASE_RANGE_ASSERT_CORE_FMV( func , aMinVal , aVal ) \
00318     XBASE_ASSERT_MSGFMT( func( aMinVal , aVal ) \
00319     , "Value is not in range.\n" \
00320       "aMinVal : %s \naVal : %s\n" \
00321     , XBASE_TO_SHORT_STRING( aMinVal ).readPtr() \
00322     , XBASE_TO_SHORT_STRING( aVal ).readPtr() \
00323     )
00324 #define XBASE_RANGE_ASSERT_CORE_FMVM( func , aMinVal , aVal , aMaxVal ) \
00325     XBASE_ASSERT_MSGFMT( func( aMinVal , aVal , aMaxVal )  \
00326     , "Value is not in range.\n" \
00327     "aMinVal : %s \naVal : %s \naMaxVal : %s\n" \
00328     , XBASE_TO_SHORT_STRING( aVal ).readPtr() \
00329     , XBASE_TO_SHORT_STRING( aMaxVal ).readPtr() \
00330     , XBASE_TO_SHORT_STRING( aMinVal ).readPtr() \
00331     )
00332 
00333 //-----------------------------------------------------------
00334 #endif
00335 // EOF
 全て クラス ネームスペース ファイル 関数 変数 型定義 列挙型 列挙型の値 マクロ定義