add error policy to check return

git-svn-id: svn://svn.code.sf.net/p/loki-lib/code/trunk@908 7ec92016-0320-0410-acc4-a06ded1c099a
This commit is contained in:
syntheticpp 2008-12-10 20:22:40 +00:00
parent 1e72cc7149
commit 541c601909
2 changed files with 40 additions and 3 deletions

View file

@ -17,6 +17,8 @@
#include <assert.h> #include <assert.h>
#include <stdio.h>
namespace Loki namespace Loki
{ {
@ -42,7 +44,26 @@ namespace Loki
/// can work with other types that have cheap copy operations. /// can work with other types that have cheap copy operations.
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
template < class Value >
struct TriggerAssert
{
static void run()
{
assert( 0 );
}
};
struct FprintfStderr
{
static void run()
{
fprintf(stderr, "CheckReturn: return value was not checked\n");
}
};
template < class Value , typename OnError = TriggerAssert>
class CheckReturn class CheckReturn
{ {
public: public:
@ -61,9 +82,10 @@ public:
/// Destructor checks if return value was used. /// Destructor checks if return value was used.
inline ~CheckReturn( void ) inline ~CheckReturn( void )
{ {
// If this assertion fails, then a function failed to check the // If m_checked is false, then a function failed to check the
// return value from a function call. // return value from a function call.
assert( m_checked ); if (!m_checked)
OnError::run();
} }
/// Conversion operator changes CheckReturn back to Value type. /// Conversion operator changes CheckReturn back to Value type.

View file

@ -26,6 +26,8 @@ typedef ::Loki::CheckReturn< bool > BoolReturn;
typedef ::Loki::CheckReturn< string > StringReturn; typedef ::Loki::CheckReturn< string > StringReturn;
typedef ::Loki::CheckReturn< bool , ::Loki::FprintfStderr > BoolReturnStderr;
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@ -41,6 +43,14 @@ BoolReturn CheckRequired( void )
return BoolReturn( true ); return BoolReturn( true );
} }
// ----------------------------------------------------------------------------
BoolReturnStderr CheckRequiredStderr( void )
{
return BoolReturnStderr( true );
}
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
BoolReturn CheckRequired( bool value ) BoolReturn CheckRequired( bool value )
@ -143,6 +153,11 @@ int main( unsigned int argc, const char * argv[] )
cout << "Made a nested call to CheckRequired." << endl; cout << "Made a nested call to CheckRequired." << endl;
} }
{
BoolReturnStderr check = CheckRequiredStderr();
}
cout << "There should be a error message: \nCheckReturn: return value was not checked" << endl;
// This should assert since caller does not check return value. // This should assert since caller does not check return value.
CheckRequired(); CheckRequired();
cout << "Should assert before this line! How did we get here?" << endl; cout << "Should assert before this line! How did we get here?" << endl;