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_EXCEPTION_TRANSLATOR_REGISTRY_HPP_INCLUDED
9 #define TWOBLUECUBES_CATCH_EXCEPTION_TRANSLATOR_REGISTRY_HPP_INCLUDED
10 
11 #include "catch_interfaces_exception.h"
12 #include "catch_tostring.h"
13 
14 #ifdef __OBJC__
15 #import "Foundation/Foundation.h"
16 #endif
17 
18 namespace Catch {
19 
20     class ExceptionTranslatorRegistry : public IExceptionTranslatorRegistry {
21     public:
~ExceptionTranslatorRegistry()22         ~ExceptionTranslatorRegistry() {
23             deleteAll( m_translators );
24         }
25 
registerTranslator(const IExceptionTranslator * translator)26         virtual void registerTranslator( const IExceptionTranslator* translator ) {
27             m_translators.push_back( translator );
28         }
29 
translateActiveException() const30         virtual std::string translateActiveException() const {
31             try {
32 #ifdef __OBJC__
33                 // In Objective-C try objective-c exceptions first
34                 @try {
35                     return tryTranslators();
36                 }
37                 @catch (NSException *exception) {
38                     return Catch::toString( [exception description] );
39                 }
40 #else
41                 return tryTranslators();
42 #endif
43             }
44             catch( TestFailureException& ) {
45                 throw;
46             }
47             catch( std::exception& ex ) {
48                 return ex.what();
49             }
50             catch( std::string& msg ) {
51                 return msg;
52             }
53             catch( const char* msg ) {
54                 return msg;
55             }
56             catch(...) {
57                 return "Unknown exception";
58             }
59         }
60 
tryTranslators() const61         std::string tryTranslators() const {
62             if( m_translators.empty() )
63                 throw;
64             else
65                 return m_translators[0]->translate( m_translators.begin()+1, m_translators.end() );
66         }
67 
68     private:
69         std::vector<const IExceptionTranslator*> m_translators;
70     };
71 }
72 
73 #endif // TWOBLUECUBES_CATCH_EXCEPTION_TRANSLATOR_REGISTRY_HPP_INCLUDED
74