WIL説明書(C++)  3.0.0
サンプルコード(実用編)

下記コードは、ユーザが作成した関数で引数のチェックを行い例外を発行する例です。

// $Revision: 1.1 $
DOUBLE* AllocateMatrix( UINT rows, UINT cols );
void FreeMatrix( DOUBLE* matrix );
void ResetMatrix( DOUBLE* matrix, UINT rows, UINT cols );
DOUBLE GetMatrixValue( UINT row, UINT col );
DOUBLE* g_matrix = NULL;
UINT g_rows = 0;
UINT g_cols = 0;
void Exception3()
{
// ------------------------------------------------------------
// ex1) 行列サイズに 0 以下が指定された.
{
DOUBLE* matrix = NULL;
try
{
matrix = AllocateMatrix( 0, 3 ); // (!) rows<=0
}
catch( const FVCL::CFveException& ex )
{
_tprintf( _T("ex1) exception=%d, errcode=%d, function=%s, message=%s\n"),
}
FreeMatrix( matrix );
matrix = NULL;
}
// ------------------------------------------------------------
// ex2) 行列サイズが物理的なメモリサイズを超えている.
{
DOUBLE* matrix = NULL;
try
{
matrix = AllocateMatrix( 32768, 32768 ); // (!) rows*cols = 1G x 8bytes
}
catch( const FVCL::CFveException& ex )
{
_tprintf( _T("ex2) exception=%d, errcode=%d, function=%s, message=%s\n"),
}
FreeMatrix( matrix );
matrix = NULL;
}
// ------------------------------------------------------------
// ex3) ResetMatrix が 3x3 の同次行列以外をサポートしていない.
{
DOUBLE* matrix = NULL;
try
{
matrix = AllocateMatrix( 5, 5 );
ResetMatrix( matrix, 5, 5 ); // (!) 5x5 は未サポート.
}
catch( const FVCL::CFveException& ex )
{
_tprintf( _T("ex3) exception=%d, errcode=%d, function=%s, message=%s\n"),
}
FreeMatrix( matrix );
matrix = NULL;
}
// ------------------------------------------------------------
// ex4) 不正な領域をアクセスした.
{
try
{
g_rows = 3;
g_cols = 3;
g_matrix = AllocateMatrix( g_rows, g_cols );
ResetMatrix( g_matrix, g_rows, g_cols );
DOUBLE value00 = GetMatrixValue( 0, 0 );
DOUBLE value11 = GetMatrixValue( 1, 1 );
DOUBLE value22 = GetMatrixValue( 2, 2 );
DOUBLE value33 = GetMatrixValue( 3, 3 ); // (!) アクセス違反.
}
catch( const FVCL::CFveException& ex )
{
_tprintf( _T("ex4) exception=%d, errcode=%d, function=%s, message=%s\n"),
}
FreeMatrix( g_matrix );
g_matrix = NULL;
}
}
// ============================================================
DOUBLE* AllocateMatrix( UINT rows, UINT cols )
{
if( rows <= 0 )
throw FVCL::CFveBadParamException( FVCL_ErrorCode::INVALID_PARAMETER, __TFUNCTION__, _T("rows <= 0") );
if( cols <= 0 )
throw FVCL::CFveBadParamException( FVCL_ErrorCode::INVALID_PARAMETER, __TFUNCTION__, _T("cols <= 0") );
size_t size = rows * cols;
if( 2*1024*1024/8 < size )
throw FVCL::CFveBadAllocException( FVCL_ErrorCode::FAILED_TO_ALLOCATE, __TFUNCTION__, _T("2*1024*1024/8 < size") );
DOUBLE* matrix = (DOUBLE*)::HeapAlloc( ::GetProcessHeap(), HEAP_ZERO_MEMORY, size*sizeof(DOUBLE) );
if( matrix == NULL )
return matrix;
}
// ============================================================
void FreeMatrix( DOUBLE* matrix )
{
if( matrix != NULL )
::HeapFree( ::GetProcessHeap(), 0, matrix );
}
// ============================================================
void ResetMatrix( DOUBLE* matrix, UINT rows, UINT cols )
{
if( matrix == NULL )
throw FVCL::CFveBadParamException( FVCL_ErrorCode::NOT_ALLOCATED, __TFUNCTION__, _T("matrix == NULL") );
if( rows != 3 || cols != 3 )
throw FVCL::CFveUnsupportedException( FVCL_ErrorCode::INVALID_PARAMETER, __TFUNCTION__, _T("rows != 3 || cols != 3") );
for( UINT r=0 ; r<rows ; r++ )
for( UINT c=0 ; c<cols ; c++ )
matrix[r * cols + c] = 0;
matrix[0 * cols + 0] = 1;
matrix[1 * cols + 1] = 1;
matrix[2 * cols + 2] = 1;
}
// ============================================================
DOUBLE GetMatrixValue( UINT row, UINT col )
{
if( g_matrix == NULL )
throw FVCL::CFveBadParamException( FVCL_ErrorCode::NOT_ALLOCATED, __TFUNCTION__, _T("g_matrix == NULL") );
if( !(0 <= row && row < g_rows) )
throw FVCL::CFveBadAccessException( FVCL_ErrorCode::INVALID_PARAMETER, __TFUNCTION__, _T("!(0 <= row && row < g_rows)") );
if( !(0 <= col && col < g_cols) )
throw FVCL::CFveBadAccessException( FVCL_ErrorCode::INVALID_PARAMETER, __TFUNCTION__, _T("!(0 <= col && col < g_cols)") );
return g_matrix[row * g_cols + col];
}
実行結果:
ex1) exception=1004, errcode=11, function=AllocateMatrix, message=rows <= 0
ex2) exception=1002, errcode=1, function=AllocateMatrix, message=2*1024*1024/8 < size
ex3) exception=1007, errcode=11, function=ResetMatrix, message=rows != 3 || cols != 3
ex4) exception=1001, errcode=11, function=GetMatrixValue, message=!(0 <= row && row < g_rows)


Documentation copyright © 2007 FAST Corporation. [B-001864]
Generated on 2024年10月10日(木) 09時12分46秒 for WIL説明書(C++) by doxygen 1.8.11