Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:39:21

0001 /*
0002  *          Copyright Andrey Semashev 2007 - 2015.
0003  * Distributed under the Boost Software License, Version 1.0.
0004  *    (See accompanying file LICENSE_1_0.txt or copy at
0005  *          http://www.boost.org/LICENSE_1_0.txt)
0006  */
0007 /*!
0008  * \file   singleton.hpp
0009  * \author Andrey Semashev
0010  * \date   20.04.2008
0011  *
0012  * \brief  This header is the Boost.Log library implementation, see the library documentation
0013  *         at http://www.boost.org/doc/libs/release/libs/log/doc/html/index.html.
0014  */
0015 
0016 #ifndef BOOST_LOG_DETAIL_SINGLETON_HPP_INCLUDED_
0017 #define BOOST_LOG_DETAIL_SINGLETON_HPP_INCLUDED_
0018 
0019 #include <boost/log/detail/config.hpp>
0020 #include <boost/log/utility/once_block.hpp>
0021 #include <boost/log/detail/header.hpp>
0022 
0023 #ifdef BOOST_HAS_PRAGMA_ONCE
0024 #pragma once
0025 #endif
0026 
0027 namespace boost {
0028 
0029 BOOST_LOG_OPEN_NAMESPACE
0030 
0031 namespace aux {
0032 
0033 //! A base class for singletons, constructed on-demand
0034 template< typename DerivedT, typename StorageT = DerivedT >
0035 class lazy_singleton
0036 {
0037 public:
0038     BOOST_DEFAULTED_FUNCTION(lazy_singleton(), {})
0039 
0040     //! Returns the singleton instance
0041     static StorageT& get()
0042     {
0043         BOOST_LOG_ONCE_BLOCK()
0044         {
0045             DerivedT::init_instance();
0046         }
0047         return get_instance();
0048     }
0049 
0050     //! Initializes the singleton instance
0051     static void init_instance()
0052     {
0053         get_instance();
0054     }
0055 
0056     BOOST_DELETED_FUNCTION(lazy_singleton(lazy_singleton const&))
0057     BOOST_DELETED_FUNCTION(lazy_singleton& operator= (lazy_singleton const&))
0058 
0059 protected:
0060     //! Returns the singleton instance (not thread-safe)
0061     static StorageT& get_instance()
0062     {
0063         static StorageT instance;
0064         return instance;
0065     }
0066 };
0067 
0068 //! A base class for singletons, constructed on namespace scope initialization stage
0069 template< typename DerivedT, typename StorageT = DerivedT >
0070 class singleton :
0071     public lazy_singleton< DerivedT, StorageT >
0072 {
0073 public:
0074     static StorageT& instance;
0075 };
0076 
0077 template< typename DerivedT, typename StorageT >
0078 StorageT& singleton< DerivedT, StorageT >::instance =
0079     lazy_singleton< DerivedT, StorageT >::get();
0080 
0081 } // namespace aux
0082 
0083 BOOST_LOG_CLOSE_NAMESPACE // namespace log
0084 
0085 } // namespace boost
0086 
0087 #include <boost/log/detail/footer.hpp>
0088 
0089 #endif // BOOST_LOG_DETAIL_SINGLETON_HPP_INCLUDED_