Chris@16: /* Chris@101: * Copyright Andrey Semashev 2007 - 2015. Chris@16: * Distributed under the Boost Software License, Version 1.0. Chris@16: * (See accompanying file LICENSE_1_0.txt or copy at Chris@16: * http://www.boost.org/LICENSE_1_0.txt) Chris@16: */ Chris@16: /*! Chris@16: * \file singleton.hpp Chris@16: * \author Andrey Semashev Chris@16: * \date 20.04.2008 Chris@16: * Chris@16: * \brief This header is the Boost.Log library implementation, see the library documentation Chris@16: * at http://www.boost.org/doc/libs/release/libs/log/doc/html/index.html. Chris@16: */ Chris@16: Chris@16: #ifndef BOOST_LOG_DETAIL_SINGLETON_HPP_INCLUDED_ Chris@16: #define BOOST_LOG_DETAIL_SINGLETON_HPP_INCLUDED_ Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #ifdef BOOST_HAS_PRAGMA_ONCE Chris@16: #pragma once Chris@16: #endif Chris@16: Chris@16: namespace boost { Chris@16: Chris@16: BOOST_LOG_OPEN_NAMESPACE Chris@16: Chris@16: namespace aux { Chris@16: Chris@16: //! A base class for singletons, constructed on-demand Chris@16: template< typename DerivedT, typename StorageT = DerivedT > Chris@16: class lazy_singleton Chris@16: { Chris@16: public: Chris@16: BOOST_DEFAULTED_FUNCTION(lazy_singleton(), {}) Chris@16: Chris@16: //! Returns the singleton instance Chris@16: static StorageT& get() Chris@16: { Chris@16: BOOST_LOG_ONCE_BLOCK() Chris@16: { Chris@16: DerivedT::init_instance(); Chris@16: } Chris@16: return get_instance(); Chris@16: } Chris@16: Chris@16: //! Initializes the singleton instance Chris@16: static void init_instance() Chris@16: { Chris@16: get_instance(); Chris@16: } Chris@16: Chris@16: BOOST_DELETED_FUNCTION(lazy_singleton(lazy_singleton const&)) Chris@16: BOOST_DELETED_FUNCTION(lazy_singleton& operator= (lazy_singleton const&)) Chris@16: Chris@16: protected: Chris@16: //! Returns the singleton instance (not thread-safe) Chris@16: static StorageT& get_instance() Chris@16: { Chris@16: static StorageT instance; Chris@16: return instance; Chris@16: } Chris@16: }; Chris@16: Chris@16: //! A base class for singletons, constructed on namespace scope initialization stage Chris@16: template< typename DerivedT, typename StorageT = DerivedT > Chris@16: class singleton : Chris@16: public lazy_singleton< DerivedT, StorageT > Chris@16: { Chris@16: public: Chris@16: static StorageT& instance; Chris@16: }; Chris@16: Chris@16: template< typename DerivedT, typename StorageT > Chris@16: StorageT& singleton< DerivedT, StorageT >::instance = Chris@16: lazy_singleton< DerivedT, StorageT >::get(); Chris@16: Chris@16: } // namespace aux Chris@16: Chris@16: BOOST_LOG_CLOSE_NAMESPACE // namespace log Chris@16: Chris@16: } // namespace boost Chris@16: Chris@16: #include Chris@16: Chris@16: #endif // BOOST_LOG_DETAIL_SINGLETON_HPP_INCLUDED_