|
Boost : |
From: Reece Dunn (msclrhd_at_[hidden])
Date: 2004-03-26 19:51:06
Eljay Love-Jensen wrote:
>Hi Reece,
>
>Cool! I think I'll add that to my repertoire.
Glad you like it :).
>A slight tweak, and it can ignore the passed in value, and peep at errno.
>I'd call that "errnocheck" (or errnoCheck ... my orthographical standard).
Sure. The class I posted is an adaption of com::result that deals with
Windows/COM HRESULT error handling that simplifies COM object usage.
It may be possible to use a policy-based approach, something like:
struct DefaultCheck
{
static bool IsError( int err )
{
return( err < 0 );
}
};
struct ErrNoCheck
{
static bool IsError( int err )
{
return( errno < 0 ); // ???
}
};
struct HRESULTCheck
{
static bool IsError( HRESULT hr )
{
return( FAILED( hr ));
}
};
template< class ErrorPolicy = DefaultCheck, typename T = int >
class errorcheck
{
public:
inline errorcheck( T e ): error( e )
{
if( ErrorPolicy::IsError( e )) throw( *this );
}
// ...
};
// example usage:
errorcheck<> error;
error = open( ... );
// ...
errorcheck< ErrNoCheck > en;
en = std::sin( -1 );
errorcheck< HRESULTCheck, HRESULT > hr;
BSTR str;
hr = pStyle -> get_color( &str );
or maybe to have extended information, so you could use something like:
error( open( ... ), "open" );
en( ::sin( -1 ), errno );
en( ::cos( 0 )); // use errno by default
Regards,
Reece
_________________________________________________________________
Express yourself with cool emoticons - download MSN Messenger today!
http://www.msn.co.uk/messenger
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk