catch_interfaces_exception.h 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * Created by Phil on 20/04/2011.
  3. * Copyright 2011 Two Blue Cubes Ltd. All rights reserved.
  4. *
  5. * Distributed under the Boost Software License, Version 1.0. (See accompanying
  6. * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. */
  8. #ifndef TWOBLUECUBES_CATCH_INTERFACES_EXCEPTION_H_INCLUDED
  9. #define TWOBLUECUBES_CATCH_INTERFACES_EXCEPTION_H_INCLUDED
  10. #include "catch_interfaces_registry_hub.h"
  11. #if defined(CATCH_CONFIG_DISABLE)
  12. #define INTERNAL_CATCH_TRANSLATE_EXCEPTION_NO_REG( translatorName, signature) \
  13. static std::string translatorName( signature )
  14. #endif
  15. #include <exception>
  16. #include <string>
  17. #include <vector>
  18. namespace Catch {
  19. using exceptionTranslateFunction = std::string(*)();
  20. struct IExceptionTranslator;
  21. using ExceptionTranslators = std::vector<std::unique_ptr<IExceptionTranslator const>>;
  22. struct IExceptionTranslator {
  23. virtual ~IExceptionTranslator();
  24. virtual std::string translate( ExceptionTranslators::const_iterator it, ExceptionTranslators::const_iterator itEnd ) const = 0;
  25. };
  26. struct IExceptionTranslatorRegistry {
  27. virtual ~IExceptionTranslatorRegistry();
  28. virtual std::string translateActiveException() const = 0;
  29. };
  30. class ExceptionTranslatorRegistrar {
  31. template<typename T>
  32. class ExceptionTranslator : public IExceptionTranslator {
  33. public:
  34. ExceptionTranslator( std::string(*translateFunction)( T& ) )
  35. : m_translateFunction( translateFunction )
  36. {}
  37. std::string translate( ExceptionTranslators::const_iterator it, ExceptionTranslators::const_iterator itEnd ) const override {
  38. #if defined(CATCH_CONFIG_DISABLE_EXCEPTIONS)
  39. return "";
  40. #else
  41. try {
  42. if( it == itEnd )
  43. std::rethrow_exception(std::current_exception());
  44. else
  45. return (*it)->translate( it+1, itEnd );
  46. }
  47. catch( T& ex ) {
  48. return m_translateFunction( ex );
  49. }
  50. #endif
  51. }
  52. protected:
  53. std::string(*m_translateFunction)( T& );
  54. };
  55. public:
  56. template<typename T>
  57. ExceptionTranslatorRegistrar( std::string(*translateFunction)( T& ) ) {
  58. getMutableRegistryHub().registerTranslator
  59. ( new ExceptionTranslator<T>( translateFunction ) );
  60. }
  61. };
  62. }
  63. ///////////////////////////////////////////////////////////////////////////////
  64. #define INTERNAL_CATCH_TRANSLATE_EXCEPTION2( translatorName, signature ) \
  65. static std::string translatorName( signature ); \
  66. CATCH_INTERNAL_START_WARNINGS_SUPPRESSION \
  67. CATCH_INTERNAL_SUPPRESS_GLOBALS_WARNINGS \
  68. namespace{ Catch::ExceptionTranslatorRegistrar INTERNAL_CATCH_UNIQUE_NAME( catch_internal_ExceptionRegistrar )( &translatorName ); } \
  69. CATCH_INTERNAL_STOP_WARNINGS_SUPPRESSION \
  70. static std::string translatorName( signature )
  71. #define INTERNAL_CATCH_TRANSLATE_EXCEPTION( signature ) INTERNAL_CATCH_TRANSLATE_EXCEPTION2( INTERNAL_CATCH_UNIQUE_NAME( catch_internal_ExceptionTranslator ), signature )
  72. #endif // TWOBLUECUBES_CATCH_INTERFACES_EXCEPTION_H_INCLUDED