catch_singletons.cpp
1 2 // Copyright Catch2 Authors 3 // Distributed under the Boost Software License, Version 1.0. 4 // (See accompanying file LICENSE.txt or copy at 5 // https://www.boost.org/LICENSE_1_0.txt) 6 7 // SPDX-License-Identifier: BSL-1.0 8 #include <catch2/internal/catch_singletons.hpp> 9 10 #include <vector> 11 12 namespace Catch { 13 14 namespace { 15 static auto getSingletons() -> std::vector<ISingleton*>*& { 16 static std::vector<ISingleton*>* g_singletons = nullptr; 17 if( !g_singletons ) 18 g_singletons = new std::vector<ISingleton*>(); 19 return g_singletons; 20 } 21 } 22 23 ISingleton::~ISingleton() = default; 24 25 void addSingleton(ISingleton* singleton ) { 26 getSingletons()->push_back( singleton ); 27 } 28 void cleanupSingletons() { 29 auto& singletons = getSingletons(); 30 for( auto singleton : *singletons ) 31 delete singleton; 32 delete singletons; 33 singletons = nullptr; 34 } 35 36 } // namespace Catch