catch_singletons.hpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. namespace Catch {
  10. struct ISingleton {
  11. virtual ~ISingleton();
  12. };
  13. void addSingleton( ISingleton* singleton );
  14. void cleanupSingletons();
  15. template<typename SingletonImplT, typename InterfaceT = SingletonImplT, typename MutableInterfaceT = InterfaceT>
  16. class Singleton : SingletonImplT, public ISingleton {
  17. static auto getInternal() -> Singleton* {
  18. static Singleton* s_instance = nullptr;
  19. if( !s_instance ) {
  20. s_instance = new Singleton;
  21. addSingleton( s_instance );
  22. }
  23. return s_instance;
  24. }
  25. public:
  26. static auto get() -> InterfaceT const& {
  27. return *getInternal();
  28. }
  29. static auto getMutable() -> MutableInterfaceT& {
  30. return *getInternal();
  31. }
  32. };
  33. } // namespace Catch
  34. #endif // TWOBLUECUBES_CATCH_SINGLETONS_HPP_INCLUDED