Translate exceptions by const reference instead of plain ref

This commit is contained in:
Martin Hořeňovský 2020-05-19 21:23:35 +02:00
parent 4b2f1da02a
commit 4394d3ae65
No known key found for this signature in database
GPG Key ID: DE48307B8B0D381A
3 changed files with 8 additions and 8 deletions

View File

@ -64,7 +64,7 @@ namespace Catch {
By default all exceptions deriving from `std::exception` will be translated to strings by calling the `what()` method. For exception types that do not derive from `std::exception` - or if `what()` does not return a suitable string - use `CATCH_TRANSLATE_EXCEPTION`. This defines a function that takes your exception type, by reference, and returns a string. It can appear anywhere in the code - it doesn't have to be in the same translation unit. For example: By default all exceptions deriving from `std::exception` will be translated to strings by calling the `what()` method. For exception types that do not derive from `std::exception` - or if `what()` does not return a suitable string - use `CATCH_TRANSLATE_EXCEPTION`. This defines a function that takes your exception type, by reference, and returns a string. It can appear anywhere in the code - it doesn't have to be in the same translation unit. For example:
```cpp ```cpp
CATCH_TRANSLATE_EXCEPTION( MyType& ex ) { CATCH_TRANSLATE_EXCEPTION( MyType const& ex ) {
return ex.message(); return ex.message();
} }
``` ```

View File

@ -12,7 +12,7 @@ namespace Catch {
class ExceptionTranslator : public IExceptionTranslator { class ExceptionTranslator : public IExceptionTranslator {
public: public:
ExceptionTranslator( std::string(*translateFunction)( T& ) ) ExceptionTranslator( std::string(*translateFunction)( T const& ) )
: m_translateFunction( translateFunction ) : m_translateFunction( translateFunction )
{} {}
@ -24,7 +24,7 @@ namespace Catch {
else else
return (*it)->translate( it+1, itEnd ); return (*it)->translate( it+1, itEnd );
} }
catch( T& ex ) { catch( T const& ex ) {
return m_translateFunction( ex ); return m_translateFunction( ex );
} }
#else #else
@ -33,12 +33,12 @@ namespace Catch {
} }
protected: protected:
std::string(*m_translateFunction)( T& ); std::string(*m_translateFunction)( T const& );
}; };
public: public:
template<typename T> template<typename T>
ExceptionTranslatorRegistrar( std::string(*translateFunction)( T& ) ) { ExceptionTranslatorRegistrar( std::string(*translateFunction)( T const& ) ) {
getMutableRegistryHub().registerTranslator getMutableRegistryHub().registerTranslator
( new ExceptionTranslator<T>( translateFunction ) ); ( new ExceptionTranslator<T>( translateFunction ) );
} }

View File

@ -122,15 +122,15 @@ TEST_CASE( "When unchecked exceptions are thrown, but caught, they do not affect
} }
CATCH_TRANSLATE_EXCEPTION( CustomException& ex ) { CATCH_TRANSLATE_EXCEPTION( CustomException const& ex ) {
return ex.getMessage(); return ex.getMessage();
} }
CATCH_TRANSLATE_EXCEPTION( CustomStdException& ex ) { CATCH_TRANSLATE_EXCEPTION( CustomStdException const& ex ) {
return ex.getMessage(); return ex.getMessage();
} }
CATCH_TRANSLATE_EXCEPTION( double& ex ) { CATCH_TRANSLATE_EXCEPTION( double const& ex ) {
return Catch::Detail::stringify( ex ); return Catch::Detail::stringify( ex );
} }