1 /*
2  *  Created by Phil on 5/8/2012.
3  *  Copyright 2012 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_REGISTRY_HUB_HPP_INCLUDED
9 #define TWOBLUECUBES_CATCH_REGISTRY_HUB_HPP_INCLUDED
10 
11 #include "catch_interfaces_registry_hub.h"
12 
13 #include "catch_test_case_registry_impl.hpp"
14 #include "catch_reporter_registry.hpp"
15 #include "catch_exception_translator_registry.hpp"
16 #include "catch_tag_alias_registry.h"
17 
18 namespace Catch {
19 
20     namespace {
21 
22         class RegistryHub : public IRegistryHub, public IMutableRegistryHub {
23 
24             RegistryHub( RegistryHub const& );
25             void operator=( RegistryHub const& );
26 
27         public: // IRegistryHub
RegistryHub()28             RegistryHub() {
29             }
getReporterRegistry() const30             virtual IReporterRegistry const& getReporterRegistry() const CATCH_OVERRIDE {
31                 return m_reporterRegistry;
32             }
getTestCaseRegistry() const33             virtual ITestCaseRegistry const& getTestCaseRegistry() const CATCH_OVERRIDE {
34                 return m_testCaseRegistry;
35             }
getExceptionTranslatorRegistry()36             virtual IExceptionTranslatorRegistry& getExceptionTranslatorRegistry() CATCH_OVERRIDE {
37                 return m_exceptionTranslatorRegistry;
38             }
getTagAliasRegistry() const39             virtual ITagAliasRegistry const& getTagAliasRegistry() const CATCH_OVERRIDE {
40                 return m_tagAliasRegistry;
41             }
42 
43 
44         public: // IMutableRegistryHub
registerReporter(std::string const & name,Ptr<IReporterFactory> const & factory)45             virtual void registerReporter( std::string const& name, Ptr<IReporterFactory> const& factory ) CATCH_OVERRIDE {
46                 m_reporterRegistry.registerReporter( name, factory );
47             }
registerListener(Ptr<IReporterFactory> const & factory)48             virtual void registerListener( Ptr<IReporterFactory> const& factory ) CATCH_OVERRIDE {
49                 m_reporterRegistry.registerListener( factory );
50             }
registerTest(TestCase const & testInfo)51             virtual void registerTest( TestCase const& testInfo ) CATCH_OVERRIDE {
52                 m_testCaseRegistry.registerTest( testInfo );
53             }
registerTranslator(const IExceptionTranslator * translator)54             virtual void registerTranslator( const IExceptionTranslator* translator ) CATCH_OVERRIDE {
55                 m_exceptionTranslatorRegistry.registerTranslator( translator );
56             }
registerTagAlias(std::string const & alias,std::string const & tag,SourceLineInfo const & lineInfo)57             virtual void registerTagAlias( std::string const& alias, std::string const& tag, SourceLineInfo const& lineInfo ) CATCH_OVERRIDE {
58                 m_tagAliasRegistry.add( alias, tag, lineInfo );
59             }
60 
61         private:
62             TestRegistry m_testCaseRegistry;
63             ReporterRegistry m_reporterRegistry;
64             ExceptionTranslatorRegistry m_exceptionTranslatorRegistry;
65             TagAliasRegistry m_tagAliasRegistry;
66         };
67 
68         // Single, global, instance
getTheRegistryHub()69         inline RegistryHub*& getTheRegistryHub() {
70             static RegistryHub* theRegistryHub = CATCH_NULL;
71             if( !theRegistryHub )
72                 theRegistryHub = new RegistryHub();
73             return theRegistryHub;
74         }
75     }
76 
getRegistryHub()77     IRegistryHub& getRegistryHub() {
78         return *getTheRegistryHub();
79     }
getMutableRegistryHub()80     IMutableRegistryHub& getMutableRegistryHub() {
81         return *getTheRegistryHub();
82     }
cleanUp()83     void cleanUp() {
84         delete getTheRegistryHub();
85         getTheRegistryHub() = CATCH_NULL;
86         cleanUpContext();
87     }
translateActiveException()88     std::string translateActiveException() {
89         return getRegistryHub().getExceptionTranslatorRegistry().translateActiveException();
90     }
91 
92 
93 } // end namespace Catch
94 
95 #endif // TWOBLUECUBES_CATCH_REGISTRY_HUB_HPP_INCLUDED
96