From: <syn...@us...> - 2008-12-10 20:22:48
|
Revision: 908 http://loki-lib.svn.sourceforge.net/loki-lib/?rev=908&view=rev Author: syntheticpp Date: 2008-12-10 20:22:40 +0000 (Wed, 10 Dec 2008) Log Message: ----------- add error policy to check return Modified Paths: -------------- trunk/include/loki/CheckReturn.h trunk/test/CheckReturn/main.cpp Modified: trunk/include/loki/CheckReturn.h =================================================================== --- trunk/include/loki/CheckReturn.h 2008-12-10 19:47:33 UTC (rev 907) +++ trunk/include/loki/CheckReturn.h 2008-12-10 20:22:40 UTC (rev 908) @@ -17,7 +17,9 @@ #include <assert.h> +#include <stdio.h> + namespace Loki { @@ -42,7 +44,26 @@ /// 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 { public: @@ -61,9 +82,10 @@ /// Destructor checks if return value was used. 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. - assert( m_checked ); + if (!m_checked) + OnError::run(); } /// Conversion operator changes CheckReturn back to Value type. Modified: trunk/test/CheckReturn/main.cpp =================================================================== --- trunk/test/CheckReturn/main.cpp 2008-12-10 19:47:33 UTC (rev 907) +++ trunk/test/CheckReturn/main.cpp 2008-12-10 20:22:40 UTC (rev 908) @@ -26,7 +26,9 @@ typedef ::Loki::CheckReturn< string > StringReturn; +typedef ::Loki::CheckReturn< bool , ::Loki::FprintfStderr > BoolReturnStderr; + // ---------------------------------------------------------------------------- bool NoCheckRequired( void ) @@ -43,6 +45,14 @@ // ---------------------------------------------------------------------------- +BoolReturnStderr CheckRequiredStderr( void ) +{ + return BoolReturnStderr( true ); +} + + +// ---------------------------------------------------------------------------- + BoolReturn CheckRequired( bool value ) { // By passing false into CheckRequired, CheckRequired calls an overloaded @@ -143,6 +153,11 @@ 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. CheckRequired(); cout << "Should assert before this line! How did we get here?" << endl; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |