Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:54:06

0001 
0002 //              Copyright Catch2 Authors
0003 // Distributed under the Boost Software License, Version 1.0.
0004 //   (See accompanying file LICENSE.txt or copy at
0005 //        https://www.boost.org/LICENSE_1_0.txt)
0006 
0007 // SPDX-License-Identifier: BSL-1.0
0008 #ifndef CATCH_SINGLETONS_HPP_INCLUDED
0009 #define CATCH_SINGLETONS_HPP_INCLUDED
0010 
0011 namespace Catch {
0012 
0013     struct ISingleton {
0014         virtual ~ISingleton(); // = default
0015     };
0016 
0017 
0018     void addSingleton( ISingleton* singleton );
0019     void cleanupSingletons();
0020 
0021 
0022     template<typename SingletonImplT, typename InterfaceT = SingletonImplT, typename MutableInterfaceT = InterfaceT>
0023     class Singleton : SingletonImplT, public ISingleton {
0024 
0025         static auto getInternal() -> Singleton* {
0026             static Singleton* s_instance = nullptr;
0027             if( !s_instance ) {
0028                 s_instance = new Singleton;
0029                 addSingleton( s_instance );
0030             }
0031             return s_instance;
0032         }
0033 
0034     public:
0035         static auto get() -> InterfaceT const& {
0036             return *getInternal();
0037         }
0038         static auto getMutable() -> MutableInterfaceT& {
0039             return *getInternal();
0040         }
0041     };
0042 
0043 } // namespace Catch
0044 
0045 #endif // CATCH_SINGLETONS_HPP_INCLUDED