1 /*
2  *  Created by Phil Nash on 15/6/2018.
3  *
4  *  Distributed under the Boost Software License, Version 1.0. (See accompanying
5  *  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6  */
7 #ifndef TWOBLUECUBES_CATCH_SINGLETONS_HPP_INCLUDED
8 #define TWOBLUECUBES_CATCH_SINGLETONS_HPP_INCLUDED
9 
10 namespace Catch {
11 
12     struct ISingleton {
13         virtual ~ISingleton();
14     };
15 
16 
17     void addSingleton( ISingleton* singleton );
18     void cleanupSingletons();
19 
20 
21     template<typename SingletonImplT, typename InterfaceT = SingletonImplT, typename MutableInterfaceT = InterfaceT>
22     class Singleton : SingletonImplT, public ISingleton {
23 
getInternal()24         static auto getInternal() -> Singleton* {
25             static Singleton* s_instance = nullptr;
26             if( !s_instance ) {
27                 s_instance = new Singleton;
28                 addSingleton( s_instance );
29             }
30             return s_instance;
31         }
32 
33     public:
get()34         static auto get() -> InterfaceT const& {
35             return *getInternal();
36         }
getMutable()37         static auto getMutable() -> MutableInterfaceT& {
38             return *getInternal();
39         }
40     };
41 
42 } // namespace Catch
43 
44 #endif // TWOBLUECUBES_CATCH_SINGLETONS_HPP_INCLUDED
45